geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject svn commit: r345366 - in /geronimo/trunk/modules: client/src/java/org/apache/geronimo/client/ jetty/src/java/org/apache/geronimo/jetty/ naming/src/java/org/apache/geronimo/naming/enc/ naming/src/test/org/apache/geronimo/naming/enc/ naming/src/test/org/...
Date Thu, 17 Nov 2005 23:39:57 GMT
Author: dain
Date: Thu Nov 17 15:39:48 2005
New Revision: 345366

URL: http://svn.apache.org/viewcvs?rev=345366&view=rev
Log:
New enterprise naming context implementation.
Implementation is 
  immutable
  caches reference lookup result (major performce increase)
  shares global bindings map (smaller memory foot print)
  simpler code (easier to debug)

Added:
    geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/
    geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/AbstractReadOnlyContext.java
  (with props)
    geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/BindingResolutionException.java
  (with props)
    geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/CachingReference.java
  (with props)
    geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/EnterpriseNamingContext.java
  (with props)
    geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/EnterpriseNamingContextNameParser.java
  (with props)
    geronimo/trunk/modules/naming/src/test/org/apache/geronimo/naming/enc/
    geronimo/trunk/modules/naming/src/test/org/apache/geronimo/naming/enc/EnterpriseNamingContextTest.java
  (with props)
Modified:
    geronimo/trunk/modules/client/src/java/org/apache/geronimo/client/StaticJndiContextPlugin.java
    geronimo/trunk/modules/jetty/src/java/org/apache/geronimo/jetty/JettyWebAppContext.java
    geronimo/trunk/modules/naming/src/test/org/apache/geronimo/naming/java/ContextBuilderTest.java
    geronimo/trunk/modules/tomcat/src/java/org/apache/geronimo/tomcat/GeronimoStandardContext.java

Modified: geronimo/trunk/modules/client/src/java/org/apache/geronimo/client/StaticJndiContextPlugin.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/client/src/java/org/apache/geronimo/client/StaticJndiContextPlugin.java?rev=345366&r1=345365&r2=345366&view=diff
==============================================================================
--- geronimo/trunk/modules/client/src/java/org/apache/geronimo/client/StaticJndiContextPlugin.java
(original)
+++ geronimo/trunk/modules/client/src/java/org/apache/geronimo/client/StaticJndiContextPlugin.java
Thu Nov 17 15:39:48 2005
@@ -24,9 +24,9 @@
 import javax.naming.NamingException;
 
 import org.apache.geronimo.naming.java.RootContext;
-import org.apache.geronimo.naming.java.SimpleReadOnlyContext;
 import org.apache.geronimo.naming.reference.KernelAwareReference;
 import org.apache.geronimo.naming.reference.ClassLoaderAwareReference;
+import org.apache.geronimo.naming.enc.EnterpriseNamingContext;
 import org.apache.geronimo.gbean.GBeanInfo;
 import org.apache.geronimo.gbean.GBeanInfoBuilder;
 import org.apache.geronimo.kernel.Kernel;
@@ -48,7 +48,7 @@
                 ((ClassLoaderAwareReference) value).setClassLoader(classLoader);
             }
         }
-        this.context = new SimpleReadOnlyContext(context);
+        this.context = EnterpriseNamingContext.createEnterpriseNamingContext(context);
     }
 
     public void startClient(ObjectName appClientModuleName, Kernel kernel, ClassLoader classLoader)
throws Exception {

Modified: geronimo/trunk/modules/jetty/src/java/org/apache/geronimo/jetty/JettyWebAppContext.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/jetty/src/java/org/apache/geronimo/jetty/JettyWebAppContext.java?rev=345366&r1=345365&r2=345366&view=diff
==============================================================================
--- geronimo/trunk/modules/jetty/src/java/org/apache/geronimo/jetty/JettyWebAppContext.java
(original)
+++ geronimo/trunk/modules/jetty/src/java/org/apache/geronimo/jetty/JettyWebAppContext.java
Thu Nov 17 15:39:48 2005
@@ -54,9 +54,9 @@
 import org.apache.geronimo.jetty.interceptor.WebApplicationContextBeforeAfter;
 import org.apache.geronimo.kernel.Kernel;
 import org.apache.geronimo.kernel.jmx.JMXUtil;
-import org.apache.geronimo.naming.java.SimpleReadOnlyContext;
 import org.apache.geronimo.naming.reference.ClassLoaderAwareReference;
 import org.apache.geronimo.naming.reference.KernelAwareReference;
+import org.apache.geronimo.naming.enc.EnterpriseNamingContext;
 import org.apache.geronimo.security.deploy.DefaultPrincipal;
 import org.apache.geronimo.security.jacc.RoleDesignateSource;
 import org.apache.geronimo.transaction.TrackedConnectionAssociator;
@@ -240,7 +240,7 @@
                     ((ClassLoaderAwareReference) value).setClassLoader(this.webClassLoader);
                 }
             }
-            enc = new SimpleReadOnlyContext(componentContext);
+            enc = EnterpriseNamingContext.createEnterpriseNamingContext(componentContext);
         }
 
         int index = 0;

Added: geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/AbstractReadOnlyContext.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/AbstractReadOnlyContext.java?rev=345366&view=auto
==============================================================================
--- geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/AbstractReadOnlyContext.java
(added)
+++ geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/AbstractReadOnlyContext.java
Thu Nov 17 15:39:48 2005
@@ -0,0 +1,379 @@
+/**
+ *
+ * Copyright 2003-2004 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.naming.enc;
+
+import javax.naming.Binding;
+import javax.naming.CompositeName;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.Name;
+import javax.naming.NameClassPair;
+import javax.naming.NameNotFoundException;
+import javax.naming.NameParser;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.NotContextException;
+import javax.naming.OperationNotSupportedException;
+import java.io.Serializable;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * @version $Rev$ $Date$
+ */
+abstract class AbstractReadOnlyContext implements Context, Serializable {
+    private final String nameInNamespace;
+    private final String path;
+
+    protected AbstractReadOnlyContext(String nameInNamespace) {
+        if (nameInNamespace == null) throw new NullPointerException("nameInNamespace is null");
+
+        this.nameInNamespace = nameInNamespace;
+        if (nameInNamespace.length() > 0) {
+            path = nameInNamespace + "/";
+        } else {
+            path = nameInNamespace;
+        }
+    }
+
+    protected abstract Map getGlobalBindings();
+    protected abstract Map getLocalBindings();
+
+    //
+    //  Lookup methods
+    //
+
+    public final Object lookup(String name) throws NamingException {
+        if (name == null) throw new NullPointerException("name is null");
+
+        if (name.length() == 0) {
+            return this;
+        }
+
+        String fullName = path + name;
+
+        // lookup the bound object
+        Map globalBindings = getGlobalBindings();
+        Object result = globalBindings.get(fullName);
+        if (result == null) {
+            if (fullName.indexOf(':') > 0) {
+                Context ctx = new InitialContext();
+                return ctx.lookup(fullName);
+            } else if (new CompositeName(fullName).size() == 0) {
+                return this;
+            }
+            throw new NameNotFoundException(fullName);
+        }
+
+        // we should only ever see a CachingReference
+        if (result instanceof CachingReference) {
+            result = ((CachingReference)result).get();
+        }
+
+        return result;
+    }
+
+    public final Object lookup(Name name) throws NamingException {
+        if (name == null) throw new NullPointerException("name is null");
+
+        return lookup(name.toString());
+    }
+
+    public final Object lookupLink(Name name) throws NamingException {
+        if (name == null) throw new NullPointerException("name is null");
+
+        return lookupLink(name.toString());
+    }
+
+    public final Object lookupLink(String name) throws NamingException {
+        if (name == null) throw new NullPointerException("name is null");
+
+        return lookup(name);
+    }
+
+    //
+    //  List Operations
+    //
+
+    public final NamingEnumeration list(String name) throws NamingException {
+        if (name == null) throw new NullPointerException("name is null");
+
+        Object o = lookup(name);
+        if (o == this) {
+            return list();
+        } else if (o instanceof AbstractReadOnlyContext) {
+            return ((AbstractReadOnlyContext) o).list();
+        } else {
+            throw new NotContextException();
+        }
+    }
+
+    public final NamingEnumeration listBindings(String name) throws NamingException {
+        if (name == null) throw new NullPointerException("name is null");
+
+        Object o = lookup(name);
+        if (o == this) {
+            return listBindings();
+        } else if (o instanceof AbstractReadOnlyContext) {
+            return ((AbstractReadOnlyContext) o).listBindings();
+        } else {
+            throw new NotContextException();
+        }
+    }
+
+    private NamingEnumeration list() {
+        Map localBindings = getLocalBindings();
+        return new ListEnumeration(localBindings);
+    }
+
+    private NamingEnumeration listBindings() {
+        Map localBindings = getLocalBindings();
+        return new ListBindingEnumeration(localBindings);
+    }
+
+    public final NamingEnumeration list(Name name) throws NamingException {
+        if (name == null) throw new NullPointerException("name is null");
+
+        return list(name.toString());
+    }
+
+    public final NamingEnumeration listBindings(Name name) throws NamingException {
+        if (name == null) throw new NullPointerException("name is null");
+
+        return listBindings(name.toString());
+    }
+
+    private static final class ListEnumeration implements NamingEnumeration {
+        private final Iterator iterator;
+
+        public ListEnumeration(Map localBindings) {
+            this.iterator = localBindings.entrySet().iterator();
+        }
+
+        public boolean hasMore() {
+            return iterator.hasNext();
+        }
+
+        public boolean hasMoreElements() {
+            return iterator.hasNext();
+        }
+
+        public Object next() {
+            return nextElement();
+        }
+
+        public Object nextElement() {
+            Map.Entry entry = (Map.Entry) iterator.next();
+            String name = (String) entry.getKey();
+            Object value = entry.getValue();
+            String className = null;
+            if (value instanceof CachingReference) {
+                CachingReference cachingReference = (CachingReference) value;
+                className = cachingReference.getClassName();
+            } else {
+                className = value.getClass().getName();
+            }
+            return new NameClassPair(name, className);
+        }
+
+        public void close() {
+        }
+    }
+
+    private static final class ListBindingEnumeration implements NamingEnumeration {
+        private final Iterator iterator;
+
+        public ListBindingEnumeration(Map localBindings) {
+            this.iterator = localBindings.entrySet().iterator();
+        }
+
+        public boolean hasMore() {
+            return iterator.hasNext();
+        }
+
+        public boolean hasMoreElements() {
+            return iterator.hasNext();
+        }
+
+        public Object next() {
+            return nextElement();
+        }
+
+        public Object nextElement() {
+            Map.Entry entry = (Map.Entry) iterator.next();
+            String name = (String) entry.getKey();
+            Object value = entry.getValue();
+            return new EnterpriseBinding(name, value);
+        }
+
+        public void close() {
+        }
+    }
+
+    private static final class EnterpriseBinding extends Binding {
+        private final Object value;
+
+        public EnterpriseBinding(String name, Object value) {
+            super(name, value);
+            this.value = value;
+        }
+
+        public void setName(String name) {
+            throw new UnsupportedOperationException("EnterpriseNamingContext can not be modified");
+        }
+
+        public String getClassName() {
+            if (value instanceof CachingReference) {
+                CachingReference cachingReference = (CachingReference) value;
+                return cachingReference.getClassName();
+            }
+            return value.getClass().getName();
+        }
+
+        public void setClassName(String name) {
+            throw new UnsupportedOperationException("EnterpriseNamingContext can not be modified");
+        }
+
+        public Object getObject() {
+            if (value instanceof CachingReference) {
+                try {
+                    CachingReference cachingReference = (CachingReference) value;
+                    return cachingReference.get();
+                } catch (NamingException e) {
+                    throw new BindingResolutionException("Unable to resolve binding " + getName(),
e);
+                }
+            }
+            return value;
+        }
+
+        public void setObject(Object obj) {
+            throw new UnsupportedOperationException("EnterpriseNamingContext can not be modified");
+        }
+
+        public boolean isRelative() {
+            return false;
+        }
+
+        public void setRelative(boolean r) {
+            throw new UnsupportedOperationException("EnterpriseNamingContext can not be modified");
+        }
+    }
+
+    //
+    // Name manipulation
+    //
+
+    public final String getNameInNamespace() {
+        return nameInNamespace;
+    }
+
+    public final NameParser getNameParser(Name name) {
+        return EnterpriseNamingContextNameParser.INSTANCE;
+    }
+
+    public final NameParser getNameParser(String name) {
+        return EnterpriseNamingContextNameParser.INSTANCE;
+    }
+
+    public final Name composeName(Name name, Name prefix) throws NamingException {
+        if (name == null) throw new NullPointerException("name is null");
+        if (prefix == null) throw new NullPointerException("prefix is null");
+
+        Name result = (Name) prefix.clone();
+        result.addAll(name);
+        return result;
+    }
+
+    public final String composeName(String name, String prefix) throws NamingException {
+        if (name == null) throw new NullPointerException("name is null");
+        if (prefix == null) throw new NullPointerException("prefix is null");
+
+        CompositeName result = new CompositeName(prefix);
+        result.addAll(new CompositeName(name));
+        return result.toString();
+    }
+
+    //
+    //  Unsupported Operations
+    //
+
+    public final Hashtable getEnvironment() {
+        return new Hashtable();
+    }
+
+    public final Object addToEnvironment(String propName, Object propVal) throws NamingException
{
+        throw new OperationNotSupportedException("EnterpriseNamingContext can not be modified");
+    }
+
+    public final Object removeFromEnvironment(String propName) throws NamingException {
+        throw new OperationNotSupportedException("EnterpriseNamingContext can not be modified");
+    }
+
+    public final void bind(Name name, Object obj) throws NamingException {
+        throw new OperationNotSupportedException("EnterpriseNamingContext can not be modified");
+    }
+
+    public final void bind(String name, Object obj) throws NamingException {
+        throw new OperationNotSupportedException("EnterpriseNamingContext can not be modified");
+    }
+
+    public final void close() throws NamingException {
+        throw new OperationNotSupportedException("EnterpriseNamingContext can not be modified");
+    }
+
+    public final Context createSubcontext(Name name) throws NamingException {
+        throw new OperationNotSupportedException("EnterpriseNamingContext can not be modified");
+    }
+
+    public final Context createSubcontext(String name) throws NamingException {
+        throw new OperationNotSupportedException("EnterpriseNamingContext can not be modified");
+    }
+
+    public final void destroySubcontext(Name name) throws NamingException {
+        throw new OperationNotSupportedException("EnterpriseNamingContext can not be modified");
+    }
+
+    public final void destroySubcontext(String name) throws NamingException {
+        throw new OperationNotSupportedException("EnterpriseNamingContext can not be modified");
+    }
+
+    public final void rebind(Name name, Object obj) throws NamingException {
+        throw new OperationNotSupportedException("EnterpriseNamingContext can not be modified");
+    }
+
+    public final void rebind(String name, Object obj) throws NamingException {
+        throw new OperationNotSupportedException("EnterpriseNamingContext can not be modified");
+    }
+
+    public final void rename(Name oldName, Name newName) throws NamingException {
+        throw new OperationNotSupportedException("EnterpriseNamingContext can not be modified");
+    }
+
+    public final void rename(String oldName, String newName) throws NamingException {
+        throw new OperationNotSupportedException("EnterpriseNamingContext can not be modified");
+    }
+
+    public final void unbind(Name name) throws NamingException {
+        throw new OperationNotSupportedException("EnterpriseNamingContext can not be modified");
+    }
+
+    public final void unbind(String name) throws NamingException {
+        throw new OperationNotSupportedException("EnterpriseNamingContext can not be modified");
+    }
+}

