[jboss-cvs] JBossAS SVN: r89475 - branches/JBPAPP_4_2_0_GA_CP/naming/src/main/org/jnp/interfaces.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed May 27 12:00:59 EDT 2009


Author: dpospisi at redhat.com
Date: 2009-05-27 12:00:58 -0400 (Wed, 27 May 2009)
New Revision: 89475

Modified:
   branches/JBPAPP_4_2_0_GA_CP/naming/src/main/org/jnp/interfaces/NamingContext.java
Log:
Reverted 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-27 15:42:57 UTC (rev 89474)
+++ branches/JBPAPP_4_2_0_GA_CP/naming/src/main/org/jnp/interfaces/NamingContext.java	2009-05-27 16:00:58 UTC (rev 89475)
@@ -34,9 +34,8 @@
 import java.net.InetSocketAddress;
 import java.rmi.ConnectException;
 import java.rmi.MarshalledObject;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
-import java.security.PrivilegedExceptionAction;
+import java.rmi.NoSuchObjectException;
+import java.rmi.RemoteException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -45,13 +44,11 @@
 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;
@@ -145,14 +142,6 @@
     * 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
@@ -171,10 +160,9 @@
    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);
@@ -190,15 +178,7 @@
       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;
@@ -215,30 +195,14 @@
    // 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 ConcurrentHashMap<InetSocketAddress, WeakReference<Naming>> cachedServers
-      = new ConcurrentHashMap<InetSocketAddress, WeakReference<Naming>>();
-   
-   /**
-    * @deprecated use {@link #addServer(InetSocketAddress, Naming)}
-    * @param name
-    * @param server
-    */
+   static HashMap cachedServers = new HashMap();
+
    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)
       {
-         WeakReference<Naming> ref = new WeakReference<Naming>(server);
-         cachedServers.put(addr, ref);
+         cachedServers.put(name, new WeakReference(server));
       }
    }
 
@@ -246,14 +210,17 @@
       throws NamingException
    {
       // Check the server cache for a host:port entry
-      InetSocketAddress key = new InetSocketAddress(host, port);
-      WeakReference<Naming> ref = cachedServers.get(key);
+      String hostKey = host + ":" + port;
+      WeakReference ref = (WeakReference) cachedServers.get(hostKey);
       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;
          }
       }
@@ -278,7 +245,7 @@
          }
          catch (IOException e)
          {
-            NamingException ex = new ServiceUnavailableException("Failed to connect to server " + key);
+            NamingException ex = new ServiceUnavailableException("Failed to connect to server " + hostKey);
             ex.setRootCause(e);
             throw ex;
          }
@@ -291,24 +258,20 @@
          s.close();
 
          // Add it to cache
-         addServer(key, server);
-         serverEnv.put("hostKey", key);
+         addServer(hostKey, server);
+         serverEnv.put("hostKey", hostKey);
 
          return server;
       }
       catch (IOException e)
       {
-         if(log.isTraceEnabled())
-            log.trace("Failed to retrieve stub from server " + key, e);
-         NamingException ex = new CommunicationException("Failed to retrieve stub from server " + key);
+         NamingException ex = new CommunicationException("Failed to retrieve stub from server " + hostKey);
          ex.setRootCause(e);
          throw ex;
       }
       catch (Exception e)
       {
-         if(log.isTraceEnabled())
-            log.trace("Failed to connect server " + key, e);
-         NamingException ex = new CommunicationException("Failed to connect to server " + key);
+         NamingException ex = new CommunicationException("Failed to connect to server " + hostKey);
          ex.setRootCause(e);
          throw ex;
       }
@@ -376,36 +339,45 @@
                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)
                {
-                  InetSocketAddress key = new InetSocketAddress(host, port);
-                  cachedServers.remove(key);
+                  cachedServers.remove(host + ":" + port);
                }
             }
             catch (NamingException ignored)
             {
             }
          }
-         Object hostKey = serverEnv.remove("hostKey");
-         if (hostKey != null)
+      }
+      
+      // JBAS-4622. Always do this.
+      Object hostKey = serverEnv.remove("hostKey");
+      if (hostKey != null)
+      {
+         synchronized (NamingContext.class)
          {
-            synchronized (NamingContext.class)
-            {
-               cachedServers.remove(hostKey);
-            }
+            cachedServers.remove(hostKey);
          }
       }
-      else
-      {
-         // Don't do anything for local server
-      }
    }
 
    /**
@@ -468,10 +440,6 @@
       return serverInfo;
    }
 
-   public static Naming getLocal()
-   {
-      return localServer;
-   }
    public static void setLocal(Naming server)
    {
       localServer = server;
@@ -536,13 +504,31 @@
          {
             if( obj != null )
                className = obj.getClass().getName();
-            obj = createMarshalledValuePair(obj);
+            // Normal object - serialize using a MarshalledValuePair
+            obj = new MarshalledValuePair(obj);
          }
          else
          {
             className = ((Reference) obj).getClassName();
          }
-         naming.rebind(getAbsoluteName(name), obj, className);
+         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;
+            }            
+         }
       }
       catch (CannotProceedException cpe)
       {
@@ -592,14 +578,32 @@
                className = obj.getClass().getName();
             
             // Normal object - serialize using a MarshalledValuePair
-            obj = createMarshalledValuePair(obj);
+            obj = new MarshalledValuePair(obj);
          }
          else
          {
             className = ((Reference) obj).getClassName();
          }
          name = getAbsoluteName(name);
-         naming.bind(name, obj, className);
+         
+         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;
+            }            
+         }
       }
       catch (CannotProceedException cpe)
       {
@@ -658,7 +662,25 @@
          {
             try
             {
-               res = naming.lookup(n);
+               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
                break;
             }
             catch (ConnectException ce)
@@ -785,7 +807,24 @@
 
       try
       {
-         naming.unbind(getAbsoluteName(name));
+         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;
+            }             
+         }
       }
       catch (CannotProceedException cpe)
       {
@@ -833,7 +872,26 @@
 
       try
       {
-         return new NamingEnumerationImpl(naming.list(getAbsoluteName(name)));
+         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);
       }
       catch (CannotProceedException cpe)
       {
@@ -869,7 +927,26 @@
       try
       {
          // Get list
-         Collection bindings = naming.listBindings(getAbsoluteName(name));
+         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 realBindings = new ArrayList(bindings.size());
          
          // Convert marshalled objects
@@ -975,7 +1052,24 @@
       try
       {
          name = getAbsoluteName(name);
-         return naming.createSubcontext(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;
+            }            
+         }
       }
       catch (CannotProceedException cpe)
       {
@@ -1029,18 +1123,13 @@
    public void destroySubcontext(String name)
       throws NamingException
    {
-      destroySubcontext(getNameParser(name).parse(name));
+      throw new OperationNotSupportedException();
    }
 
    public void destroySubcontext(Name name)
       throws NamingException
    {
-      if (!list(name).hasMore())
-      {
-         unbind(name);
-      }
-      else
-         throw new ContextNotEmptyException();
+      throw new OperationNotSupportedException();
    }
 
    public Object lookupLink(String name)
@@ -1058,12 +1147,6 @@
    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);
 
@@ -1071,7 +1154,25 @@
       try
       {
          Name n = getAbsoluteName(name);
-         link = naming.lookup(n);
+         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;
+            }            
+         }
          if (!(link instanceof LinkRef) && link instanceof Reference)
             link = getObjectInstance(link, name, null);
          ;
@@ -1121,47 +1222,6 @@
    // 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
@@ -1282,14 +1342,13 @@
 
          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)
          {
@@ -1390,12 +1449,11 @@
          String serverHost;
          int serverPort;
 
-         Object[] hostAndPort = {myServer, 0};
-         parseHostPort(myServer, hostAndPort, DEFAULT_DISCOVERY_GROUP_PORT);
-         serverHost = (String) hostAndPort[HOST_INDEX];
-         serverPort = (Integer) hostAndPort[PORT_INDEX];
-         if (serverHost != null)
+         int colon = myServer.indexOf(':');
+         if (colon >= 0)
          {
+            serverHost = myServer.substring(0, colon);
+            serverPort = Integer.valueOf(myServer.substring(colon + 1)).intValue();
             server = getServer(serverHost, serverPort, serverEnv);
          }
          return server;
@@ -1452,11 +1510,23 @@
                String server = parseNameForScheme(urlAsName, null);
                if (server != null)
                   url = server;
-               //
-               Object[] hostAndPort = {url, 0};
-               parseHostPort(url, hostAndPort, 1099);
-               host = (String) hostAndPort[HOST_INDEX];
-               port = (Integer) hostAndPort[PORT_INDEX];
+               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;
+                  }
+               }
                try
                {
                   // Get server from cache
@@ -1468,6 +1538,7 @@
                   log.debug("Failed to connect to " + host + ":" + port, e);
                }
             }
+
             // If there is still no
             Exception discoveryFailure = null;
             if (naming == null)
@@ -1530,65 +1601,6 @@
       }
    }
 
-   /**
-    * 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
    {
@@ -1614,6 +1626,50 @@
       }
       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 -------------------------------------------------
 }




More information about the jboss-cvs-commits mailing list