svn commit: r814390 - in /ofbiz/trunk/framework/base: config/ofbiz-containers.xml src/org/ofbiz/base/container/NamingServiceContainer.java src/org/ofbiz/base/util/RMIExtendedSocketFactory.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r814390 - in /ofbiz/trunk/framework/base: config/ofbiz-containers.xml src/org/ofbiz/base/container/NamingServiceContainer.java src/org/ofbiz/base/util/RMIExtendedSocketFactory.java

jleroux@apache.org
Author: jleroux
Date: Sun Sep 13 21:49:21 2009
New Revision: 814390

URL: http://svn.apache.org/viewvc?rev=814390&view=rev
Log:
A patch from Deyan Tsvetanov "NamingServiceContainer binds to all network interfaces." (https://issues.apache.org/jira/browse/OFBIZ-2559) - OFBIZ-2559
org.ofbiz.base.container.NamingServiceContainer by default binds a server socket to all available network interfaces. Configuration of this container is done in
framework/base/config/ofbiz-containers.xml

<!-- load the naming (JNDI) server -->
<container name="naming-container" class="org.ofbiz.base.container.NamingServiceContainer">
<property name="port" value="1099"/>
</container>

Only the port can be configured.
Additional configuration property needs to be implemented:

<property name="host" value="0.0.0.0"/>

Added:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/RMIExtendedSocketFactory.java
Modified:
    ofbiz/trunk/framework/base/config/ofbiz-containers.xml
    ofbiz/trunk/framework/base/src/org/ofbiz/base/container/NamingServiceContainer.java

Modified: ofbiz/trunk/framework/base/config/ofbiz-containers.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/config/ofbiz-containers.xml?rev=814390&r1=814389&r2=814390&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/config/ofbiz-containers.xml (original)
+++ ofbiz/trunk/framework/base/config/ofbiz-containers.xml Sun Sep 13 21:49:21 2009
@@ -37,6 +37,7 @@
 
     <!-- load the naming (JNDI) server -->
     <container name="naming-container" class="org.ofbiz.base.container.NamingServiceContainer">
+        <property name="host" value="0.0.0.0"/>
         <property name="port" value="1099"/>
     </container>
 

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/container/NamingServiceContainer.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/container/NamingServiceContainer.java?rev=814390&r1=814389&r2=814390&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/container/NamingServiceContainer.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/container/NamingServiceContainer.java Sun Sep 13 21:49:21 2009
@@ -18,12 +18,15 @@
  *******************************************************************************/
 package org.ofbiz.base.container;
 
+import java.net.UnknownHostException;
 import java.rmi.registry.Registry;
 import java.rmi.registry.LocateRegistry;
 import java.rmi.RemoteException;
 import java.rmi.NoSuchObjectException;
 import java.rmi.server.UnicastRemoteObject;
 
+import org.ofbiz.base.util.RMIExtendedSocketFactory;
+
 /**
  * NamingServiceContainer
  *
@@ -37,6 +40,9 @@
     protected boolean isRunning = false;
     protected Registry registry = null;
     protected int namingPort = 1099;
+    protected String namingHost = null;
+
+    protected RMIExtendedSocketFactory rmiSocketFactory;
 
     public void init(String[] args, String configFile) throws ContainerException {
         this.configFileLocation = configFile;
@@ -53,11 +59,23 @@
             }
         }
 
+        // get the telnet-host
+        ContainerConfig.Container.Property host = cfg.getProperty("host");
+        if (host != null && host.value != null) {
+            this.namingHost =  host.value ;
+        }
+
+        try {
+            rmiSocketFactory = new RMIExtendedSocketFactory( namingHost );
+        } catch ( UnknownHostException uhEx ) {
+            throw new ContainerException("Invalid host defined in container [naming-container] configuration; not a valid IP address", uhEx);
+        }
+
     }
 
     public boolean start() throws ContainerException {
         try {
-            registry = LocateRegistry.createRegistry(namingPort);
+            registry = LocateRegistry.createRegistry(namingPort, rmiSocketFactory, rmiSocketFactory);
         } catch (RemoteException e) {
             throw new ContainerException("Unable to locate naming service", e);
         }

Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/RMIExtendedSocketFactory.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/RMIExtendedSocketFactory.java?rev=814390&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/RMIExtendedSocketFactory.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/RMIExtendedSocketFactory.java Sun Sep 13 21:49:21 2009
@@ -0,0 +1,86 @@
+package org.ofbiz.base.util;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.UnknownHostException;
+import java.rmi.server.RMISocketFactory;
+
+/**
+ * A <code>RMISocketFactory</code> implementation that creates <code>ServerSocket</code>s bound
+ * on a specified network interface.  
+ */
+public class RMIExtendedSocketFactory extends RMISocketFactory {
+    
+    /**
+     * The network interface to bind the <code>ServerSocket</code> to. If null than bind to all interfaces.
+     */
+    private InetAddress hostInetAddress;
+    
+    /**
+     * Default constructor. Bind the server sockets on all interfaces.
+     */
+    public RMIExtendedSocketFactory() {
+        // leave hostInetAddress null
+    }
+    
+    /**
+     * Creates a new <code>RMIExtendedSocketFactory</code> which will create <code>ServerSocket</code>s
+     * bound on the specified network interface.
+     *
+     * @param inetAddress The <code>InetAddress</code> of the network interface.
+     */
+    public RMIExtendedSocketFactory( InetAddress inetAddress ) {
+        this.hostInetAddress = inetAddress;
+    }
+
+    /**
+     * Creates a new <code>RMIExtendedSocketFactory</code> which will create <code>ServerSocket</code>s
+     * bound on the specified network interface.
+     *
+     * @param host The IP address of the interface to bind the server sockets to.
+     * @throws UnknownHostException If an invalid IP address is provided.
+     */
+    public RMIExtendedSocketFactory( String hostIpAddress ) throws UnknownHostException {
+        
+        // check if host length is at least equal to "0.0.0.0"
+        if ( hostIpAddress != null && hostIpAddress.length() >= 7 ) {
+            String[] octets = hostIpAddress.split( "\\." );
+            
+            if ( octets == null || octets.length != 4 ) {
+                throw new UnknownHostException( "Invalid IP address: " + hostIpAddress );
+            }
+            
+            byte[] ipAddr = new byte[4];
+            for ( int i = 0; i < octets.length; i++ ) {
+                try {
+                    ipAddr[i] = ( byte ) Integer.parseInt( octets[i] );
+                } catch ( NumberFormatException nfEx ) {
+                    throw new UnknownHostException( "Invalid IP address: " + hostIpAddress );
+                }
+            }
+            
+            hostInetAddress = InetAddress.getByAddress( ipAddr );
+            
+        }
+        
+        
+    }
+    
+    @Override
+    public ServerSocket createServerSocket(int port) throws IOException {
+        if ( hostInetAddress !=  null ) {
+            return new ServerSocket( port, 0, hostInetAddress );
+        } else {
+            return new ServerSocket( port );
+        }
+    }
+
+    @Override
+    public Socket createSocket(String host, int port) throws IOException {
+        
+        return new Socket( host, port );
+    }
+
+}