[jboss-cvs] JBossAS SVN: r60420 - in branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3: naming and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Feb 8 05:09:48 EST 2007


Author: wolfc
Date: 2007-02-08 05:09:48 -0500 (Thu, 08 Feb 2007)
New Revision: 60420

Added:
   branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/naming/MultiplexerContext.java
   branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/naming/NamingEnumerationImpl.java
   branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/naming/SimpleMultiplexer.java
Modified:
   branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/EJB3Deployer.java
   branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/ThreadLocalENCFactory.java
Log:
EJBTHREE-812: ported multiplexer context to AS 4.2

Modified: branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/EJB3Deployer.java
===================================================================
--- branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/EJB3Deployer.java	2007-02-08 01:34:28 UTC (rev 60419)
+++ branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/EJB3Deployer.java	2007-02-08 10:09:48 UTC (rev 60420)
@@ -39,17 +39,22 @@
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
 import javax.naming.LinkRef;
+import javax.naming.RefAddr;
+import javax.naming.Reference;
+import javax.naming.StringRefAddr;
 
 import org.jboss.deployment.DeploymentException;
 import org.jboss.deployment.DeploymentInfo;
 import org.jboss.deployment.SubDeployer;
 import org.jboss.deployment.SubDeployerSupport;
+import org.jboss.ejb3.naming.SimpleMultiplexer;
 import org.jboss.logging.Logger;
 import org.jboss.metadata.MetaData;
 import org.jboss.metadata.XmlFileLoader;
 import org.jboss.mx.loading.LoaderRepositoryFactory;
 import org.jboss.mx.util.MBeanProxyExt;
 import org.jboss.mx.util.ObjectNameConverter;
+import org.jboss.naming.ENCFactory;
 import org.jboss.system.ServiceControllerMBean;
 import org.jboss.util.file.ArchiveBrowser;
 import org.jboss.util.file.ClassFileFilter;
@@ -314,11 +319,37 @@
       initializeJavaComp(iniCtx);
    }
 
+   private static void hackJNDI(Context ctx) throws NamingException
+   {
+      log.info("Starting java:comp hack");
+      // rename to something SimpleMultiPlexer knows.
+      // (doesn't work, because rename starts with a lookup and thus resolves
+      // to a enc context bound to the current class loader).
+      //ctx.rename("comp", "comp.original");
+      {
+         ctx.unbind("comp");
+         // copied out of NamingBeanImpl
+         RefAddr refAddr = new StringRefAddr("nns", "ENC");
+         Reference envRef = new Reference("javax.namingMain.Context", refAddr, ENCFactory.class.getName(), null);
+         ctx.bind("comp.original", envRef);
+      }
+      
+      {
+         RefAddr refAddr = new StringRefAddr("nns", "ENC-MULTIPLEXER");
+         Reference ref = new Reference("javax.naming.Context", refAddr, SimpleMultiplexer.class.getName(), null);
+         ctx.bind("comp", ref);
+      }
+   }
+   
    public static void initializeJavaComp(InitialContext iniCtx)
            throws NamingException
    {
+      RefAddr refAddr = new StringRefAddr("nns", "ENC-EJB3");
+      Reference envRef = new Reference("javax.naming.Context", refAddr, ThreadLocalENCFactory.class.getName(), null);
       Context ctx = (Context) iniCtx.lookup("java:");
-      ctx.rebind("comp.ejb3", new LinkRef("java:comp"));
+      ctx.rebind("comp.ejb3", envRef);
+      
+      hackJNDI(ctx);
    }
 
    /**

Modified: branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/ThreadLocalENCFactory.java
===================================================================
--- branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/ThreadLocalENCFactory.java	2007-02-08 01:34:28 UTC (rev 60419)
+++ branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/ThreadLocalENCFactory.java	2007-02-08 10:09:48 UTC (rev 60420)
@@ -25,6 +25,7 @@
 import java.util.LinkedList;
 import javax.naming.Context;
 import javax.naming.Name;
+import javax.naming.NameNotFoundException;
 import javax.naming.spi.ObjectFactory;
 import org.jnp.interfaces.NamingContext;
 import org.jnp.server.NamingServer;
@@ -39,8 +40,8 @@
 public class ThreadLocalENCFactory
         implements ObjectFactory
 {
-   private static ThreadLocal enc = new ThreadLocal();
-   private static ThreadLocal stack = new ThreadLocal();
+   private static ThreadLocal<Context> enc = new ThreadLocal<Context>();
+   private static ThreadLocal<LinkedList<Context>> stack = new ThreadLocal<LinkedList<Context>>();
 
    public static Context create(Context parent) throws Exception
    {
@@ -55,10 +56,10 @@
          enc.set(ctx);
          return;
       }
-      LinkedList currentStack = (LinkedList) stack.get();
+      LinkedList<Context> currentStack = stack.get();
       if (currentStack == null)
       {
-         currentStack = new LinkedList();
+         currentStack = new LinkedList<Context>();
          stack.set(currentStack);
       }
       currentStack.addLast(enc.get());
@@ -67,7 +68,7 @@
 
    public static void pop()
    {
-      LinkedList currentStack = (LinkedList) stack.get();
+      LinkedList<Context> currentStack = stack.get();
       if (currentStack == null)
       {
          enc.set(null);
@@ -78,7 +79,7 @@
          enc.set(null);
          return;
       }
-      Object previous = currentStack.removeLast();
+      Context previous = currentStack.removeLast();
       enc.set(previous);
    }
    // Constructors --------------------------------------------------
@@ -90,7 +91,8 @@
                                    Hashtable environment)
            throws Exception
    {
-      return enc.get();
+      Context ctx = enc.get();
+      return ctx;
    }
 
 }

Copied: branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/naming/MultiplexerContext.java (from rev 59197, trunk/ejb3/src/main/org/jboss/ejb3/naming/MultiplexerContext.java)
===================================================================
--- branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/naming/MultiplexerContext.java	                        (rev 0)
+++ branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/naming/MultiplexerContext.java	2007-02-08 10:09:48 UTC (rev 60420)
@@ -0,0 +1,275 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt 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.jboss.ejb3.naming;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Hashtable;
+
+import javax.naming.Binding;
+import javax.naming.Context;
+import javax.naming.Name;
+import javax.naming.NameClassPair;
+import javax.naming.NameParser;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.OperationNotSupportedException;
+
+import org.jboss.logging.Logger;
+
+/**
+ * A context which combines two contexts.
+ * 
+ * Read operations are combined, write operations are done on the first context.
+ * All other operations are not supported.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class MultiplexerContext implements Context, Serializable
+{
+   private static final long serialVersionUID = -2306711582586456135L;
+   
+   private static final Logger log = Logger.getLogger(MultiplexerContext.class);
+   
+   private Context contextOne;
+   private Context contextTwo;
+
+   public MultiplexerContext(Context contextOne, Context contextTwo) throws NamingException
+   {
+      assert contextOne != null;
+      assert contextTwo != null;
+      
+      this.contextOne = contextOne;
+      this.contextTwo = contextTwo;
+   }
+   
+   private <T extends NameClassPair> void addAll(Collection<T> collection, NamingEnumeration<T> ne) throws NamingException
+   {
+      // TODO: how about duplicates?
+      while(ne.hasMore())
+      {
+         T ncp = ne.next();
+         collection.add(ncp);
+      }
+   }
+   
+   public Object addToEnvironment(String propName, Object propVal) throws NamingException
+   {
+      throw new OperationNotSupportedException();
+   }
+
+   public void bind(Name name, Object obj) throws NamingException
+   {
+      log.trace("bind: " + name + " -> " +obj);
+      getContextOne().bind(name, obj);  
+   }
+
+   public void bind(String name, Object obj) throws NamingException
+   {
+      log.trace("bind: " + name + " -> " +obj);
+      getContextOne().bind(name, obj);
+   }
+
+   public void close() throws NamingException
+   {
+      // do nothing
+   }
+
+   public Name composeName(Name name, Name prefix) throws NamingException
+   {
+      return getContextOne().composeName(name, prefix);
+   }
+
+   public String composeName(String name, String prefix) throws NamingException
+   {
+      return getContextOne().composeName(name, prefix);
+   }
+
+   public Context createSubcontext(Name name) throws NamingException
+   {
+      return getContextOne().createSubcontext(name);
+   }
+
+   public Context createSubcontext(String name) throws NamingException
+   {
+      return getContextOne().createSubcontext(name);
+   }
+
+   public void destroySubcontext(Name name) throws NamingException
+   {
+      getContextOne().destroySubcontext(name);
+   }
+
+   public void destroySubcontext(String name) throws NamingException
+   {
+      getContextOne().destroySubcontext(name);
+   }
+
+   private Context getContextOne() throws NamingException
+   {
+      return contextOne;
+   }
+   
+   private Context getContextTwo() throws NamingException
+   {
+      return contextTwo;
+   }
+   
+   public Hashtable<?, ?> getEnvironment() throws NamingException
+   {
+      throw new UnsupportedOperationException();
+   }
+
+   public String getNameInNamespace() throws NamingException
+   {
+      throw new UnsupportedOperationException();
+   }
+
+   public NameParser getNameParser(Name name) throws NamingException
+   {
+      return getContextOne().getNameParser(name);
+   }
+
+   public NameParser getNameParser(String name) throws NamingException
+   {
+      return getContextOne().getNameParser(name);
+   }
+
+   public NamingEnumeration<NameClassPair> list(Name name) throws NamingException
+   {
+      Collection<NameClassPair> set = new ArrayList<NameClassPair>();
+      addAll(set, getContextOne().list(name));
+      addAll(set, getContextTwo().list(name));
+      return new NamingEnumerationImpl<NameClassPair>(set);
+   }
+
+   public NamingEnumeration<NameClassPair> list(String name) throws NamingException
+   {
+      Collection<NameClassPair> set = new ArrayList<NameClassPair>();
+      addAll(set, getContextOne().list(name));
+      addAll(set, getContextTwo().list(name));
+      return new NamingEnumerationImpl<NameClassPair>(set);
+   }
+
+   public NamingEnumeration<Binding> listBindings(Name name) throws NamingException
+   {
+      Collection<Binding> set = new ArrayList<Binding>();
+      addAll(set, getContextOne().listBindings(name));
+      addAll(set, getContextTwo().listBindings(name));
+      return new NamingEnumerationImpl<Binding>(set);
+   }
+
+   public NamingEnumeration<Binding> listBindings(String name) throws NamingException
+   {
+      Collection<Binding> set = new ArrayList<Binding>();
+      addAll(set, getContextOne().listBindings(name));
+      addAll(set, getContextTwo().listBindings(name));
+      return new NamingEnumerationImpl<Binding>(set);
+   }
+
+   public Object lookup(Name name) throws NamingException
+   {
+      log.trace("lookup: " + name);
+      try
+      {
+         return getContextOne().lookup(name);
+      }
+      catch(NamingException e)
+      {
+         return getContextTwo().lookup(name);
+      }
+   }
+
+   public Object lookup(String name) throws NamingException
+   {
+      log.trace("lookup: " + name);
+      try
+      {
+         return getContextOne().lookup(name);
+      }
+      catch(NamingException e)
+      {
+         return getContextTwo().lookup(name);
+      }
+   }
+
+   public Object lookupLink(Name name) throws NamingException
+   {
+      try
+      {
+         return getContextOne().lookupLink(name);
+      }
+      catch(NamingException e)
+      {
+         return getContextTwo().lookupLink(name);
+      }
+   }
+
+   public Object lookupLink(String name) throws NamingException
+   {
+      try
+      {
+         return getContextOne().lookupLink(name);
+      }
+      catch(NamingException e)
+      {
+         return getContextTwo().lookupLink(name);
+      }
+   }
+
+   public void rebind(Name name, Object obj) throws NamingException
+   {
+      getContextOne().rebind(name, obj);
+   }
+
+   public void rebind(String name, Object obj) throws NamingException
+   {
+      getContextOne().rebind(name, obj);
+   }
+
+   public Object removeFromEnvironment(String propName) throws NamingException
+   {
+      throw new UnsupportedOperationException();
+   }
+
+   public void rename(Name oldName, Name newName) throws NamingException
+   {
+      getContextOne().rename(oldName, newName);
+   }
+
+   public void rename(String oldName, String newName) throws NamingException
+   {
+      getContextOne().rename(oldName, newName);
+   }
+
+   public void unbind(Name name) throws NamingException
+   {
+      getContextOne().unbind(name);
+   }
+
+   public void unbind(String name) throws NamingException
+   {
+      getContextOne().unbind(name);
+   }
+}

Copied: branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/naming/NamingEnumerationImpl.java (from rev 59197, trunk/ejb3/src/main/org/jboss/ejb3/naming/NamingEnumerationImpl.java)
===================================================================
--- branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/naming/NamingEnumerationImpl.java	                        (rev 0)
+++ branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/naming/NamingEnumerationImpl.java	2007-02-08 10:09:48 UTC (rev 60420)
@@ -0,0 +1,85 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt 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.jboss.ejb3.naming;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+import javax.naming.NameClassPair;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class NamingEnumerationImpl<T extends NameClassPair> implements NamingEnumeration<T>
+{
+   private Iterator<T> iterator;
+   
+   NamingEnumerationImpl(Collection<T> set)
+   {
+      this.iterator = set.iterator();
+   }
+
+   /* (non-Javadoc)
+    * @see javax.naming.NamingEnumeration#close()
+    */
+   public void close() throws NamingException
+   {
+      // do nothing
+   }
+
+   /* (non-Javadoc)
+    * @see javax.naming.NamingEnumeration#hasMore()
+    */
+   public boolean hasMore() throws NamingException
+   {
+      return hasMoreElements();
+   }
+
+   /* (non-Javadoc)
+    * @see javax.naming.NamingEnumeration#next()
+    */
+   public T next() throws NamingException
+   {
+      return nextElement();
+   }
+
+   /* (non-Javadoc)
+    * @see java.util.Enumeration#hasMoreElements()
+    */
+   public boolean hasMoreElements()
+   {
+      return iterator.hasNext();
+   }
+
+   /* (non-Javadoc)
+    * @see java.util.Enumeration#nextElement()
+    */
+   public T nextElement()
+   {
+      return iterator.next();
+   }
+}

