[jboss-cvs] JBossAS SVN: r88684 - in projects/naming/trunk/jnpserver/src: test/java/org/jnp/test and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon May 11 19:47:26 EDT 2009


Author: bstansberry at jboss.com
Date: 2009-05-11 19:47:26 -0400 (Mon, 11 May 2009)
New Revision: 88684

Added:
   projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/BootstrapURLUnitTest.java
Modified:
   projects/naming/trunk/jnpserver/src/main/java/org/jnp/server/Main.java
   projects/naming/trunk/jnpserver/src/main/java/org/jnp/server/MainMBean.java
   projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/NamingTestSuite.java
Log:
[JBNAME-33] Expose the bootstrap URL

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	2009-05-11 23:46:36 UTC (rev 88683)
+++ projects/naming/trunk/jnpserver/src/main/java/org/jnp/server/Main.java	2009-05-11 23:47:26 UTC (rev 88684)
@@ -26,6 +26,7 @@
 import java.io.ObjectOutputStream;
 import java.io.OutputStream;
 import java.lang.reflect.Method;
+import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
@@ -35,6 +36,7 @@
 import java.rmi.server.RMIClientSocketFactory;
 import java.rmi.server.RMIServerSocketFactory;
 import java.rmi.server.UnicastRemoteObject;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.Executor;
@@ -98,6 +100,8 @@
    /** 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;
+   /** URLs that clients can use to connect to the bootstrap socket */
+   protected List<String> bootstrapURLs;
    /** A flag indicating if theServer will be set as the NamingContext.setLocal value */
    protected boolean InstallGlobalService = true;
    /** A flag indicating if theServer will try to use the NamingContext.setLocal value */
@@ -275,6 +279,39 @@
    }
 
    
