Package com.martiansoftware.nailgun
Class NGServer
- java.lang.Object
-
- com.martiansoftware.nailgun.NGServer
-
- All Implemented Interfaces:
java.lang.Runnable
public class NGServer extends java.lang.Object implements java.lang.Runnable
Listens for new connections from NailGun clients and launches NGSession threads to process them.
This class can be run as a standalone server or can be embedded within larger applications as a means of providing command-line interaction with the application.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
NGServer.NGServerShutdowner
A shutdown hook that will cleanly bring down the NGServer if it is interrupted.
-
Field Summary
Fields Modifier and Type Field Description private AliasManager
aliasManager
This NGServer's AliasManager, which maps aliases to classesprivate java.util.Map
allNailStats
a collection of all classes executed by this server so farprivate boolean
allowNailsByClassName
If true, fully-qualified classnames are valid commandsstatic int
DEFAULT_SESSIONPOOLSIZE
Default size for thread poolprivate java.lang.Class
defaultNailClass
The default class to use if an invalid alias or classname is specified by the client.java.io.PrintStream
err
System.err
at the time of the NGServer's creationprivate int
heartbeatTimeoutMillis
java.io.InputStream
in
System.in
at the time of the NGServer's creationprivate NGListeningAddress
listeningAddress
The address on which to listenprivate java.lang.SecurityManager
originalSecurityManager
Remember the security manager we start with so we can restore it laterjava.io.PrintStream
out
System.out
at the time of the NGServer's creationprivate java.util.concurrent.atomic.AtomicBoolean
running
True if this NGServer has been started and is accepting connectionsprivate java.net.ServerSocket
serversocket
The socket doing the listeningprivate NGSessionPool
sessionPool
A pool of NGSessions ready to handle client connectionsprivate java.util.concurrent.atomic.AtomicBoolean
shutdown
True if this NGServer has received instructions to shut down
-
Constructor Summary
Constructors Constructor Description NGServer()
Creates a new NGServer that will listen on the default port (defined inNGConstants.DEFAULT_PORT
).NGServer(NGListeningAddress listeningAddress, int sessionPoolSize, int timeoutMillis)
Creates a new NGServer that will listen at the specified address and on the specified port with the specified session pool size.NGServer(java.net.InetAddress addr, int port)
Creates a new NGServer that will listen at the specified address and on the specified port with the default session pool size.NGServer(java.net.InetAddress addr, int port, int sessionPoolSize, int timeoutMillis)
Creates a new NGServer that will listen at the specified address and on the specified port with the specified session pool size.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
allowsNailsByClassName()
Returns a flag that indicates whether Nail lookups by classname are allowed.AliasManager
getAliasManager()
Returns the AliasManager in use by this NGServer.java.lang.Class
getDefaultNailClass()
Returns the default class that will be used if no Nails can be found via alias or classname.int
getHeartbeatTimeout()
java.util.Map
getNailStats()
Returns a snapshot of this NGServer's nail statistics.private NailStats
getOrCreateStatsFor(java.lang.Class nailClass)
Returns the current NailStats object for the specified class, creating a new one if necessaryint
getPort()
Returns the port on which this server is (or will be) listening.boolean
isRunning()
Returns true iff the server is currently running.static void
main(java.lang.String[] args)
Creates and starts a newNGServer
.(package private) void
nailFinished(java.lang.Class nailClass)
Provides a means for an NGSession to register the completion of a nails execution with the server.(package private) void
nailStarted(java.lang.Class nailClass)
Provides a means for an NGSession to register the starting of a nail execution with the server.void
run()
Listens for new connections and launches NGSession threads to process them.void
setAllowNailsByClassName(boolean allowNailsByClassName)
Sets a flag that determines whether Nails can be executed by class name.void
setDefaultNailClass(java.lang.Class defaultNailClass)
Sets the default class to use for the Nail if no Nails can be found via alias or classname.void
shutdown(boolean exitVM)
Shuts down the server.private static void
usage()
-
-
-
Field Detail
-
DEFAULT_SESSIONPOOLSIZE
public static final int DEFAULT_SESSIONPOOLSIZE
Default size for thread pool- See Also:
- Constant Field Values
-
listeningAddress
private final NGListeningAddress listeningAddress
The address on which to listen
-
serversocket
private java.net.ServerSocket serversocket
The socket doing the listening
-
shutdown
private final java.util.concurrent.atomic.AtomicBoolean shutdown
True if this NGServer has received instructions to shut down
-
running
private final java.util.concurrent.atomic.AtomicBoolean running
True if this NGServer has been started and is accepting connections
-
aliasManager
private final AliasManager aliasManager
This NGServer's AliasManager, which maps aliases to classes
-
allowNailsByClassName
private boolean allowNailsByClassName
If true, fully-qualified classnames are valid commands
-
defaultNailClass
private java.lang.Class defaultNailClass
The default class to use if an invalid alias or classname is specified by the client.
-
sessionPool
private final NGSessionPool sessionPool
A pool of NGSessions ready to handle client connections
-
out
public final java.io.PrintStream out
System.out
at the time of the NGServer's creation
-
err
public final java.io.PrintStream err
System.err
at the time of the NGServer's creation
-
in
public final java.io.InputStream in
System.in
at the time of the NGServer's creation
-
allNailStats
private final java.util.Map allNailStats
a collection of all classes executed by this server so far
-
originalSecurityManager
private java.lang.SecurityManager originalSecurityManager
Remember the security manager we start with so we can restore it later
-
heartbeatTimeoutMillis
private final int heartbeatTimeoutMillis
-
-
Constructor Detail
-
NGServer
public NGServer(java.net.InetAddress addr, int port, int sessionPoolSize, int timeoutMillis)
Creates a new NGServer that will listen at the specified address and on the specified port with the specified session pool size. This does not cause the server to start listening. To do so, create a newThread
wrapping thisNGServer
and start it.- Parameters:
addr
- the address at which to listen, ornull
to bind to all local addressesport
- the port on which to listen.sessionPoolSize
- the max number of idle sessions allowed by the pool
-
NGServer
public NGServer(java.net.InetAddress addr, int port)
Creates a new NGServer that will listen at the specified address and on the specified port with the default session pool size. This does not cause the server to start listening. To do so, create a newThread
wrapping thisNGServer
and start it.- Parameters:
addr
- the address at which to listen, ornull
to bind to all local addressesport
- the port on which to listen.
-
NGServer
public NGServer()
Creates a new NGServer that will listen on the default port (defined inNGConstants.DEFAULT_PORT
). This does not cause the server to start listening. To do so, create a newThread
wrapping thisNGServer
and start it.
-
NGServer
public NGServer(NGListeningAddress listeningAddress, int sessionPoolSize, int timeoutMillis)
Creates a new NGServer that will listen at the specified address and on the specified port with the specified session pool size. This does not cause the server to start listening. To do so, create a newThread
wrapping thisNGServer
and start it.- Parameters:
listeningAddress
- the address at which to listensessionPoolSize
- the max number of idle sessions allowed by the pooltimeoutMillis
- timeout in millis to wait for a heartbeat from the client before disconnecting them
-
-
Method Detail
-
setAllowNailsByClassName
public void setAllowNailsByClassName(boolean allowNailsByClassName)
Sets a flag that determines whether Nails can be executed by class name. If this is false, Nails can only be run via aliases (and you should probably remove ng-alias from the AliasManager).- Parameters:
allowNailsByClassName
- true iff Nail lookups by classname are allowed
-
allowsNailsByClassName
public boolean allowsNailsByClassName()
Returns a flag that indicates whether Nail lookups by classname are allowed. If this is false, Nails can only be run via aliases.- Returns:
- a flag that indicates whether Nail lookups by classname are allowed.
-
setDefaultNailClass
public void setDefaultNailClass(java.lang.Class defaultNailClass)
Sets the default class to use for the Nail if no Nails can be found via alias or classname. (may benull
, in which case NailGun will use its own default)- Parameters:
defaultNailClass
- the default class to use for the Nail if no Nails can be found via alias or classname. (may benull
, in which case NailGun will use its own default)
-
getDefaultNailClass
public java.lang.Class getDefaultNailClass()
Returns the default class that will be used if no Nails can be found via alias or classname.- Returns:
- the default class that will be used if no Nails can be found via alias or classname.
-
getOrCreateStatsFor
private NailStats getOrCreateStatsFor(java.lang.Class nailClass)
Returns the current NailStats object for the specified class, creating a new one if necessary- Parameters:
nailClass
- the class for which we're gathering stats- Returns:
- a NailStats object for the specified class
-
nailStarted
void nailStarted(java.lang.Class nailClass)
Provides a means for an NGSession to register the starting of a nail execution with the server.- Parameters:
nailClass
- the nail class that was launched
-
nailFinished
void nailFinished(java.lang.Class nailClass)
Provides a means for an NGSession to register the completion of a nails execution with the server.- Parameters:
nailClass
- the nail class that finished
-
getNailStats
public java.util.Map getNailStats()
Returns a snapshot of this NGServer's nail statistics. The result is ajava.util.Map
, keyed by class name, with NailStats objects as values.- Returns:
- a snapshot of this NGServer's nail statistics.
-
getAliasManager
public AliasManager getAliasManager()
Returns the AliasManager in use by this NGServer.- Returns:
- the AliasManager in use by this NGServer.
-
shutdown
public void shutdown(boolean exitVM)
Shuts down the server. The server will stop listening and its thread will finish. Any running nails will be allowed to finish.
Any nails that provide a
method will have this method called with this NGServer as its sole parameter.public static void nailShutdown(NGServer)
- Parameters:
exitVM
- if true, this method will also exit the JVM after calling nailShutdown() on any nails. This may prevent currently running nails from exiting gracefully, but may be necessary in order to perform some tasks, such as shutting down any AWT or Swing threads implicitly launched by your nails.
-
isRunning
public boolean isRunning()
Returns true iff the server is currently running.- Returns:
- true iff the server is currently running.
-
getPort
public int getPort()
Returns the port on which this server is (or will be) listening.- Returns:
- the port on which this server is (or will be) listening.
-
run
public void run()
Listens for new connections and launches NGSession threads to process them.- Specified by:
run
in interfacejava.lang.Runnable
-
usage
private static void usage()
-
main
public static void main(java.lang.String[] args) throws java.lang.NumberFormatException, java.net.UnknownHostException
Creates and starts a newNGServer
. A single optional argument is valid, specifying the port on which thisNGServer
should listen. If omitted,NGServer.DEFAULT_PORT
will be used.- Parameters:
args
- a single optional argument specifying the port on which to listen.- Throws:
java.lang.NumberFormatException
- if a non-numeric port is specifiedjava.net.UnknownHostException
-
getHeartbeatTimeout
public int getHeartbeatTimeout()
-
-