Package com.martiansoftware.nailgun
Class NGContext
- java.lang.Object
-
- com.martiansoftware.nailgun.NGContext
-
public class NGContext extends java.lang.Object
Provides quite a bit of potentially useful information to classes specifically written for NailGun. The NailGun server itself, its AliasManager, the remote client's environment variables, and other information is available via this class. For all intents and purposes, the NGContext represents a single connection from a NailGun client.
If a class is written with a
method, that method will be called by NailGun instead of the traditionalpublic static void nailMain(NGContext context)
main(String[])
method normally used for programs. A fully populatedNGContext
object will then be provided tonailMain()
.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String[]
args
Command line arguments for the nailprivate java.lang.String
command
The command that was issued for this connectionjava.io.PrintStream
err
The client's stderrprivate java.io.PrintStream
exitStream
A stream to which a client exit code can be printedjava.io.InputStream
in
The client's stdinjava.io.PrintStream
out
The client's stdoutprivate java.util.Properties
remoteEnvironment
The remote host's environment variablesprivate java.net.InetAddress
remoteHost
The remote host's addressprivate int
remotePort
The port on the remote host that is communicating with NailGunprivate NGServer
server
The NGServer that accepted this connectionprivate java.lang.String
workingDirectory
-
Constructor Summary
Constructors Constructor Description NGContext()
Creates a new, empty NGContext
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addClientListener(NGClientListener listener)
void
addHeartbeatListener(NGHeartbeatListener listener)
void
assertLocalClient()
Throws ajava.lang.SecurityException
if the client is not connected from the local machine.void
assertLoopbackClient()
Throws ajava.lang.SecurityException
if the client is not connected via the loopback address.void
exit(int exitCode)
Sends an exit command with the specified exit code to the client.java.lang.String[]
getArgs()
Returns the command line arguments for the command implementation (nail) on the server.java.lang.String
getCommand()
Returns the command that was issued by the client (either an alias or the name of a class).java.util.Properties
getEnv()
Returns ajava.util.Properties
object containing a copy of the client's environment variablesjava.lang.String
getFileSeparator()
Returns the file separator ('/' or '\\') used by the client's os.java.net.InetAddress
getInetAddress()
Returns the address of the client at the other side of this connection.private NGInputStream
getInputStream()
NGServer
getNGServer()
Returns the NGServer that accepted this connectionjava.lang.String
getPathSeparator()
Returns the path separator (':' or ';') used by the client's os.int
getPort()
Returns the port on the client connected to the NailGun server.java.lang.String
getWorkingDirectory()
Returns the current working directory of the client, as reported by the client.boolean
isClientConnected()
void
removeAllClientListeners()
Do not notify about client exitvoid
removeClientListener(NGClientListener listener)
void
removeHeartbeatListener(NGHeartbeatListener listener)
void
setArgs(java.lang.String[] args)
void
setCommand(java.lang.String command)
(package private) void
setEnv(java.util.Properties remoteEnvironment)
void
setExitStream(java.io.PrintStream exitStream)
(package private) void
setInetAddress(java.net.InetAddress remoteHost)
(package private) void
setNGServer(NGServer server)
void
setPort(int remotePort)
(package private) void
setWorkingDirectory(java.lang.String workingDirectory)
-
-
-
Field Detail
-
remoteEnvironment
private java.util.Properties remoteEnvironment
The remote host's environment variables
-
remoteHost
private java.net.InetAddress remoteHost
The remote host's address
-
remotePort
private int remotePort
The port on the remote host that is communicating with NailGun
-
args
private java.lang.String[] args
Command line arguments for the nail
-
exitStream
private java.io.PrintStream exitStream
A stream to which a client exit code can be printed
-
server
private NGServer server
The NGServer that accepted this connection
-
command
private java.lang.String command
The command that was issued for this connection
-
workingDirectory
private java.lang.String workingDirectory
-
in
public java.io.InputStream in
The client's stdin
-
out
public java.io.PrintStream out
The client's stdout
-
err
public java.io.PrintStream err
The client's stderr
-
-
Method Detail
-
setExitStream
public void setExitStream(java.io.PrintStream exitStream)
-
setPort
public void setPort(int remotePort)
-
setCommand
public void setCommand(java.lang.String command)
-
getCommand
public java.lang.String getCommand()
Returns the command that was issued by the client (either an alias or the name of a class). This allows multiple aliases to point to the same class but result in different behaviors.- Returns:
- the command issued by the client
-
setWorkingDirectory
void setWorkingDirectory(java.lang.String workingDirectory)
-
getWorkingDirectory
public java.lang.String getWorkingDirectory()
Returns the current working directory of the client, as reported by the client. This is a String that will use the client'sFile.separator
('/' or '\'), which may differ from the separator on the server.- Returns:
- the current working directory of the client
-
setEnv
void setEnv(java.util.Properties remoteEnvironment)
-
setInetAddress
void setInetAddress(java.net.InetAddress remoteHost)
-
setArgs
public void setArgs(java.lang.String[] args)
-
setNGServer
void setNGServer(NGServer server)
-
getEnv
public java.util.Properties getEnv()
Returns ajava.util.Properties
object containing a copy of the client's environment variables- Returns:
- a
java.util.Properties
object containing a copy of the client's environment variables - See Also:
Properties
-
getFileSeparator
public java.lang.String getFileSeparator()
Returns the file separator ('/' or '\\') used by the client's os.- Returns:
- the file separator ('/' or '\\') used by the client's os.
-
getPathSeparator
public java.lang.String getPathSeparator()
Returns the path separator (':' or ';') used by the client's os.- Returns:
- the path separator (':' or ';') used by the client's os.
-
getInetAddress
public java.net.InetAddress getInetAddress()
Returns the address of the client at the other side of this connection.- Returns:
- the address of the client at the other side of this connection.
-
getArgs
public java.lang.String[] getArgs()
Returns the command line arguments for the command implementation (nail) on the server.- Returns:
- the command line arguments for the command implementation (nail) on the server.
-
getNGServer
public NGServer getNGServer()
Returns the NGServer that accepted this connection- Returns:
- the NGServer that accepted this connection
-
exit
public void exit(int exitCode)
Sends an exit command with the specified exit code to the client. The client will exit immediately with the specified exit code; you probably want to return from nailMain immediately after calling this.- Parameters:
exitCode
- the exit code with which the client should exit
-
getPort
public int getPort()
Returns the port on the client connected to the NailGun server.- Returns:
- the port on the client connected to the NailGun server.
-
assertLoopbackClient
public void assertLoopbackClient()
Throws ajava.lang.SecurityException
if the client is not connected via the loopback address.
-
assertLocalClient
public void assertLocalClient()
Throws ajava.lang.SecurityException
if the client is not connected from the local machine.
-
getInputStream
private NGInputStream getInputStream()
- Returns:
- the
NGInputStream
for this session.
-
isClientConnected
public boolean isClientConnected()
- Returns:
- true if client is connected, false if a client exit has been detected.
-
addClientListener
public void addClientListener(NGClientListener listener)
- Parameters:
listener
- theNGClientListener
to be notified of client events.
-
removeClientListener
public void removeClientListener(NGClientListener listener)
- Parameters:
listener
- theNGClientListener
to no longer be notified of client events.
-
removeAllClientListeners
public void removeAllClientListeners()
Do not notify about client exit
-
addHeartbeatListener
public void addHeartbeatListener(NGHeartbeatListener listener)
- Parameters:
listener
- theNGHeartbeatListener
to be notified of client events.
-
removeHeartbeatListener
public void removeHeartbeatListener(NGHeartbeatListener listener)
- Parameters:
listener
- theNGHeartbeatListener
to no longer be notified of client events.
-
-