[jboss-cvs] JBossAS SVN: r89425 - in branches/JBPAPP_4_2_0_GA_CP: testsuite/src/main/org/jboss/test/web/test and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue May 26 04:44:10 EDT 2009


Author: dpospisi at redhat.com
Date: 2009-05-26 04:44:10 -0400 (Tue, 26 May 2009)
New Revision: 89425

Modified:
   branches/JBPAPP_4_2_0_GA_CP/naming/src/main/org/jnp/interfaces/NamingContext.java
   branches/JBPAPP_4_2_0_GA_CP/testsuite/src/main/org/jboss/test/web/test/SSOBaseCase.java
Log:
JBPAPP-1522, JBNAME-25 - Add support for parsing IPv6 addresses in org.jnp.interfaces.NamingContext


Modified: branches/JBPAPP_4_2_0_GA_CP/naming/src/main/org/jnp/interfaces/NamingContext.java
===================================================================
--- branches/JBPAPP_4_2_0_GA_CP/naming/src/main/org/jnp/interfaces/NamingContext.java	2009-05-26 08:29:31 UTC (rev 89424)
+++ branches/JBPAPP_4_2_0_GA_CP/naming/src/main/org/jnp/interfaces/NamingContext.java	2009-05-26 08:44:10 UTC (rev 89425)
@@ -34,8 +34,9 @@
 import java.net.InetSocketAddress;
 import java.rmi.ConnectException;
 import java.rmi.MarshalledObject;
-import java.rmi.NoSuchObjectException;
-import java.rmi.RemoteException;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -44,11 +45,13 @@
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.StringTokenizer;
+import java.util.concurrent.ConcurrentHashMap;
 import javax.naming.Binding;
 import javax.naming.CannotProceedException;
 import javax.naming.CommunicationException;
 import javax.naming.ConfigurationException;
 import javax.naming.Context;
+import javax.naming.ContextNotEmptyException;
 import javax.naming.InitialContext;
 import javax.naming.InvalidNameException;
 import javax.naming.LinkRef;
@@ -142,6 +145,14 @@
     * will be made.
     */ 
    public static final String JNP_MAX_RETRIES = "jnp.maxRetries";
+  /**
+    * The Naming instance to use for the root Context creation
+    */
+   public static final String JNP_NAMING_INSTANCE = "jnp.namingInstance";
+   /**
+    * The name to associate with Naming instance to use for the root Context
+    */
+   public static final String JNP_NAMING_INSTANCE_NAME = "jnp.namingInstanceName";
 
    /**
     * The default discovery multicast information
@@ -160,9 +171,10 @@
    private static Logger log = Logger.getLogger(NamingContext.class);
 
    // Static --------------------------------------------------------
-   
-   public static Hashtable haServers = new Hashtable();
 
+   /** HAJNDI keyed by partition name */
+   private static Hashtable<String, Naming> haServers = new Hashtable<String, Naming>();
+
    public static void setHANamingServerForPartition(String partitionName, Naming haServer)
    {
       haServers.put(partitionName, haServer);
@@ -178,7 +190,15 @@
       return (Naming) haServers.get(partitionName);
    }
 
+   /**
+    * The jvm local server used for non-transport access to the naming
+    * server
+    * @see #checkRef(Hashtable)
+    * @see {@linkplain LocalOnlyContextFactory}
+    */
    public static Naming localServer;
+   private static int HOST_INDEX = 0;
+   private static int PORT_INDEX = 1;
 
    // Attributes ----------------------------------------------------
    Naming naming;
@@ -195,14 +215,30 @@
    // calls, which will improve performance.
    // Weak references are used so if no contexts use a particular server
    // it will be removed from the cache.
