📄 namingcontext.java
字号:
/*------------------------------------------------*/ /** * Overwrite or create a binding * * @param name a <code>Name</code> value * @param obj an <code>Object</code> value * @exception NamingException if an error occurs */ public void rebind(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) { //check if it is a Referenceable Object objToBind = NamingManager.getStateToBind(obj, name, this, null); if (objToBind instanceof Referenceable) { objToBind = ((Referenceable)objToBind).getReference(); } addBinding (cname, objToBind); } else { //walk down the subcontext hierarchy if(Log.isDebugEnabled())Log.debug("Checking for existing binding for name="+cname+" for first element of name="+cname.get(0)); String firstComponent = cname.get(0); Object ctx = null; if (firstComponent.equals("")) ctx = this; else { Binding binding = getBinding (name.get(0)); if (binding == null) throw new NameNotFoundException (name.get(0)+ " 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).rebind (cname.getSuffix(1), obj); } else throw new NotContextException ("Object bound at "+firstComponent +" is not a Context"); } } /*------------------------------------------------*/ /** * Overwrite or create a binding from Name to Object * * @param name a <code>String</code> value * @param obj an <code>Object</code> value * @exception NamingException if an error occurs */ public void rebind (String name, Object obj) throws NamingException { rebind (_parser.parse(name), obj); } /*------------------------------------------------*/ /** * Not supported. * * @param name a <code>String</code> value * @exception NamingException if an error occurs */ public void unbind (String name) throws NamingException { unbind(_parser.parse(name)); } /*------------------------------------------------*/ /** * Not supported. * * @param name a <code>String</code> value * @exception NamingException if an error occurs */ public void unbind (Name name) throws NamingException { if (name.size() == 0) return; 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 unbind it if (cname.size() == 1) { removeBinding (cname); } else { //walk down the subcontext hierarchy if(Log.isDebugEnabled())Log.debug("Checking for existing binding for name="+cname+" for first element of name="+cname.get(0)); String firstComponent = cname.get(0); Object ctx = null; if (firstComponent.equals("")) ctx = this; else { Binding binding = getBinding (name.get(0)); if (binding == null) throw new NameNotFoundException (name.get(0)+ " 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).unbind (cname.getSuffix(1)); } else throw new NotContextException ("Object bound at "+firstComponent +" is not a Context"); } } /*------------------------------------------------*/ /** * Not supported * * @param oldName a <code>Name</code> value * @param newName a <code>Name</code> value * @exception NamingException if an error occurs */ public void rename(Name oldName, Name newName) throws NamingException { throw new OperationNotSupportedException(); } /*------------------------------------------------*/ /** * Not supported * * @param oldName a <code>Name</code> value * @param newName a <code>Name</code> value * @exception NamingException if an error occurs */ public void rename(String oldName, String newName) throws NamingException { throw new OperationNotSupportedException(); } /*------------------------------------------------*/ /** Join two names together. These are treated as * CompoundNames. * * @param name a <code>Name</code> value * @param prefix a <code>Name</code> value * @return a <code>Name</code> value * @exception NamingException if an error occurs */ public Name composeName(Name name, Name prefix) throws NamingException { if (name == null) throw new NamingException ("Name cannot be null"); if (prefix == null) throw new NamingException ("Prefix cannot be null"); Name compoundName = (CompoundName)prefix.clone(); compoundName.addAll (name); return compoundName; } /*------------------------------------------------*/ /** Join two names together. These are treated as * CompoundNames. * * @param name a <code>Name</code> value * @param prefix a <code>Name</code> value * @return a <code>Name</code> value * @exception NamingException if an error occurs */ public String composeName (String name, String prefix) throws NamingException { if (name == null) throw new NamingException ("Name cannot be null"); if (prefix == null) throw new NamingException ("Prefix cannot be null"); Name compoundName = _parser.parse(prefix); compoundName.add (name); return compoundName.toString(); } /*------------------------------------------------*/ /** * Do nothing * * @exception NamingException if an error occurs */ public void close () throws NamingException { } /*------------------------------------------------*/ /** * Return a NameParser for this Context. * * @param name a <code>Name</code> value * @return a <code>NameParser</code> value */ public NameParser getNameParser (Name name) { return _parser; } /*------------------------------------------------*/ /** * Return a NameParser for this Context. * * @param name a <code>Name</code> value * @return a <code>NameParser</code> value */ public NameParser getNameParser (String name) { return _parser; } /*------------------------------------------------*/ /** * Get the full name of this Context node * by visiting it's ancestors back to root. * * NOTE: if this Context has a URL namespace then * the URL prefix will be missing * * @return the full name of this Context * @exception NamingException if an error occurs */ public String getNameInNamespace () throws NamingException { Name name = _parser.parse(""); NamingContext c = this; while (c != null) { String str = c.getName(); if (str != null) name.add(0, str); c = (NamingContext)c.getParent(); } return name.toString(); } /*------------------------------------------------*/ /** * Add an environment setting to this Context * * @param propName name of the property to add * @param propVal value of the property to add * @return propVal or previous value of the property * @exception NamingException if an error occurs */ public Object addToEnvironment(String propName, Object propVal) throws NamingException { if (isLocked() && !(propName.equals(UNLOCK_PROPERTY))) throw new NamingException ("This context is immutable"); return _env.put (propName, propVal); } /*------------------------------------------------*/ /** * Remove a property from this Context's environment. * * @param propName name of property to remove * @return value of property or null if it didn't exist * @exception NamingException if an error occurs */ public Object removeFromEnvironment(String propName) throws NamingException { if (isLocked()) throw new NamingException ("This context is immutable"); return _env.remove (propName); } /*------------------------------------------------*/ /** * Get the environment of this Context. * * @return a copy of the environment of this Context. */ public Hashtable getEnvironment () { return (Hashtable)_env.clone(); } /*------------------------------------------------*/ /** * Add a name to object binding to this Context. * * @param name a <code>Name</code> value * @param obj an <code>Object</code> value */ protected void addBinding (Name name, Object obj) { String key = name.toString(); if(Log.isDebugEnabled())Log.debug("Adding binding with key="+key+" obj="+obj+" for context="+_name); _bindings.put (key, new Binding (key, obj)); } /*------------------------------------------------*/ /** * Get a name to object binding from this Context * * @param name a <code>Name</code> value * @return a <code>Binding</code> value */ protected Binding getBinding (Name name) { if(Log.isDebugEnabled())Log.debug("Looking up binding for "+name.toString()+" for context="+_name); return (Binding) _bindings.get(name.toString()); } /*------------------------------------------------*/ /** * Get a name to object binding from this Context * * @param name as a String * @return null or the Binding */ protected Binding getBinding (String name) { if(Log.isDebugEnabled())Log.debug("Looking up binding for "+name+" for context="+_name); return (Binding) _bindings.get(name); } protected void removeBinding (Name name) { String key = name.toString(); if (Log.isDebugEnabled()) Log.debug("Removing binding with key="+key); _bindings.remove(key); } /*------------------------------------------------*/ /** * Remove leading or trailing empty components from * name. Eg "/comp/env/" -> "comp/env" * * @param name the name to normalize * @return normalized name */ public Name toCanonicalName (Name name) { Name canonicalName = name; if (name != null) { if (canonicalName.size() > 1) { if (canonicalName.get(0).equals("")) canonicalName = canonicalName.getSuffix(1); if (canonicalName.get(canonicalName.size()-1).equals("")) canonicalName = canonicalName.getPrefix(canonicalName.size()-1); } } return canonicalName; } private boolean isLocked() { if ((_env.get(LOCK_PROPERTY) == null) && (_env.get(UNLOCK_PROPERTY) == null)) return false; Object lockKey = _env.get(LOCK_PROPERTY); Object unlockKey = _env.get(UNLOCK_PROPERTY); if ((lockKey != null) && (unlockKey != null) && (lockKey.equals(unlockKey))) return false; return true; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -