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/24 19:54:00 UTC

svn commit: r434447 - in /geronimo/xbean/branches/colossus/xbean-naming/src: main/java/org/apache/xbean/naming/context/ test/java/org/apache/xbean/naming/context/

Author: dain
Date: Thu Aug 24 10:53:59 2006
New Revision: 434447

URL: http://svn.apache.org/viewvc?rev=434447&view=rev
Log:
Added support for delegation of bind and unbind to federated contexts

Modified:
    geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractContext.java
    geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractFederatedContext.java
    geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/ContextFederation.java
    geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/ImmutableContext.java
    geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/WritableContext.java
    geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/FederationTest.java

Modified: geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractContext.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractContext.java?rev=434447&r1=434446&r2=434447&view=diff
==============================================================================
--- geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractContext.java (original)
+++ geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractContext.java Thu Aug 24 10:53:59 2006
@@ -289,7 +289,7 @@
      * @param rebind if true, this method will replace any exsiting binding, otherwise a NamingException will be thrown
      * @throws NamingException if a problem occurs while (re)binding
      */
-    private void addBinding(Context context, String name, Object value, boolean rebind) throws NamingException {
+    protected void addBinding(Context context, String name, Object value, boolean rebind) throws NamingException {
         if (context == this || (context instanceof AbstractContext && isNestedSubcontext(context))) {
             AbstractContext abstractContext = (AbstractContext) context;
             abstractContext.addBinding(name, value, rebind);
@@ -302,7 +302,7 @@
         }
     }
 
-    protected abstract void addBinding(String name, Object value, boolean rebind) throws NamingException;
+    protected abstract boolean addBinding(String name, Object value, boolean rebind) throws NamingException;
 
     /**
      * Creates a context tree which will be rooted at the specified path and contain a single entry located down
@@ -339,7 +339,7 @@
      * @param name name under which the value should be bound
      * @throws NamingException if a problem occurs during the bind such as a value already being bound
      */
-    protected abstract void removeBinding(String name) throws NamingException;
+    protected abstract boolean removeBinding(String name) throws NamingException;
 
     protected void removeDeepBinding(Name name, boolean pruneEmptyContexts) throws NamingException {
         if (name == null) throw new NullPointerException("name is null");
@@ -630,7 +630,7 @@
             throw new NullPointerException("name is null");
         } else if (oldName.isEmpty() || newName.isEmpty()) {
             throw new InvalidNameException("Name cannot be empty");
-        } 
+        }
         this.bind(newName, this.lookup(oldName));
         this.unbind(oldName);
     }

Modified: 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=434447&r1=434446&r2=434447&view=diff
==============================================================================
--- geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractFederatedContext.java (original)
+++ geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractFederatedContext.java Thu Aug 24 10:53:59 2006
@@ -16,22 +16,23 @@
  */
 package org.apache.xbean.naming.context;
 
-import javax.naming.NamingException;
-import javax.naming.Name;
-import javax.naming.NamingEnumeration;
 import javax.naming.Context;
+import javax.naming.Name;
+import javax.naming.NameAlreadyBoundException;
+import javax.naming.NamingException;
 import javax.naming.OperationNotSupportedException;
-import java.util.Map;
-import java.util.HashMap;
+import java.util.Collections;
 import java.util.Iterator;
