You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by da...@apache.org on 2006/10/20 00:44:26 UTC

svn commit: r465917 - in /geronimo/server/trunk/modules: geronimo-client/src/main/java/org/apache/geronimo/client/ geronimo-naming/src/main/java/org/apache/geronimo/naming/enc/ geronimo-naming/src/test/java/org/apache/geronimo/gjndi/

Author: dain
Date: Thu Oct 19 15:44:24 2006
New Revision: 465917

URL: http://svn.apache.org/viewvc?view=rev&rev=465917
Log:
Fixed GERONIMO-2508 An empty enterprise naming context does not cotain a context at java:comp/env by adding an empty env context to the ENC if no env bindings are present in the context map

Added:
    geronimo/server/trunk/modules/geronimo-naming/src/test/java/org/apache/geronimo/gjndi/JavaCompGBeanTest.java
Modified:
    geronimo/server/trunk/modules/geronimo-client/src/main/java/org/apache/geronimo/client/StaticJndiContextPlugin.java
    geronimo/server/trunk/modules/geronimo-naming/src/main/java/org/apache/geronimo/naming/enc/EnterpriseNamingContext.java

Modified: geronimo/server/trunk/modules/geronimo-client/src/main/java/org/apache/geronimo/client/StaticJndiContextPlugin.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/modules/geronimo-client/src/main/java/org/apache/geronimo/client/StaticJndiContextPlugin.java?view=diff&rev=465917&r1=465916&r2=465917
==============================================================================
--- geronimo/server/trunk/modules/geronimo-client/src/main/java/org/apache/geronimo/client/StaticJndiContextPlugin.java (original)
+++ geronimo/server/trunk/modules/geronimo-client/src/main/java/org/apache/geronimo/client/StaticJndiContextPlugin.java Thu Oct 19 15:44:24 2006
@@ -18,6 +18,7 @@
 
 import java.util.Iterator;
 import java.util.Map;
+import java.util.Collections;
 import javax.naming.InitialContext;
 import javax.naming.Context;
 import javax.naming.NamingException;
@@ -30,6 +31,7 @@
 import org.apache.geronimo.gbean.GBeanInfoBuilder;
 import org.apache.geronimo.gbean.AbstractName;
 import org.apache.geronimo.kernel.Kernel;
