📄 namingcontext.java
字号:
// ========================================================================// $Id: NamingContext.java 887 2006-09-05 13:46:42Z janb $// Copyright 1999-2006 Mort Bay Consulting Pty. Ltd.// ------------------------------------------------------------------------// 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.mortbay.naming;import java.util.Enumeration;import java.util.Hashtable;import java.util.NoSuchElementException;import javax.naming.Binding;import javax.naming.CompoundName;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.LinkRef;import javax.naming.Name;import javax.naming.NameAlreadyBoundException;import javax.naming.NameClassPair;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.Reference;import javax.naming.Referenceable;import javax.naming.spi.NamingManager;import org.mortbay.log.Log;/*------------------------------------------------*/ /** NamingContext * <p>Implementation of Context interface. * * <p><h4>Notes</h4> * <p>All Names are expected to be Compound, not Composite. * * <p><h4>Usage</h4> * <pre> *//** </pre>** @see** @author <a href="mailto:janb@mortbay.com">Jan Bartel</a>* @version 1.0*/public class NamingContext implements Context, Cloneable{ public static final String LOCK_PROPERTY = "org.mortbay.jndi.lock"; public static final String UNLOCK_PROPERTY = "org.mortbay.jndi.unlock"; public static final Enumeration EMPTY_ENUM = new Enumeration () { public boolean hasMoreElements() { return false; } public Object nextElement () { throw new NoSuchElementException(); } }; protected Context _parent = null; protected String _name = null; protected Hashtable _env = null; protected Hashtable _bindings = new Hashtable(); protected NameParser _parser = null; /*------------------------------------------------*/ /** NameEnumeration * <p>Implementation of NamingEnumeration interface. * * <p><h4>Notes</h4> * <p>Used for returning results of Context.list(); * * <p><h4>Usage</h4> * <pre> */ /* * </pre> * * @see * */ public class NameEnumeration implements NamingEnumeration { Enumeration _delegate; public NameEnumeration (Enumeration e) { _delegate = e; } public void close() throws NamingException { } public boolean hasMore () throws NamingException { return _delegate.hasMoreElements(); } public Object next() throws NamingException { Binding b = (Binding)_delegate.nextElement(); return new NameClassPair (b.getName(), b.getClassName(), true); } public boolean hasMoreElements() { return _delegate.hasMoreElements(); } public Object nextElement() { Binding b = (Binding)_delegate.nextElement(); return new NameClassPair (b.getName(), b.getClassName(), true); } } /*------------------------------------------------*/ /** BindingEnumeration * <p>Implementation of NamingEnumeration * * <p><h4>Notes</h4> * <p>Used to return results of Context.listBindings(); * * <p><h4>Usage</h4> * <pre> */ /* * </pre> * * @see * */ public class BindingEnumeration implements NamingEnumeration { Enumeration _delegate; public BindingEnumeration (Enumeration e) { _delegate = e; } public void close() throws NamingException { } public boolean hasMore () throws NamingException { return _delegate.hasMoreElements(); } public Object next() throws NamingException { Binding b = (Binding)_delegate.nextElement(); return new Binding (b.getName(), b.getClassName(), b.getObject(), true); } public boolean hasMoreElements() { return _delegate.hasMoreElements(); } public Object nextElement() { Binding b = (Binding)_delegate.nextElement(); return new Binding (b.getName(), b.getClassName(), b.getObject(),true); } } /*------------------------------------------------*/ /** * Constructor * * @param env environment properties * @param name relative name of this context * @param parent immediate ancestor Context (can be null) * @param parser NameParser for this Context */ public NamingContext(Hashtable env, String name, Context parent, NameParser parser) { if (env == null) _env = new Hashtable(); else _env = new Hashtable(env); _name = name; _parent = parent; _parser = parser; } /*------------------------------------------------*/ /** * Creates a new <code>NamingContext</code> instance. * * @param env a <code>Hashtable</code> value */ public NamingContext (Hashtable env) { if (env == null) _env = new Hashtable(); else _env = new Hashtable(env); } /*------------------------------------------------*/ /** * Constructor * */ public NamingContext () { _env = new Hashtable(); } /*------------------------------------------------*/ /** * Clone this NamingContext * * @return copy of this NamingContext * @exception CloneNotSupportedException if an error occurs */ public Object clone () throws CloneNotSupportedException { NamingContext ctx = (NamingContext)super.clone(); ctx._env = (Hashtable)_env.clone(); ctx._bindings = (Hashtable)_bindings.clone(); return ctx; } /*------------------------------------------------*/ /** * Getter for _name * * @return name of this Context (relative, not absolute) */ public String getName () { return _name; } /*------------------------------------------------*/ /** * Getter for _parent * * @return parent Context */ public Context getParent() { return _parent; } /*------------------------------------------------*/ /** * Setter for _parser * * */ public void setNameParser (NameParser parser) { _parser = parser; } /*------------------------------------------------*/ /** * Bind a name to an object * * @param name Name of the object * @param obj object to bind * @exception NamingException if an error occurs */ public void bind(Name name, Object obj) throws NamingException { if (isLocked()) throw new NamingException ("This context is immutable"); Name cname = toCanonicalName(name); if (cname == null) throw new NamingException ("Name is null"); if (cname.size() == 0) throw new NamingException ("Name is empty"); //if no subcontexts, just bind it if (cname.size() == 1) { //get the object to be bound Object objToBind = NamingManager.getStateToBind(obj, name,this, null); // Check for Referenceable if (objToBind instanceof Referenceable) { objToBind = ((Referenceable)objToBind).getReference(); } //anything else we should be able to bind directly Binding binding = getBinding (cname); if (binding == null) addBinding (cname, objToBind); else throw new NameAlreadyBoundException (cname.toString()); } else { if(Log.isDebugEnabled())Log.debug("Checking for existing binding for name="+cname+" for first element of name="+cname.get(0)); //walk down the subcontext hierarchy //need to ignore trailing empty "" name components String firstComponent = cname.get(0); Object ctx = null; if (firstComponent.equals("")) ctx = this; else { Binding binding = getBinding (firstComponent); if (binding == null) throw new NameNotFoundException (firstComponent+ " is not bound"); ctx = binding.getObject(); if (ctx instanceof Reference) { //deference the object try { ctx = NamingManager.getObjectInstance(ctx, getNameParser("").parse(firstComponent), this, _env); } catch (NamingException e) { throw e; } catch (Exception e) { Log.warn("",e); throw new NamingException (e.getMessage()); } } } if (ctx instanceof Context) { ((Context)ctx).bind (cname.getSuffix(1), obj); } else throw new NotContextException ("Object bound at "+firstComponent +" is not a Context"); } } /*------------------------------------------------*/ /** * Bind a name (as a String) to an object * * @param name a <code>String</code> value * @param obj an <code>Object</code> value * @exception NamingException if an error occurs */ public void bind(String name, Object obj) throws NamingException { bind (_parser.parse(name), obj); } /*------------------------------------------------*/ /** * Create a context as a child of this one * * @param name a <code>Name</code> value * @return a <code>Context</code> value * @exception NamingException if an error occurs */ public Context createSubcontext (Name name) throws NamingException { if (isLocked()) { NamingException ne = new NamingException ("This context is immutable"); ne.setRemainingName(name); throw ne; } Name cname = toCanonicalName (name); if (cname == null) throw new NamingException ("Name is null"); if (cname.size() == 0) throw new NamingException ("Name is empty"); if (cname.size() == 1) { //not permitted to bind if something already bound at that name Binding binding = getBinding (cname); if (binding != null) throw new NameAlreadyBoundException (cname.toString()); Context ctx = new NamingContext ((Hashtable)_env.clone(), cname.get(0), this, _parser); addBinding (cname, ctx); return ctx; } //If the name has multiple subcontexts, walk the hierarchy by //fetching the first one. All intermediate subcontexts in the //name must already exist. String firstComponent = cname.get(0); Object ctx = null; if (firstComponent.equals("")) ctx = this; else { Binding binding = getBinding (firstComponent); if (binding == null) throw new NameNotFoundException (firstComponent + " is not bound"); ctx = binding.getObject(); if (ctx instanceof Reference) { //deference the object if(Log.isDebugEnabled())Log.debug("Object bound at "+firstComponent +" is a Reference"); try { ctx = NamingManager.getObjectInstance(ctx, getNameParser("").parse(firstComponent), this, _env);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -