/** * 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, _env); if (objToBind instanceof Referenceable) { objToBind = ((Referenceable) objToBind).getReference(); } removeBinding(cname); 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"); } }
/** * Binds a name to an object. All intermediate contexts and the target context (that named by all * but terminal atomic component of the name) must already exist. * * @param name the name to bind; may not be empty * @param obj the object to bind; possibly null * @param rebind if true, then perform a rebind (ie, overwrite) * @exception NameAlreadyBoundException if name is already bound * @exception javax.naming.directory.InvalidAttributesException if object did not supply all * mandatory attributes * @exception NamingException if a naming exception is encountered */ protected void bind(Name name, Object obj, boolean rebind) throws NamingException { if (!checkWritable()) { return; } while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) throw new NamingException(sm.getString("namingContext.invalidName")); NamingEntry entry = bindings.get(name.get(0)); if (name.size() > 1) { if (entry == null) { throw new NameNotFoundException( sm.getString("namingContext.nameNotBound", name, name.get(0))); } if (entry.type == NamingEntry.CONTEXT) { if (rebind) { ((Context) entry.value).rebind(name.getSuffix(1), obj); } else { ((Context) entry.value).bind(name.getSuffix(1), obj); } } else { throw new NamingException(sm.getString("namingContext.contextExpected")); } } else { if ((!rebind) && (entry != null)) { throw new NameAlreadyBoundException( sm.getString("namingContext.alreadyBound", name.get(0))); } else { // Getting the type of the object and wrapping it within a new // NamingEntry Object toBind = NamingManager.getStateToBind(obj, name, this, env); if (toBind instanceof Context) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.CONTEXT); } else if (toBind instanceof LinkRef) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.LINK_REF); } else if (toBind instanceof Reference) { entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE); } else if (toBind instanceof Referenceable) { toBind = ((Referenceable) toBind).getReference(); entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE); } else { entry = new NamingEntry(name.get(0), toBind, NamingEntry.ENTRY); } bindings.put(name.get(0), entry); } } }
/** * 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, _env); // Check for Referenceable if (objToBind instanceof Referenceable) { objToBind = ((Referenceable) objToBind).getReference(); } // anything else we should be able to bind directly addBinding(cname, objToBind); } 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) { if (_supportDeepBinding) { Name subname = _parser.parse(firstComponent); Context subctx = new NamingContext((Hashtable) _env.clone(), firstComponent, this, _parser); addBinding(subname, subctx); binding = getBinding(subname); } else { 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"); } }