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

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sat Sep 22 01:21:28 EDT 2007


Author: bstansberry at jboss.com
Date: 2007-09-22 01:21:28 -0400 (Sat, 22 Sep 2007)
New Revision: 65551

Modified:
   branches/Branch_4_2/naming/src/main/org/jnp/interfaces/NamingContext.java
Log:
[JBAS-4615] Flush cached Naming stub and retry on NoSuchObjectException
[JBAS-4622] Ensure JBAS-4615 fix works when no java.naming.provider.url is specified

Modified: branches/Branch_4_2/naming/src/main/org/jnp/interfaces/NamingContext.java
===================================================================
--- branches/Branch_4_2/naming/src/main/org/jnp/interfaces/NamingContext.java	2007-09-22 05:19:05 UTC (rev 65550)
+++ branches/Branch_4_2/naming/src/main/org/jnp/interfaces/NamingContext.java	2007-09-22 05:21:28 UTC (rev 65551)
@@ -34,6 +34,8 @@
 import java.net.InetSocketAddress;
 import java.rmi.ConnectException;
 import java.rmi.MarshalledObject;
+import java.rmi.NoSuchObjectException;
+import java.rmi.RemoteException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -216,6 +218,9 @@
          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;
          }
       }
@@ -362,19 +367,17 @@
             {
             }
          }
-         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
-      }
    }
 
    /**
@@ -508,7 +511,24 @@
          {
             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)
       {
@@ -565,7 +585,25 @@
             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)
       {
@@ -624,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)
@@ -751,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)
       {
@@ -799,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)
       {
@@ -835,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
@@ -941,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)
       {
@@ -1026,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);
          ;
@@ -1480,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