📄 namingcontext.java
字号:
} catch (NamingException e) { throw e; } catch (Exception e) { Log.warn("",e); throw new NamingException (e.getMessage()); } } } if (ctx instanceof Context) { return ((Context)ctx).createSubcontext (cname.getSuffix(1)); } else throw new NotContextException (firstComponent +" is not a Context"); } /*------------------------------------------------*/ /** * Create a Context as a child of this one * * @param name a <code>String</code> value * @return a <code>Context</code> value * @exception NamingException if an error occurs */ public Context createSubcontext (String name) throws NamingException { return createSubcontext(_parser.parse(name)); } /*------------------------------------------------*/ /** * Not supported * * @param name name of subcontext to remove * @exception NamingException if an error occurs */ public void destroySubcontext (String name) throws NamingException { removeBinding(_parser.parse(name)); } /*------------------------------------------------*/ /** * Not supported * * @param name name of subcontext to remove * @exception NamingException if an error occurs */ public void destroySubcontext (Name name) throws NamingException { removeBinding(name); } /*------------------------------------------------*/ /** * Lookup a binding by name * * @param name name of bound object * @exception NamingException if an error occurs */ public Object lookup(Name name) throws NamingException { if(Log.isDebugEnabled())Log.debug("Looking up name=\""+name+"\""); Name cname = toCanonicalName(name); if ((cname == null) || (cname.size() == 0)) { Log.debug("Null or empty name, returning copy of this context"); NamingContext ctx = new NamingContext (_env, _name, _parent, _parser); ctx._bindings = _bindings; return ctx; } if (cname.size() == 1) { Binding binding = getBinding (cname); if (binding == null) { NameNotFoundException nnfe = new NameNotFoundException(); nnfe.setRemainingName(cname); throw nnfe; } Object o = binding.getObject(); //handle links by looking up the link if (o instanceof LinkRef) { //if link name starts with ./ it is relative to current context String linkName = ((LinkRef)o).getLinkName(); if (linkName.startsWith("./")) return this.lookup (linkName.substring(2)); else { //link name is absolute InitialContext ictx = new InitialContext(); return ictx.lookup (linkName); } } else if (o instanceof Reference) { //deference the object try { return NamingManager.getObjectInstance(o, cname, this, _env); } catch (NamingException e) { throw e; } catch (Exception e) { Log.warn("",e); throw new NamingException (e.getMessage()); } } else return o; } //it is a multipart name, recurse to the first subcontext String firstComponent = cname.get(0); Object ctx = null; if (firstComponent.equals("")) ctx = this; else { Binding binding = getBinding (firstComponent); if (binding == null) { NameNotFoundException nnfe = new NameNotFoundException(); nnfe.setRemainingName(cname); throw nnfe; } //as we have bound a reference to an object factory //for the component specific contexts //at "comp" we need to resolve the reference 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)) throw new NotContextException(); return ((Context)ctx).lookup (cname.getSuffix(1)); } /*------------------------------------------------*/ /** * Lookup binding of an object by name * * @param name name of bound object * @return object bound to name * @exception NamingException if an error occurs */ public Object lookup (String name) throws NamingException { return lookup (_parser.parse(name)); } /*------------------------------------------------*/ /** * Lookup link bound to name * * @param name name of link binding * @return LinkRef or plain object bound at name * @exception NamingException if an error occurs */ public Object lookupLink (Name name) throws NamingException { Name cname = toCanonicalName(name); if (cname == null) { NamingContext ctx = new NamingContext (_env, _name, _parent, _parser); ctx._bindings = _bindings; return ctx; } if (cname.size() == 0) throw new NamingException ("Name is empty"); if (cname.size() == 1) { Binding binding = getBinding (cname); if (binding == null) throw new NameNotFoundException(); Object o = binding.getObject(); //handle links by looking up the link if (o instanceof Reference) { //deference the object try { return NamingManager.getObjectInstance(o, cname.getPrefix(1), this, _env); } catch (NamingException e) { throw e; } catch (Exception e) { Log.warn("",e); throw new NamingException (e.getMessage()); } } else { //object is either a LinkRef which we don't dereference //or a plain object in which case spec says we return it return o; } } //it is a multipart name, recurse to the first subcontext String firstComponent = cname.get(0); Object ctx = null; if (firstComponent.equals("")) ctx = this; else { Binding binding = getBinding (firstComponent); if (binding == null) throw new NameNotFoundException (); 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)) throw new NotContextException(); return ((Context)ctx).lookup (cname.getSuffix(1)); } /*------------------------------------------------*/ /** * Lookup link bound to name * * @param name name of link binding * @return LinkRef or plain object bound at name * @exception NamingException if an error occurs */ public Object lookupLink (String name) throws NamingException { return lookupLink (_parser.parse(name)); } /*------------------------------------------------*/ /** * List all names bound at Context named by Name * * @param name a <code>Name</code> value * @return a <code>NamingEnumeration</code> value * @exception NamingException if an error occurs */ public NamingEnumeration list(Name name) throws NamingException { if(Log.isDebugEnabled())Log.debug("list() on Context="+getName()+" for name="+name); Name cname = toCanonicalName(name); if (cname == null) { return new NameEnumeration(EMPTY_ENUM); } if (cname.size() == 0) { return new NameEnumeration (_bindings.elements()); } //multipart name String firstComponent = cname.get(0); Object ctx = null; if (firstComponent.equals("")) ctx = this; else { Binding binding = getBinding (firstComponent); if (binding == null) throw new NameNotFoundException (); ctx = binding.getObject(); if (ctx instanceof Reference) { //deference the object if(Log.isDebugEnabled())Log.debug("Dereferencing Reference for "+name.get(0)); 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)) throw new NotContextException(); return ((Context)ctx).list (cname.getSuffix(1)); } /*------------------------------------------------*/ /** * List all names bound at Context named by Name * * @param name a <code>Name</code> value * @return a <code>NamingEnumeration</code> value * @exception NamingException if an error occurs */ public NamingEnumeration list(String name) throws NamingException { return list(_parser.parse(name)); } /*------------------------------------------------*/ /** * List all Bindings present at Context named by Name * * @param name a <code>Name</code> value * @return a <code>NamingEnumeration</code> value * @exception NamingException if an error occurs */ public NamingEnumeration listBindings(Name name) throws NamingException { Name cname = toCanonicalName (name); if (cname == null) { return new BindingEnumeration(EMPTY_ENUM); } if (cname.size() == 0) { return new BindingEnumeration (_bindings.elements()); } //multipart name String firstComponent = cname.get(0); Object ctx = null; //if a name has a leading "/" it is parsed as "" so ignore it by staying //at this level in the tree if (firstComponent.equals("")) ctx = this; else { //it is a non-empty name component Binding binding = getBinding (firstComponent); if (binding == null) throw new NameNotFoundException (); 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)) throw new NotContextException(); return ((Context)ctx).listBindings (cname.getSuffix(1)); } /*------------------------------------------------*/ /** * List all Bindings at Name * * @param name a <code>String</code> value * @return a <code>NamingEnumeration</code> value * @exception NamingException if an error occurs */ public NamingEnumeration listBindings(String name) throws NamingException { return listBindings (_parser.parse(name)); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -