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/17 19:13:29 UTC
svn commit: r432298 - in
/geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context:
AbstractContext.java GeronimoGlobalContext.java
Author: dain
Date: Thu Aug 17 10:13:29 2006
New Revision: 432298
URL: http://svn.apache.org/viewvc?rev=432298&view=rev
Log:
Extrated an abstract super class context which contains the basic context implementation but leaves the internal datastructures to a subclass.
Added:
geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractContext.java
- copied, changed from r432270, geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/GeronimoGlobalContext.java
geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/GeronimoGlobalContext.java
Copied: geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractContext.java (from r432270, geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/GeronimoGlobalContext.java)
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractContext.java?p2=geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractContext.java&p1=geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/GeronimoGlobalContext.java&r1=432270&r2=432298&rev=432298&view=diff
==============================================================================
--- geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/GeronimoGlobalContext.java (original)
+++ geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractContext.java Thu Aug 17 10:13:29 2006
@@ -22,7 +22,6 @@
import javax.naming.CompositeName;
import javax.naming.Context;
import javax.naming.InvalidNameException;
-import javax.naming.LinkRef;
import javax.naming.Name;
import javax.naming.NameAlreadyBoundException;
import javax.naming.NameClassPair;
@@ -30,53 +29,45 @@
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.NotContextException;
-import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
-public class GeronimoGlobalContext implements Context {
+public abstract class AbstractContext implements Context {
protected Hashtable env;
- protected Map bindings;
- protected GeronimoGlobalContext parent;
+ protected AbstractContext parent;
protected Name contextAtomicName;
- public GeronimoGlobalContext() {
- bindings = new HashMap();
+ public AbstractContext() {
env = new Hashtable();
}
- protected GeronimoGlobalContext(GeronimoGlobalContext parent, Hashtable environment, Name contextAtomicName) {
+ protected AbstractContext(AbstractContext parent, Hashtable environment, Name contextAtomicName) {
this.env = environment;
this.parent = parent;
- bindings = new HashMap();
this.contextAtomicName = contextAtomicName;
}
- public GeronimoGlobalContext(Hashtable env) {
+ public AbstractContext(Hashtable env) {
if (env == null) {
this.env = new Hashtable();
} else {
this.env = new Hashtable(env);
}
- bindings = new HashMap();
- }
-
- protected GeronimoGlobalContext(GeronimoGlobalContext clone, Hashtable env) {
- if (env == null) {
- this.env = new Hashtable();
- } else {
- this.env = new Hashtable(env);
- }
- bindings = clone.bindings;
}
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 {
- GeronimoGlobalContext parentContext = parent;
+ AbstractContext parentContext = parent;
if (parentContext == null) {
return "ROOT CONTEXT";
}
@@ -114,32 +105,7 @@
if (name.isEmpty()) {
throw new InvalidNameException("Cannot unbind empty name");
}
- Map bindings = this.bindings;
- if (name.size() == 1) {
- synchronized (bindings) {
- bindings.remove(name.toString());
- }
- } else {
- String segment = null;
- int lastIndex = name.size() - 1;
- Object terminalContext = null;
- for (int i = 0; i < lastIndex; i++) {
- segment = name.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 NameAlreadyBoundException(
- " An object that is not a context is already bound at element "
- + segment + "of name " + name);
- } else {
- bindings = ((GeronimoGlobalContext) terminalContext).bindings;
- }
- }
- segment = name.get(lastIndex);
- ((Context) terminalContext).unbind(segment);
- }
+ removeBindings(name);
}
public Object lookup(String name) throws NamingException {
@@ -171,52 +137,6 @@
return internalLookup(name, true);
}
- protected Object internalLookup(Name name, boolean resolveLinks) throws NamingException {
- Object result = null;
- Map bindings = this.bindings;
- Object terminalContext = null;
- if (name.isEmpty()) {
- return new GeronimoGlobalContext(this, this.env);
- }
- int index = name.get(0).indexOf(':');
- if (index != -1) {
- String temp = name.get(0).substring(index + 1);
- name.remove(0);
- name.add(0, temp);
- }
- if (name.size() == 1) {
- result = bindings.get(name.toString());
- } else {
- String segment = null;
- int lastIndex = name.size() - 1;
- for (int i = 0; i < lastIndex; i++) {
- segment = name.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 = ((GeronimoGlobalContext) terminalContext).bindings;
- }
- }
- segment = name.get(lastIndex);
- result = ((Context) terminalContext).lookup(segment);
- }
-
- if (result instanceof LinkRef) {
- LinkRef ref = (LinkRef) result;
- result = lookup(ref.getLinkName());
- }
-
- return result;
-
- }
-
public Object lookupLink(Name name) throws NamingException {
if (name.isEmpty()) {
throw new InvalidNameException("Name cannot be empty");
@@ -244,69 +164,14 @@
if (name.isEmpty()) {
throw new InvalidNameException("Cannot bind empty name");
}
- Map bindings = this.bindings;
- if (name.size() == 1) {
- if (bindings.get(name.toString()) == null) {
- synchronized (bindings) {
- bindings.put(name.toString(), obj);
- }
- } else {
- throw new NameAlreadyBoundException("The name " + name
- + "is already bound");
- }
- } else {
- String segment = null;
- int lastIndex = name.size() - 1;
- Object terminalContext = null;
- for (int i = 0; i < lastIndex; i++) {
- segment = name.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 NameAlreadyBoundException(
- " An object that is not a context is already bound at element "
- + segment + "of name " + name);
- } else {
- bindings = ((GeronimoGlobalContext) terminalContext).bindings;
- }
- }
- segment = name.get(lastIndex);
- ((Context) terminalContext).bind(segment, obj);
- }
+ addBinding(name, obj, false);
}
public void rebind(Name name, Object obj) throws NamingException {
if (name.isEmpty()) {
throw new InvalidNameException("Cannot bind empty name");
}
- Map bindings = this.bindings;
- if (name.size() == 1) {
- synchronized (bindings) {
- bindings.put(name.toString(), obj);
- }
- } else {
- String segment = null;
- int lastIndex = name.size() - 1;
- Object terminalContext = null;
- for (int i = 0; i < lastIndex; i++) {
- segment = name.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 NameAlreadyBoundException(
- " An object that is not a context is already bound at element "
- + segment + "of name " + name);
- } else {
- bindings = ((GeronimoGlobalContext) terminalContext).bindings;
- }
- }
- segment = name.get(lastIndex);
- ((Context) terminalContext).rebind(segment, obj);
- }
+ addBinding(name, obj, true);
}
public synchronized void rename(String oldName, String newName) throws NamingException {
@@ -321,38 +186,12 @@
if (name.isEmpty()) {
throw new InvalidNameException("Cannot create a subcontext if the name is empty");
}
- Map bindings = this.bindings;
- GeronimoGlobalContext geronimoGlobalContext = new GeronimoGlobalContext(this, this.env, name);
- if (name.size() == 1) {
- if (bindings.get(name.toString()) == null) {
- synchronized (bindings) {
- bindings.put(name.toString(), geronimoGlobalContext);
- }
- } else {
- throw new NameAlreadyBoundException("The name " + name + "is already bound");
- }
- } else {
- String segment = null;
- int lastIndex = name.size() - 1;
- Object terminalContext = null;
- for (int i = 0; i < lastIndex; i++) {
- segment = name.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 NameAlreadyBoundException(" An object that is not a context is already bound at element "
- + segment + "of name " + name);
- } else {
- bindings = ((GeronimoGlobalContext) terminalContext).bindings;
- }
- }
- segment = name.get(lastIndex);
- ((Context) terminalContext).bind(segment, geronimoGlobalContext);
- }
- return geronimoGlobalContext;
+ AbstractContext abstractContext = newSubcontext(name);
+ addBinding(name, abstractContext, false);
+ return abstractContext;
}
+
public synchronized void rename(Name oldName, Name newName) throws NamingException {
if (oldName == null || newName == null) {
throw new InvalidNameException("Name cannot be null");
@@ -383,7 +222,7 @@
public NamingEnumeration list(Name name) throws NamingException {
if (name.isEmpty()) {
- return new ListEnumeration(bindings);
+ return new ListEnumeration(getBindingsCopy());
}
Object target = lookup(name);
if (target instanceof Context) {
@@ -394,7 +233,7 @@
public NamingEnumeration listBindings(Name name) throws NamingException {
if (name.isEmpty()) {
- return new ListBindingEnumeration(bindings);
+ return new ListBindingEnumeration(getBindingsCopy());
}
Object target = lookup(name);
if (target instanceof Context) {
@@ -413,8 +252,7 @@
}
public String composeName(String name, String prefix) throws NamingException {
- return composeName(new CompositeName(name), new CompositeName(prefix))
- .toString();
+ return composeName(new CompositeName(name), new CompositeName(prefix)) .toString();
}
public Name composeName(Name name, Name prefix) throws NamingException {
@@ -430,7 +268,7 @@
return result;
}
- private final class ListEnumeration implements NamingEnumeration {
+ private static final class ListEnumeration implements NamingEnumeration {
private final Iterator iterator;
public ListEnumeration(Map localBindings) {
@@ -462,7 +300,7 @@
}
}
- private final class ListBindingEnumeration implements NamingEnumeration {
+ private static final class ListBindingEnumeration implements NamingEnumeration {
private final Iterator iterator;
public ListBindingEnumeration(Map localBindings) {
@@ -489,58 +327,6 @@
}
public void close() {
- }
- }
-
- protected void internalBind(String name, Object obj) throws NamingException {
- internalBind(new CompositeName(name), obj);
- }
-
- /**
- * InternalBind same as bind but creates a new SubContext. Can be used to
- * bind names like a/b/c/d. 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
- */
- protected void internalBind(Name name, Object obj) throws NamingException {
- if (name.isEmpty()) {
- throw new InvalidNameException("Cannot bind empty name");
- }
- Map bindings = this.bindings;
- if (name.size() == 1) {
- if (bindings.get(name.toString()) == null) {
- synchronized (bindings) {
- bindings.put(name.toString(), obj);
- }
- } else {
- throw new NameAlreadyBoundException("The name " + name + "is already bound");
- }
- } else {
- String segment = null;
- int lastIndex = name.size() - 1;
- Object terminalContext = null;
- for (int i = 0; i < lastIndex; i++) {
- segment = name.get(i);
- terminalContext = bindings.get(segment);
- if (terminalContext == null) {
- terminalContext = new GeronimoGlobalContext(this, this.env, name);
- synchronized (bindings) {
- bindings.put(segment, terminalContext);
- }
- } else if (!(terminalContext instanceof Context)) {
- throw new NameAlreadyBoundException(" An object that is not a context is already bound at element "
- + segment + "of name " + name);
- }
- bindings = ((GeronimoGlobalContext) terminalContext).bindings;
-
- }
- segment = name.get(lastIndex);
- ((Context) terminalContext).bind(segment, obj);
}
}
}
Added: geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/GeronimoGlobalContext.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/GeronimoGlobalContext.java?rev=432298&view=auto
==============================================================================
--- geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/GeronimoGlobalContext.java (added)
+++ geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/GeronimoGlobalContext.java Thu Aug 17 10:13:29 2006
@@ -0,0 +1,172 @@
+/**
+ *
+ * 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.Context;
+import javax.naming.Name;
+import javax.naming.NamingException;
+import javax.naming.NameAlreadyBoundException;
+import javax.naming.LinkRef;
+import java.util.Map;
+import java.util.Hashtable;
+import java.util.HashMap;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class GeronimoGlobalContext extends AbstractContext {
+ protected Map bindings;
+
+ public GeronimoGlobalContext() {
+ bindings = new HashMap();
+ }
+
+ public GeronimoGlobalContext(AbstractContext parent, Hashtable environment, Name contextAtomicName) {
+ super(parent, environment, contextAtomicName);
+ }
+
+ public GeronimoGlobalContext(Hashtable env) {
+ super(env);
+ bindings = new HashMap();
+ }
+
+ public GeronimoGlobalContext(GeronimoGlobalContext clone, Hashtable env) {
+ super(env);
+ bindings = clone.bindings;
+ }
+
+ protected AbstractContext newSubcontext(Name name) {
+ return new GeronimoGlobalContext(this, this.env, name);
+ }
+
+ protected void removeBindings(Name name) throws NamingException {
+ Map bindings = this.bindings;
+ if (name.size() == 1) {
+ synchronized (bindings) {
+ bindings.remove(name.toString());
+ }
+ } else {
+ String segment = null;
+ int lastIndex = name.size() - 1;
+ Object terminalContext = null;
+ for (int i = 0; i < lastIndex; i++) {
+ segment = name.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 NameAlreadyBoundException(
+ " An object that is not a context is already bound at element "
+ + segment + "of name " + name);
+ } else {
+ bindings = ((GeronimoGlobalContext) terminalContext).bindings;
+ }
+ }
+ segment = name.get(lastIndex);
+ ((Context) terminalContext).unbind(segment);
+ }
+ }
+
+ protected Object internalLookup(Name name, boolean resolveLinks) throws NamingException {
+ Object result = null;
+ Map bindings = this.bindings;
+ Object terminalContext = null;
+ if (name.isEmpty()) {
+ return this;
+ }
+ int index = name.get(0).indexOf(':');
+ if (index != -1) {
+ String temp = name.get(0).substring(index + 1);
+ name.remove(0);
+ name.add(0, temp);
+ }
+ if (name.size() == 1) {
+ result = bindings.get(name.toString());
+ } else {
+ String segment = null;
+ int lastIndex = name.size() - 1;
+ for (int i = 0; i < lastIndex; i++) {
+ segment = name.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 = ((GeronimoGlobalContext) terminalContext).bindings;
+ }
+ }
+ segment = name.get(lastIndex);
+ result = ((Context) terminalContext).lookup(segment);
+ }
+
+ if (result instanceof LinkRef) {
+ LinkRef ref = (LinkRef) result;
+ result = lookup(ref.getLinkName());
+ }
+
+ return result;
+
+ }
+
+ protected void addBinding(Name name, Object obj, boolean rebind) throws NamingException {
+ Map bindings = this.bindings;
+ if (name.size() == 1) {
+ if (rebind || bindings.get(name.toString()) == null) {
+ synchronized (bindings) {
+ bindings.put(name.toString(), obj);
+ }
+ } else {
+ throw new NameAlreadyBoundException("The name " + name
+ + "is already bound");
+ }
+ } else {
+ String segment = null;
+ int lastIndex = name.size() - 1;
+ Object terminalContext = null;
+ for (int i = 0; i < lastIndex; i++) {
+ segment = name.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 NameAlreadyBoundException(
+ " An object that is not a context is already bound at element "
+ + segment + "of name " + name);
+ } else {
+ bindings = ((GeronimoGlobalContext) terminalContext).bindings;
+ }
+ }
+ segment = name.get(lastIndex);
+ if (rebind) {
+ ((Context) terminalContext).rebind(segment, obj);
+ } else {
+ ((Context) terminalContext).bind(segment, obj);
+ }
+ }
+ }
+
+ protected Map getBindingsCopy() {
+ return bindings;
+ }
+}