-   static HashMap cachedServers = new HashMap();
-
+   static ConcurrentHashMap<InetSocketAddress, WeakReference<Naming>> cachedServers
+      = new ConcurrentHashMap<InetSocketAddress, WeakReference<Naming>>();
+   
+   /**
+    * @deprecated use {@link #addServer(InetSocketAddress, Naming)}
+    * @param name
+    * @param server
+    */
    static void addServer(String name, Naming server)
    {
+      Object[] hostAndPort = {name, 0};
+      parseHostPort(name, hostAndPort, 0);
+      String host = (String) hostAndPort[HOST_INDEX];
+      Integer port = (Integer) hostAndPort[PORT_INDEX];
+      InetSocketAddress addr = new InetSocketAddress(host, port);
+      addServer(addr, server);
+   }
+   static void addServer(InetSocketAddress addr, Naming server)
+   {
       // Add server to map
       synchronized (NamingContext.class)
       {
-         cachedServers.put(name, new WeakReference(server));
+         WeakReference<Naming> ref = new WeakReference<Naming>(server);
+         cachedServers.put(addr, ref);
       }
    }
 
@@ -210,17 +246,14 @@
       throws NamingException
    {
       // Check the server cache for a host:port entry
-      String hostKey = host + ":" + port;
-      WeakReference ref = (WeakReference) cachedServers.get(hostKey);
+      InetSocketAddress key = new InetSocketAddress(host, port);
+      WeakReference<Naming> ref = cachedServers.get(key);
       Naming server;
       if (ref != null)
       {
          server = (Naming) ref.get();
          if (server != null)
          {
-            // JBAS-4622. Ensure the env for the request has the
-            // hostKey so we can remove the cache entry if there is a failure
-            serverEnv.put("hostKey", hostKey);
             return server;
          }
       }
@@ -245,7 +278,7 @@
          }
          catch (IOException e)
          {
-            NamingException ex = new ServiceUnavailableException("Failed to connect to server " + hostKey);
+            NamingException ex = new ServiceUnavailableException("Failed to connect to server " + key);
             ex.setRootCause(e);
             throw ex;
          }
@@ -258,20 +291,24 @@
          s.close();
 
          // Add it to cache
-         addServer(hostKey, server);
-         serverEnv.put("hostKey", hostKey);
+         addServer(key, server);
+         serverEnv.put("hostKey", key);
 
          return server;
       }
       catch (IOException e)
       {
-         NamingException ex = new CommunicationException("Failed to retrieve stub from server " + hostKey);
+         if(log.isTraceEnabled())
+            log.trace("Failed to retrieve stub from server " + key, e);
+         NamingException ex = new CommunicationException("Failed to retrieve stub from server " + key);
          ex.setRootCause(e);
          throw ex;
       }
       catch (Exception e)
       {
-         NamingException ex = new CommunicationException("Failed to connect to server " + hostKey);
+         if(log.isTraceEnabled())
+            log.trace("Failed to connect server " + key, e);
+         NamingException ex = new CommunicationException("Failed to connect to server " + key);
          ex.setRootCause(e);
          throw ex;
       }
@@ -339,45 +376,36 @@
                String server = parseNameForScheme(urlAsName, null);
                if (server != null)
                   url = server;
-               int colon = url.indexOf(':');
-               if (colon < 0)
-               {
-                  host = url.trim();
-               }
-               else
-               {
-                  host = url.substring(0, colon).trim();
-                  try
-                  {
-                     port = Integer.parseInt(url.substring(colon + 1).trim());
-                  }
-                  catch (Exception ex)
-                  {
-                     // Use default;
-                  }
-               }
 
+               Object[] hostAndPort = {url, 1099};
+               parseHostPort(url, hostAndPort, 1099);
+               host = (String) hostAndPort[HOST_INDEX];
+               port = (Integer) hostAndPort[PORT_INDEX];
+
                // Remove server from map
                synchronized (NamingContext.class)
                {
-                  cachedServers.remove(host + ":" + port);
+                  InetSocketAddress key = new InetSocketAddress(host, port);
+                  cachedServers.remove(key);
                }
             }
             catch (NamingException ignored)
             {
             }
          }
-      }
-      
-      // JBAS-4622. Always do this.
-      Object hostKey = serverEnv.remove("hostKey");
-      if (hostKey != null)
-      {
-         synchronized (NamingContext.class)
+         Object hostKey = serverEnv.remove("hostKey");
+         if (hostKey != null)
          {
-            cachedServers.remove(hostKey);
+            synchronized (NamingContext.class)
+            {
+               cachedServers.remove(hostKey);
+            }
          }
       }