+import java.util.Map;
 
 /**
  * @version $Rev$ $Date$
  */
 public abstract class AbstractFederatedContext extends AbstractContext {
-    private final ContextFederation contextFederation = new ContextFederation(this);
+    private final ContextFederation contextFederation;
     private final ContextAccess contextAccess;
     private final boolean modifiable;
+    private final AbstractFederatedContext masterContext;
 
     public AbstractFederatedContext() {
         this("", ContextAccess.MODIFIABLE);
@@ -41,10 +42,20 @@
         this(nameInNamespace, ContextAccess.MODIFIABLE);
     }
 
-    protected AbstractFederatedContext(String nameInNamespace, ContextAccess contextAccess) {
+    public AbstractFederatedContext(String nameInNamespace, ContextAccess contextAccess) {
         super(nameInNamespace);
+        this.masterContext = this;
+        this.contextFederation = new ContextFederation(this);
         this.contextAccess = contextAccess;
-        modifiable = contextAccess.isModifiable(getParsedNameInNamespace());
+        this.modifiable = contextAccess.isModifiable(getParsedNameInNamespace());
+    }
+
+    public AbstractFederatedContext(AbstractFederatedContext masterContext, String path) throws NamingException {
+        super(masterContext.getNameInNamespace(path));
+        this.masterContext = masterContext;
+        this.contextFederation = this.masterContext.contextFederation.createSubcontextFederation(path, this);
+        this.contextAccess = masterContext.contextAccess;
+        this.modifiable = masterContext.contextAccess.isModifiable(getParsedNameInNamespace());
     }
 
     protected Object faultLookup(String stringName, Name parsedName) {
@@ -55,42 +66,77 @@
         return super.faultLookup(stringName, parsedName);
     }
 
-    protected NamingEnumeration list() throws NamingException {
-        Map bindings = getListBindings();
-        return new ContextUtil.ListEnumeration(bindings);
+    protected final Map getBindings() throws NamingException {
+        Map bindings = contextFederation.getFederatedBindings();
+        bindings.putAll(getWrapperBindings());
+        return bindings;
     }
 
-    protected NamingEnumeration listBindings() throws NamingException {
-        Map bindings = getListBindings();
-        return new ContextUtil.ListBindingEnumeration(bindings);
+    protected abstract Map getWrapperBindings() throws NamingException;
+
+    protected boolean addBinding(String name, Object value, boolean rebind) throws NamingException {
+        if (!(value instanceof Context && !isNestedSubcontext(value))) {
+            return contextFederation.addBinding(name, value, rebind);
+        } else if (value instanceof Context && !isNestedSubcontext(value)) {
+            Context federatedContext = (Context) value;
+
+            // if we already have a context bound at the specified value
+            Object existingValue = getBinding(name);
+            if (existingValue != null) {
+                if (!(existingValue instanceof AbstractFederatedContext)) {
+                    throw new NameAlreadyBoundException(name);
+                }
+
+                AbstractFederatedContext nestedContext = (AbstractFederatedContext) existingValue;
+                addFederatedContext(nestedContext, federatedContext);
+                return true;
+            } else {
+                AbstractFederatedContext nestedContext = (AbstractFederatedContext) createNestedSubcontext(name, Collections.EMPTY_MAP);
+                addFederatedContext(nestedContext, federatedContext);
+
+                // call back into this method using the new nested context
+                // this gives subclasses a chance to handle the binding
+                return addBinding(name, nestedContext, rebind);
+            }
+        }
+
+        return false;
     }
 
-    protected Map getListBindings() throws NamingException {
-        Map bindings = new HashMap();
-        bindings.putAll(getBindings());
-        bindings.putAll(contextFederation.getFederatedBindings());
-        return bindings;
+    protected boolean removeBinding(String name) throws NamingException {
+        return contextFederation.removeBinding(name);
     }
 
-    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);
+    protected static void addFederatedContext(AbstractFederatedContext wrappingContext, Context innerContext) throws NamingException {
+        wrappingContext.contextFederation.addContext(innerContext);
+        for (Iterator iterator = wrappingContext.getWrapperBindings().entrySet().iterator(); iterator.hasNext();) {
+            Map.Entry entry = (Map.Entry) iterator.next();
+            String name = (String) entry.getKey();
+            Object value = entry.getValue();
+            if (value instanceof AbstractFederatedContext) {
+                AbstractFederatedContext nestedContext = (AbstractFederatedContext) value;
+
+                Name parsedName = wrappingContext.getNameParser().parse(name);
+                Name nameInNamespace = wrappingContext.getNameInNamespace(parsedName);
+
+                VirtualSubcontext virtualSubcontext = new VirtualSubcontext(nameInNamespace, innerContext);
+                addFederatedContext(nestedContext, virtualSubcontext);
             }
         }
     }
 
     public boolean isNestedSubcontext(Object value) {
-        if (value instanceof AbstractNestedFederatedContext) {
-            AbstractFederatedContext.AbstractNestedFederatedContext context = (AbstractNestedFederatedContext) value;
-            return this == context.getOuterContext();
+        if (value instanceof AbstractFederatedContext) {
+            AbstractFederatedContext context = (AbstractFederatedContext) value;
+            return getMasterContext() == context.getMasterContext();
         }
         return false;
     }
 
+    protected AbstractFederatedContext getMasterContext() {
+        return masterContext;
+    }
+
     public void bind(String name, Object obj) throws NamingException {
         if (!modifiable) {
             throw new OperationNotSupportedException("Context is read only");
@@ -173,150 +219,5 @@
             throw new OperationNotSupportedException("Context is read only");
         }
         super.destroySubcontext(name);
-    }
-
-    public abstract class AbstractNestedFederatedContext extends AbstractContext {
-        private final ContextFederation contextFederation;
-        private final boolean modifiable;
-
-        public AbstractNestedFederatedContext(String path) throws NamingException {
-            super(AbstractFederatedContext.this.getNameInNamespace(path));
-
-            AbstractFederatedContext outerContext = getOuterContext();
-            this.contextFederation = outerContext.contextFederation.createSubcontextFederation(path, this);
-            this.modifiable = outerContext.contextAccess.isModifiable(getParsedNameInNamespace());
-        }
-
-        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);
-                }
-            }
-        }
-
-        public void bind(String name, Object obj) throws NamingException {
-            if (!modifiable) {
-                throw new OperationNotSupportedException("Context is read only");
-            }
-            super.bind(name, obj);
-        }
-
-        public void bind(Name name, Object obj) throws NamingException {
-            if (!modifiable) {
-                throw new OperationNotSupportedException("Context is read only");
-            }
-            super.bind(name, obj);
-        }
-
-        public void rebind(String name, Object obj) throws NamingException {
-            if (!modifiable) {
-                throw new OperationNotSupportedException("Context is read only");
-            }
-            super.rebind(name, obj);
-        }
-
-        public void rebind(Name name, Object obj) throws NamingException {
-            if (!modifiable) {
-                throw new OperationNotSupportedException("Context is read only");
-            }
-            super.rebind(name, obj);
-        }
-
-        public void rename(String oldName, String newName) throws NamingException {
-            if (!modifiable) {
-                throw new OperationNotSupportedException("Context is read only");
-            }
-            super.rename(oldName, newName);
-        }
-
-        public void rename(Name oldName, Name newName) throws NamingException {
-            if (!modifiable) {
-                throw new OperationNotSupportedException("Context is read only");
-            }
-            super.rename(oldName, newName);
-        }
-
-        public void unbind(String name) throws NamingException {
-            if (!modifiable) {
-                throw new OperationNotSupportedException("Context is read only");
-            }
-            super.unbind(name);
-        }
-
-        public void unbind(Name name) throws NamingException {
-            if (!modifiable) {
-                throw new OperationNotSupportedException("Context is read only");
-            }
-            super.unbind(name);
-        }
-
-        public Context createSubcontext(String name) throws NamingException {
-            if (!modifiable) {
-                throw new OperationNotSupportedException("Context is read only");
-            }
-            return super.createSubcontext(name);
-        }
-
-        public Context createSubcontext(Name name) throws NamingException {
-            if (!modifiable) {
-                throw new OperationNotSupportedException("Context is read only");
-            }
-            return super.createSubcontext(name);
-        }
-
-        public void destroySubcontext(String name) throws NamingException {
-            if (!modifiable) {
-                throw new OperationNotSupportedException("Context is read only");
-            }
-            super.destroySubcontext(name);
-        }
-
-        public void destroySubcontext(Name name) throws NamingException {
-            if (!modifiable) {
-                throw new OperationNotSupportedException("Context is read only");
-            }
-            super.destroySubcontext(name);
-        }
     }
 }