Copied: branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/naming/SimpleMultiplexer.java (from rev 59197, trunk/ejb3/src/main/org/jboss/ejb3/naming/SimpleMultiplexer.java)
===================================================================
--- branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/naming/SimpleMultiplexer.java	                        (rev 0)
+++ branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/naming/SimpleMultiplexer.java	2007-02-08 10:09:48 UTC (rev 60420)
@@ -0,0 +1,77 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt 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.jboss.ejb3.naming;
+
+import java.util.Hashtable;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.Name;
+import javax.naming.NamingException;
+import javax.naming.spi.ObjectFactory;
+
+import org.jboss.logging.Logger;
+
+/**
+ * An object factory which creates a multiplexing context to "comp.ejb3" and "comp.original"
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class SimpleMultiplexer implements ObjectFactory
+{
+   private static final Logger log = Logger.getLogger(SimpleMultiplexer.class);
+   
+   public SimpleMultiplexer() throws NamingException
+   {
+   }
+   
+   private Context createMultiplexer(Context ctx) throws NamingException
+   {
+      Context ctxTwo = (Context) ctx.lookup("comp.ejb3");
+      Context ctxOne = (Context) ctx.lookup("comp.original");
+      log.trace("contextClassLoader = " + Thread.currentThread().getContextClassLoader() + " ctxOne = " + ctxOne);
+      if(ctxTwo == null)
+         return ctxOne;
+      return new MultiplexerContext(ctxOne, ctxTwo);
+   }
+   
+   public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception
+   {
+      if(nameCtx == null)
+      {
+         nameCtx = (Context) new InitialContext().lookup("java:");
+         try
+         {
+            return createMultiplexer(nameCtx);
+         }
+         finally
+         {
+            nameCtx.close();
+         }
+      }
+      else
+      {
+         return createMultiplexer(nameCtx);
+      }
+   }
+}




More information about the jboss-cvs-commits mailing list