[jboss-cvs] JBossAS SVN: r80558 - in projects/naming/trunk/jnpserver/src: test/resources/org/jnp/test and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Nov 5 13:47:47 EST 2008


Author: scott.stark at jboss.org
Date: 2008-11-05 13:47:47 -0500 (Wed, 05 Nov 2008)
New Revision: 80558

Added:
   projects/naming/trunk/jnpserver/src/test/resources/org/jnp/test/NamingMCUnitTest#testMultipleLocalOnlyContextFactory.xml
Modified:
   projects/naming/trunk/jnpserver/src/main/java/org/jnp/interfaces/LocalOnlyContextFactory.java
Log:
JBNAME-19, allow the Naming instance to be injected into the LocalOnlyContextFactory

Modified: projects/naming/trunk/jnpserver/src/main/java/org/jnp/interfaces/LocalOnlyContextFactory.java
===================================================================
--- projects/naming/trunk/jnpserver/src/main/java/org/jnp/interfaces/LocalOnlyContextFactory.java	2008-11-05 18:32:22 UTC (rev 80557)
+++ projects/naming/trunk/jnpserver/src/main/java/org/jnp/interfaces/LocalOnlyContextFactory.java	2008-11-05 18:47:47 UTC (rev 80558)
@@ -1,64 +1,121 @@
 /*
-  * JBoss, Home of Professional Open Source
-  * Copyright 2005, JBoss Inc., 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.
-  */
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., 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.jnp.interfaces;
 
+import java.lang.ref.WeakReference;
 import java.util.Hashtable;
+import java.util.concurrent.ConcurrentHashMap;
+
 import javax.naming.Context;
-import javax.naming.Name;
 import javax.naming.NamingException;
-import javax.naming.Reference;
 import javax.naming.spi.InitialContextFactory;
-import javax.naming.spi.ObjectFactory;
 
+import org.jboss.logging.Logger;
+
 /**
- * An InitialContextFactory that uses the NamingContex.localServer naming server
- * which has to have been set.
- *
+ * An InitialContextFactory that uses the either NamingContex.localServer naming
+ * server which has to have been set, or injected.
+ * 
  * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
+ * @author Scott.Stark at jboss.org
  * @version $Revision$
  */
-public class LocalOnlyContextFactory
-        implements InitialContextFactory, ObjectFactory
-     {
-         // InitialContextFactory implementation --------------------------
-         public Context getInitialContext(Hashtable env)
-           throws NamingException
-         {
-            Naming localServer = NamingContext.getLocal();
-             if (localServer == null)
-                throw new NamingException("Local server is not initialized");
-             return new NamingContext(env, null, localServer);
-         }
+public class LocalOnlyContextFactory implements InitialContextFactory
+{
+   private static final Logger log = Logger.getLogger(LocalOnlyContextFactory.class);
 
-        // ObjectFactory implementation ----------------------------------
-        public Object getObjectInstance(Object obj,
-                                        Name name,
-                                        Context nameCtx,
-                                        Hashtable environment)
-                              throws Exception
-        {
-           Context ctx = getInitialContext(environment);
-           Reference ref = (Reference)obj;
-           return ctx.lookup((String)ref.get("URL").getContent());
-        }
+   /** A set of Naming instances with names */
+   private static ConcurrentHashMap<String, WeakReference<Naming>> localServers = new ConcurrentHashMap<String, WeakReference<Naming>>();
+   private Naming naming;
 
-     }
+   public Naming getNaming()
+   {
+      return naming;
+   }
+   /**
+    * Set the Naming instance to use for the root content.
+    * @param naming - the Naming instance to use, null if the global
+    * NamingContext.getLocal value should be used.
+    */
+   public void setNaming(Naming naming)
+   {
+      this.naming = naming;
+   }
+
+   // InitialContextFactory implementation --------------------------
+   public Context getInitialContext(Hashtable<?,?> env)
+      throws NamingException
+   {
+      boolean trace = log.isTraceEnabled();
+      if(trace)
+         log.trace("getInitialContext, env: "+env);
+      String name = null;
+      Naming localServer = null;
+      if(env != null)
+      {
+         // First try the naming property instance
+         localServer = (Naming) env.get(NamingContext.JNP_NAMING_INSTANCE);
+         if(trace && localServer != null)
+            log.trace("Set naming from "+NamingContext.JNP_NAMING_INSTANCE);
+         name = (String) env.get(NamingContext.JNP_NAMING_INSTANCE_NAME);
+      }
+      // Next try the injected naming instance
+      if (localServer == null)
+      {
+         localServer = naming;
+         if(trace && localServer != null)
+            log.trace("Set naming from injected value");
+      }
+      // Next try to locate the instance by name
+      if (localServer == null && name != null)
+      {
+         WeakReference<Naming> lswr = localServers.get(name);
+         if(lswr != null)
+            localServer = lswr.get();
+         if(trace && localServer != null)
+            log.trace("Set naming from localServers#"+name);
+      }
+      // Lastly try the JVM global NamingContext.local value
+      if (localServer == null)
+      {
+         localServer = NamingContext.getLocal();
+         if(trace && localServer != null)
+            log.trace("Set naming from NamingContext.getLocal");
+      }
+
+      if (localServer == null)
+         throw new NamingException("Failed to determine local server from: "+env);
+
+      // If this is a named instance add it to the localServers set
+      if (name != null && localServers.containsKey(name) == false)
+      {
+         localServers.put(name, new WeakReference<Naming>(localServer));
+         if(trace)
+            log.trace("Set localServers:"+name);
+      }
+
+      // Pass in an empty env if its null
+      if(env == null)
+         env = new Hashtable();
+      return new NamingContext(env, null, localServer);
+   }
+}

Added: projects/naming/trunk/jnpserver/src/test/resources/org/jnp/test/NamingMCUnitTest#testMultipleLocalOnlyContextFactory.xml
===================================================================
--- projects/naming/trunk/jnpserver/src/test/resources/org/jnp/test/NamingMCUnitTest#testMultipleLocalOnlyContextFactory.xml	                        (rev 0)
+++ projects/naming/trunk/jnpserver/src/test/resources/org/jnp/test/NamingMCUnitTest#testMultipleLocalOnlyContextFactory.xml	2008-11-05 18:47:47 UTC (rev 80558)
@@ -0,0 +1,183 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<deployment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="urn:jboss:bean-deployer bean-deployer_2_0.xsd"
+    xmlns="urn:jboss:bean-deployer:2.0">
+    
+    <!-- An InitialContextFactory that uses the NamingContext.getLocal() Naming -->
+    <bean name="InitialContextFactory#1" class="org.jboss.naming.InitialContextFactoryBean">
+        <alias>InitialContextFactory</alias>
+        <property name="env">
+            <map class="java.util.Properties" keyClass="java.lang.String" valueClass="java.lang.String">
+                <entry>
+                    <key>java.naming.factory.initial</key>
+                    <value>org.jnp.interfaces.LocalOnlyContextFactory</value>
+                </entry>
+                <entry>
+                    <key>java.naming.factory.url</key>
+                    <value>org.jboss.naming:org.jnp.interfaces</value>
+                </entry>
+            </map>
+        </property>
+        <depends>testLocaNamingBeanImpl#1</depends>
+    </bean>
+    <!-- An InitialContextFactory that uses the env jnp.namingInstanceName Naming -->
+    <bean name="InitialContextFactory#2" class="org.jboss.naming.InitialContextFactoryBean">
+        <property name="env">
+            <map class="java.util.Properties" keyClass="java.lang.String" valueClass="java.lang.String">
+                <entry>
+                    <key>java.naming.factory.initial</key>
+                    <value>org.jnp.interfaces.LocalOnlyContextFactory</value>
+                </entry>
+                <entry>
+                    <key>java.naming.factory.url</key>
+                    <value>org.jboss.naming:org.jnp.interfaces</value>
+                </entry>
+                <entry>
+                    <key>jnp.namingInstance</key>
+                    <value><inject bean="testLocaNamingBeanImpl#2" property="namingInstance"/></value>
+                </entry>
+                <entry>
+                    <key>jnp.namingInstanceName</key>
+                    <value>testLocaNamingBeanImpl#2</value>
+                </entry>
+            </map>
+        </property>
+        <depends>testLocaNamingBeanImpl#2</depends>
+    </bean>
+    <!-- An InitialContextFactory that uses the testLocaNamingBeanImpl#2 Naming bean -->
+    <bean name="InitialContextFactory#3" class="org.jnp.interfaces.LocalOnlyContextFactory">
+        <property name="naming"><inject bean="testLocaNamingBeanImpl#3" property="namingInstance"/></property>
+    </bean>
+    <bean name="InitialContext#3" class="javax.naming.InitialContext">
+        <constructor factoryMethod="getInitialContext">
+            <factory bean="InitialContextFactory#3"/>
+            <parameter><null/></parameter>
+        </constructor>
+    </bean>
+    
+    <bean name="JndiBindings#1" class="org.jboss.naming.BindingsInitializer">
+        <property name="ctx">
+            <inject bean="InitialContextFactory#1" property="ctx"/>
+        </property>
+        <property name="bindings">
+            <map keyClass="java.lang.String">
+                <entry>
+                    <key>ints/1</key>
+                    <value class="java.lang.Integer">1</value>
+                </entry>
+                <entry>
+                    <key>strings/1</key>
+                    <value class="java.lang.String">String1</value>
+                </entry>
+                <entry>
+                    <key>bigint/1</key>
+                    <value class="java.math.BigInteger">123456789</value>
+                </entry>
+                <entry>
+                    <key>env-props</key>
+                    <value>
+                        <map class="java.util.Properties" keyClass="java.lang.String" valueClass="java.lang.String">
+                            <entry>
+                                <key>java.naming.factory.initial</key>
+                                <value>org.jnp.interfaces.LocalOnlyContextFactory</value>
+                            </entry>
+                            <entry>
+                                <key>java.naming.factory.url</key>
+                                <value>org.jboss.naming:org.jnp.interfaces</value>
+                            </entry>
+                        </map>
+                    </value>
+                </entry>
+            </map>
+        </property>
+    </bean>
+    <bean name="JndiBindings#2" class="org.jboss.naming.BindingsInitializer">
+        <property name="ctx">
+            <inject bean="InitialContextFactory#2" property="ctx"/>
+        </property>
+        <property name="bindings">
+            <map keyClass="java.lang.String">
+                <entry>
+                    <key>ints/2</key>
+                    <value class="java.lang.Integer">2</value>
+                </entry>
+                <entry>
+                    <key>strings/2</key>
+                    <value class="java.lang.String">String2</value>
+                </entry>
+                <entry>
+                    <key>bigint/2</key>
+                    <value class="java.math.BigInteger">987654321</value>
+                </entry>
+                <entry>
+                    <key>env-props</key>
+                    <value>
+                        <map class="java.util.Properties" keyClass="java.lang.String" valueClass="java.lang.String">
+                            <entry>
+                                <key>java.naming.factory.initial</key>
+                                <value>org.jnp.interfaces.LocalOnlyContextFactory#2</value>
+                            </entry>
+                            <entry>
+                                <key>java.naming.factory.url</key>
+                                <value>factory#2</value>
+                            </entry>
+                        </map>
+                    </value>
+                </entry>
+            </map>
+        </property>
+    </bean>
+    <bean name="JndiBindings#3" class="org.jboss.naming.BindingsInitializer">
+        <property name="ctx">
+            <inject bean="InitialContext#3" />
+        </property>
+        <property name="bindings">
+            <map keyClass="java.lang.String">
+                <entry>
+                    <key>ints/3</key>
+                    <value class="java.lang.Integer">3</value>
+                </entry>
+                <entry>
+                    <key>strings/3</key>
+                    <value class="java.lang.String">String3</value>
+                </entry>
+                <entry>
+                    <key>bigint/3</key>
+                    <value class="java.math.BigInteger">333333333</value>
+                </entry>
+                <entry>
+                    <key>env-props</key>
+                    <value>
+                        <map class="java.util.Properties" keyClass="java.lang.String" valueClass="java.lang.String">
+                            <entry>
+                                <key>java.naming.factory.initial</key>
+                                <value>org.jnp.interfaces.LocalOnlyContextFactory#3</value>
+                            </entry>
+                            <entry>
+                                <key>java.naming.factory.url</key>
+                                <value>factory#3</value>
+                            </entry>
+                        </map>
+                    </value>
+                </entry>
+            </map>
+        </property>
+    </bean>
+
+    <bean name="testLocaNamingBeanImpl#1" class="org.jnp.server.NamingBeanImpl">
+        <!-- Install this bean as the global JVM NamingServer -->
+        <property name="installGlobalService">true</property>
+        <property name="useGlobalService">false</property>
+    </bean>
+    <bean name="testLocaNamingBeanImpl#2" class="org.jnp.server.NamingBeanImpl">
+        <!-- Do not install this bean as the global JVM NamingServer -->
+        <property name="installGlobalService">false</property>
+        <property name="useGlobalService">false</property>
+    </bean>
+    <bean name="testLocaNamingBeanImpl#3" class="org.jnp.server.NamingBeanImpl">
+        <!-- Do not install this bean as the global JVM NamingServer -->
+        <property name="installGlobalService">false</property>
+        <property name="useGlobalService">false</property>
+    </bean>
+</deployment>




More information about the jboss-cvs-commits mailing list