+import org.apache.xbean.naming.context.ImmutableContext;
 
 /**
  * @version $Rev$ $Date$
@@ -38,17 +40,7 @@
     private final Context context;
 
     public StaticJndiContextPlugin(Map context, Kernel kernel, ClassLoader classLoader) throws NamingException {
-        // create ReadOnlyContext
-        for (Iterator iterator = context.values().iterator(); iterator.hasNext();) {
-            Object value = iterator.next();
-            if (value instanceof KernelAwareReference) {
-                ((KernelAwareReference) value).setKernel(kernel);
-            }
-            if (value instanceof ClassLoaderAwareReference) {
-                ((ClassLoaderAwareReference) value).setClassLoader(classLoader);
-            }
-        }
-        this.context = EnterpriseNamingContext.createEnterpriseNamingContext(context);
+        this.context = EnterpriseNamingContext.createEnterpriseNamingContext(context, null, kernel, classLoader);
     }
 
     public void startClient(AbstractName appClientModuleName, Kernel kernel, ClassLoader classLoader) throws Exception {

Modified: geronimo/server/trunk/modules/geronimo-naming/src/main/java/org/apache/geronimo/naming/enc/EnterpriseNamingContext.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/modules/geronimo-naming/src/main/java/org/apache/geronimo/naming/enc/EnterpriseNamingContext.java?view=diff&rev=465917&r1=465916&r2=465917
==============================================================================
--- geronimo/server/trunk/modules/geronimo-naming/src/main/java/org/apache/geronimo/naming/enc/EnterpriseNamingContext.java (original)
+++ geronimo/server/trunk/modules/geronimo-naming/src/main/java/org/apache/geronimo/naming/enc/EnterpriseNamingContext.java Thu Oct 19 15:44:24 2006
@@ -20,6 +20,7 @@
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.Collections;
 
 import javax.naming.Context;
 import javax.naming.NamingException;
@@ -41,14 +42,26 @@
             map.putAll(componentContext);
         }
 
-        for (Iterator iterator = map.values().iterator(); iterator.hasNext();) {
-            Object value = iterator.next();
+        boolean containsEnv = false;
+        for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
+            Map.Entry entry = (Map.Entry) iterator.next();
+            String name = (String) entry.getKey();
+            Object value = entry.getValue();
+
+            if (name.startsWith("env/")) {
+                containsEnv = true;
+            }
             if (value instanceof KernelAwareReference) {
                 ((KernelAwareReference) value).setKernel(kernel);
             }
             if (value instanceof ClassLoaderAwareReference) {
                 ((ClassLoaderAwareReference) value).setClassLoader(classLoader);
             }
+        }
+
+        if (!containsEnv) {
+            Context env = new ImmutableContext("java:comp/env", Collections.EMPTY_MAP, false);
+            map.put("env", env);
         }
 
         if (userTransaction != null) {

Added: geronimo/server/trunk/modules/geronimo-naming/src/test/java/org/apache/geronimo/gjndi/JavaCompGBeanTest.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/modules/geronimo-naming/src/test/java/org/apache/geronimo/gjndi/JavaCompGBeanTest.java?view=auto&rev=465917
==============================================================================
--- geronimo/server/trunk/modules/geronimo-naming/src/test/java/org/apache/geronimo/gjndi/JavaCompGBeanTest.java (added)
+++ geronimo/server/trunk/modules/geronimo-naming/src/test/java/org/apache/geronimo/gjndi/JavaCompGBeanTest.java Thu Oct 19 15:44:24 2006
@@ -0,0 +1,120 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.geronimo.gjndi;
+
+import org.apache.geronimo.gbean.GBeanData;
+import org.apache.geronimo.kernel.Kernel;
+import org.apache.geronimo.kernel.KernelFactory;
+import org.apache.geronimo.kernel.config.ConfigurationData;
+import org.apache.geronimo.kernel.config.ConfigurationUtil;
+import org.apache.geronimo.kernel.config.EditableConfigurationManager;
+import org.apache.geronimo.kernel.config.EditableKernelConfigurationManager;
+import org.apache.geronimo.kernel.repository.Artifact;
+import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
+import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
+import org.apache.geronimo.naming.enc.EnterpriseNamingContext;
+import org.apache.geronimo.naming.java.RootContext;
+import org.apache.xbean.naming.context.ImmutableContext;
+import org.apache.xbean.naming.global.GlobalContextManager;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.naming.NameNotFoundException;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class JavaCompGBeanTest extends AbstractContextTest {
+    private Kernel kernel;
+    private Hashtable contextEnv;
+
+    public void testLookupEnv() throws Exception {
+        Map javaCompBindings = new HashMap();
+        javaCompBindings.put("foo", "bar");
+
+        // a regular context doesn't contain env
+        RootContext.setComponentContext(new ImmutableContext(javaCompBindings));
+        try {
+            new InitialContext(contextEnv).lookup("java:comp/env");
+            fail("Expected NameNotFoundException");
+        } catch (NameNotFoundException expected) {
+            // expected
+        }
+
+        // ENC adds env if not present
+        RootContext.setComponentContext(EnterpriseNamingContext.createEnterpriseNamingContext(javaCompBindings, null, null, null));
+        new InitialContext(contextEnv).lookup("java:comp/env");
+    }
+
+    protected Map getNestedBindings(Map globalBindings, String nestedPath) {
+        HashMap nestedBindings = new HashMap();
+        for (Iterator iterator = globalBindings.entrySet().iterator(); iterator.hasNext();) {
+            Map.Entry entry = (Map.Entry) iterator.next();
+            String globalName = (String) entry.getKey();
+            Object value = entry.getValue();
+
+            if (globalName.startsWith(nestedPath)) {
+                String nestedName = globalName.substring(nestedPath.length());
+                nestedBindings.put(nestedName, value);
+            }
+        }
+        return nestedBindings;
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        kernel = KernelFactory.newInstance().createKernel("test");
+        kernel.boot();
+
+        ConfigurationData bootstrap = new ConfigurationData(new Artifact("bootstrap", "bootstrap", "", "car"), kernel.getNaming());
+
+        GBeanData artifactManagerData = bootstrap.addGBean("ArtifactManager", DefaultArtifactManager.GBEAN_INFO);
+
+        GBeanData artifactResolverData = bootstrap.addGBean("ArtifactResolver", DefaultArtifactResolver.GBEAN_INFO);
+        artifactResolverData.setReferencePattern("ArtifactManager", artifactManagerData.getAbstractName());
+
+        GBeanData configurationManagerData = bootstrap.addGBean("ConfigurationManager", EditableKernelConfigurationManager.GBEAN_INFO);
+        configurationManagerData.setReferencePattern("ArtifactManager", artifactManagerData.getAbstractName());
+        configurationManagerData.setReferencePattern("ArtifactResolver", artifactResolverData.getAbstractName());
+
+        ConfigurationUtil.loadBootstrapConfiguration(kernel, bootstrap, getClass().getClassLoader());
+
+        EditableConfigurationManager configurationManager = ConfigurationUtil.getEditableConfigurationManager(kernel);
+
+        ConfigurationData configurationData = new ConfigurationData(new Artifact("test", "test", "", "car"), kernel.getNaming());
+        configurationData.addGBean("GlobalContext", GlobalContextGBean.GBEAN_INFO);
+        configurationData.addGBean("JavaComp", JavaCompContextGBean.GBEAN_INFO);
+
+        configurationManager.loadConfiguration(configurationData);
+        configurationManager.startConfiguration(configurationData.getId());
+
+
+        contextEnv = new Hashtable();
+        contextEnv.put(Context.INITIAL_CONTEXT_FACTORY, GlobalContextManager.class.getName());
+    }
+
+    protected void tearDown() throws Exception {
+        kernel.shutdown();
+        super.tearDown();
+    }
+}



Re: svn commit: r465917 - in /geronimo/server/trunk/modules: geronimo-client/src/main/java/org/apache/geronimo/client/ geronimo-naming/src/main/java/org/apache/geronimo/naming/enc/ geronimo-naming/src/test/java/org/apache/geronimo/gjndi/

Posted by David Jencks <da...@yahoo.com>.
On Oct 19, 2006, at 11:19 PM, Jacek Laskowski wrote:

> On 10/20/06, dain@apache.org <da...@apache.org> wrote:
>> Author: dain
>> Date: Thu Oct 19 15:44:24 2006
>> New Revision: 465917
> ...
>> +        boolean containsEnv = false;
>> +        for (Iterator iterator = map.entrySet().iterator();  
>> iterator.hasNext();) {
>> +            Map.Entry entry = (Map.Entry) iterator.next();
>> +            String name = (String) entry.getKey();
>> +            Object value = entry.getValue();
>> +
>> +            if (name.startsWith("env/")) {
>> +                containsEnv = true;
>> +            }
>>              if (value instanceof KernelAwareReference) {
>>                  ((KernelAwareReference) value).setKernel(kernel);
>>              }
>>              if (value instanceof ClassLoaderAwareReference) {
>>                  ((ClassLoaderAwareReference) value).setClassLoader 
>> (classLoader);
>>              }
>> +        }
>> +
>> +        if (!containsEnv) {
>> +            Context env = new ImmutableContext("java:comp/env",  
>> Collections.EMPTY_MAP, false);
>> +            map.put("env", env);
>>          }
>
> I haven't tested it out, but...reading the code seems to set
> containsEnv to false every time. It's because name.startsWith("env/")
> checks whether env + '/' whereas map contains only env (with no
> trailing slash).
>
> BTW, why doesn't it simply use map.get("env") to verify whether
> java:comp/env is available?

My understanding of this code is that the map keys are the complete  
paths to the objects bound into jndi.  You can't really bind anything  
to env, it would have to be env/foo, and although I don't think  
there's any way to bind anything to envthisisalongername it's easier  
to just check startsWith("env/") to avoid any confusion.  So, you'd  
never get map.get("env") != null, and startsWith("env/") while  
probably redundant is certainly accurate.

thanks
david jencks

>
> -- 
> Jacek Laskowski
> http://www.jaceklaskowski.pl


Re: svn commit: r465917 - in /geronimo/server/trunk/modules: geronimo-client/src/main/java/org/apache/geronimo/client/ geronimo-naming/src/main/java/org/apache/geronimo/naming/enc/ geronimo-naming/src/test/java/org/apache/geronimo/gjndi/

Posted by Jacek Laskowski <ja...@laskowski.net.pl>.
On 10/20/06, dain@apache.org <da...@apache.org> wrote:
> Author: dain
> Date: Thu Oct 19 15:44:24 2006
> New Revision: 465917
...
> +        boolean containsEnv = false;
> +        for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
> +            Map.Entry entry = (Map.Entry) iterator.next();
> +            String name = (String) entry.getKey();
> +            Object value = entry.getValue();
> +
> +            if (name.startsWith("env/")) {
> +                containsEnv = true;
> +            }
>              if (value instanceof KernelAwareReference) {
>                  ((KernelAwareReference) value).setKernel(kernel);
>              }
>              if (value instanceof ClassLoaderAwareReference) {
>                  ((ClassLoaderAwareReference) value).setClassLoader(classLoader);
>              }
> +        }
> +
> +        if (!containsEnv) {
> +            Context env = new ImmutableContext("java:comp/env", Collections.EMPTY_MAP, false);
> +            map.put("env", env);
>          }

I haven't tested it out, but...reading the code seems to set
containsEnv to false every time. It's because name.startsWith("env/")
checks whether env + '/' whereas map contains only env (with no
trailing slash).

BTW, why doesn't it simply use map.get("env") to verify whether
java:comp/env is available?

-- 
Jacek Laskowski
http://www.jaceklaskowski.pl