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/08/23 23:00:53 UTC

svn commit: r434176 - in /geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context: AbstractFederatedContext.java UnmodifiableContext.java WritableContext.java

Author: dain
Date: Wed Aug 23 14:00:51 2006
New Revision: 434176

URL: http://svn.apache.org/viewvc?rev=434176&view=rev
Log:
Extracted an abstract base class for contexts that support federation.  This makes WritableContext and UnmodifiableContext much more readable.

Added:
    geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractFederatedContext.java
Modified:
    geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/UnmodifiableContext.java
    geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/WritableContext.java

Added: geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractFederatedContext.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractFederatedContext.java?rev=434176&view=auto
==============================================================================
--- geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractFederatedContext.java (added)
+++ geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractFederatedContext.java Wed Aug 23 14:00:51 2006
@@ -0,0 +1,143 @@
+/**
+ *
+ * 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.xbean.naming.context;
+
+import javax.naming.NamingException;
+import javax.naming.Name;
+import javax.naming.NamingEnumeration;
+import javax.naming.Context;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Iterator;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public abstract class AbstractFederatedContext extends AbstractContext {
+    private final ContextFederation contextFederation = new ContextFederation(this);
+
+    public AbstractFederatedContext() {
+        this("");
+    }
+
+    public AbstractFederatedContext(String nameInNamespace) {
+        super(nameInNamespace);
+    }
+
+    protected Object faultLookup(String stringName, Name parsedName) {
+        Object value = contextFederation.lookup(parsedName);
+        if (value != null) {
+            return value;
+        }
+        return super.faultLookup(stringName, parsedName);
+    }
+
+    protected NamingEnumeration list() throws NamingException {
+        Map bindings = getListBindings();
+        return new ContextUtil.ListEnumeration(bindings);
+    }
+
+    protected NamingEnumeration listBindings() throws NamingException {
+        Map bindings = getListBindings();
+        return new ContextUtil.ListBindingEnumeration(bindings);
+    }
+
+    protected Map getListBindings() throws NamingException {
+        Map bindings = new HashMap();
+        bindings.putAll(getBindings());
+        bindings.putAll(contextFederation.getFederatedBindings());
+        return bindings;
+    }
+
+    protected void addFederatedContext(Context federatedContext) throws NamingException {
+        contextFederation.addContext(federatedContext);
+        for (Iterator iterator = getBindings().values().iterator(); iterator.hasNext();) {
+            Object value = iterator.next();
+            if (value instanceof AbstractNestedFederatedContext) {
+                AbstractNestedFederatedContext nestedContext = (AbstractNestedFederatedContext) value;
+                nestedContext.addFederatedContext(federatedContext);
+            }
+        }
+    }
+
+    public boolean isNestedSubcontext(Object value) {
+        if (value instanceof AbstractNestedFederatedContext) {
+            AbstractFederatedContext.AbstractNestedFederatedContext context = (AbstractNestedFederatedContext) value;
+            return this == context.getOuterContext();
+        }
+        return false;
+    }
+
+    public abstract class AbstractNestedFederatedContext extends AbstractContext {
+        private final ContextFederation contextFederation;
+
+        public AbstractNestedFederatedContext(String path) throws NamingException {
+            super(AbstractFederatedContext.this.getNameInNamespace(path));
+
+            ContextFederation outerContextFederation = getOuterContext().contextFederation;
+            this.contextFederation = outerContextFederation.createSubcontextFederation(path, this);
+        }
+
+        public boolean isNestedSubcontext(Object value) {
+            if (value instanceof AbstractNestedFederatedContext) {
+                AbstractNestedFederatedContext context = (AbstractNestedFederatedContext) value;
+                return getOuterContext() == context.getOuterContext();
+            }
+            return false;
+        }
+
+        protected Object faultLookup(String stringName, Name parsedName) {
+            Object value = contextFederation.lookup(parsedName);
+            if (value != null) {
+                return value;
+            }
+            return super.faultLookup(stringName, parsedName);
+        }
+
+        protected NamingEnumeration list() throws NamingException {
+            Map bindings = getListBindings();
+            return new ContextUtil.ListEnumeration(bindings);
+        }
+
+        protected NamingEnumeration listBindings() throws NamingException {
+            Map bindings = getListBindings();
+            return new ContextUtil.ListBindingEnumeration(bindings);
+        }
+
+        protected Map getListBindings() throws NamingException {
+            Map bindings = new HashMap();
+            bindings.putAll(getBindings());
+            bindings.putAll(contextFederation.getFederatedBindings());
+            return bindings;
+        }
+
+        protected AbstractFederatedContext getOuterContext() {
+            return AbstractFederatedContext.this;
+        }
+
+        protected void addFederatedContext(Context federatedContext) throws NamingException {
+            contextFederation.addContext(federatedContext);
+            for (Iterator iterator = getBindings().values().iterator(); iterator.hasNext();) {
+                Object value = iterator.next();
+                if (value instanceof AbstractNestedFederatedContext) {
+                    AbstractNestedFederatedContext nestedContext = (AbstractNestedFederatedContext) value;
+                    nestedContext.addFederatedContext(federatedContext);
+                }
+            }
+        }
+    }
+}

Modified: geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/UnmodifiableContext.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/UnmodifiableContext.java?rev=434176&r1=434175&r2=434176&view=diff
==============================================================================
--- geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/UnmodifiableContext.java (original)
+++ geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/UnmodifiableContext.java Wed Aug 23 14:00:51 2006
@@ -45,16 +45,8 @@
         super(nameInNamespace, bindings, cacheReferences);
     }
 
-    public boolean isNestedSubcontext(Object value) {
-        if (value instanceof NestedUnmodifiableContext) {
-            NestedUnmodifiableContext context = (NestedUnmodifiableContext) value;
-            return this == context.getUnmodifiableContext();
-        }
-        return false;
-    }
-
     public Context createNestedSubcontext(String path, Map bindings) throws NamingException {
-        return new NestedUnmodifiableContext(path, bindings, contextFederation);
+        return new NestedUnmodifiableContext(path, bindings);
     }
 
     public final void bind(Name name, Object obj) throws NamingException {
@@ -113,25 +105,14 @@
      * Nested context which shares the absolute index map in MapContext.
      */
     public class NestedUnmodifiableContext extends NestedWritableContext {
-        public NestedUnmodifiableContext(String path, Map bindings, ContextFederation contextFederation) throws NamingException {
-            super(path, bindings, contextFederation);
-        }
-
-        public boolean isNestedSubcontext(Object value) {
-            if (value instanceof NestedUnmodifiableContext) {
-                NestedUnmodifiableContext context = (NestedUnmodifiableContext) value;
-                return getUnmodifiableContext() == context.getUnmodifiableContext();
-            }
-            return false;
+        public NestedUnmodifiableContext(String path, Map bindings) throws NamingException {
+            super(path, bindings);
         }
 
         public Context createNestedSubcontext(String path, Map bindings) throws NamingException {
-            return new NestedUnmodifiableContext(path, bindings, contextFederation);
+            return new NestedUnmodifiableContext(path, bindings);
         }
 
-        protected UnmodifiableContext getUnmodifiableContext() {
-            return UnmodifiableContext.this;
-        }
         public final void bind(Name name, Object obj) throws NamingException {
             throw new OperationNotSupportedException("Context is read only");
         }

Modified: geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/WritableContext.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/WritableContext.java?rev=434176&r1=434175&r2=434176&view=diff
==============================================================================
--- geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/WritableContext.java (original)
+++ geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/WritableContext.java Wed Aug 23 14:00:51 2006
@@ -22,9 +22,7 @@
 import org.apache.xbean.naming.reference.CachingReference;
 
 import javax.naming.Context;
-import javax.naming.Name;
 import javax.naming.NameNotFoundException;
-import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
 import java.util.Collections;
 import java.util.HashMap;
@@ -34,11 +32,10 @@
 /**
  * @version $Rev$ $Date$
  */
-public class WritableContext extends AbstractContext {
+public class WritableContext extends AbstractFederatedContext {
     private final Lock writeLock = new ReentrantLock();
     private final AtomicReference bindingsRef;
     private final AtomicReference indexRef;
-    protected final ContextFederation contextFederation = new ContextFederation(this);
 
     public WritableContext() throws NamingException {
         this("", Collections.EMPTY_MAP, true);
@@ -69,31 +66,6 @@
         this.indexRef = new AtomicReference(Collections.unmodifiableMap(buildIndex("", localBindings)));
     }
 
-    protected Object faultLookup(String stringName, Name parsedName) {
-        Object value = contextFederation.lookup(parsedName);
-        if (value != null) {
-            return value;
-        }
-        return super.faultLookup(stringName, parsedName);
-    }
-
-    protected NamingEnumeration list() throws NamingException {
-        Map bindings = getListBindings();
-        return new ContextUtil.ListEnumeration(bindings);
-    }
-
-    protected NamingEnumeration listBindings() throws NamingException {
-        Map bindings = getListBindings();
-        return new ContextUtil.ListBindingEnumeration(bindings);
-    }
-
-    protected Map getListBindings() throws NamingException {
-        Map bindings = new HashMap();
-        bindings.putAll(getBindings());
-        bindings.putAll(contextFederation.getFederatedBindings());
-        return bindings;
-    }
-
     protected void addBinding(String name, Object value, boolean rebind) throws NamingException {
         addBinding(bindingsRef, name, value);
     }
@@ -109,14 +81,12 @@
                 // if we already have a context bound at the specified value
                 if (bindings.containsKey(name)) {
                     NestedWritableContext nestedContext = (NestedWritableContext) bindings.get(name);
-                    // push new context into all children
-                    Map nestedBindings = (Map) nestedContext.bindingsRef.get();
-                    addFederatedContext(nestedBindings, federatedContext);
+                    nestedContext.addFederatedContext(federatedContext);
                     return;
                 }
 
                 NestedWritableContext nestedContext = (NestedWritableContext) createNestedSubcontext(name, Collections.EMPTY_MAP);
-                nestedContext.contextFederation.addContext(federatedContext);
+                nestedContext.addFederatedContext(federatedContext);
                 value = nestedContext;
             }
 
@@ -131,18 +101,6 @@
         }
     }
 
-    protected void addFederatedContext(Map bindings, Context federatedContext) {
-        for (Iterator iterator = bindings.values().iterator(); iterator.hasNext();) {
-            Object value = iterator.next();
-            if (value instanceof NestedWritableContext) {
-                NestedWritableContext nestedContext = (NestedWritableContext) value;
-                nestedContext.contextFederation.addContext(federatedContext);
-                Map nestedBindings = (Map) nestedContext.bindingsRef.get();
-                addFederatedContext(nestedBindings, federatedContext);
-            }
-        }
-    }
-
     private Map addToIndex(String name, Object value) {
         Map index = (Map) indexRef.get();
         Map newIndex = new HashMap(index);
@@ -193,16 +151,8 @@
         return newIndex;
     }
 
-    public boolean isNestedSubcontext(Object value) {
-        if (value instanceof NestedWritableContext) {
-            NestedWritableContext context = (NestedWritableContext) value;
-            return this == context.getUnmodifiableContext();
-        }
-        return false;
-    }
-
     public Context createNestedSubcontext(String path, Map bindings) throws NamingException {
-        return new NestedWritableContext(path,bindings, contextFederation);
+        return new NestedWritableContext(path,bindings);
     }
 
     private static Map buildIndex(String nameInNamespace, Map bindings) {
@@ -238,32 +188,21 @@
     /**
      * Nested context which shares the absolute index map in MapContext.
      */
-    public class NestedWritableContext extends AbstractContext {
+    public class NestedWritableContext extends AbstractNestedFederatedContext {
         private final AtomicReference bindingsRef;
         private final String pathWithSlash;
-        protected final ContextFederation contextFederation;
 
-        public NestedWritableContext(String path, Map bindings, ContextFederation parentContextFederation) throws NamingException {
+        public NestedWritableContext(String path, Map bindings) throws NamingException {
             super(WritableContext.this.getNameInNamespace(path));
 
             if (!path.endsWith("/")) path += "/";
             this.pathWithSlash = path;
 
             this.bindingsRef = new AtomicReference(Collections.unmodifiableMap(bindings));
-
-            this.contextFederation = parentContextFederation.createSubcontextFederation(path, this);
-        }
-
-        public boolean isNestedSubcontext(Object value) {
-            if (value instanceof NestedWritableContext) {
-                NestedWritableContext context = (NestedWritableContext) value;
-                return getUnmodifiableContext() == context.getUnmodifiableContext();
-            }
-            return false;
         }
 
         public Context createNestedSubcontext(String path, Map bindings) throws NamingException {
-            return new NestedWritableContext(path, bindings, contextFederation);
+            return new NestedWritableContext(path, bindings);
         }
 
         protected Object getDeepBinding(String name) {
@@ -276,41 +215,12 @@
             return bindings;
         }
 
-        protected Object faultLookup(String stringName, Name parsedName) {
-            Object value = contextFederation.lookup(parsedName);
-            if (value != null) {
-                return value;
-            }
-            return super.faultLookup(stringName, parsedName);
-        }
-
-        protected NamingEnumeration list() throws NamingException {
-            Map bindings = getListBindings();
-            return new ContextUtil.ListEnumeration(bindings);
-        }
-
-        protected NamingEnumeration listBindings() throws NamingException {
-            Map bindings = getListBindings();
-            return new ContextUtil.ListBindingEnumeration(bindings);
-        }
-
-        protected Map getListBindings() throws NamingException {
-            Map bindings = new HashMap();
-            bindings.putAll(getBindings());
-            bindings.putAll(contextFederation.getFederatedBindings());
-            return bindings;
-        }
-
         protected void addBinding(String name, Object value, boolean rebind) throws NamingException {
             WritableContext.this.addBinding(bindingsRef, name, value);
         }
 
         protected void removeBinding(String name) throws NameNotFoundException {
             WritableContext.this.removeBinding(bindingsRef, name);
-        }
-
-        private WritableContext getUnmodifiableContext() {
-            return WritableContext.this;
         }
     }
 }