org.objectweb.dsrg.sofa.deployment.zeroconf
Class ZeroConfServer

java.lang.Object
  extended by org.objectweb.dsrg.sofa.deployment.zeroconf.ZeroConfServer
All Implemented Interfaces:
ZeroConfServerMBean

public class ZeroConfServer
extends java.lang.Object
implements ZeroConfServerMBean

Zero Configuration Server.

The configuration can be read from file/stream during construction. Later it can be altered via the setProperty(String, String) method.

After a call to listen(...) the server is listening on some port (given or default) for UDP configuration requests to which it responds by sending UDP datagram containing the configuration.

Much of server's functionality can be made available through JMX interface, see ZeroConfServerMBean.

Server can be run as a standalone program or used within another program. For help on running as standalone program, run it with the -h option.


Nested Class Summary
 
Nested classes/interfaces inherited from interface org.objectweb.dsrg.sofa.deployment.zeroconf.ZeroConfServerMBean
ZeroConfServerMBean.LauncherInfo
 
Field Summary
static int DEFAULT_PORT
          Default server port.
 
Constructor Summary
ZeroConfServer()
          Creates a configuration server with empty initial configuration.
ZeroConfServer(java.io.InputStream config)
          Creates a configuration server with initial configuration read from an input stream.
ZeroConfServer(java.io.Reader config)
          Creates a configuration server with initial configuration read from a reader.
ZeroConfServer(java.lang.String configFile)
          Creates a configuration server with initial configuration read from a file.
 
Method Summary
protected  void clearNotRespondingLaunchers()
           
 ZeroConfServerMBean.LauncherInfo[] getLauncherInfos()
          Returns the information about all the registered launchers.
 java.lang.String getProperty(java.lang.String name)
          Returns the value of property name, or null if there is no property named name.
 boolean hangup(java.net.InetSocketAddress sa)
          Stops listening on the given socket address.
 boolean hangup(int port)
          Stops listening on port on local machine.
 boolean hangup(java.lang.String hostname, int port)
          Stops listening on hostname:port.
 void hangupAll()
          Stops listening for requests on any sockets.
 void listen()
          Starts listening for configuration requests on local machine and the default port.
 void listen(java.net.InetSocketAddress sa)
          Starts listening for configuration requests on the given socket address.
 void listen(int port)
          Starts listening for configuration requests on local machine and given port.
 void listen(java.lang.String hostname, int port)
          Starts listening for configuration requests on the given local host and port.
 java.lang.String[] listeningAddresses()
          Returns a list of socket addresses on which the server is listening to requests.
 java.lang.String[] listProperties()
          Returns an array of all set properties in property=value format with special characters escaped as in java.util.Properties stored in a file.
 java.lang.String[] listPropertyNames()
          Returns an array of names of all set properties
static void main(java.lang.String[] args)
          Used to start the server from command line as a standalone process.
 void registerLauncher(java.lang.String url, java.lang.String objectName)
          Registers the launcher in the zeroconf server.
 java.lang.String setProperty(java.lang.String name, java.lang.String value)
          Sets the value of property name to value.
 void startJMX()
          Starts the JMX interface, using default name of the MBean and RMI registry on the default port.
 void startJMX(java.lang.Integer rmiPort, java.lang.String name)
          Starts the JMX interface.
 void stop()
          Stops listening for requests on any sockets and closes the JMX interface.
 boolean stopJMX()
          Stops the JMX interface.
protected  boolean tryResponse(ZeroConfServerMBean.LauncherInfo l)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PORT

public static final int DEFAULT_PORT
Default server port.

See Also:
Constant Field Values
Constructor Detail

ZeroConfServer

public ZeroConfServer()
Creates a configuration server with empty initial configuration.


ZeroConfServer

public ZeroConfServer(java.lang.String configFile)
               throws java.io.FileNotFoundException,
                      java.io.IOException
Creates a configuration server with initial configuration read from a file.

Parameters:
configFile - name of the file to read configuration from. This file has a format of Properties described in Properties.load(Reader). UTF-8 encoding is assumed. If you need to specify another encoding, use ZeroConfServer(Reader) instead.
Throws:
java.io.FileNotFoundException
java.io.IOException

ZeroConfServer

public ZeroConfServer(java.io.InputStream config)
               throws java.io.IOException
Creates a configuration server with initial configuration read from an input stream.

Parameters:
config - input stream to read configuration from. The input has a format of Properties described in Properties.load(Reader). UTF-8 encoding is assumed. If you need to specify another encoding, use ZeroConfServer(Reader) instead.
Throws:
java.io.IOException

ZeroConfServer

public ZeroConfServer(java.io.Reader config)
               throws java.io.IOException
Creates a configuration server with initial configuration read from a reader.

Parameters:
config - reader to read configuration from. The input has a format of Properties described in Properties.load(Reader).
Throws:
java.io.IOException
Method Detail

getProperty

public java.lang.String getProperty(java.lang.String name)
Description copied from interface: ZeroConfServerMBean
Returns the value of property name, or null if there is no property named name.

Specified by:
getProperty in interface ZeroConfServerMBean

setProperty

public java.lang.String setProperty(java.lang.String name,
                                    java.lang.String value)
Description copied from interface: ZeroConfServerMBean
Sets the value of property name to value.

Specified by:
setProperty in interface ZeroConfServerMBean
Returns:
The previous value of property name, or null if none was set.

listPropertyNames

public java.lang.String[] listPropertyNames()
Description copied from interface: ZeroConfServerMBean
Returns an array of names of all set properties

Specified by:
listPropertyNames in interface ZeroConfServerMBean

listProperties

public java.lang.String[] listProperties()
Description copied from interface: ZeroConfServerMBean
Returns an array of all set properties in property=value format with special characters escaped as in java.util.Properties stored in a file.

Specified by:
listProperties in interface ZeroConfServerMBean

listen

public void listen()
            throws java.net.SocketException
Description copied from interface: ZeroConfServerMBean
Starts listening for configuration requests on local machine and the default port. Server will listen in a new thread. This call returns after the thread starts.

Specified by:
listen in interface ZeroConfServerMBean
Throws:
java.net.SocketException - if binding to the default port on local machine fails.

listen

public void listen(int port)
            throws java.net.SocketException
Description copied from interface: ZeroConfServerMBean
Starts listening for configuration requests on local machine and given port. Server will listen in a new thread. This call returns after the thread starts.

Specified by:
listen in interface ZeroConfServerMBean
Parameters:
port - port to listen on
Throws:
java.net.SocketException - if binding to port on local machine fails.

listen

public void listen(java.lang.String hostname,
                   int port)
            throws java.net.SocketException
Description copied from interface: ZeroConfServerMBean
Starts listening for configuration requests on the given local host and port. Server will listen in a new thread. This call returns after the thread starts.

Specified by:
listen in interface ZeroConfServerMBean
Parameters:
hostname - local host name to listen on
port - port to listen on
Throws:
java.net.SocketException - if binding to port on the given host machine fails.

listen

public void listen(java.net.InetSocketAddress sa)
            throws java.net.SocketException
Starts listening for configuration requests on the given socket address. Server will listen in a new thread. This call returns after the thread starts.

Throws:
java.net.SocketException - if binding to sa fails.

listeningAddresses

public java.lang.String[] listeningAddresses()
Description copied from interface: ZeroConfServerMBean
Returns a list of socket addresses on which the server is listening to requests.

Specified by:
listeningAddresses in interface ZeroConfServerMBean

hangupAll

public void hangupAll()
Description copied from interface: ZeroConfServerMBean
Stops listening for requests on any sockets.

Specified by:
hangupAll in interface ZeroConfServerMBean

hangup

public boolean hangup(int port)
Description copied from interface: ZeroConfServerMBean
Stops listening on port on local machine.

Specified by:
hangup in interface ZeroConfServerMBean
Returns:
false if the server was not listening on port on local machine, true otherwise.

hangup

public boolean hangup(java.lang.String hostname,
                      int port)
Description copied from interface: ZeroConfServerMBean
Stops listening on hostname:port.

Specified by:
hangup in interface ZeroConfServerMBean
Returns:
false if the server was not listening on hostname:port, true otherwise.

hangup

public boolean hangup(java.net.InetSocketAddress sa)
Stops listening on the given socket address.

Returns:
false if the server was not listening on sa, true otherwise.

stop

public void stop()
Description copied from interface: ZeroConfServerMBean
Stops listening for requests on any sockets and closes the JMX interface. This will stop all non-daemon threads started by the server. If the server is running as a standalone process, this will effectively cause the server to stop.

Specified by:
stop in interface ZeroConfServerMBean

startJMX

public void startJMX()
              throws java.io.IOException,
                     javax.management.JMException
Starts the JMX interface, using default name of the MBean and RMI registry on the default port.

Throws:
java.io.IOException
javax.management.JMException
See Also:
startJMX(Integer, String)

startJMX

public void startJMX(java.lang.Integer rmiPort,
                     java.lang.String name)
              throws java.io.IOException,
                     javax.management.JMException
Starts the JMX interface. This involves registering the MBean and creating and starting the RMI connector server.

The MBean is registered under name ZeroConfServer:name=name.

The connector will use the RMI registry on port rmiPort.

On return, remote management will be available through service:jmx:rmi:///jndi/rmi://<host>:rmiPort/name, where <host> is a hostname of local machine.

A second call to startJMX() without prior call to stopJMX() will result in an IllegalStateException.

Parameters:
rmiPort - port on which RMI registry is running. If null , default RMI registry port will be used. Remeber to make sure that the RMI registry is running on this port.
name - Optional portion of the MBean's object name and service URL. You need to specify your own name if you want to run more ZeroConfServers on the same machine. If null, defaults to "zeroconf".
Throws:
java.io.IOException - propagates any exception thrown during creation or start of the connector server.
javax.management.JMException - propagates exception thrown during MBean registration.

stopJMX

public boolean stopJMX()
Stops the JMX interface.

Returns:
true if JMX interface was started, false otherwise.

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException,
                        java.net.SocketException,
                        javax.management.JMException
Used to start the server from command line as a standalone process.

Throws:
java.io.IOException
java.net.SocketException
javax.management.JMException

tryResponse

protected boolean tryResponse(ZeroConfServerMBean.LauncherInfo l)

clearNotRespondingLaunchers

protected void clearNotRespondingLaunchers()

registerLauncher

public void registerLauncher(java.lang.String url,
                             java.lang.String objectName)
Description copied from interface: ZeroConfServerMBean
Registers the launcher in the zeroconf server.

Specified by:
registerLauncher in interface ZeroConfServerMBean
Parameters:
url - The jmx service url of the launcher.
objectName - The objectname of the launcher.

getLauncherInfos

public ZeroConfServerMBean.LauncherInfo[] getLauncherInfos()
Description copied from interface: ZeroConfServerMBean
Returns the information about all the registered launchers.

Specified by:
getLauncherInfos in interface ZeroConfServerMBean