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/21 20:11:52 UTC
svn commit: r433313 - in
/geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context:
AbstractContext.java AbstractUnmodifiableContext.java WritableContext.java
Author: dain
Date: Mon Aug 21 11:11:51 2006
New Revision: 433313
URL: http://svn.apache.org/viewvc?rev=433313&view=rev
Log:
Pulled up lookup binding method up to AbstractContext from AbstractUnmodifiableContext instead of the version in WritableContext. This version recursively calls lookup on sub contexts, instead of iterating. This allows for a heterogeneous tree.
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/AbstractUnmodifiableContext.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=433313&r1=433312&r2=433313&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 Mon Aug 21 11:11:51 2006
@@ -26,6 +26,8 @@
import javax.naming.NamingException;
import javax.naming.NotContextException;
import javax.naming.LinkRef;
+import javax.naming.NameNotFoundException;
+import javax.naming.InitialContext;
import java.io.Serializable;
import java.util.Collections;
import java.util.Hashtable;
@@ -44,6 +46,103 @@
//Ignore. Explicitly do not close the context
}
+ //
+ // Lookup Binding
+ //
+
+ /**
+ * Gets the object bound to the name. The name will not contain slashes.
+ * @param name the name
+ * @return the object bound to the name, or null if not found
+ */
+ protected Object getBinding(String name) throws NamingException {
+ Map bindings = getBindings();
+ return bindings.get(name);
+ }
+
+ /**
+ * Finds the specified entry. Normally there is no need to override this method; instead you should
+ * simply the getBindings(String) method.
+ *
+ * This method will follow links except for the final element which is always just returned without
+ * inspection. This means this method can be used to implement lookupLink.
+ *
+ * @param stringName the string version of the name; maybe null
+ * @param parsedName the parsed name; may be null
+ * @return the value bound to the name
+ * @throws NamingException if no value is bound to that name or if a problem occurs during the lookup
+ */
+ protected Object lookup(String stringName, Name parsedName) throws NamingException {
+ if (stringName == null && parsedName == null) {
+ throw new IllegalArgumentException("Both stringName and parsedName are null");
+ }
+ if (stringName == null) stringName = parsedName.toString();
+
+ // if the parsed name has no parts, they are asking for the current context
+ if (parsedName == null) parsedName = getNameParser().parse(stringName);
+ if (parsedName.isEmpty()) {
+ return this;
+ }
+
+ // we didn't find an entry, pop the first element off the parsed name and attempt to
+ // get a context from the bindings and delegate to that context
+ Object localValue;
+ String firstNameElement = parsedName.get(0);
+ if (firstNameElement.length() == 0) {
+ // the element is null... this is normally caused by looking up with a trailing '/' character
+ localValue = this;
+ } else {
+ localValue = getBinding(firstNameElement);
+ }
+
+ if (localValue != null) {
+
+ // if the name only had one part, we've looked up everything
+ if (parsedName.size() == 1) {
+ localValue = ContextUtil.resolve(stringName, localValue);
+ return localValue;
+ }
+
+ // if we have a link ref, follow it
+ if (localValue instanceof LinkRef) {
+ LinkRef linkRef = (LinkRef) localValue;
+ localValue = lookup(linkRef.getLinkName());
+ }
+
+ // we have more to lookup so we better have a context object
+ if (!(localValue instanceof Context)) {
+ throw new NameNotFoundException(stringName);
+ }
+
+ // delegate to the sub-context
+ return ((Context) localValue).lookup(parsedName.getSuffix(1));
+ }
+
+ // if we didn't find an entry, it may be an absolute name
+ if (stringName.indexOf(':') > 0) {
+ Context ctx = new InitialContext();
+ return ctx.lookup(parsedName);
+ }
+ throw new NameNotFoundException(stringName);
+ }
+
+ //
+ // List Bindings
+ //
+
+ /**
+ * Gets a map of the bindings for the current node (i.e., no names with slashes).
+ * This method must not return null.
+ *
+ * @return a Map from binding name to binding value
+ * @throws NamingException if a problem occurs while getting the bindigns
+ */
+ protected abstract Map getBindings() throws NamingException;
+
+ //
+ // Add Binding
+ //
+
protected void addDeepBinding(Name name, Object value, boolean rebind) throws NamingException {
if (name.size() == 1) {
addBinding(name.get(0), value, rebind);
@@ -74,9 +173,11 @@
protected abstract void addBinding(String name, Object value, boolean rebind) throws NamingException;
+ //
+ // Remove Binding
+ //
+
protected abstract void removeBindings(Name name) throws NamingException;
- protected abstract Object lookup(String stringName, Name parsedName) throws NamingException;
- protected abstract Map getBindings() throws NamingException;
//
// Environment
Modified: geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractUnmodifiableContext.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractUnmodifiableContext.java?rev=433313&r1=433312&r2=433313&view=diff
==============================================================================
--- geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractUnmodifiableContext.java (original)
+++ geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractUnmodifiableContext.java Mon Aug 21 11:11:51 2006
@@ -18,11 +18,8 @@
package org.apache.xbean.naming.context;
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.NamingException;
import javax.naming.NotContextException;
import javax.naming.OperationNotSupportedException;
@@ -62,16 +59,6 @@
}
/**
- * Gets the object bound to the name. The name will not contain slashes.
- * @param name the name
- * @return the object bound to the name, or null if not found
- */
- protected Object getBinding(String name) throws NamingException {
- Map bindings = getBindings();
- return bindings.get(name);
- }
-
- /**
* Finds the specified entry. Normally there is no need to override this method; instead you should
* simply implement the getDeepBindings(String) and getBindings(String) method.
*
@@ -95,53 +82,8 @@
return ContextUtil.resolve(stringName, directLookup);
}
- // if the parsed name has no parts, they are asking for the current context
- if (parsedName == null) parsedName = getNameParser().parse(stringName);
- if (parsedName.size() == 0) {
- return this;
- }
-
-
- // we didn't find an entry, pop the first element off the parsed name and attempt to
- // get a context from the bindings and delegate to that context
- Object localValue;
- String firstNameElement = parsedName.get(0);
- if (firstNameElement.length() == 0) {
- // the element is null... this is normally caused by looking up with a trailing '/' character
- localValue = this;
- } else {
- localValue = getBinding(firstNameElement);
- }
-
- if (localValue != null) {
-
- // if the name only had one part, we've looked up everything
- if (parsedName.size() == 1) {
- localValue = ContextUtil.resolve(stringName, localValue);
- return localValue;
- }
-
- // if we have a link ref, follow it
- if (localValue instanceof LinkRef) {
- LinkRef linkRef = (LinkRef) localValue;
- localValue = lookup(linkRef.getLinkName());
- }
-
- // we have more to lookup so we better have a context object
- if (!(localValue instanceof Context)) {
- throw new NameNotFoundException(stringName);
- }
-
- // delegate to the sub-context
- return ((Context) localValue).lookup(parsedName.getSuffix(1));
- }
-
- // if we didn't find an entry, it may be an absolute name
- if (stringName.indexOf(':') > 0) {
- Context ctx = new InitialContext();
- return ctx.lookup(parsedName);
- }
- throw new NameNotFoundException(stringName);
+ Object value = super.lookup(stringName, parsedName);
+ return value;
}
//
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=433313&r1=433312&r2=433313&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 Mon Aug 21 11:11:51 2006
@@ -28,7 +28,7 @@
* @version $Rev$ $Date$
*/
public class WritableContext extends AbstractContext {
- protected Map bindings;
+ protected final Map bindings;
public WritableContext() {
super("");
@@ -45,7 +45,10 @@
}
protected void removeBindings(Name name) throws NamingException {
- Map bindings = this.bindings;
+ Map bindings;
+ synchronized (this.bindings) {
+ bindings = this.bindings;
+ }
if (name.size() == 1) {
synchronized (bindings) {
bindings.remove(name.toString());
@@ -65,60 +68,15 @@
" An object that is not a context is already bound at element "
+ segment + "of name " + name);
} else {
- bindings = ((WritableContext) terminalContext).bindings;
+ WritableContext writableContext = ((WritableContext) terminalContext);
+ synchronized (writableContext.bindings) {
+ bindings = writableContext.bindings;
+ }
}
}
segment = name.get(lastIndex);
((Context) terminalContext).unbind(segment);
}
- }
-
- protected Object lookup(String stringName, Name parsedName) throws NamingException {
- if (parsedName == null) parsedName = getNameParser().parse(stringName);
-
- Object result = null;
- Map bindings = this.bindings;
- Object terminalContext = null;
- if (parsedName.isEmpty()) {
- return this;
- }
- int index = parsedName.get(0).indexOf(':');
- if (index != -1) {
- String temp = parsedName.get(0).substring(index + 1);
- parsedName.remove(0);
- parsedName.add(0, temp);
- }
- if (parsedName.size() == 1) {
- result = bindings.get(parsedName.toString());
- } else {
- String segment = null;
- int lastIndex = parsedName.size() - 1;
- for (int i = 0; i < lastIndex; i++) {
- segment = parsedName.get(i);
- terminalContext = bindings.get(segment);
- if (terminalContext == null) {
- throw new NamingException("The intermediate context "
- + segment + " does not exist");
- } else if (!(terminalContext instanceof Context)) {
- throw new NamingException(
- "One of the intermediate names i.e. "
- + segment
- + " refer to an object that is not a context");
- } else {
- bindings = ((WritableContext) terminalContext).bindings;
- }
- }
- segment = parsedName.get(lastIndex);
- result = ((Context) terminalContext).lookup(segment);
- }
-
- if (result instanceof LinkRef) {
- LinkRef ref = (LinkRef) result;
- result = lookup(ref.getLinkName());
- }
-
- return result;
-
}
protected void addBinding(String name, Object value, boolean rebind) throws NamingException {