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.
Modifier and Type | Class and Description |
---|---|
private static class |
NGServer.NGServerShutdowner
A shutdown hook that will cleanly bring down the NGServer if it is
interrupted.
|
Modifier and Type | Field and Description |
---|---|
private java.net.InetAddress |
addr
The address on which to listen, or null to listen on all local addresses
|
private AliasManager |
aliasManager
This NGServer's AliasManager, which maps aliases to classes
|
private java.util.Map |
allNailStats
a collection of all classes executed by this server so far
|
private boolean |
allowNailsByClassName
If true, fully-qualified classnames are valid commands
|
static int |
DEFAULT_SESSIONPOOLSIZE
Default size for thread pool
|
private 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 creation |
java.io.InputStream |
in
System.in at the time of the NGServer's creation |
private java.lang.SecurityManager |
originalSecurityManager
Remember the security manager we start with so we can restore it later
|
java.io.PrintStream |
out
System.out at the time of the NGServer's creation |
private int |
port
The port on which to listen, or zero to select a port automatically
|
private boolean |
running
True if this NGServer has been started and is accepting connections
|
private java.net.ServerSocket |
serversocket
The socket doing the listening
|
private NGSessionPool |
sessionPool
A pool of NGSessions ready to handle client connections
|
private boolean |
shutdown
True if this NGServer has received instructions to shut down
|
Constructor and Description |
---|
NGServer()
Creates a new NGServer that will listen on the default port (defined in
NGConstants.DEFAULT_PORT ). |
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)
Creates a new NGServer that will listen at the specified address and on
the specified port with the specified session pool size.
|
Modifier and Type | Method and 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.
|
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 necessary
|
int |
getPort()
Returns the port on which this server is (or will be) listening.
|
private void |
init(java.net.InetAddress addr,
int port,
int sessionPoolSize)
Sets up the NGServer internals
|
boolean |
isRunning()
Returns true iff the server is currently running.
|
static void |
main(java.lang.String[] args)
Creates and starts a new
NGServer . |
(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() |
public static final int DEFAULT_SESSIONPOOLSIZE
private java.net.InetAddress addr
private int port
private java.net.ServerSocket serversocket
private boolean shutdown
private boolean running
private AliasManager aliasManager
private boolean allowNailsByClassName
private java.lang.Class defaultNailClass
private NGSessionPool sessionPool
public final java.io.PrintStream out
System.out
at the time of the NGServer's creationpublic final java.io.PrintStream err
System.err
at the time of the NGServer's creationpublic final java.io.InputStream in
System.in
at the time of the NGServer's creationprivate java.util.Map allNailStats
private java.lang.SecurityManager originalSecurityManager
public NGServer(java.net.InetAddress addr, int port, int sessionPoolSize)
Thread
wrapping this
NGServer
and start it.addr
- the address at which to listen, or
null
to bind to all local addressesport
- the port on which to listen.sessionPoolSize
- the max number of idle sessions allowed by the
poolpublic NGServer(java.net.InetAddress addr, int port)
Thread
wrapping this
NGServer
and start it.addr
- the address at which to listen, or
null
to bind to all local addressesport
- the port on which to listen.sessionPoolSize
- the max number of idle sessions allowed by the
poolpublic NGServer()
NGConstants.DEFAULT_PORT
). This does not cause the
server to start listening. To do so, create a new
Thread
wrapping this
NGServer
and start it.private void init(java.net.InetAddress addr, int port, int sessionPoolSize)
addr
- the InetAddress to bind toport
- the port on which to listensessionPoolSize
- the max number of idle sessions allowed by the
poolpublic void setAllowNailsByClassName(boolean allowNailsByClassName)
allowNailsByClassName
- true iff Nail lookups by classname are
allowedpublic boolean allowsNailsByClassName()
public void setDefaultNailClass(java.lang.Class defaultNailClass)
null
, in which case NailGun will use its own default)defaultNailClass
- the default class to use for the Nail if no Nails
can be found via alias or classname. (may be
null
, in which case NailGun will use its own default)public java.lang.Class getDefaultNailClass()
private NailStats getOrCreateStatsFor(java.lang.Class nailClass)
nailClass
- the class for which we're gathering statsvoid nailStarted(java.lang.Class nailClass)
nailClass
- the nail class that was launchedvoid nailFinished(java.lang.Class nailClass)
nailClass
- the nail class that finishedpublic java.util.Map getNailStats()
java.util.Map
, keyed by class name, with NailStats objects as values.public AliasManager getAliasManager()
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
public static void nailShutdown(NGServer)
method
will have this method called with this NGServer as its sole
parameter.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.public boolean isRunning()
public int getPort()
public void run()
run
in interface java.lang.Runnable
private static void usage()
public static void main(java.lang.String[] args) throws java.lang.NumberFormatException, java.net.UnknownHostException
NGServer
. A single optional argument is valid, specifying
the port on which this
NGServer
should listen. If omitted,
NGServer.DEFAULT_PORT
will be used.args
- a single optional argument specifying the port on which to
listen.java.lang.NumberFormatException
- if a non-numeric port is specifiedjava.net.UnknownHostException