Propchange: geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/AbstractReadOnlyContext.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/AbstractReadOnlyContext.java
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Id"

Added: geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/BindingResolutionException.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/BindingResolutionException.java?rev=345366&view=auto
==============================================================================
--- geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/BindingResolutionException.java
(added)
+++ geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/BindingResolutionException.java
Thu Nov 17 15:39:48 2005
@@ -0,0 +1,30 @@
+/**
+ *
+ * Copyright 2005 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.naming.enc;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class BindingResolutionException extends RuntimeException {
+    public BindingResolutionException(String message) {
+        super(message);
+    }
+
+    public BindingResolutionException(String message, Throwable cause) {
+        super(message, cause);
+    }
+}

Propchange: geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/BindingResolutionException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/BindingResolutionException.java
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Id"

Added: geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/CachingReference.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/CachingReference.java?rev=345366&view=auto
==============================================================================
--- geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/CachingReference.java
(added)
+++ geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/CachingReference.java
Thu Nov 17 15:39:48 2005
@@ -0,0 +1,76 @@
+/**
+ *
+ * Copyright 2005 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.naming.enc;
+
+import org.apache.geronimo.naming.reference.SimpleReference;
+
+import javax.naming.NamingException;
+import javax.naming.Reference;
+import javax.naming.spi.NamingManager;
+import java.util.Hashtable;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class CachingReference {
+    private final Object lock = new Object();
+    private final String fullName;
+    private final Reference reference;
+    private final String className;
+    private Object value;
+
+    public CachingReference(String fullName, Reference reference) {
+        this.fullName = fullName;
+        this.reference = reference;
+        className = reference.getClassName();
+    }
+
+    public Object get() throws NamingException {
+        synchronized(lock) {
+            if (value == null) {
+                value = resolveReference();
+            }
+            return value;
+        }
+    }
+
+    private Object resolveReference() throws NamingException {
+        // for SimpleReference we can just call the getContext method
+        if (reference instanceof SimpleReference) {
+            try {
+                return ((SimpleReference) reference).getContent();
+            } catch (NamingException e) {
+                throw e;
+            } catch (Exception e) {
+                throw (NamingException) new NamingException("Could not look up : " + fullName).initCause(e);
+            }
+        }
+
+        // for normal References we have to do it the slow way
+        try {
+            return NamingManager.getObjectInstance(reference, null, null, new Hashtable());
+        } catch (NamingException e) {
+            throw e;
+        } catch (Exception e) {
+            throw (NamingException) new NamingException("Could not look up : " + fullName).initCause(e);
+        }
+    }
+
+    public String getClassName() {
+        return className;
+    }
+}

Propchange: geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/CachingReference.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/CachingReference.java
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Id"

Added: geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/EnterpriseNamingContext.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/EnterpriseNamingContext.java?rev=345366&view=auto
==============================================================================
--- geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/EnterpriseNamingContext.java
(added)
+++ geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/EnterpriseNamingContext.java
Thu Nov 17 15:39:48 2005
@@ -0,0 +1,195 @@
+/**
+ *
+ * Copyright 2003-2004 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.naming.enc;
+
+import javax.naming.Context;
+import javax.naming.LinkRef;
+import javax.naming.Name;
+import javax.naming.NamingException;
+import javax.naming.Reference;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public final class EnterpriseNamingContext extends AbstractReadOnlyContext {
+    private final Map localBindings;
+
+    private final Map globalBindings;
+
+    public static Context createEnterpriseNamingContext(Map context) throws NamingException
{
+        return new EnterpriseNamingContext(context);
+    }
+
+    public EnterpriseNamingContext(Map context) throws NamingException {
+        super("");
+        validateBindings(context);
+        preprocessBindings(context);
+
+        Node rootContext = buildTree(context);
+
+        Map localBindings = new HashMap(rootContext.size());
+        for (Iterator iterator = rootContext.entrySet().iterator(); iterator.hasNext();)
{
+            Map.Entry entry = (Map.Entry) iterator.next();
+            String name = (String) entry.getKey();
+            Object value = entry.getValue();
+            if (value instanceof Node) {
+                Context nestedContext = new NestedEnterpriseNamingContext(name, (Node) value);
+                localBindings.put(name, nestedContext);
+            } else {
+                localBindings.put(name, value);
+            }
+        }
+        this.localBindings = Collections.unmodifiableMap(localBindings);
+
+
+        Map globalBindings = buildGlobalBindings("", localBindings);
+        this.globalBindings = Collections.unmodifiableMap(globalBindings);
+    }
+
+    private static void validateBindings(Map bindings) {
+        for (Iterator iterator = bindings.entrySet().iterator(); iterator.hasNext();) {
+            Map.Entry entry = (Map.Entry) iterator.next();
+            String name = (String) entry.getKey();
+            Object value = entry.getValue();
+            if (value instanceof Context) {
+                throw new IllegalArgumentException("EnterpriseNamingContext can not contain
a nested Context object: name=" + name);
+            }
+            if (value instanceof LinkRef) {
+                throw new IllegalArgumentException("EnterpriseNamingContext can not contain
a nested LinkRef object: name=" + name);
+            }
+        }
+    }
+
+    private static void preprocessBindings(Map bindings) {
+        for (Iterator iterator = new HashMap(bindings).entrySet().iterator(); iterator.hasNext();)
{
+            Map.Entry entry = (Map.Entry) iterator.next();
+            String name = (String) entry.getKey();
+            Object value = entry.getValue();
+            if (value instanceof Reference) {
+                bindings.put(name, new CachingReference(name, (Reference)value));
+            }
+        }
+    }
+
+    private static Node buildTree(Map context) throws NamingException {
+        Node rootContext = new Node();
+
+        // ENC must always contain an env context
+        rootContext.put("env", new Node());
+
+        for (Iterator iterator = context.entrySet().iterator(); iterator.hasNext();) {
+            Map.Entry entry = (Map.Entry) iterator.next();
+            String name = (String) entry.getKey();
+            Object value = entry.getValue();
+
+            Node parentContext = rootContext;
+
+            Name compoundName = EnterpriseNamingContextNameParser.INSTANCE.parse(name);
+            for (Enumeration parts = compoundName.getAll(); parts.hasMoreElements(); ) {
+                String part = (String) parts.nextElement();
+                // the last element in the path is the name of the value
+                if (parts.hasMoreElements()) {
+                    // nest context into parent
+                    Node bindings = (Node) parentContext.get(part);
+                    if (bindings == null) {
+                        bindings = new Node();
+                        parentContext.put(part, bindings);
+                    }
+
+                    parentContext = bindings;
+                }
+            }
+
+            parentContext.put(compoundName.get(compoundName.size() - 1), value);
+        }
+        return rootContext;
+    }
+
+    private static Map buildGlobalBindings(String nameInNamespace, Map context) {
+        String path = nameInNamespace;
+        if (path.length() > 0) {
+            path += "/";
+        }
+
+        Map globalBindings = new HashMap();
+        for (Iterator iterator = context.entrySet().iterator(); iterator.hasNext();) {
+            Map.Entry entry = (Map.Entry) iterator.next();
+            String name = (String) entry.getKey();
+            Object value = entry.getValue();
+            if (value instanceof NestedEnterpriseNamingContext) {
+                NestedEnterpriseNamingContext nestedContext = (NestedEnterpriseNamingContext)value;
+                globalBindings.putAll(buildGlobalBindings(nestedContext.getNameInNamespace(),
nestedContext.localBindings));
+            }
+            globalBindings.put(path + name, value);
+        }
+        return globalBindings;
+    }
+
+    protected Map getGlobalBindings() {
+        return globalBindings;
+    }
+
+    protected Map getLocalBindings() {
+        return localBindings;
+    }
+
+    /**
+     * Nested context which shares the global bindings map.
+     */
+    public final class NestedEnterpriseNamingContext extends AbstractReadOnlyContext {
+        private final Map localBindings;
+
+        public NestedEnterpriseNamingContext(String nameInNamespace, Node bindings) {
+            super(nameInNamespace);
+            if (nameInNamespace.length() == 0) throw new IllegalArgumentException("nameInNamespace
is empty");
+
+            Map localBindings = new HashMap(bindings.size());
+            for (Iterator iterator = bindings.entrySet().iterator(); iterator.hasNext();)
{
+                Map.Entry entry = (Map.Entry) iterator.next();
+                String name = (String) entry.getKey();
+                Object value = entry.getValue();
+                if (value instanceof Node) {
+                    Context context = new NestedEnterpriseNamingContext(nameInNamespace +
"/" + name, (Node) value);
+                    localBindings.put(name, context);
+                } else {
+                    localBindings.put(name, value);
+                }
+            }
+            this.localBindings = Collections.unmodifiableMap(localBindings);
+        }
+
+        protected Map getGlobalBindings() {
+            return globalBindings;
+        }
+
+        protected Map getLocalBindings() {
+            return localBindings;
+        }
+    }
+
+    /**
+     * Lame subclass of hashmap used to differentiate between a Map in the context an a nested
element during tree building
+     */
+    private static final class Node extends HashMap {
+    }
+}

