[jboss-cvs] JBossAS SVN: r86964 - projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/kernel.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Apr 8 09:15:14 EDT 2009


Author: jaikiran
Date: 2009-04-08 09:15:13 -0400 (Wed, 08 Apr 2009)
New Revision: 86964

Modified:
   projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/kernel/JNDIKernelRegistryPlugin.java
Log:
EJBTHREE-1798 Fixed the implemented to first check the JNDI for the presence of the jndiName. If not found in JNDI name then we need to return null to MC else return a lazy entry

Modified: projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/kernel/JNDIKernelRegistryPlugin.java
===================================================================
--- projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/kernel/JNDIKernelRegistryPlugin.java	2009-04-08 13:12:31 UTC (rev 86963)
+++ projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/kernel/JNDIKernelRegistryPlugin.java	2009-04-08 13:15:13 UTC (rev 86964)
@@ -23,8 +23,14 @@
 
 import java.util.Hashtable;
 
+import javax.naming.Binding;
 import javax.naming.Context;
+import javax.naming.Name;
+import javax.naming.NameNotFoundException;
+import javax.naming.NameParser;
+import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
+import javax.naming.NotContextException;
 
 import org.jboss.ejb3.InitialContextFactory;
 import org.jboss.kernel.spi.registry.KernelRegistryEntry;
@@ -72,7 +78,19 @@
    }
 
    /**
+    * Returns a lazy entry {@link LazyJNDIKernelRegistryEntry} corresponding
+    * to the passed name, if:
+    * <ol>
+    * <li>the name starts with {@link #JNDI_DEPENDENCY_PREFIX}</li>
+    * <li>AND the name is bound in JNDI</li>
+    * </ol>
+    * Note that to check whether the name is bound in JNDI, "lookup"
+    * is NOT done.
+    * 
+    * If the name is not bound then returns null (MC "implies" this contract)
+    * 
     * @see KernelRegistryPlugin#getEntry(Object)
+    * 
     */
    public KernelRegistryEntry getEntry(Object name)
    {
@@ -91,7 +109,32 @@
          log.trace("get entry for " + name);
 
       String jndiName = s.substring(JNDI_DEPENDENCY_PREFIX.length());
-      return new LazyJNDIKernelRegistryEntry(this.context, jndiName);
+      try
+      {
+         if (isBoundInJNDI(jndiName))
+         {
+            if (log.isTraceEnabled())
+            {
+               log.trace("Found in jndi " + jndiName + "for MC name " + name);
+            }
+            // bound in jndi, so return a Lazy entry
+            return new LazyJNDIKernelRegistryEntry(this.context,jndiName);
+         }
+         if (log.isTraceEnabled())
+         {
+            log.trace("Not available in jndi " + jndiName + " for MC name " + name);
+         }
+         // not bound in JNDI, return null
+         return null;
+      }
+      catch (NamingException ne)
+      {
+
+         log.error("Can't resolve JNDI name " + jndiName + " for MC name " + name);
+         throw new RuntimeException("Can't resolve JNDI name " + jndiName + " for MC name " + name, ne);
+      }
+
+      
    }
 
    public void setEnvironment(Hashtable<?, ?> env)
@@ -100,4 +143,91 @@
          throw new IllegalStateException("context already initialized");
       this.environment = env;
    }
+
+   /**
+    * Checks whether the passed <code>jndiName</code> is available
+    * in JNDI
+    * 
+    * @param jndiName The name to check
+    * @return Returns true if the name is available in JNDI. Else returns false
+    * @throws NamingException
+    */
+   private boolean isBoundInJNDI(String jndiName) throws NamingException
+   {
+      NameParser nameParser = this.context.getNameParser(jndiName);
+      Name nameInJNDI = nameParser.parse(jndiName);
+      if (nameInJNDI.isEmpty())
+      {
+         return false;
+      }
+      // Number of components in the Name
+      int numberOfComponents = nameInJNDI.size();
+      
+      // If it's a just a simple name without any context/subcontexts 
+      // (ex: SimpleBean), then we need to just travese the bindings
+      // and look for a match
+      if (numberOfComponents == 1)
+      {
+         NamingEnumeration<Binding> bindings = this.context.listBindings("");
+         while (bindings.hasMoreElements())
+         {
+            Binding binding = bindings.nextElement();
+            // compare the binding with the name
+            if (binding.getName().equals(nameInJNDI.get(0)))
+            {
+               // match found in JNDI
+               return true;
+            }
+         }
+         return false;
+      }
+      if (numberOfComponents > 1)
+      {
+         // if the Name consists of context/subcontexts
+         // (ex: MyApp/SimpleBean/remote) then we need list the 
+         // bindings for the parent context (in this example,
+         // MyApp/SimpleBean) and then compare the bindings in that
+         // context with the "atom" of this Name
+         Name parentCtx = nameInJNDI.getPrefix(numberOfComponents - 1);
+         String atom = nameInJNDI.get(numberOfComponents - 1);
+         try
+         {
+            // list the bindings of the parent context
+            NamingEnumeration<Binding> bindings = this.context.listBindings(parentCtx);
+            while (bindings.hasMoreElements())
+            {
+               // compare the "atom" with the binding
+               Binding binding = bindings.nextElement();
+               if (binding.getName().equals(atom))
+               {
+                  // match, found in JNDI
+                  return true;
+               }
+            }
+
+         }
+         catch (NotContextException nce)
+         {
+            if (log.isTraceEnabled())
+            {
+               log.trace("Not found in JNDI " + jndiName + " since " + parentCtx + " is not bound");
+            }
+            // this means, that the parent context (MyApp/SimpleBean) is not
+            // present. So effectively there's no such name bound to JNDI.
+            return false;
+         } 
+         catch (NameNotFoundException nnfe)
+         {
+            if (log.isTraceEnabled())
+            {
+               log.trace("Not found in JNDI " + jndiName + " since sub-context(s) within parentCtx " + parentCtx + " is not bound");
+            }
+            // this means, some sub context wasn't available, effectively
+            // the jndiName isn't bound in JNDI
+            return false;
+         }
+      }
+      // no match, so return false
+      return false;
+   }
 }




More information about the jboss-cvs-commits mailing list