+      else
+      {
+         // Don't do anything for local server
+      }
    }
 
    /**
@@ -440,6 +468,10 @@
       return serverInfo;
    }
 
+   public static Naming getLocal()
+   {
+      return localServer;
+   }
    public static void setLocal(Naming server)
    {
       localServer = server;
@@ -504,31 +536,13 @@
          {
             if( obj != null )
                className = obj.getClass().getName();
-            // Normal object - serialize using a MarshalledValuePair
-            obj = new MarshalledValuePair(obj);
+            obj = createMarshalledValuePair(obj);
          }
          else
          {
             className = ((Reference) obj).getClassName();
          }
-         try
-         {
-            naming.rebind(getAbsoluteName(name), obj, className);
-         }
-         catch (RemoteException re)
-         {
-            // Check for JBAS-4615.
-            if (handleStaleNamingStub(re, refEnv))
-            {
-               // try again with new naming stub                  
-               naming.rebind(getAbsoluteName(name), obj, className);
-            }
-            else
-            {
-               // Not JBAS-4615. Throw exception and let outer logic handle it.
-               throw re;
-            }            
-         }
+         naming.rebind(getAbsoluteName(name), obj, className);
       }
       catch (CannotProceedException cpe)
       {
@@ -578,32 +592,14 @@
                className = obj.getClass().getName();
             
             // Normal object - serialize using a MarshalledValuePair
-            obj = new MarshalledValuePair(obj);
+            obj = createMarshalledValuePair(obj);
          }
          else
          {
             className = ((Reference) obj).getClassName();
          }
          name = getAbsoluteName(name);
-         
-         try
-         {
-            naming.bind(name, obj, className);
-         }
-         catch (RemoteException re)
-         {
-            // Check for JBAS-4615.
-            if (handleStaleNamingStub(re, refEnv))
-            {
-               // try again with new naming stub                  
-               naming.bind(name, obj, className);
-            }
-            else
-            {
-               // Not JBAS-4615. Throw exception and let outer logic handle it.
-               throw re;
-            }            
-         }
+         naming.bind(name, obj, className);
       }
       catch (CannotProceedException cpe)
       {
@@ -662,25 +658,7 @@
          {
             try
             {
-               try
-               {
-                  res = naming.lookup(n);
-               }
-               catch (RemoteException re)
-               {
-                  // Check for JBAS-4615.
-                  if (handleStaleNamingStub(re, refEnv))
-                  {
-                     // try again with new naming stub                  
-                     res = naming.lookup(n);
-                  }
-                  else
-                  {
-                     // Not JBAS-4615. Throw exception and let outer logic handle it.
-                     throw re;
-                  }
-               }
-               // If we got here, we succeeded, so break the loop
+               res = naming.lookup(n);
                break;
             }
             catch (ConnectException ce)
@@ -807,24 +785,7 @@
 
       try
       {
-         try
-         {
-            naming.unbind(getAbsoluteName(name));
-         }
-         catch (RemoteException re)
-         {
-            // Check for JBAS-4615.
-            if (handleStaleNamingStub(re, refEnv))
-            {
-               // try again with new naming stub                  
-               naming.unbind(getAbsoluteName(name));
-            }
-            else
-            {
-               // Not JBAS-4615. Throw exception and let outer logic handle it.
-               throw re;
-            }             
-         }
+         naming.unbind(getAbsoluteName(name));
       }
       catch (CannotProceedException cpe)
       {
@@ -872,26 +833,7 @@
 
       try
       {
-         Collection c = null;
-         try
-         {
-            c = naming.list(getAbsoluteName(name));
-         }
-         catch (RemoteException re)
-         {
-            // Check for JBAS-4615.
-            if (handleStaleNamingStub(re, refEnv))
-            {
-               // try again with new naming stub                  
-               c = naming.list(getAbsoluteName(name));
-            }
-            else
-            {
-               // Not JBAS-4615. Throw exception and let outer logic handle it.
-               throw re;
-            }            
-         }
-         return new NamingEnumerationImpl(c);
+         return new NamingEnumerationImpl(naming.list(getAbsoluteName(name)));
       }
       catch (CannotProceedException cpe)
       {
@@ -927,26 +869,7 @@
       try
       {
          // Get list
-         Collection bindings = null;
-         try
-         {
-            // Get list
-            bindings = naming.listBindings(getAbsoluteName(name));
-         }
-         catch (RemoteException re)
-         {
-            // Check for JBAS-4615.
-            if (handleStaleNamingStub(re, refEnv))
-            {
-               // try again with new naming stub                  
-               bindings = naming.listBindings(getAbsoluteName(name));
-            }
-            else
-            {
-               // Not JBAS-4615. Throw exception and let outer logic handle it.
-               throw re;
-            }            
-         }
+         Collection bindings = naming.listBindings(getAbsoluteName(name));
          Collection realBindings = new ArrayList(bindings.size());
          
          // Convert marshalled objects
@@ -1052,24 +975,7 @@
       try
       {
          name = getAbsoluteName(name);
-         try
-         {
-            return naming.createSubcontext(name);
-         }
-         catch (RemoteException re)
-         {
-            // Check for JBAS-4615.
-            if (handleStaleNamingStub(re, refEnv))
-            {
-               // try again with new naming stub                  
-               return naming.createSubcontext(name);
-            }
-            else
-            {
-               // Not JBAS-4615. Throw exception and let outer logic handle it.
-               throw re;
-            }            
-         }
+         return naming.createSubcontext(name);
       }
       catch (CannotProceedException cpe)
       {
@@ -1123,13 +1029,18 @@
    public void destroySubcontext(String name)
       throws NamingException
    {
-      throw new OperationNotSupportedException();
+      destroySubcontext(getNameParser(name).parse(name));
    }
 
    public void destroySubcontext(Name name)
       throws NamingException
    {
-      throw new OperationNotSupportedException();
+      if (!list(name).hasMore())
+      {
+         unbind(name);
+      }
+      else
+         throw new ContextNotEmptyException();
    }
 
    public Object lookupLink(String name)
@@ -1147,6 +1058,12 @@
    public Object lookupLink(Name name)
       throws NamingException
    {
+      Hashtable refEnv = getEnv(name);
+      checkRef(refEnv);
+      Name parsedName = (Name) refEnv.get(JNP_PARSED_NAME);
+      if (parsedName != null)
+         name = parsedName;
+
       if (name.isEmpty())
          return lookup(name);
 
@@ -1154,25 +1071,7 @@
       try
       {
          Name n = getAbsoluteName(name);
-         try
-         {
-            link = naming.lookup(n);
-         }
-         catch (RemoteException re)
-         {
-            // Check for JBAS-4615.
-            // TODO if we resolve JBAS-4616, need to use refEnv
-            if (handleStaleNamingStub(re, env))
-            {
-               // try again with new naming stub                  
-               link = naming.lookup(n);
-            }
-            else
-            {
-               // Not JBAS-4615. Throw exception and let outer logic handle it.
-               throw re;
-            }            
-         }
+         link = naming.lookup(n);
          if (!(link instanceof LinkRef) && link instanceof Reference)
             link = getObjectInstance(link, name, null);
          ;
@@ -1222,6 +1121,47 @@
    // Private -------------------------------------------------------
 
    /**
+    * Isolate the creation of the MarshalledValuePair in a privileged block
+    * when running under a security manager so the following permissions can
+    * be isolated from the caller:
+    * RuntimePermission("createClassLoader")
+      ReflectPermission("suppressAccessChecks")
+      SerializablePermission("enableSubstitution")
+      @return the MarshalledValuePair wrapping obj
+    */
+   private Object createMarshalledValuePair(final Object obj)
+      throws IOException
+   {
+      MarshalledValuePair mvp = null;
+      SecurityManager sm = System.getSecurityManager();
+      if(sm != null)
+      {
+         try
+         {
+            mvp = AccessController.doPrivileged(new PrivilegedExceptionAction<MarshalledValuePair>()
+            {
+               public MarshalledValuePair run() throws Exception
+               {
+                  return new MarshalledValuePair(obj);
+               }
+            }
+            );
+         }
+         catch(PrivilegedActionException e)
+         {
+            IOException ioe = new IOException();
+            ioe.initCause(e.getException());
+            throw ioe;
+         }
+      }
+      else
+      {
+         mvp = new MarshalledValuePair(obj);
+      }
+      return mvp;
+   }
+
+   /**
     * Determine the form of the name to pass to the NamingManager operations.
     * This is supposed to be a context relative name according to the javaodcs
     * for NamingManager, but historically the absolute name of the target
@@ -1342,13 +1282,14 @@
 
          int ttl = 16;
 
+         String discoveryGroup = (String) serverEnv.get(JNP_DISCOVERY_GROUP);
+         if (discoveryGroup != null)
+            group = discoveryGroup;
+
          String discoveryTTL = (String) serverEnv.get(JNP_DISCOVERY_TTL);
          if(discoveryTTL != null)
             ttl = Integer.parseInt(discoveryTTL);
 
-         String discoveryGroup = (String) serverEnv.get(JNP_DISCOVERY_GROUP);
-         if (discoveryGroup != null)
-            group = discoveryGroup;
          String discoveryTimeout = (String) serverEnv.get(JNP_DISCOVERY_TIMEOUT);
          if (discoveryTimeout == null)
          {
@@ -1449,11 +1390,12 @@
          String serverHost;
          int serverPort;
 
-         int colon = myServer.indexOf(':');
-         if (colon >= 0)
+         Object[] hostAndPort = {myServer, 0};
+         parseHostPort(myServer, hostAndPort, DEFAULT_DISCOVERY_GROUP_PORT);
+         serverHost = (String) hostAndPort[HOST_INDEX];
+         serverPort = (Integer) hostAndPort[PORT_INDEX];
+         if (serverHost != null)
          {
-            serverHost = myServer.substring(0, colon);
-            serverPort = Integer.valueOf(myServer.substring(colon + 1)).intValue();
             server = getServer(serverHost, serverPort, serverEnv);
          }
          return server;
@@ -1510,23 +1452,11 @@
                String server = parseNameForScheme(urlAsName, null);
                if (server != null)
                   url = server;
-               int colon = url.indexOf(':');
-               if (colon < 0)
-               {
-                  host = url;
-               }
-               else
-               {
-                  host = url.substring(0, colon).trim();
-                  try
-                  {
-                     port = Integer.parseInt(url.substring(colon + 1).trim());
-                  }
-                  catch (Exception ex)
-                  {
-                     // Use default;
-                  }
-               }
+               //
+               Object[] hostAndPort = {url, 0};
+               parseHostPort(url, hostAndPort, 1099);
+               host = (String) hostAndPort[HOST_INDEX];
+               port = (Integer) hostAndPort[PORT_INDEX];
                try
                {
                   // Get server from cache
@@ -1538,7 +1468,6 @@
                   log.debug("Failed to connect to " + host + ":" + port, e);
                }
             }
-
             // If there is still no
             Exception discoveryFailure = null;
             if (naming == null)
@@ -1601,6 +1530,65 @@
       }
    }
 
+   /**
+    * Parse a naming provider url for the host/port information
+    * @param url - the naming provider url string to parse
+    * @param output, [0] = the host name/address, [1] = the parsed port as an Integer
+    * @param defaultPort - the default port to return in output[1] if no port
+    * was seen in the url string.
+    * @return the index of the port separator if found, -1 otherwise.
+    */
+   static private int parseHostPort(String url, Object[] output, int defaultPort)
+   {
+
+      // chcech [host]:port syntax
+      int colon = url.indexOf(']');
+      if (colon > 0) {
+          output[HOST_INDEX] = url.substring(1, colon);
+          if(url.length() > (colon + 1))
+          {
+              output[PORT_INDEX] = Integer.parseInt(url.substring(colon + 2));
+          }
+          else
+          {
+              output[PORT_INDEX] = new Integer(defaultPort);
+          }
+          return colon + 1;
+      }
+
+      // First look for a @ separating the host and port
+      colon = url.indexOf('@');
+      if(colon < 0)
+      {
+         // If there are multiple ':' assume its an IPv6 address
+         colon = url.lastIndexOf(':');
+         int firstColon = url.indexOf(':');
+         if(colon > firstColon)
+            colon = -1;
+      }
+
+      if(colon < 0)
+      {
+         output[HOST_INDEX] = url;
+         output[PORT_INDEX] = new Integer(defaultPort);
+      }
+      else
+      {
+         output[HOST_INDEX] = url.substring(0, colon);
+         try
+         {
+            output[PORT_INDEX] = Integer.parseInt(url.substring(colon+1).trim());
+         }
+         catch (Exception ex)
+         {
+            // Use default port
+            output[PORT_INDEX] = new Integer(defaultPort);
+         }
+      }
+      return colon;
+   }
+
+
    private Name getAbsoluteName(Name n)
       throws NamingException
    {
@@ -1626,50 +1614,6 @@
       }
       return nameEnv;
    }
-   
-   /**
-    * JBAS-4615. Check if the given exception is because the server has 
-    * been restarted while the cached naming stub hasn't been dgc-ed yet. 
-    * If yes, we will flush out the naming stub from our cache and
-    * acquire a new stub. BW.
-    * 
-    * @param e  the exception that may be due to a stale stub
-    * @param refEnv the naming environment associated with the failed call
-    * 
-    * @return <code>true</code> if <code>e</code> indicates a stale
-    *         naming stub and we were able to succesfully flush the
-    *         cache and acquire a new stub; <code>false</code> otherwise.
-    */
-   private boolean handleStaleNamingStub(RemoteException e, Hashtable refEnv)
-   {
-      if (e instanceof NoSuchObjectException
-            || e.getCause() instanceof NoSuchObjectException)
-      {
-         try
-         {
-            if( log.isTraceEnabled() )
-            {
-               log.trace("Call failed with recoverable RMI failure, " +
-                         "flushing server cache and reaquiring Naming ref", e);
-            }
-            naming = null;
-            removeServer(refEnv);
-              
-            checkRef(refEnv);
-            
-            return true;
-         }
-         catch (Exception e1)
-         {
-            // Just log and return false; let caller continue processing
-            // the original exception passed in to this method
-            log.error("Caught exception flushing server cache and " +
-                      "re-establish naming after exception " + 
-                      e.getLocalizedMessage(), e1);
-         }
-      }
-      return false;
-   }
 
    // Inner classes -------------------------------------------------
 }

Modified: branches/JBPAPP_4_2_0_GA_CP/testsuite/src/main/org/jboss/test/web/test/SSOBaseCase.java
===================================================================
--- branches/JBPAPP_4_2_0_GA_CP/testsuite/src/main/org/jboss/test/web/test/SSOBaseCase.java	2009-05-26 08:29:31 UTC (rev 89424)
+++ branches/JBPAPP_4_2_0_GA_CP/testsuite/src/main/org/jboss/test/web/test/SSOBaseCase.java	2009-05-26 08:44:10 UTC (rev 89425)
@@ -228,16 +228,26 @@
       {
          targetServer = targetServer.substring(index + 3);
       }
-      index = targetServer.indexOf(":");
+
+      // check for IPV6 literal
+      index = targetServer.indexOf("]");
       if (index > -1)
-      {
-         targetServer = targetServer.substring(0, index);
-      }
-      index = targetServer.indexOf("/");
-      if (index > -1)
-      {
-         targetServer = targetServer.substring(0, index);
-      }
+        {
+          targetServer = targetServer.substring(0, index + 1);
+        }
+        else
+        {
+          index = targetServer.indexOf(":");
+          if (index > -1)
+          {
+             targetServer = targetServer.substring(0, index);
+          }
+          index = targetServer.indexOf("/");
+          if (index > -1)
+          {
+             targetServer = targetServer.substring(0, index);
+          }
+        }
 
       Cookie copy = new Cookie(targetServer, toCopy.getName(), toCopy.getValue(), "/", null, false);
       return copy;




More information about the jboss-cvs-commits mailing list