User: starksm Date: 01/07/19 13:09:40 Modified: src/main/org/jnp/server Tag: Branch_2_4 Main.java MainMBean.java Log: Add support for binding the jnp port on a specific address Revision Changes Path No revision No revision 1.7.2.1 +255 -208 jnp/src/main/org/jnp/server/Main.java Index: Main.java =================================================================== RCS file: /cvsroot/jboss/jnp/src/main/org/jnp/server/Main.java,v retrieving revision 1.7 retrieving revision 1.7.2.1 diff -u -r1.7 -r1.7.2.1 --- Main.java 2001/06/22 19:08:25 1.7 +++ Main.java 2001/07/19 20:09:40 1.7.2.1 @@ -8,10 +8,13 @@ package org.jnp.server; import java.io.FileNotFoundException; +import java.io.InputStream; import java.io.IOException; import java.io.ObjectOutputStream; +import java.net.InetAddress; import java.net.Socket; import java.net.ServerSocket; +import java.net.UnknownHostException; import java.net.URL; import java.rmi.Remote; import java.rmi.RemoteException; @@ -31,219 +34,263 @@ import org.jnp.interfaces.java.javaURLContextFactory; /** A main() entry point for running the jnp naming service implementation as -a standalone process. - -@author oberg -@author [EMAIL PROTECTED] -@version $Revision: 1.7 $ -*/ -public class Main - implements Runnable, MainMBean + a standalone process. + + @author oberg + @author [EMAIL PROTECTED] + @version $Revision: 1.7.2.1 $ + */ +public class Main implements Runnable, MainMBean { - // Constants ----------------------------------------------------- - - // Attributes ---------------------------------------------------- - /** The Naming interface server implementation */ - protected NamingServer theServer; - protected MarshalledObject serverStub; - /** The jnp server socket through which the NamingServer stub is vended */ - protected ServerSocket serverSocket; - /** An optional custom client socket factory */ - protected RMIClientSocketFactory clientSocketFactory; - /** An optional custom server socket factory */ - protected RMIServerSocketFactory serverSocketFactory; - /** The class name of the optional custom client socket factory */ - protected String clientSocketFactoryName; - /** The class name of the optional custom server socket factory */ - protected String serverSocketFactoryName; - /** The jnp protocol listening port. The default is 1099, the same as - the RMI registry default port. */ - protected int port = 1099; - /** The RMI port on which the Naming implementation will be exported. The - default is 0 which means use any available port. */ - protected int rmiPort = 0; - protected Category log; + // Constants ----------------------------------------------------- + + // Attributes ---------------------------------------------------- + /** The Naming interface server implementation */ + protected NamingServer theServer; + protected MarshalledObject serverStub; + /** The jnp server socket through which the NamingServer stub is vended */ + protected ServerSocket serverSocket; + /** An optional custom client socket factory */ + protected RMIClientSocketFactory clientSocketFactory; + /** An optional custom server socket factory */ + protected RMIServerSocketFactory serverSocketFactory; + /** The class name of the optional custom client socket factory */ + protected String clientSocketFactoryName; + /** The class name of the optional custom server socket factory */ + protected String serverSocketFactoryName; + /** The interface to bind to. This is useful for multi-homed hosts + that want control over which interfaces accept connections. + */ + protected InetAddress bindAddress; + /** The serverSocket listen queue depth */ + protected int backlog = 50; + /** The jnp protocol listening port. The default is 1099, the same as + the RMI registry default port. */ + protected int port = 1099; + /** The RMI port on which the Naming implementation will be exported. The + default is 0 which means use any available port. */ + protected int rmiPort = 0; + protected Category log; - // Static -------------------------------------------------------- - public static void main(String[] args) + // Static -------------------------------------------------------- + public static void main(String[] args) throws Exception - { - // Make sure the config file can be found - ClassLoader loader = Thread.currentThread().getContextClassLoader(); - URL url = loader.getResource("log4j.properties"); - if( url == null ) - System.err.println("Failed to find log4j.properties"); - else - PropertyConfigurator.configure(url); - new Main().start(); - } - - // Constructors -------------------------------------------------- - public Main() - { - this("Naming"); - } - public Main(String categoryName) - { - // Load properties from properties file - try - { - System.getProperties().load(getClass().getClassLoader().getResourceAsStream("jnp.properties")); - } catch (Exception e) - { + { + // Make sure the config file can be found + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + URL url = loader.getResource("log4j.properties"); + if( url == null ) + System.err.println("Failed to find log4j.properties"); + else + PropertyConfigurator.configure(url); + new Main().start(); + } + + // Constructors -------------------------------------------------- + public Main() + { + this("org.jboss.naming.Naming"); + } + public Main(String categoryName) + { + // Load properties from properties file + try + { + ClassLoader loader = getClass().getClassLoader(); + InputStream is = loader.getResourceAsStream("jnp.properties"); + System.getProperties().load(is); + } + catch (Exception e) + { // Ignore - } + } - // Set configuration from the system properties - setPort(Integer.getInteger("jnp.port",getPort()).intValue()); - setRmiPort(Integer.getInteger("jnp.rmiPort",getRmiPort()).intValue()); - log = Category.getInstance(categoryName); - } - - // Public -------------------------------------------------------- - public void setRmiPort(int p) { rmiPort = p; } - public int getRmiPort() { return rmiPort; } - - public void setPort(int p) { port = p; } - public int getPort() { return port; } - - public String getClientSocketFactory() - { - return serverSocketFactoryName; - } - public void setClientSocketFactory(String factoryClassName) - throws ClassNotFoundException, InstantiationException, IllegalAccessException - { - this.clientSocketFactoryName = factoryClassName; - ClassLoader loader = Thread.currentThread().getContextClassLoader(); - Class clazz = loader.loadClass(clientSocketFactoryName); - clientSocketFactory = (RMIClientSocketFactory) clazz.newInstance(); - } - - public String getServerSocketFactory() - { - return serverSocketFactoryName; - } - public void setServerSocketFactory(String factoryClassName) - throws ClassNotFoundException, InstantiationException, IllegalAccessException - { - this.serverSocketFactoryName = factoryClassName; - ClassLoader loader = Thread.currentThread().getContextClassLoader(); - Class clazz = loader.loadClass(serverSocketFactoryName); - serverSocketFactory = (RMIServerSocketFactory) clazz.newInstance(); - } - - public void start() + // Set configuration from the system properties + setPort(Integer.getInteger("jnp.port",getPort()).intValue()); + setRmiPort(Integer.getInteger("jnp.rmiPort",getRmiPort()).intValue()); + log = Category.getInstance(categoryName); + } + + // Public -------------------------------------------------------- + public void setRmiPort(int p) + { + rmiPort = p; + } + public int getRmiPort() + { + return rmiPort; + } + + public void setPort(int p) + { + port = p; + } + public int getPort() + { + return port; + } + + public String getBindAddress() + { + String address = null; + if( bindAddress != null ) + address = bindAddress.getHostAddress(); + return address; + } + public void setBindAddress(String host) throws UnknownHostException + { + bindAddress = InetAddress.getByName(host); + } + + public int getBacklog() + { + return backlog; + } + public void setBacklog(int backlog) + { + if( backlog <= 0 ) + backlog = 50; + this.backlog = backlog; + } + + public String getClientSocketFactory() + { + return serverSocketFactoryName; + } + public void setClientSocketFactory(String factoryClassName) + throws ClassNotFoundException, InstantiationException, IllegalAccessException + { + this.clientSocketFactoryName = factoryClassName; + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + Class clazz = loader.loadClass(clientSocketFactoryName); + clientSocketFactory = (RMIClientSocketFactory) clazz.newInstance(); + } + + public String getServerSocketFactory() + { + return serverSocketFactoryName; + } + public void setServerSocketFactory(String factoryClassName) + throws ClassNotFoundException, InstantiationException, IllegalAccessException + { + this.serverSocketFactoryName = factoryClassName; + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + Class clazz = loader.loadClass(serverSocketFactoryName); + serverSocketFactory = (RMIServerSocketFactory) clazz.newInstance(); + } + + public void start() throws Exception - { - log.info("Starting jnp server"); - // Create remote object - theServer = new NamingServer(); - - // Set local server reference - NamingContext.setLocal(theServer); - - // Export server - Remote stub = UnicastRemoteObject.exportObject(theServer, rmiPort, clientSocketFactory, serverSocketFactory); - serverStub = new MarshalledObject(stub); - - // Start listener - try - { - serverSocket = null; - serverSocket = new ServerSocket(port); - // If an anonymous port was specified get the actual port used - if( port == 0 ) - port = serverSocket.getLocalPort(); - String msg = "Started jnpPort=" + port +", rmiPort=" + rmiPort - + ", Client SocketFactory="+clientSocketFactory+", Server SocketFactory="+serverSocketFactory; - log.info(msg); - listen(); - } catch (IOException e) - { - log.error("Could not start on port " + port, e); - } - } - - public void stop() - { - try - { - log.info("Stopping"); - - // Unexport server - UnicastRemoteObject.unexportObject(theServer, false); - - // Stop listener - ServerSocket s = serverSocket; - serverSocket = null; - s.close(); - log.info("Stopped"); - } catch (Exception e) - { - log.error("Exception during shutdown", e); - } - } - - // Runnable implementation --------------------------------------- - public void run() - { - Socket socket = null; - - // Accept a connection - try - { - socket = serverSocket.accept(); - } catch (IOException e) - { - if (serverSocket == null) return; // Stopped by normal means - log.error("Naming stopped", e); - log.info("Restarting naming"); - try - { - start(); - } catch (Exception ex) - { - log.error("Restart failed", ex); - return; - } - } - - // Create a new thread to accept the next connection - listen(); - - // Return the naming server stub - try - { - ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); - out.writeObject(serverStub); - } - catch (IOException ex) - { - log.error("Error writing response", ex); - } - finally - { - try - { - socket.close(); - } catch (IOException e) - { - } - } - } - - // Y overrides --------------------------------------------------- - - // Package protected --------------------------------------------- - - // Protected ----------------------------------------------------- - protected void listen() - { + { + log.info("Starting jnp server"); + // Create remote object + theServer = new NamingServer(); + + // Set local server reference + NamingContext.setLocal(theServer); + + // Export server + Remote stub = UnicastRemoteObject.exportObject(theServer, rmiPort, clientSocketFactory, serverSocketFactory); + serverStub = new MarshalledObject(stub); + + // Start listener + try + { + serverSocket = null; + serverSocket = new ServerSocket(port, backlog, bindAddress); + // If an anonymous port was specified get the actual port used + if( port == 0 ) + port = serverSocket.getLocalPort(); + String msg = "Started jnpPort=" + port +", rmiPort=" + rmiPort + + ", backlog="+backlog+", bindAddress="+bindAddress + + ", Client SocketFactory="+clientSocketFactory+", Server SocketFactory="+serverSocketFactory; + log.info(msg); + listen(); + } catch (IOException e) + { + log.error("Could not start on port " + port, e); + } + } + + public void stop() + { + try + { + log.info("Stopping"); + + // Unexport server + UnicastRemoteObject.unexportObject(theServer, false); + + // Stop listener + ServerSocket s = serverSocket; + serverSocket = null; + s.close(); + log.info("Stopped"); + } catch (Exception e) + { + log.error("Exception during shutdown", e); + } + } + + // Runnable implementation --------------------------------------- + public void run() + { + Socket socket = null; + + // Accept a connection + try + { + socket = serverSocket.accept(); + } catch (IOException e) + { + if (serverSocket == null) return; // Stopped by normal means + log.error("Naming stopped", e); + log.info("Restarting naming"); + try + { + start(); + } catch (Exception ex) + { + log.error("Restart failed", ex); + return; + } + } + + // Create a new thread to accept the next connection + listen(); + + // Return the naming server stub + try + { + ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); + out.writeObject(serverStub); + } + catch (IOException ex) + { + log.error("Error writing response", ex); + } + finally + { + try + { + socket.close(); + } catch (IOException e) + { + } + } + } + + // Y overrides --------------------------------------------------- + + // Package protected --------------------------------------------- + + // Protected ----------------------------------------------------- + protected void listen() + { new Thread(this).start(); - } - - // Private ------------------------------------------------------- - - // Inner classes ------------------------------------------------- + } + + // Private ------------------------------------------------------- + + // Inner classes ------------------------------------------------- } 1.4.2.1 +9 -1 jnp/src/main/org/jnp/server/MainMBean.java Index: MainMBean.java =================================================================== RCS file: /cvsroot/jboss/jnp/src/main/org/jnp/server/MainMBean.java,v retrieving revision 1.4 retrieving revision 1.4.2.1 diff -u -r1.4 -r1.4.2.1 --- MainMBean.java 2001/05/30 01:59:10 1.4 +++ MainMBean.java 2001/07/19 20:09:40 1.4.2.1 @@ -7,11 +7,13 @@ */ package org.jnp.server; +import java.net.UnknownHostException; + /** The Mbean interface for the jnp provider server. * * @author oberg * @author [EMAIL PROTECTED] - * @version $Revision: 1.4 $ + * @version $Revision: 1.4.2.1 $ */ public interface MainMBean { @@ -23,6 +25,12 @@ public void setPort(int p); public int getPort(); + + public String getBindAddress(); + public void setBindAddress(String host) throws UnknownHostException; + + public int getBacklog(); + public void setBacklog(int backlog); public String getClientSocketFactory(); public void setClientSocketFactory(String factoryClassName) _______________________________________________ Jboss-development mailing list [EMAIL PROTECTED] http://lists.sourceforge.net/lists/listinfo/jboss-development