[jboss-cvs] JBossAS SVN: r59197 - in trunk/ejb3/src: main/org/jboss/ejb3 main/org/jboss/ejb3/naming test/org/jboss/ejb3/test/enventry test/org/jboss/ejb3/test/enventry/unit

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Dec 21 10:10:29 EST 2006


Author: wolfc
Date: 2006-12-21 10:10:12 -0500 (Thu, 21 Dec 2006)
New Revision: 59197

Added:
   trunk/ejb3/src/main/org/jboss/ejb3/naming/MultiplexerContext.java
   trunk/ejb3/src/main/org/jboss/ejb3/naming/NamingEnumerationImpl.java
   trunk/ejb3/src/main/org/jboss/ejb3/naming/SimpleMultiplexer.java
Modified:
   trunk/ejb3/src/main/org/jboss/ejb3/EJB3Deployer.java
   trunk/ejb3/src/main/org/jboss/ejb3/ThreadLocalENCFactory.java
   trunk/ejb3/src/test/org/jboss/ejb3/test/enventry/TestEnvEntry.java
   trunk/ejb3/src/test/org/jboss/ejb3/test/enventry/TestEnvEntryBean.java
   trunk/ejb3/src/test/org/jboss/ejb3/test/enventry/unit/EnvEntryTestCase.java
Log:
EJBTHREE-812: multiplexer context

Modified: trunk/ejb3/src/main/org/jboss/ejb3/EJB3Deployer.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/EJB3Deployer.java	2006-12-21 14:34:01 UTC (rev 59196)
+++ trunk/ejb3/src/main/org/jboss/ejb3/EJB3Deployer.java	2006-12-21 15:10:12 UTC (rev 59197)
@@ -45,6 +45,9 @@
 import org.jboss.deployment.DeploymentInfo;
 import org.jboss.deployment.SubDeployer;
 import org.jboss.deployment.SubDeployerSupport;
+import org.jboss.ejb3.naming.MultiplexerContext;
+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;
@@ -67,6 +70,8 @@
 public class EJB3Deployer extends SubDeployerSupport
    implements SubDeployer, EJB3DeployerMBean
 {
+   private final static Logger log = Logger.getLogger(EJB3Deployer.class);
+   
    private ServiceControllerMBean serviceController;
 
    /** A map of current deployments */
@@ -312,6 +317,16 @@
       initializeJavaComp(iniCtx);
    }
 
