[jboss-cvs] JBossAS SVN: r79114 - in projects/naming/trunk/jnpserver/src: main/java/org/jnp/server and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sat Oct 4 10:15:54 EDT 2008


Author: scott.stark at jboss.org
Date: 2008-10-04 10:15:54 -0400 (Sat, 04 Oct 2008)
New Revision: 79114

Added:
   projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/support/ClientSocketFactoryBean.java
   projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/support/ServerSocketFactoryBean.java
   projects/naming/trunk/jnpserver/src/test/resources/org/jnp/test/NamingMCUnitTest#testMainBeanSFs.xml
Modified:
   projects/naming/trunk/jnpserver/src/main/java/org/jnp/interfaces/TimedSocketFactory.java
   projects/naming/trunk/jnpserver/src/main/java/org/jnp/server/Main.java
   projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/NamingMCUnitTest.java
Log:
JBNAME-16, Update Main to use an Executor for the bootstrap socket and test accessing the naming server via rmi with custom socket factories

Modified: projects/naming/trunk/jnpserver/src/main/java/org/jnp/interfaces/TimedSocketFactory.java
===================================================================
--- projects/naming/trunk/jnpserver/src/main/java/org/jnp/interfaces/TimedSocketFactory.java	2008-10-04 09:14:42 UTC (rev 79113)
+++ projects/naming/trunk/jnpserver/src/main/java/org/jnp/interfaces/TimedSocketFactory.java	2008-10-04 14:15:54 UTC (rev 79114)
@@ -24,11 +24,15 @@
 import java.io.IOException;
 import java.net.ConnectException;
 import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.net.Socket;
+import java.net.SocketAddress;
 import java.net.UnknownHostException;
 import java.util.Hashtable;
 import javax.net.SocketFactory;
 