Propchange: geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/EnterpriseNamingContext.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/EnterpriseNamingContext.java
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Id"

Added: geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/EnterpriseNamingContextNameParser.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/EnterpriseNamingContextNameParser.java?rev=345366&view=auto
==============================================================================
--- geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/EnterpriseNamingContextNameParser.java
(added)
+++ geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/EnterpriseNamingContextNameParser.java
Thu Nov 17 15:39:48 2005
@@ -0,0 +1,43 @@
+/**
+ *
+ * Copyright 2005 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.naming.enc;
+
+import javax.naming.NameParser;
+import javax.naming.Name;
+import javax.naming.NamingException;
+import javax.naming.CompoundName;
+import java.util.Properties;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public final class EnterpriseNamingContextNameParser implements NameParser {
+    private static final Properties PARSER_PROPERTIES = new Properties();
+    static {
+        PARSER_PROPERTIES.put("jndi.syntax.direction", "left_to_right");
+        PARSER_PROPERTIES.put("jndi.syntax.separator", "/");
+    }
+
+    public final static EnterpriseNamingContextNameParser INSTANCE = new EnterpriseNamingContextNameParser();
+
+    private EnterpriseNamingContextNameParser() {
+    }
+
+    public Name parse(String name) throws NamingException {
+        return new CompoundName(name, PARSER_PROPERTIES);
+    }
+}

Propchange: geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/EnterpriseNamingContextNameParser.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/trunk/modules/naming/src/java/org/apache/geronimo/naming/enc/EnterpriseNamingContextNameParser.java
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Id"

Added: geronimo/trunk/modules/naming/src/test/org/apache/geronimo/naming/enc/EnterpriseNamingContextTest.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/naming/src/test/org/apache/geronimo/naming/enc/EnterpriseNamingContextTest.java?rev=345366&view=auto
==============================================================================
--- geronimo/trunk/modules/naming/src/test/org/apache/geronimo/naming/enc/EnterpriseNamingContextTest.java
(added)
+++ geronimo/trunk/modules/naming/src/test/org/apache/geronimo/naming/enc/EnterpriseNamingContextTest.java
Thu Nov 17 15:39:48 2005
@@ -0,0 +1,149 @@
+/**
+ *
+ * Copyright 2005 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.naming.enc;
+
+import junit.framework.TestCase;
+
+import javax.naming.Context;
+import javax.naming.Name;
+import javax.naming.NameClassPair;
+import javax.naming.Binding;
+import javax.naming.NamingEnumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class EnterpriseNamingContextTest extends TestCase {
+    private static final String STRING_VAL = "some string";
+
+    public void testLookup() throws Exception {
+        Map map = new HashMap();
+        map.put("string", STRING_VAL);
+        map.put("nested/context/string", STRING_VAL);
+
+        Context context = EnterpriseNamingContext.createEnterpriseNamingContext(map);
+
+        Name stringName = context.getNameParser("").parse("string");
+        assertEquals(STRING_VAL, context.lookup("string"));
+        assertEquals(STRING_VAL, context.lookup(stringName));
+        assertEquals(STRING_VAL, context.lookupLink("string"));
+        assertEquals(STRING_VAL, context.lookupLink(stringName));
+
+        Name nestedContextStringName = context.getNameParser("").parse("nested/context/string");
+        assertEquals(STRING_VAL, context.lookup("nested/context/string"));
+        assertEquals(STRING_VAL, context.lookup(nestedContextStringName));
+        assertEquals(STRING_VAL, context.lookupLink("nested/context/string"));
+        assertEquals(STRING_VAL, context.lookupLink(nestedContextStringName));
+    }
+
+    public void testList() throws Exception {
+        Map map = new HashMap();
+        map.put("one", new Integer(1));
+        map.put("two", new Integer(2));
+        map.put("three", new Integer(3));
+
+        Context context = EnterpriseNamingContext.createEnterpriseNamingContext(map);
+
+        Map result = toListResults(context.list(""));
+        assertEquals(4, result.size());
+        assertEquals(Integer.class.getName(), result.get("one"));
+        assertEquals(Integer.class.getName(), result.get("two"));
+        assertEquals(Integer.class.getName(), result.get("three"));
+        assertNotNull(result.get("env"));
+
+        result = toListBindingResults(context.listBindings(""));
+        assertEquals(4, result.size());
+        assertEquals(new Integer(1), result.get("one"));
+        assertEquals(new Integer(2), result.get("two"));
+        assertEquals(new Integer(3), result.get("three"));
+        assertNotNull(result.get("env"));
+    }
+
+    private Map toListResults(NamingEnumeration enumeration) {
+        Map result = new HashMap();
+        while (enumeration.hasMoreElements()) {
+            NameClassPair nameClassPair = (NameClassPair) enumeration.nextElement();
+            String name = nameClassPair.getName();
+            assertFalse(result.containsKey(name));
+            result.put(name, nameClassPair.getClassName());
+        }
+        return result;
+    }
+
+    private Map toListBindingResults(NamingEnumeration enumeration) {
+        Map result = new HashMap();
+        while (enumeration.hasMoreElements()) {
+            Binding binding = (Binding) enumeration.nextElement();
+            String name = binding.getName();
+            assertFalse(result.containsKey(name));
+            result.put(name, binding.getObject());
+        }
+        return result;
+    }
+
+    public void testNestedLookup() throws Exception {
+        Map map = new HashMap();
+        map.put("a/b/c/d/e/string", STRING_VAL);
+
+        Context context = EnterpriseNamingContext.createEnterpriseNamingContext(map);
+
+        assertEquals(STRING_VAL, context.lookup("a/b/c/d/e/string"));
+        Context nestedContext = (Context) context.lookup("a/b/c");
+        assertNotNull(nestedContext);
+        assertEquals("a/b/c", nestedContext.getNameInNamespace());
+        assertEquals(STRING_VAL, nestedContext.lookup("d/e/string"));
+    }
+
+    public void testNestedList() throws Exception {
+        Map map = new HashMap();
+        map.put("a/b/c/d/e/one", new Integer(1));
+        map.put("a/b/c/d/e/two", new Integer(2));
+        map.put("a/b/c/d/e/three", new Integer(3));
+
+        Context context = EnterpriseNamingContext.createEnterpriseNamingContext(map);
+
+        Map result = toListResults(context.list("a/b/c/d/e"));
+        assertEquals(3, result.size());
+        assertEquals(Integer.class.getName(), result.get("one"));
+        assertEquals(Integer.class.getName(), result.get("two"));
+        assertEquals(Integer.class.getName(), result.get("three"));
+
+        result = toListBindingResults(context.listBindings("a/b/c/d/e"));
+        assertEquals(3, result.size());
+        assertEquals(new Integer(1), result.get("one"));
+        assertEquals(new Integer(2), result.get("two"));
+        assertEquals(new Integer(3), result.get("three"));
+
+        Context nestedContext = (Context) context.lookup("a/b/c");
+        assertNotNull(nestedContext);
+        assertEquals("a/b/c", nestedContext.getNameInNamespace());
+
+        result = toListResults(nestedContext.list("d/e"));
+        assertEquals(3, result.size());
+        assertEquals(Integer.class.getName(), result.get("one"));
+        assertEquals(Integer.class.getName(), result.get("two"));
+        assertEquals(Integer.class.getName(), result.get("three"));
+
+        result = toListBindingResults(nestedContext.listBindings("d/e"));
+        assertEquals(3, result.size());
+        assertEquals(new Integer(1), result.get("one"));
+        assertEquals(new Integer(2), result.get("two"));
+        assertEquals(new Integer(3), result.get("three"));
+    }
+}

Propchange: geronimo/trunk/modules/naming/src/test/org/apache/geronimo/naming/enc/EnterpriseNamingContextTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/trunk/modules/naming/src/test/org/apache/geronimo/naming/enc/EnterpriseNamingContextTest.java
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Id"

Modified: geronimo/trunk/modules/naming/src/test/org/apache/geronimo/naming/java/ContextBuilderTest.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/naming/src/test/org/apache/geronimo/naming/java/ContextBuilderTest.java?rev=345366&r1=345365&r2=345366&view=diff
==============================================================================
--- geronimo/trunk/modules/naming/src/test/org/apache/geronimo/naming/java/ContextBuilderTest.java
(original)
+++ geronimo/trunk/modules/naming/src/test/org/apache/geronimo/naming/java/ContextBuilderTest.java
Thu Nov 17 15:39:48 2005
@@ -22,10 +22,13 @@
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.Map;
+import java.util.HashMap;
 import javax.management.ObjectName;
 import javax.naming.NameClassPair;
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
+import javax.naming.Context;
 
 import junit.framework.TestCase;
 
@@ -35,6 +38,7 @@
 import org.apache.geronimo.kernel.KernelRegistry;
 import org.apache.geronimo.kernel.KernelFactory;
 import org.apache.geronimo.kernel.Kernel;
+import org.apache.geronimo.naming.enc.EnterpriseNamingContext;
 
 /**
  * @version $Rev$ $Date$
@@ -64,7 +68,7 @@
         builder.addEnvEntry("double", Double.class.getName(), doubleVal.toString(), null);
         builder.addEnvEntry("boolean", Boolean.class.getName(), booleanVal.toString(), null);
 
-        SimpleReadOnlyContext context = new SimpleReadOnlyContext(builder.getContext());
+        Context context = EnterpriseNamingContext.createEnterpriseNamingContext(builder.getContext());
         Set actual = new HashSet();
         for (NamingEnumeration e = context.listBindings("env"); e.hasMore();) {
             NameClassPair pair = (NameClassPair) e.next();
@@ -87,7 +91,7 @@
         proxy = new ArrayList();
 //        builder.addResourceEnvRef("resourceenvref", List.class, localRef);
 
-        SimpleReadOnlyContext context = new SimpleReadOnlyContext(builder.getContext());
+        Context context = EnterpriseNamingContext.createEnterpriseNamingContext(builder.getContext());
         Kernel kernel = KernelFactory.newInstance().createKernel("test.kernel");
         kernel.boot();
         try {
@@ -105,9 +109,9 @@
     }
 
     public void testEmptyEnvironment() throws NamingException {
-        SimpleReadOnlyContext context = new SimpleReadOnlyContext(builder.getContext());
+        Context context = EnterpriseNamingContext.createEnterpriseNamingContext(builder.getContext());
         try {
-            ReadOnlyContext env = (ReadOnlyContext) context.lookup("env");
+            Context env = (Context) context.lookup("env");
             assertNotNull(env);
         } catch (NamingException e) {
             fail();

Modified: geronimo/trunk/modules/tomcat/src/java/org/apache/geronimo/tomcat/GeronimoStandardContext.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/tomcat/src/java/org/apache/geronimo/tomcat/GeronimoStandardContext.java?rev=345366&r1=345365&r2=345366&view=diff
==============================================================================
--- geronimo/trunk/modules/tomcat/src/java/org/apache/geronimo/tomcat/GeronimoStandardContext.java
(original)
+++ geronimo/trunk/modules/tomcat/src/java/org/apache/geronimo/tomcat/GeronimoStandardContext.java
Thu Nov 17 15:39:48 2005
@@ -30,21 +30,19 @@
 import org.apache.catalina.LifecycleException;
 import org.apache.catalina.Valve;
 import org.apache.catalina.Wrapper;
-import org.apache.catalina.Pipeline;
 import org.apache.catalina.cluster.CatalinaCluster;
 import org.apache.catalina.connector.Request;
 import org.apache.catalina.connector.Response;
 import org.apache.catalina.valves.ValveBase;
 import org.apache.catalina.core.StandardContext;
-import org.apache.catalina.core.StandardPipeline;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 import org.apache.geronimo.common.DeploymentException;
 import org.apache.geronimo.kernel.StoredObject;
-import org.apache.geronimo.naming.java.SimpleReadOnlyContext;
 import org.apache.geronimo.naming.reference.ClassLoaderAwareReference;
 import org.apache.geronimo.naming.reference.KernelAwareReference;
+import org.apache.geronimo.naming.enc.EnterpriseNamingContext;
 import org.apache.geronimo.security.ContextManager;
 import org.apache.geronimo.security.IdentificationPrincipal;
 import org.apache.geronimo.security.SubjectId;
@@ -89,7 +87,7 @@
                         ((ClassLoaderAwareReference) value).setClassLoader(ctx.getWebClassLoader());
                     }
                 }
-                enc = new SimpleReadOnlyContext(componentContext);
+                enc = EnterpriseNamingContext.createEnterpriseNamingContext(componentContext);
             }
         } catch (NamingException ne) {
             log.error(ne);



Mime
View raw message