+   private static void hackJNDI(Context ctx) throws NamingException
+   {
+      log.info("Starting java:comp hack");
+      // rename to something SimpleMultiPlexer knows.
+      ctx.rename("comp", "comp.original");
+      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
    {
@@ -319,6 +334,8 @@
       Reference envRef = new Reference("javax.naming.Context", refAddr, ThreadLocalENCFactory.class.getName(), null);
       Context ctx = (Context) iniCtx.lookup("java:");
       ctx.rebind("comp.ejb3", envRef);
+      
+      hackJNDI(ctx);
    }
 
    /**

Modified: trunk/ejb3/src/main/org/jboss/ejb3/ThreadLocalENCFactory.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/ThreadLocalENCFactory.java	2006-12-21 14:34:01 UTC (rev 59196)
+++ trunk/ejb3/src/main/org/jboss/ejb3/ThreadLocalENCFactory.java	2006-12-21 15:10:12 UTC (rev 59197)
@@ -39,8 +39,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 +55,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 +67,7 @@
 
    public static void pop()
    {
-      LinkedList currentStack = (LinkedList) stack.get();
+      LinkedList<Context> currentStack = stack.get();
       if (currentStack == null)
       {
          enc.set(null);
@@ -78,7 +78,7 @@
          enc.set(null);
          return;
       }
-      Object previous = currentStack.removeLast();
+      Context previous = currentStack.removeLast();
       enc.set(previous);
    }
    // Constructors --------------------------------------------------

Added: trunk/ejb3/src/main/org/jboss/ejb3/naming/MultiplexerContext.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/naming/MultiplexerContext.java	2006-12-21 14:34:01 UTC (rev 59196)
+++ trunk/ejb3/src/main/org/jboss/ejb3/naming/MultiplexerContext.java	2006-12-21 15:10:12 UTC (rev 59197)
@@ -0,0 +1,283 @@
+/*
+ * 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.InitialContext;
+import javax.naming.Name;
+import javax.naming.NameClassPair;
+import javax.naming.NameParser;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.OperationNotSupportedException;
+
+/**
+ * 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 transient InitialContext initialCtx;
+   
+   private String contextOneName;
+   private String contextTwoName;
+
+   public MultiplexerContext(String contextOneName, String contextTwoName) throws NamingException
+   {
+      assert contextOneName != null;
+      assert contextTwoName != null;
+      
+      this.contextOneName = contextOneName;
+      this.contextTwoName = contextTwoName;
+      
+      // make sure this works further down the line
+      getContextOne();
+      getContextTwo();
+   }
+   
+   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
+   {
+      getContextOne().bind(name, obj);  
+   }
+
+   public void bind(String name, Object obj) throws NamingException
+   {
+      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 (Context) getInitialContext().lookup(contextOneName);
+   }
+   
+   private Context getContextTwo() throws NamingException
+   {
+      return (Context) getInitialContext().lookup(contextTwoName);
+   }
+   
+   public Hashtable<?, ?> getEnvironment() throws NamingException
+   {
+      throw new UnsupportedOperationException();
+   }
+
+   private InitialContext getInitialContext() throws NamingException
+   {
+      if(initialCtx == null)
+      {
+         initialCtx = new InitialContext();
+      }
+      return initialCtx;
+   }
+   
+   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
+   {
+      try
+      {
+         return getContextOne().lookup(name);
+      }
+      catch(NamingException e)
+      {
+         return getContextTwo().lookup(name);
+      }
+   }
+
+   public Object lookup(String name) throws NamingException
+   {
+      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);
+   }
+}

Added: trunk/ejb3/src/main/org/jboss/ejb3/naming/NamingEnumerationImpl.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/naming/NamingEnumerationImpl.java	2006-12-21 14:34:01 UTC (rev 59196)
+++ trunk/ejb3/src/main/org/jboss/ejb3/naming/NamingEnumerationImpl.java	2006-12-21 15:10:12 UTC (rev 59197)
@@ -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();
+   }
+}

Added: trunk/ejb3/src/main/org/jboss/ejb3/naming/SimpleMultiplexer.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/naming/SimpleMultiplexer.java	2006-12-21 14:34:01 UTC (rev 59196)
+++ trunk/ejb3/src/main/org/jboss/ejb3/naming/SimpleMultiplexer.java	2006-12-21 15:10:12 UTC (rev 59197)
@@ -0,0 +1,51 @@
+/*
+ * 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.Name;
+import javax.naming.NamingException;
+import javax.naming.spi.ObjectFactory;
+
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class SimpleMultiplexer implements ObjectFactory
+{
+   private Context context;
+   
+   public SimpleMultiplexer() throws NamingException
+   {
+      this.context = new MultiplexerContext("java:comp.original", "java:comp.ejb3");
+   }
+   
+   public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception
+   {
+      return context;
+   }
+}

Modified: trunk/ejb3/src/test/org/jboss/ejb3/test/enventry/TestEnvEntry.java
===================================================================
--- trunk/ejb3/src/test/org/jboss/ejb3/test/enventry/TestEnvEntry.java	2006-12-21 14:34:01 UTC (rev 59196)
+++ trunk/ejb3/src/test/org/jboss/ejb3/test/enventry/TestEnvEntry.java	2006-12-21 15:10:12 UTC (rev 59197)
@@ -21,12 +21,16 @@
   */
 package org.jboss.ejb3.test.enventry;
 
+import javax.naming.NamingException;
+
 /**
  * @author <a href="mailto:bdecoste at jboss.com">William DeCoste</a>
  * @version <tt>$Revision$</tt>
  */
 public interface TestEnvEntry
 {
+   void checkJNDI() throws NamingException;
+   
    int getMaxExceptions();
    
    int getMinExceptions();

Modified: trunk/ejb3/src/test/org/jboss/ejb3/test/enventry/TestEnvEntryBean.java
===================================================================
--- trunk/ejb3/src/test/org/jboss/ejb3/test/enventry/TestEnvEntryBean.java	2006-12-21 14:34:01 UTC (rev 59196)
+++ trunk/ejb3/src/test/org/jboss/ejb3/test/enventry/TestEnvEntryBean.java	2006-12-21 15:10:12 UTC (rev 59197)
@@ -24,6 +24,8 @@
 import javax.annotation.Resource;
 import javax.ejb.Stateless;
 import javax.ejb.Remote;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
 
 import org.jboss.annotation.ejb.RemoteBinding;
 
@@ -47,6 +49,13 @@
    
    private int minExceptions = 1;
    
+   public void checkJNDI() throws NamingException
+   {
+      InitialContext ctx = new InitialContext();
+      Integer i = (Integer) ctx.lookup("java:comp/env/maxExceptions");
+      log.info("maxExceptions = " + i);
+   }
+   
    public int getMaxExceptions()
    {
       return maxExceptions;

Modified: trunk/ejb3/src/test/org/jboss/ejb3/test/enventry/unit/EnvEntryTestCase.java
===================================================================
--- trunk/ejb3/src/test/org/jboss/ejb3/test/enventry/unit/EnvEntryTestCase.java	2006-12-21 14:34:01 UTC (rev 59196)
+++ trunk/ejb3/src/test/org/jboss/ejb3/test/enventry/unit/EnvEntryTestCase.java	2006-12-21 15:10:12 UTC (rev 59197)
@@ -107,6 +107,14 @@
       }
    }
 
+   public void testJNDI() throws Exception
+   {
+      TestEnvEntry test = (TestEnvEntry)getInitialContext().lookup("TestEnvEntry");
+      assertNotNull(test);
+      
+      test.checkJNDI();
+   }
+   
    public static Test suite() throws Exception
    {
       return getDeploySetup(EnvEntryTestCase.class, "enventry.jar");




More information about the jboss-cvs-commits mailing list