geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject svn commit: r432428 - in /geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context: AbstractContext.java AbstractReadOnlyContext.java WritableContext.java
Date Thu, 17 Aug 2006 23:20:59 GMT
Author: dain
Date: Thu Aug 17 16:20:59 2006
New Revision: 432428

URL: http://svn.apache.org/viewvc?rev=432428&view=rev
Log:
Made AbstractReadOnlyContext a subclass of AbstractContext
Rearranged the AbstractContext methods into logical groupings
Merged common code between AbstractContext and AbstractReadOnlyContext

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/AbstractReadOnlyContext.java
    geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/WritableContext.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=432428&r1=432427&r2=432428&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 17 16:20:59 2006
@@ -16,8 +16,6 @@
  */
 package org.apache.xbean.naming.context;
 
-import org.apache.geronimo.naming.enc.EnterpriseNamingContextNameParser;
-
 import javax.naming.CompositeName;
 import javax.naming.Context;
 import javax.naming.InvalidNameException;
@@ -27,118 +25,143 @@
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
 import javax.naming.NotContextException;
+import javax.naming.LinkRef;
+import java.io.Serializable;
+import java.util.Collections;
 import java.util.Hashtable;
 import java.util.Map;
 
-public abstract class AbstractContext implements Context {
-    protected Hashtable env;
-    protected AbstractContext parent;
-    protected Name contextAtomicName;
-
-    public AbstractContext() {
-        env = new Hashtable();
-    }
+public abstract class AbstractContext implements Context, ContextFactory, Serializable {
+    private static final long serialVersionUID = 6481918425692261483L;
+    private final String nameInNamespace;
 
-    protected AbstractContext(AbstractContext parent, Hashtable environment, Name contextAtomicName)
{
-        this.env = environment;
-        this.parent = parent;
-        this.contextAtomicName = contextAtomicName;
+    protected AbstractContext(String nameInNamespace) {
+        this.nameInNamespace = nameInNamespace;
     }
 
-    public AbstractContext(Hashtable env) {
-        if (env == null) {
-            this.env = new Hashtable();
-        } else {
-            this.env = new Hashtable(env);
-        }
-    }
 
     public void close() throws NamingException {
         //Ignore. Explicitly do not close the context
     }
 
-    protected abstract AbstractContext newSubcontext(Name name);
     protected abstract void removeBindings(Name name) throws NamingException;
     protected abstract Object internalLookup(Name name, boolean resolveLinks) throws NamingException;
     protected abstract void addBinding(Name name, Object obj, boolean rebind) throws NamingException;
     protected abstract Map getBindingsCopy();
 
-    public String getNameInNamespace() throws NamingException {
-        AbstractContext parentContext = parent;
-        if (parentContext == null) {
-            return "ROOT CONTEXT";
-        }
-        Name name = EnterpriseNamingContextNameParser.INSTANCE.parse("");
-        name.add(contextAtomicName.toString());
-
-        // Get parent's names
-        while (parentContext != null && parentContext.contextAtomicName != null)
{
-            name.add(0, parentContext.contextAtomicName.toString());
-            parentContext = parentContext.parent;
+    /**
+     * Gets the name of this context withing the global namespace.  This method may return
null
+     * if the location of the node in the global namespace is not known
+     * @return the name of this context within the global namespace or null if unknown.
+     */
+    public String getNameInNamespace() {
+        return nameInNamespace;
+    }
+
+    /**
+     * Gets the name of a path withing the global namespace context.
+     */
+    protected String getNameInNamespace(String path) {
+        String nameInNamespace = getNameInNamespace();
+        if (nameInNamespace == null || nameInNamespace.length() == 0) {
+            return path;
+        } else {
+            return nameInNamespace + "/" + path;
         }
-        return name.toString();
     }
 
-    public void destroySubcontext(String name) throws NamingException {
-        if (name == null) throw new NullPointerException("name is null");
-        destroySubcontext(new CompositeName(name));
+    //
+    // Environment
+    //
+
+    /**
+     * Always returns a new (empty) Hashtable.
+     * @return a new (empty) Hashtable
+     */
+    public Hashtable getEnvironment() {
+        return new Hashtable();
     }
 
-    public void unbind(String name) throws NamingException {
-        if (name == null) throw new NullPointerException("name is null");
-        unbind(new CompositeName(name));
+    public Object addToEnvironment(String propName, Object propVal) throws NamingException
{
+        if (propName == null) throw new NullPointerException("propName is null");
+        if (propVal == null) throw new NullPointerException("propVal is null");
+
+        Map env = getEnvironment();
+        return env.put(propName, propVal);
     }
 
-    public Hashtable getEnvironment() throws NamingException {
-        return env;
+    public Object removeFromEnvironment(String propName) throws NamingException {
+        if (propName == null) throw new NullPointerException("propName is null");
+
+        Map env = getEnvironment();
+        return env.remove(propName);
     }
 
-    public void destroySubcontext(Name name) throws NamingException {
-        if (name == null) throw new NullPointerException("name is null");
-        if (name.isEmpty()) {
-            throw new InvalidNameException("Cannot destroy subcontext with empty name");
-        }
-        unbind(name);
+    //
+    // Name handling
+    //
+
+    /**
+     * A parser that can turn Strings into javax.naming.Name objects.
+     * @return ContextUtil.NAME_PARSER
+     */
+    protected NameParser getNameParser() {
+        return ContextUtil.NAME_PARSER;
     }
 
-    public void unbind(Name name) throws NamingException {
-        if (name == null) throw new NullPointerException("name is null");
-        if (name.isEmpty()) {
-            throw new InvalidNameException("Cannot unbind empty name");
-        }
-        removeBindings(name);
+    public NameParser getNameParser(Name name) {
+        return getNameParser();
     }
 
-    public Object lookup(String name) throws NamingException {
-        if (name == null) throw new NullPointerException("name is null");
-        return lookup(new CompositeName(name));
+    public NameParser getNameParser(String name) {
+        return getNameParser();
     }
 
-    public Object lookupLink(String name) throws NamingException {
+    public Name composeName(Name name, Name prefix) throws NamingException {
         if (name == null) throw new NullPointerException("name is null");
-        return lookupLink(new CompositeName(name));
-    }
+        if (prefix == null) throw new NullPointerException("prefix is null");
 
-    public Object removeFromEnvironment(String propName) throws NamingException {
-        if (propName == null) throw new NullPointerException("propName is null");
-        Object obj = env.get(propName);
-        env.remove(propName);
-        return obj;
+        Name result = (Name) prefix.clone();
+        result.addAll(name);
+        return result;
     }
 
-    public void bind(String name, Object obj) throws NamingException {
+    public String composeName(String name, String prefix) throws NamingException {
         if (name == null) throw new NullPointerException("name is null");
-        bind(new CompositeName(name), obj);
+        if (prefix == null) throw new NullPointerException("prefix is null");
+
+        CompositeName result = new CompositeName(prefix);
+        result.addAll(new CompositeName(name));
+        return result.toString();
     }
 
-    public void rebind(String name, Object obj) throws NamingException {
+    //
+    // Lookup
+    //
+
+    public Object lookup(String name) throws NamingException {
         if (name == null) throw new NullPointerException("name is null");
-        rebind(new CompositeName(name), obj);
+        return lookup(new CompositeName(name));
     }
 
     public Object lookup(Name name) throws NamingException {
         if (name == null) throw new NullPointerException("name is null");
-        return internalLookup(name, true);
+
+        Object value = internalLookup(name, true);
+
+
+        // if we got a link back we need to resolve it
+        if (value instanceof LinkRef) {
+            LinkRef linkRef = (LinkRef) value;
+            value = lookup(linkRef.getLinkName());
+        }
+
+        return value;
+    }
+
+    public Object lookupLink(String name) throws NamingException {
+        if (name == null) throw new NullPointerException("name is null");
+        return lookupLink(new CompositeName(name));
     }
 
     public Object lookupLink(Name name) throws NamingException {
@@ -146,22 +169,18 @@
         return internalLookup(name, false);
     }
 
-    /**
-     * Binds a name to an object. The logic of this method is as follows If the
-     * name consists of only one element, then it is bound directly to the
-     * underlying HashMap. If the name consists of many parts, then the object
-     * is bound to the target context with the terminal atomic component of the
-     * name. If any of the atomic names other than the terminal one is bound to
-     * an object that is not a Context a NameAlreadyBoundException is thrown. If
-     * the terminal atomic name is already bound to any object then a
-     * NameAlreadyBoundException is thrown. If any of the subContexts included
-     * in the Name do not exist then a NamingException is thrown.
-     *
-     * @param name the name to bind; may not be empty
-     * @param obj  the object to bind; possibly null
-     * @throws NameAlreadyBoundException if name is already bound
-     * @throws NamingException           if a naming exception is encountered
-     */
+    //
+    // Bind, rebind, rename and unbind
+    //
+
+    public void bind(String name, Object obj) throws NamingException {
+        if (name == null) throw new NullPointerException("name is null");
+        if (name.length() == 0) {
+            throw new NameAlreadyBoundException("Cannot bind to an empty name (this context)");
+        }
+        bind(new CompositeName(name), obj);
+    }
+
     public void bind(Name name, Object obj) throws NamingException {
         if (name == null) throw new NullPointerException("name is null");
         if (name.isEmpty()) {
@@ -170,6 +189,11 @@
         addBinding(name, obj, false);
     }
 
+    public void rebind(String name, Object obj) throws NamingException {
+        if (name == null) throw new NullPointerException("name is null");
+        rebind(new CompositeName(name), obj);
+    }
+
     public void rebind(Name name, Object obj) throws NamingException {
         if (name == null) throw new NullPointerException("name is null");
         if (name.isEmpty()) {
@@ -178,29 +202,13 @@
         addBinding(name, obj, true);
     }
 
-    public synchronized void rename(String oldName, String newName) throws NamingException
{
+    public void rename(String oldName, String newName) throws NamingException {
         if (oldName == null) throw new NullPointerException("oldName is null");
         if (newName == null) throw new NullPointerException("newName is null");
         rename(new CompositeName(oldName), new CompositeName(newName));
     }
 
-    public Context createSubcontext(String name) throws NamingException {
-        if (name == null) throw new NullPointerException("name is null");
-        return createSubcontext(new CompositeName(name));
-    }
-
-    public Context createSubcontext(Name name) throws NamingException {
-        if (name == null) throw new NullPointerException("name is null");
-        if (name.isEmpty()) {
-            throw new InvalidNameException("Cannot create a subcontext if the name is empty");
-        }
-        AbstractContext abstractContext = newSubcontext(name);
-        addBinding(name, abstractContext, false);
-        return abstractContext;
-    }
-
-
-    public synchronized void rename(Name oldName, Name newName) throws NamingException {
+    public void rename(Name oldName, Name newName) throws NamingException {
         if (oldName == null || newName == null) {
             throw new NullPointerException("name is null");
         } else if (oldName.isEmpty() || newName.isEmpty()) {
@@ -212,26 +220,28 @@
         this.unbind(oldName);
     }
 
-    public NameParser getNameParser(String name) throws NamingException {
+    public void unbind(String name) throws NamingException {
         if (name == null) throw new NullPointerException("name is null");
-        return getNameParser(new CompositeName(name));
+        unbind(new CompositeName(name));
     }
 
-    public NameParser getNameParser(Name name) throws NamingException {
+    public void unbind(Name name) throws NamingException {
         if (name == null) throw new NullPointerException("name is null");
-        return EnterpriseNamingContextNameParser.INSTANCE;
+        if (name.isEmpty()) {
+            throw new InvalidNameException("Cannot unbind empty name");
+        }
+        removeBindings(name);
     }
 
+    //
+    // List
+    //
+
     public NamingEnumeration list(String name) throws NamingException {
         if (name == null) throw new NullPointerException("name is null");
         return list(new CompositeName(name));
     }
 
-    public NamingEnumeration listBindings(String name) throws NamingException {
-        if (name == null) throw new NullPointerException("name is null");
-        return listBindings(new CompositeName(name));
-    }
-
     public NamingEnumeration list(Name name) throws NamingException {
         if (name == null) throw new NullPointerException("name is null");
         if (name.isEmpty()) {
@@ -244,6 +254,11 @@
         throw new NotContextException("The name " + name + " cannot be listed");
     }
 
+    public NamingEnumeration listBindings(String name) throws NamingException {
+        if (name == null) throw new NullPointerException("name is null");
+        return listBindings(new CompositeName(name));
+    }
+
     public NamingEnumeration listBindings(Name name) throws NamingException {
         if (name == null) throw new NullPointerException("name is null");
         if (name.isEmpty()) {
@@ -256,33 +271,35 @@
         throw new NotContextException("The name " + name + " cannot be listed");
     }
 
-    public Object addToEnvironment(String propName, Object propVal) throws NamingException
{
-        if (propName == null || propVal == null) {
-            throw new NamingException("The parameters for this method cannot be null");
+    //
+    // Subcontexts
+    //
+
+    public Context createSubcontext(String name) throws NamingException {
+        if (name == null) throw new NullPointerException("name is null");
+        return createSubcontext(new CompositeName(name));
+    }
+
+    public Context createSubcontext(Name name) throws NamingException {
+        if (name == null) throw new NullPointerException("name is null");
+        if (name.isEmpty()) {
+            throw new InvalidNameException("Cannot create a subcontext if the name is empty");
         }
-        Object obj = env.get(propName);
-        env.put(propName, propVal);
-        return obj;
+        Context abstractContext = createContext(name.toString(), Collections.EMPTY_MAP);
+        addBinding(name, abstractContext, false);
+        return abstractContext;
     }
 
-    public String composeName(String name, String prefix) throws NamingException {
+    public void destroySubcontext(String name) throws NamingException {
         if (name == null) throw new NullPointerException("name is null");
-        if (prefix == null) throw new NullPointerException("prefix is null");
-        return composeName(new CompositeName(name), new CompositeName(prefix)) .toString();
+        destroySubcontext(new CompositeName(name));
     }
 
-    public Name composeName(Name name, Name prefix) throws NamingException {
+    public void destroySubcontext(Name name) throws NamingException {
         if (name == null) throw new NullPointerException("name is null");
-        if (prefix == null) throw new NullPointerException("prefix is null");
-        if (name == null) {
-            throw new NullPointerException("name is null");
-        }
-        if (prefix == null) {
-            throw new NullPointerException("prefix is null");
+        if (name.isEmpty()) {
+            throw new InvalidNameException("Cannot destroy subcontext with empty name");
         }
-
-        Name result = (Name) prefix.clone();
-        result.addAll(name);
-        return result;
+        unbind(name);
     }
 }

Modified: geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractReadOnlyContext.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractReadOnlyContext.java?rev=432428&r1=432427&r2=432428&view=diff
==============================================================================
--- geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractReadOnlyContext.java
(original)
+++ geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractReadOnlyContext.java
Thu Aug 17 16:20:59 2006
@@ -17,70 +17,28 @@
 
 package org.apache.xbean.naming.context;
 
-import javax.naming.CompositeName;
 import javax.naming.Context;
 import javax.naming.InitialContext;
+import javax.naming.InvalidNameException;
+import javax.naming.LinkRef;
 import javax.naming.Name;
 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 javax.naming.InvalidNameException;
-import javax.naming.LinkRef;
 import java.io.Serializable;
-import java.util.Hashtable;
-import java.util.Map;
 import java.util.Collections;
+import java.util.Map;
 
 /**
  * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
  */
-public abstract class AbstractReadOnlyContext implements Context, ContextFactory, Serializable
{
+public abstract class AbstractReadOnlyContext extends AbstractContext implements Context,
ContextFactory, Serializable {
     private static final long serialVersionUID = 3808693663629444493L;
-    private final String nameInNamespace;
 
     protected AbstractReadOnlyContext(String nameInNamespace) {
-        this.nameInNamespace = nameInNamespace;
-    }
-
-    /**
-     * Gets the name of this context withing the global namespace.  This method may return
null
-     * if the location of the node in the global namespace is not known
-     * @return the name of this context within the global namespace or null if unknown.
-     */
-    public String getNameInNamespace() {
-        return nameInNamespace;
-    }
-
-    /**
-     * Gets the name of a path withing the global namespace context.
-     */
-    protected String getNameInNamespace(String path) {
-        String nameInNamespace = getNameInNamespace();
-        if (nameInNamespace == null || nameInNamespace.length() == 0) {
-            return path;
-        } else {
-            return nameInNamespace + "/" + path;
-        }
-    }
-
-
-    /**
-     * Always returns a new (empty) Hashtable.
-     * @return a new (empty) Hashtable
-     */
-    public Hashtable getEnvironment() {
-        return new Hashtable();
-    }
-
-    /**
-     * A parser that can turn Strings into javax.naming.Name objects.
-     * @return ContextUtil.NAME_PARSER
-     */
-    protected NameParser getNameParser() {
-        return ContextUtil.NAME_PARSER;
+        super(nameInNamespace);
     }
 
     //
@@ -465,46 +423,8 @@
     }
 
     //
-    // Name manipulation
-    //
-
-    public final NameParser getNameParser(Name name) {
-        return getNameParser();
-    }
-
-    public final NameParser getNameParser(String name) {
-        return getNameParser();
-    }
-
-    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 Object addToEnvironment(String propName, Object propVal) throws NamingException
{
-        throw new OperationNotSupportedException("Context is read only");
-    }
-
-    public final Object removeFromEnvironment(String propName) throws NamingException {
-        throw new OperationNotSupportedException("Context is read only");
-    }
 
     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=432428&r1=432427&r2=432428&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 17 16:20:59 2006
@@ -22,7 +22,6 @@
 import javax.naming.NameAlreadyBoundException;
 import javax.naming.LinkRef;
 import java.util.Map;
-import java.util.Hashtable;
 import java.util.HashMap;
 
 /**
@@ -32,26 +31,17 @@
     protected Map bindings;
 
     public WritableContext() {
+        super("");
         bindings = new HashMap();
     }
 
-    public WritableContext(AbstractContext parent, Hashtable environment, Name contextAtomicName)
{
-        super(parent, environment, contextAtomicName);
+    public WritableContext(String nameInNamespace) {
+        super(nameInNamespace);
         bindings = new HashMap();
     }
 
-    public WritableContext(Hashtable env) {
-        super(env);
-        bindings = new HashMap();
-    }
-
-    public WritableContext(WritableContext clone, Hashtable env) {
-        super(env);
-        bindings = clone.bindings;
-    }
-
-    protected AbstractContext newSubcontext(Name name) {
-        return new WritableContext(this, this.env, name);
+    public Context createContext(String path, Map bindings) {
+        return new WritableContext(getNameInNamespace(path));
     }
 
     protected void removeBindings(Name name) throws NamingException {



Mime
View raw message