+import org.jboss.logging.Logger;
+
 /** A concrete implementation of the SocketFactory that supports a configurable
  timeout for the initial socket connection as well as the SO_TIMEOUT used to
  determine how long a read will block waiting for data.
@@ -38,6 +42,7 @@
  */
 public class TimedSocketFactory extends SocketFactory
 {
+   private static Logger log = Logger.getLogger(TimedSocketFactory.class);
    public static final String JNP_TIMEOUT = "jnp.timeout";
    public static final String JNP_SO_TIMEOUT = "jnp.sotimeout";
 
@@ -50,7 +55,7 @@
    public TimedSocketFactory()
    {
    }
-   public TimedSocketFactory(Hashtable env)
+   public TimedSocketFactory(Hashtable<String, ?> env)
    {
       String value = (String) env.get(JNP_TIMEOUT);
       if( value != null )
@@ -79,97 +84,18 @@
    public Socket createSocket(InetAddress hostAddr, int port, InetAddress localAddr, int localPort)
       throws IOException
    {
-      Socket socket = null;
-      if( timeout <= 0 )
-         socket = new Socket(hostAddr, port, localAddr, localPort);
-      else
-         socket = createSocket(hostAddr, port, localAddr, localPort, timeout);
-
+      log.debug("createSocket, hostAddr: "+hostAddr+", port: "+port+", localAddr: "+localAddr+", localPort: "+localPort+", timeout: "+timeout);
+      Socket socket = new Socket();
+      InetSocketAddress connectAddr = new InetSocketAddress(hostAddr, port);
+      if(localAddr != null)
+      {
+         // Bind 
+         InetSocketAddress bindAddr = new InetSocketAddress(localAddr, localPort);
+         socket.bind(bindAddr);
+      }
       socket.setSoTimeout(soTimeout);
+      socket.connect(connectAddr, timeout);
       return socket;
    }
 
-   protected Socket createSocket(InetAddress hostAddr, int port,
-      InetAddress localAddr, int localPort, int connectTimeout)
-      throws IOException
-   {
-      ConnectThread t = new ConnectThread();
-      Socket socket = t.createSocket(hostAddr, port, localAddr, localPort, connectTimeout);
-      return socket;
-   }
-
-   /** A subclass of Thread used to time the blocking connect operation
-    and notify the thread attempting the socket connect of a timeout.
-    */
-   class ConnectThread extends Thread
-   {
-      IOException ex;
-      InetAddress hostAddr;
-      InetAddress localAddr;
-      int port;
-      int localPort;
-      int connectTimeout;
-      Socket socket;
-
-      ConnectThread()
-      {
-         super("JNP ConnectThread");
-         super.setDaemon(true);
-      }
-
-      /** Perform the connection in a background thread and wait upto
-       connectTimeout milliseconds for it to complete before throwing
-       a ConnectionException
-       */
-      Socket createSocket(InetAddress hostAddr, int port,
-         InetAddress localAddr, int localPort, int connectTimeout)
-         throws IOException
-      {
-         this.hostAddr = hostAddr;
-         this.port = port;
-         this.localAddr = localAddr;
-         this.localPort = localPort;
-         this.connectTimeout = connectTimeout;
-
-         try
-         {
-            synchronized( this )
-            {
-               // Perform the socket connection in a background thread
-               this.start();
-               // Wait for upto connectTimeout milliseconds for the connection
-               this.wait(connectTimeout);
-            }
-         }
-         catch(InterruptedException e)
-         {
-            throw new ConnectException("Connect attempt timed out");
-         }
-
-         // See if the connect thread exited due to an exception
-         if( ex != null )
-            throw ex;
-         // If socket is null we timed out while waiting
-         if( socket == null )
-            throw new ConnectException("Connect attempt timed out");
-
-         return socket;
-      }
-
-      public void run()
-      {
-         try
-         {
-            socket = new Socket(hostAddr, port, localAddr, localPort);
-            synchronized( this )
-            {
-               this.notify();
-            }
-         }
-         catch(IOException e)
-         {
-            this.ex = e;
-         }
-      }
-   }
 }

Modified: projects/naming/trunk/jnpserver/src/main/java/org/jnp/server/Main.java
===================================================================
--- projects/naming/trunk/jnpserver/src/main/java/org/jnp/server/Main.java	2008-10-04 09:14:42 UTC (rev 79113)
+++ projects/naming/trunk/jnpserver/src/main/java/org/jnp/server/Main.java	2008-10-04 14:15:54 UTC (rev 79114)
@@ -156,6 +156,12 @@
    {
       return lookupExector;
    }
+   /**
+    * Set the Executor to use for bootstrap socket lookup handling. Note
+    * that this must support at least 2 thread to avoid hanging the AcceptHandler
+    * accept loop.
+    * @param lookupExector - An Executor that supports at least 2 threads
+    */
    public void setLookupExector(Executor lookupExector)
    {
       this.lookupExector = lookupExector;
@@ -432,7 +438,7 @@
 
       if( lookupExector == null  )
       {
-         lookupExector = Executors.newSingleThreadExecutor(new ThreadFactory()
+         lookupExector = Executors.newFixedThreadPool(2, new ThreadFactory()
          {
             public Thread newThread(Runnable r)
             {
@@ -557,10 +563,14 @@
          // Return the naming server stub
          try
          {
+            if(log.isTraceEnabled())
+               log.trace("BootstrapRequestHandler.run start");
             OutputStream os = socket.getOutputStream();
             ObjectOutputStream out = new ObjectOutputStream(os);
             out.writeObject(serverStub);
             out.close();
+            if(log.isTraceEnabled())
+               log.trace("BootstrapRequestHandler.run end");
          }
          catch (IOException ex)
          {

Modified: projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/NamingMCUnitTest.java
===================================================================
--- projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/NamingMCUnitTest.java	2008-10-04 09:14:42 UTC (rev 79113)
+++ projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/NamingMCUnitTest.java	2008-10-04 14:15:54 UTC (rev 79114)
@@ -22,6 +22,7 @@
 package org.jnp.test;
 
 import java.math.BigInteger;
+import java.net.InetAddress;
 import java.util.Properties;
 
 import javax.naming.InitialContext;
@@ -30,6 +31,8 @@
 
 import org.jboss.beans.metadata.api.annotations.Inject;
 import org.jboss.test.kernel.junit.MicrocontainerTest;
+import org.jnp.interfaces.NamingContext;
+import org.jnp.interfaces.TimedSocketFactory;
 
 /**
  * Test bootstraping the naming service using the mc
@@ -108,6 +111,12 @@
       validateCtx(ctx);
    }
 
+   /**
+    * Test the org.jnp.server.Main bean that wraps a NamingBean with remote
+    * access via an rmi proxy
+    * 
+    * @throws Exception
+    */
    public void testMainBean()
       throws Exception
    {
@@ -118,6 +127,28 @@
       InitialContext ic = new InitialContext(env);
       validateCtx(ic);
    }
+   /**
+    * Test the org.jnp.server.Main bean that wraps a NamingBean with remote
+    * access via an rmi proxy using custom socket factories
+    * 
+    * @throws Exception
+    */
+   public void testMainBeanSFs()
+      throws Exception
+   {
+      InetAddress localAddr = InetAddress.getLocalHost();
+      getLog().debug("InetAddress.getLocalHost(): "+localAddr);
+      Properties env = new Properties();
+      env.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
+      env.setProperty("java.naming.provider.url", "localhost:2099");
+      env.setProperty("java.naming.factory.url", "org.jboss.naming:org.jnp.interfaces");
+      /*
+      env.setProperty(TimedSocketFactory.JNP_TIMEOUT, "1000");
+      env.setProperty(TimedSocketFactory.JNP_SO_TIMEOUT, "1000");
+      */
+      InitialContext ic = new InitialContext(env);
+      validateCtx(ic);
+   }
 
    /**
     * 
@@ -127,13 +158,13 @@
    protected void validateCtx(InitialContext ic)
       throws Exception
    {
-      Integer i1 = (Integer) ctx.lookup("ints/1");
+      Integer i1 = (Integer) ic.lookup("ints/1");
       assertEquals("ints/1", new Integer(1), i1);
-      String s1 = (String) ctx.lookup("strings/1");
+      String s1 = (String) ic.lookup("strings/1");
       assertEquals("strings/1", "String1", s1);
-      BigInteger bi1 = (BigInteger) ctx.lookup("bigint/1");
+      BigInteger bi1 = (BigInteger) ic.lookup("bigint/1");
       assertEquals("bigint/1", new BigInteger("123456789"), bi1);
-      Properties env = (Properties) ctx.lookup("env-props");
+      Properties env = (Properties) ic.lookup("env-props");
       Properties expected = new Properties();
       expected.setProperty("java.naming.factory.initial", "org.jnp.interfaces.LocalOnlyContextFactory");
       expected.setProperty("java.naming.factory.url", "org.jboss.naming:org.jnp.interfaces");

Added: projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/support/ClientSocketFactoryBean.java
===================================================================
--- projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/support/ClientSocketFactoryBean.java	                        (rev 0)
+++ projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/support/ClientSocketFactoryBean.java	2008-10-04 14:15:54 UTC (rev 79114)
@@ -0,0 +1,45 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jnp.test.support;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.net.Socket;
+import java.rmi.server.RMIClientSocketFactory;
+
+/**
+ * @author Scott.Stark at jboss.org
+ * @version $Revision:$
+ */
+public class ClientSocketFactoryBean
+   implements RMIClientSocketFactory, Serializable
+{
+   private static final long serialVersionUID = 1;
+
+   public Socket createSocket(String host, int port) throws IOException
+   {
+      System.out.println("ClientSocketFactoryBean, createSocket host:"+host+", port:"+port);
+      Socket s = new Socket(host, port);
+      s.setSoTimeout(5000);
+      return s;
+   }
+}

Added: projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/support/ServerSocketFactoryBean.java
===================================================================
--- projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/support/ServerSocketFactoryBean.java	                        (rev 0)
+++ projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/support/ServerSocketFactoryBean.java	2008-10-04 14:15:54 UTC (rev 79114)
@@ -0,0 +1,67 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jnp.test.support;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.rmi.server.RMIServerSocketFactory;
+
+/**
+ * @author Scott.Stark at jboss.org
+ * @version $Revision:$
+ */
+public class ServerSocketFactoryBean implements RMIServerSocketFactory
+{
+   private InetAddress rmiBindAddress;
+   private int backlog = 0;
+   
+   
+   public InetAddress getRmiBindAddress()
+   {
+      return rmiBindAddress;
+   }
+   public void setRmiBindAddress(InetAddress rmiBindAddress)
+   {
+      this.rmiBindAddress = rmiBindAddress;
+      // Set the java.rmi.server.hostname to the bind address if not set
+      if(System.getProperty("java.rmi.server.hostname") == null)
+         System.setProperty("java.rmi.server.hostname", rmiBindAddress.getHostName());
+   }
+
+   public int getBacklog()
+   {
+      return backlog;
+   }
+   public void setBacklog(int backlog)
+   {
+      this.backlog = backlog;
+   }
+
+   public ServerSocket createServerSocket(int port) throws IOException
+   {
+      System.out.println("ServerSocketFactoryBean, createServerSocket port: "+port+", bindAddr: "+rmiBindAddress);
+      ServerSocket ss = new ServerSocket(port, backlog, rmiBindAddress);
+      return ss;
+   }
+
+}

Added: projects/naming/trunk/jnpserver/src/test/resources/org/jnp/test/NamingMCUnitTest#testMainBeanSFs.xml
===================================================================
--- projects/naming/trunk/jnpserver/src/test/resources/org/jnp/test/NamingMCUnitTest#testMainBeanSFs.xml	                        (rev 0)
+++ projects/naming/trunk/jnpserver/src/test/resources/org/jnp/test/NamingMCUnitTest#testMainBeanSFs.xml	2008-10-04 14:15:54 UTC (rev 79114)
@@ -0,0 +1,107 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    Test the org.jnp.server.Main bean with custom socket factories
+    $Id:$
+-->
+<deployment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="urn:jboss:bean-deployer bean-deployer_2_0.xsd"
+    xmlns="urn:jboss:bean-deployer:2.0">
+
+    <bean name="InitialContextFactory" class="org.jboss.naming.InitialContextFactoryBean">
+        <property name="env">
+            <map class="java.util.Properties" keyClass="java.lang.String" valueClass="java.lang.String">
+                <entry>
+                    <key>java.naming.factory.initial</key>
+                    <value>org.jnp.interfaces.LocalOnlyContextFactory</value>
+                </entry>
+                <entry>
+                    <key>java.naming.factory.url</key>
+                    <value>org.jboss.naming:org.jnp.interfaces</value>
+                </entry>
+            </map>
+        </property>
+        <depends>testLocaNamingBeanImpl</depends>
+    </bean>
+    <bean name="JndiBindings" class="org.jboss.naming.BindingsInitializer">
+        <property name="ctx">
+            <inject bean="InitialContextFactory" property="ctx"/>
+        </property>
+        <property name="bindings">
+            <map keyClass="java.lang.String">
+                <entry>
+                    <key>ints/1</key>
+                    <value class="java.lang.Integer">1</value>
+                </entry>
+                <entry>
+                    <key>strings/1</key>
+                    <value class="java.lang.String">String1</value>
+                </entry>
+                <entry>
+                    <key>bigint/1</key>
+                    <value class="java.math.BigInteger">123456789</value>
+                </entry>
+                <entry>
+                    <key>env-props</key>
+                    <value>
+                        <map class="java.util.Properties" keyClass="java.lang.String" valueClass="java.lang.String">
+                        <entry>
+                            <key>java.naming.factory.initial</key>
+                            <value>org.jnp.interfaces.LocalOnlyContextFactory</value>
+                        </entry>
+                        <entry>
+                            <key>java.naming.factory.url</key>
+                            <value>org.jboss.naming:org.jnp.interfaces</value>
+                        </entry>
+                        </map>
+                    </value>
+                </entry>
+            </map>
+        </property>
+    </bean>
+
+    <bean name="LookupPool">
+        <constructor factoryMethod="newFixedThreadPool"
+            factoryClass="java.util.concurrent.Executors">
+            <parameter>2</parameter>
+        </constructor>
+    </bean>
+    <bean name="testLocaNamingBeanImpl" class="org.jnp.server.NamingBeanImpl">
+        <!-- Install this bean as the global JVM NamingServer -->
+        <property name="installGlobalService">true</property>
+        
+        <property name="useGlobalService">false</property>
+    </bean>
+    <bean name="jboss:service=Naming" class="org.jnp.server.Main">
+        <property name="namingInfo"><inject bean="testLocaNamingBeanImpl" /></property>
+        <!-- The call by value mode. true if all lookups are unmarshalled using
+            the caller's TCL, false if in VM lookups return the value by reference.
+        -->
+        <property name="callByValue">false</property>
+        
+        <!-- The listening port for the bootstrap JNP service. Set this to -1
+            to run the NamingService without the JNP invoker listening port.
+        -->
+        <property name="port">2099</property>
+
+        <!-- The bootstrap JNP server bind address. This also sets the default
+            RMI service bind address. Empty == all addresses
+        -->
+        <property name="bindAddress">${jboss.bind.address:localhost}</property>
+        <!-- The port of the RMI naming service, 0 == anonymous -->
+        <property name="rmiPort">2100</property>
+        <!-- The thread pool service used to control the bootstrap lookups -->
+        <property name="lookupExector"><inject bean="LookupPool"/></property>
+
+        <property name="serverSocketFactoryBean"><inject bean="ServerSocketFactoryBean"/></property>
+        <property name="clientSocketFactoryBean"><inject bean="ClientSocketFactoryBean"/></property>
+    </bean>
+
+    <bean name="ServerSocketFactoryBean" class="org.jnp.test.support.ServerSocketFactoryBean">
+        <!-- The RMI service bind address. Empty == all addresses
+        -->
+        <property name="rmiBindAddress">${jboss.bind.address:localhost}</property>
+    </bean>
+    <bean name="ClientSocketFactoryBean" class="org.jnp.test.support.ClientSocketFactoryBean">
+    </bean>
+
+</deployment>




More information about the jboss-cvs-commits mailing list