+   /**
+    * Returns a URL suitable for use as a java.naming.provider.url value in
+    * a set of naming environment properties; i.e. one that can be used to 
+    * connect to the lookup socket.
+    * <p>
+    * If there are {@link #getBootstrapURLs() multiple bootstrap URLs}, returns
+    * the first one in the list. TODO: that is is pretty arbitrary
+    * </p>
+    * 
+    * @return the URL, or <code>null</code> if no bound lookup socket exists
+    */
+   public String getBootstrapURL()
+   {    
+      if (bootstrapURLs != null && bootstrapURLs.size() > 0)
+      {
+         return bootstrapURLs.get(0);
+      }
+      return null;
+   }
+   
+   /**
+    * Returns a list of URLs suitable for use as a java.naming.provider.url 
+    * value in a set of naming environment properties; i.e. ones that can be used to 
+    * connect to the lookup socket. There will be one URL per configured
+    * {@link #getBindAddresses() bind address}.
+    * 
+    * @return the URLs, or <code>null</code> if no bound lookup socket exists
+    */
+   public List<String> getBootstrapURLs()
+   {
+      return bootstrapURLs;
+   }
+   
    public boolean isEnableRmiServerHostname()
    {
       return enableRmiServerHostname;
@@ -430,6 +467,10 @@
       {
          log.error("Exception during shutdown", e);
       }
+      finally
+      {
+         bootstrapURLs = null;
+      }
    }
 
    /** This code should be moved to a seperate invoker in the org.jboss.naming
@@ -471,12 +512,16 @@
             lookupExector = Executors.newFixedThreadPool(count, BootstrapThreadFactory.getInstance());
          }
 
+         bootstrapURLs = new ArrayList<String>(addresses.size());
          for(InetAddress address : addresses)
          {
             serverSocket = jnpServerSocketFactory.createServerSocket(port, backlog, address);
             // If an anonymous port was specified get the actual port used
             if( port == 0 )
                port = serverSocket.getLocalPort();
+            
+            bootstrapURLs.add(createBootstrapURL(serverSocket, port));
+            
             String msg = "JNDI bootstrap JNP=" + address + ":" + port
                + ", RMI=" + address + ":" + rmiPort
                + ", backlog="+backlog;
@@ -573,6 +618,30 @@
          serverSocketFactory = null;
       }
    }
+   
+   private static String createBootstrapURL(ServerSocket serverSocket, int port)
+   {   
+      if (serverSocket == null || serverSocket.getInetAddress() == null)
+         return null;
+      
+      // Determine the bootstrap URL
+      StringBuilder sb = new StringBuilder("jnp://");
+      InetAddress addr = serverSocket.getInetAddress();
+      if (addr instanceof Inet6Address)
+      {
+         sb.append('[');
+         sb.append(addr.getHostAddress());
+         sb.append(']');
+      }
+      else
+      {
+         sb.append(addr.getHostAddress());
+      }
+      sb.append(':');
+      sb.append(port);
+      return sb.toString();
+      
+   }
 
    private class AcceptHandler implements Runnable
    {

Modified: projects/naming/trunk/jnpserver/src/main/java/org/jnp/server/MainMBean.java
===================================================================
--- projects/naming/trunk/jnpserver/src/main/java/org/jnp/server/MainMBean.java	2009-05-11 23:46:36 UTC (rev 88683)
+++ projects/naming/trunk/jnpserver/src/main/java/org/jnp/server/MainMBean.java	2009-05-11 23:47:26 UTC (rev 88684)
@@ -25,6 +25,7 @@
 import java.net.UnknownHostException;
 import java.rmi.server.RMIClientSocketFactory;
 import java.rmi.server.RMIServerSocketFactory;
+import java.util.List;
 
 import javax.net.ServerSocketFactory;
 
@@ -53,6 +54,29 @@
 
    void setBacklog(int backlog);   
    int getBacklog();
+   
+   /**
+    * Returns a URL suitable for use as a java.naming.provider.url value in
+    * a set of naming environment properties; i.e. one that can be used to 
+    * connect to the lookup socket.
+    * <p>
+    * If there are {@link #getBootstrapURLs() multiple bootstrap URLs}, returns
+    * the first one in the list. TODO: that is is pretty arbitrary
+    * </p>
+    * 
+    * @return the URL, or <code>null</code> if no bound lookup socket exists
+    */
+   String getBootstrapURL();
+   
+   /**
+    * Returns a list of URLs suitable for use as a java.naming.provider.url 
+    * value in a set of naming environment properties; i.e. ones that can be used to 
+    * connect to the lookup socket. There will be one URL per configured
+    * {@link #getBindAddresses() bind address}.
+    * 
+    * @return the URLs, or <code>null</code> if no bound lookup socket exists
+    */
+   List<String> getBootstrapURLs();
 
    public NamingBean getNamingInfo();
    /**

Added: projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/BootstrapURLUnitTest.java
===================================================================
--- projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/BootstrapURLUnitTest.java	                        (rev 0)
+++ projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/BootstrapURLUnitTest.java	2009-05-11 23:47:26 UTC (rev 88684)
@@ -0,0 +1,127 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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;
+
+import junit.framework.Test;
+
+import org.jboss.test.BaseTestCase;
+import org.jnp.server.Main;
+import org.jnp.server.NamingBeanImpl;
+
+/**
+ *
+ *
+ * @author Brian Stansberry
+ * 
+ * @version $Revision: $
+ */
+public class BootstrapURLUnitTest extends BaseTestCase
+{
+   
+   /** The actual namingMain service impl bean */
+   private static NamingBeanImpl namingBean;
+
+   private Main namingMain;
+   
+   /**
+    * Create a new BootstrapURLUnitTest.
+    * 
+    * @param name
+    */
+   public BootstrapURLUnitTest(String name)
+   {
+      super(name);
+   }
+
+   public static Test suite()
+   {
+      return suite(BootstrapURLUnitTest.class);
+   }
+   
+   
+   
+   @Override
+   protected void setUp() throws Exception
+   {
+      super.setUp();
+      if (namingBean == null)
+      {
+         namingBean = new NamingBeanImpl();
+         namingBean.start();
+      }
+      namingMain = new Main("org.jnp.server");
+      namingMain.setNamingInfo(namingBean);
+   }
+   
+   
+
+   @Override
+   protected void tearDown() throws Exception
+   {
+      super.tearDown();
+      
+      if (namingMain != null)
+      {
+         namingMain.stop();
+      }
+   }
+
+   public void testLocalhost() throws Exception
+   {
+      namingMain.setPort(0);
+      namingMain.setBindAddress("127.0.0.1");
+      namingMain.start();
+      int port = namingMain.getPort();
+      assertTrue(port > 0);
+      assertEquals("jnp://127.0.0.1:" + port, namingMain.getBootstrapURL());
+   }
+
+   public void testIPv6Localhost() throws Exception
+   {
+      namingMain.setPort(0);
+      namingMain.setBindAddress("::1");
+      namingMain.start();
+      int port = namingMain.getPort();
+      assertTrue(port > 0);
+      assertEquals("jnp://[0:0:0:0:0:0:0:1]:" + port, namingMain.getBootstrapURL());
+   }
+   
+   public void testAnyAddress() throws Exception
+   {
+      namingMain.setPort(0);
+      namingMain.setBindAddress("0.0.0.0");
+      namingMain.start();
+      int port = namingMain.getPort();
+      assertTrue(port > 0);
+      assertEquals("jnp://0.0.0.0:" + port, namingMain.getBootstrapURL());      
+   }
+   
+   public void testNoServerSocket() throws Exception
+   {
+      namingMain.setPort(-1);
+      namingMain.setBindAddress("localhost");
+      namingMain.start(); 
+      assertNull(namingMain.getBootstrapURL());
+   }
+
+}


Property changes on: projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/BootstrapURLUnitTest.java
___________________________________________________________________
Name: svn:keywords
   + 

Modified: projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/NamingTestSuite.java
===================================================================
--- projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/NamingTestSuite.java	2009-05-11 23:46:36 UTC (rev 88683)
+++ projects/naming/trunk/jnpserver/src/test/java/org/jnp/test/NamingTestSuite.java	2009-05-11 23:47:26 UTC (rev 88684)
@@ -48,6 +48,7 @@
       suite.addTest(NamingServerSecurityManagerUnitTest.suite());
       suite.addTest(TestJNPSockets.suite());
       suite.addTest(NamingContextUnitTest.suite());
+      suite.addTest(BootstrapURLUnitTest.suite());
       
       return suite;
    }




More information about the jboss-cvs-commits mailing list