Modified: geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/ContextFederation.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/ContextFederation.java?rev=434447&r1=434446&r2=434447&view=diff
==============================================================================
--- geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/ContextFederation.java (original)
+++ geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/ContextFederation.java Thu Aug 24 10:53:59 2006
@@ -22,6 +22,7 @@
 import javax.naming.Name;
 import javax.naming.NamingException;
 import javax.naming.NamingEnumeration;
+import javax.naming.Binding;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
@@ -70,10 +71,47 @@
             NamingEnumeration namingEnumeration = context.listBindings("");
 
             // add to bindings
-            Map map = ContextUtil.listBindingsToMap(namingEnumeration);
-            bindings.putAll(map);
+            while (namingEnumeration.hasMoreElements()) {
+                Binding binding = (Binding) namingEnumeration.nextElement();
+                String name = binding.getName();
+
+                // don't overwrite existing bindings
+                if (!bindings.containsKey(name)) {
+                    bindings.put(name, binding.getObject());
+                }
+            }
         }
         return bindings;
+    }
+
+    protected boolean addBinding(String name, Object value, boolean rebind) throws NamingException {
+        for (Iterator iterator = getFederatedContexts().iterator(); iterator.hasNext();) {
+            Context context = (Context) iterator.next();
+
+            try {
+                if (rebind) {
+                    context.rebind(name, value);
+                } else {
+                    context.bind(name, value);
+                }
+                return true;
+            } catch (NamingException ignored) {
+            }
+        }
+        return false;
+    }
+
+    protected boolean removeBinding(String name) throws NamingException {
+        for (Iterator iterator = getFederatedContexts().iterator(); iterator.hasNext();) {
+            Context context = (Context) iterator.next();
+
+            try {
+                context.unbind(name);
+                return true;
+            } catch (NamingException ignored) {
+            }
+        }
+        return false;
     }
 
     public Object lookup(Name name) {

Modified: geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/ImmutableContext.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/ImmutableContext.java?rev=434447&r1=434446&r2=434447&view=diff
==============================================================================
--- geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/ImmutableContext.java (original)
+++ geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/ImmutableContext.java Thu Aug 24 10:53:59 2006
@@ -89,7 +89,7 @@
         throw new OperationNotSupportedException("Context is immutable");
     }
 
-    protected final void addBinding(String name, Object value, boolean rebind) throws NamingException {
+    protected final boolean addBinding(String name, Object value, boolean rebind) throws NamingException {
         throw new OperationNotSupportedException("Context is immutable");
     }
 
@@ -97,7 +97,7 @@
         throw new OperationNotSupportedException("Context is immutable");
     }
 
-    protected final void removeBinding(String name) throws NamingException {
+    protected final boolean removeBinding(String name) throws NamingException {
         throw new OperationNotSupportedException("Context is immutable");
     }
 
@@ -194,7 +194,7 @@
             throw new OperationNotSupportedException("Context is immutable");
         }
 
-        protected final void addBinding(String name, Object value, boolean rebind) throws NamingException {
+        protected final boolean addBinding(String name, Object value, boolean rebind) throws NamingException {
             throw new OperationNotSupportedException("Context is immutable");
         }
 
@@ -202,7 +202,7 @@
             throw new OperationNotSupportedException("Context is immutable");
         }
 
-        protected final void removeBinding(String name) throws NamingException {
+        protected final boolean removeBinding(String name) throws NamingException {
             throw new OperationNotSupportedException("Context is immutable");
         }
 

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=434447&r1=434446&r2=434447&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 Thu Aug 24 10:53:59 2006
@@ -22,7 +22,6 @@
 import org.apache.xbean.naming.reference.CachingReference;
 
 import javax.naming.Context;
-import javax.naming.NameNotFoundException;
 import javax.naming.NamingException;
 import java.util.Collections;
 import java.util.HashMap;
@@ -72,8 +71,13 @@
         this.indexRef = new AtomicReference(Collections.unmodifiableMap(buildIndex("", localBindings)));
     }
 
-    protected void addBinding(String name, Object value, boolean rebind) throws NamingException {
+    protected boolean addBinding(String name, Object value, boolean rebind) throws NamingException {
+        if (super.addBinding(name, value, rebind)) {
+            return true;
+        }
+
         addBinding(bindingsRef, name, value);
+        return true;
     }
 
     protected void addBinding(AtomicReference bindingsRef, String name, Object value) throws NamingException {
@@ -81,21 +85,6 @@
         try {
             Map bindings = (Map) bindingsRef.get();
 
-            if (value instanceof Context && !isNestedSubcontext(value)) {
-                Context federatedContext = (Context) value;
-
-                // if we already have a context bound at the specified value
-                if (bindings.containsKey(name)) {
-                    NestedWritableContext nestedContext = (NestedWritableContext) bindings.get(name);
-                    nestedContext.addFederatedContext(federatedContext);
-                    return;
-                }
-
-                NestedWritableContext nestedContext = (NestedWritableContext) createNestedSubcontext(name, Collections.EMPTY_MAP);
-                nestedContext.addFederatedContext(federatedContext);
-                value = nestedContext;
-            }
-
             if (cacheReferences) {
                 value = CachingReference.wrapReference(getNameInNamespace(name), value);
             }
@@ -117,23 +106,27 @@
         newIndex.put(name, value);
         if (value instanceof NestedWritableContext) {
             NestedWritableContext nestedcontext = (NestedWritableContext) value;
-            Map newIndexValues = buildIndex(name, nestedcontext.getBindings());
+            Map newIndexValues = buildIndex(name, (Map) nestedcontext.bindingsRef.get());
             newIndex.putAll(newIndexValues);
         }
         return newIndex;
     }
 
-    protected void removeBinding(String name) throws NamingException {
+    protected boolean removeBinding(String name) throws NamingException {
+        if (super.removeBinding(name)) {
+            return true;
+        }
         removeBinding(bindingsRef, name);
+        return true;
     }
 
-    private void removeBinding(AtomicReference bindingsRef, String name) {
+    private boolean removeBinding(AtomicReference bindingsRef, String name) {
         writeLock.lock();
         try {
             Map bindings = (Map) bindingsRef.get();
             if (!bindings.containsKey(name)) {
                 // remove is idempotent meaning remove succeededs even if there was no value bound
-                return;
+                return false;
             }
 
             Map newBindings = new HashMap(bindings);
@@ -142,6 +135,7 @@
 
             Map newIndex = removeFromIndex(name);
             indexRef.set(newIndex);
+            return true;
         } finally {
             writeLock.unlock();
         }
@@ -153,7 +147,7 @@
         Object oldValue = newIndex.remove(name);
         if (oldValue instanceof NestedWritableContext) {
             NestedWritableContext nestedcontext = (NestedWritableContext) oldValue;
-            Map removedIndexValues = buildIndex(name, nestedcontext.getBindings());
+            Map removedIndexValues = buildIndex(name, (Map) nestedcontext.bindingsRef.get());
             for (Iterator iterator = removedIndexValues.keySet().iterator(); iterator.hasNext();) {
                 String key = (String) iterator.next();
                 newIndex.remove(key);
@@ -179,7 +173,7 @@
             Object value = entry.getValue();
             if (value instanceof NestedWritableContext) {
                 NestedWritableContext nestedContext = (NestedWritableContext)value;
-                absoluteIndex.putAll(buildIndex(nestedContext.pathWithSlash, nestedContext.getBindings()));
+                absoluteIndex.putAll(buildIndex(nestedContext.pathWithSlash, (Map) nestedContext.bindingsRef.get()));
             }
             absoluteIndex.put(path + name, value);
         }
@@ -191,7 +185,7 @@
         return index.get(name);
     }
 
-    protected Map getBindings() {
+    protected Map getWrapperBindings() throws NamingException {
         Map bindings = (Map) bindingsRef.get();
         return bindings;
     }
@@ -199,12 +193,12 @@
     /**
      * Nested context which shares the absolute index map in MapContext.
      */
-    public class NestedWritableContext extends AbstractNestedFederatedContext {
+    public class NestedWritableContext extends AbstractFederatedContext {
         private final AtomicReference bindingsRef;
         private final String pathWithSlash;
 
         public NestedWritableContext(String path, Map bindings) throws NamingException {
-            super(WritableContext.this.getNameInNamespace(path));
+            super(WritableContext.this, path);
 
             if (!path.endsWith("/")) path += "/";
             this.pathWithSlash = path;
@@ -221,17 +215,25 @@
             return WritableContext.this.getDeepBinding(absoluteName);
         }
 
-        protected Map getBindings() {
+        protected Map getWrapperBindings() throws NamingException {
             Map bindings = (Map) bindingsRef.get();
             return bindings;
         }
 
-        protected void addBinding(String name, Object value, boolean rebind) throws NamingException {
+        protected boolean addBinding(String name, Object value, boolean rebind) throws NamingException {
+            if (super.addBinding(name, value, rebind)) {
+                return true;
+            }
+
             WritableContext.this.addBinding(bindingsRef, name, value);
+            return true;
         }
 
-        protected void removeBinding(String name) throws NameNotFoundException {
-            WritableContext.this.removeBinding(bindingsRef, name);
+        protected boolean removeBinding(String name) throws NamingException {
+            if (WritableContext.this.removeBinding(bindingsRef, name)) {
+                return true;
+            }
+            return super.removeBinding(name);
         }
     }
 }

Modified: geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/FederationTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/FederationTest.java?rev=434447&r1=434446&r2=434447&view=diff
==============================================================================
--- geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/FederationTest.java (original)
+++ geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/FederationTest.java Thu Aug 24 10:53:59 2006
@@ -28,9 +28,11 @@
  */
 public class FederationTest extends AbstractContextTest {
     private Context rootContext;
-    private Map env2Bindings;
-    private MutableContext actualEnv2Context;
+    private MutableContext unmodifibleContext;
+    private Context writableContext;
     private Map rootBindings;
+    private Map unmodifibleBindings;
+    private Map writableBindings;
 
     private final class MutableContext extends WritableContext {
         public MutableContext(Map bindings) throws NamingException {
@@ -62,17 +64,29 @@
 
         assertEq(rootBindings, rootContext);
 
-        env2Bindings = new HashMap();
-        env2Bindings.put("string", "blah");
-        env2Bindings.put("one", new Integer(1));
-        env2Bindings.put("two", new Integer(2));
-        env2Bindings.put("three", new Integer(3));
+        unmodifibleBindings = new HashMap();
+        unmodifibleBindings.put("string", "blah");
+        unmodifibleBindings.put("one", new Integer(1));
+        unmodifibleBindings.put("two", new Integer(2));
+        unmodifibleBindings.put("three", new Integer(3));
+
+        unmodifibleContext = new MutableContext(unmodifibleBindings);
+        assertEq(unmodifibleBindings, unmodifibleContext);
+
+        rootContext.bind("java:comp/unmodifible", unmodifibleContext);
+        putAllBindings(rootBindings, "java:comp/unmodifible", unmodifibleBindings);
+
+        writableBindings = new HashMap();
+        writableBindings.put("string", "blah");
+        writableBindings.put("one", new Integer(1));
+        writableBindings.put("two", new Integer(2));
+        writableBindings.put("three", new Integer(3));
 
-        actualEnv2Context = new MutableContext(env2Bindings);
-        assertEq(env2Bindings, actualEnv2Context);
+        writableContext = new WritableContext("", writableBindings);
+        assertEq(writableBindings, writableContext);
 
-        rootContext.bind("java:comp/env2", actualEnv2Context);
-        putAllBindings(rootBindings, "java:comp/env2", env2Bindings);
+        rootContext.bind("java:comp/writable", writableContext);
+        putAllBindings(rootBindings, "java:comp/writable", writableBindings);
     }
 
     public void testBasic() throws Exception {
@@ -81,66 +95,117 @@
 
     public void testMutability() throws Exception {
         assertModifiable(rootContext);
-        assertUnmodifiable(actualEnv2Context);
-        assertModifiable(lookupSubcontext(rootContext, "java:comp/env2"));
+        assertUnmodifiable(unmodifibleContext);
+        assertModifiable(writableContext);
+        assertModifiable(lookupSubcontext(rootContext, "java:comp/unmodifible"));
+        assertModifiable(lookupSubcontext(rootContext, "java:comp/writable"));
     }
 
-    public void testBindOverFederated() throws Exception {
-        // update the verification map
-        rootBindings.put("java:comp/env2/TEST", "TEST_VALUE");
+    public void testBindOverUnmodifiable() throws Exception {
+        // bind into root context OVER the unmodifible context
+        rootContext.bind("java:comp/unmodifible/TEST", "TEST_VALUE");
 
-        // bind into root context OVER the env2 context
-        rootContext.bind("java:comp/env2/TEST", "TEST_VALUE");
+        // visible from root context
+        rootBindings.put("java:comp/unmodifible/TEST", "TEST_VALUE");
+        assertEq(rootBindings, rootContext);
+
+        // not-visible from unmodifibleContext
+        assertEq(unmodifibleBindings, unmodifibleContext);
+    }
+
+    public void testBindDirectIntoUnmodifiable() throws Exception {
+        // bind directly into the unmodifible context
+        unmodifibleContext.addDeepBinding(parse("DIRECT"), "DIRECT_VALUE", false, true);
 
         // visible from root context
+        rootBindings.put("java:comp/unmodifible/DIRECT", "DIRECT_VALUE");
         assertEq(rootBindings, rootContext);
 
-        // not-visible from actualEnv2Context
-        assertEq(env2Bindings, actualEnv2Context);
+        // visible from unmodifibleContext
+        unmodifibleBindings.put("DIRECT", "DIRECT_VALUE");
+        assertEq(unmodifibleBindings, unmodifibleContext);
     }
 
-    public void testBindDirectIntoFederated() throws Exception {
-        // update the verification maps
-        rootBindings.put("java:comp/env2/DIRECT", "DIRECT_VALUE");
-        env2Bindings.put("DIRECT", "DIRECT_VALUE");
+    public void testUnbindOverUnmodifiable() throws Exception {
+        // unbind value under unmodifible... no exception occurs since unbind is idempotent
+        rootContext.unbind("java:comp/unmodifible/three");
+
+        // no change in root context
+        assertEq(rootBindings, rootContext);
+
+        // no change in unmodifibleContext
+        assertEq(unmodifibleBindings, unmodifibleContext);
 
-        // bind directly into the actual env2 context
-        actualEnv2Context.addDeepBinding(parse("DIRECT"), "DIRECT_VALUE", false, true);
+        // unbind value deep unmodifible... no exception occurs since unbind is idempotent
+        rootContext.unbind("java:comp/unmodifible/three");
+    }
+
+    public void testUnbindDirectIntoUnmodifiable() throws Exception {
+        // unbind directly from the unmodifible context
+        unmodifibleContext.removeDeepBinding(parse("three"), true);
 
         // visible from root context
+        rootBindings.remove("java:comp/unmodifible/three");
         assertEq(rootBindings, rootContext);
 
-        // visible from actualEnv2Context
-        assertEq(env2Bindings, actualEnv2Context);
+        // visible from unmodifibleContext
+        unmodifibleBindings.remove("three");
+        assertEq(unmodifibleBindings, unmodifibleContext);
     }
 
-    public void testUnbindOverFederated() throws Exception {
-        // unbind value under env2... no exception occurs since unbind is idempotent
-        rootContext.unbind("java:comp/env2/three");
+    public void testBindIntoWritable() throws Exception {
+        // bind into root context OVER the writable context
+        rootContext.bind("java:comp/writable/TEST", "TEST_VALUE");
 
-        // no change in root context
+        // visible from root context
+        rootBindings.put("java:comp/writable/TEST", "TEST_VALUE");
         assertEq(rootBindings, rootContext);
 
-        // no change in actualEnv2Context
-        assertEq(env2Bindings, actualEnv2Context);
+        // visible from writableContext
+        writableBindings.put("TEST", "TEST_VALUE");
+        assertEq(writableBindings, writableContext);
+    }
+
+    public void testBindDirectIntoWritable() throws Exception {
+        // bind directly into the writable context
+        writableContext.bind("DIRECT", "DIRECT_VALUE");
+
+        // visible from root context
+        rootBindings.put("java:comp/writable/DIRECT", "DIRECT_VALUE");
+        assertEq(rootBindings, rootContext);
 
-        // unbind value deep env2... no exception occurs since unbind is idempotent
-        rootContext.unbind("java:comp/env2/three");
+        // visible from writableContext
+        writableBindings.put("DIRECT", "DIRECT_VALUE");
+        assertEq(writableBindings, writableContext);
     }
 
-    public void testUnbindDirectIntoFederated() throws Exception {
-        // update the verification maps
-        rootBindings.remove("java:comp/env2/three");
-        env2Bindings.remove("three");
+    public void testUnbindOverWritable() throws Exception {
+        // unbind value under writable... no exception occurs since unbind is idempotent
+        rootContext.unbind("java:comp/writable/three");
+
+        // visible from root context
+        rootBindings.remove("java:comp/writable/three");
+        assertEq(rootBindings, rootContext);
+
+        // visible from writableContext
+        writableBindings.remove("three");
+        assertEq(writableBindings, writableContext);
+
+        // unbind value deep writable... no exception occurs since unbind is idempotent
+        rootContext.unbind("java:comp/writable/three");
+    }
 
-        // bind directly into the actual env2 context
-        actualEnv2Context.removeDeepBinding(parse("three"), true);
+    public void testUnbindDirectIntoWritable() throws Exception {
+        // unbind directly from the writable context
+        writableContext.unbind("three");
 
         // visible from root context
+        rootBindings.remove("java:comp/writable/three");
         assertEq(rootBindings, rootContext);
 
-        // visible from actualEnv2Context
-        assertEq(env2Bindings, actualEnv2Context);
+        // visible from writableContext
+        writableBindings.remove("three");
+        assertEq(writableBindings, writableContext);
     }