private Object lookupImpl(Name name) throws NamingException { if (log.isLoggable(Level.FINEST)) log.finest(L.l("JNDI lookup `{0}'", name)); if (name == null) return create(_model, _env); AbstractModel model = _model; for (int i = 0; i < name.size(); i++) { String first = name.get(i); Object value = model.lookup(first); if (value instanceof AbstractModel) { model = (AbstractModel) value; continue; } value = dereference(value, null, model); if (i + 1 == name.size()) { return value; } else if (value instanceof Context) { return ((Context) value).lookup(name.getSuffix(i + 1)); } else if (value != null) throw new NotContextException( L.l("{0}: expected intermediate context at `{1}'", getFullPath(name), value)); else throw new NameNotFoundException(getFullPath(name)); } return create(getFullPath(name), model, _env); }
/** * This postparses a name, after it has been returned from the jndi operation. It assumes that it * has got a jndi <i>CompositeName</i> that needs to be converted to a legal ldap dn (i.e. an ldap * <i>CompoundName</i>). If this is *not* the case, there will be trouble... * * @param name the post jndi operation name. * @return the re-formatted version used by the application, as a DN object. */ public Name postParse(String name) { /* EMERGENCY HACK * (JNDI apparently does not handle terminating spaces correctly - it * retains the escape characters, but trims the actual space, resulting * in an illegal ldap dn) */ if (name.charAt(name.length() - 1) == '\\') { name = NameUtility.checkEndSpaces(name); } try { Name cn = new CompositeName(name); if (cn.size() == 0) // if the name is empty ... return new DN(); // ... just return an empty DN return new DN( cn.get( cn.size() - 1)); // get the last element of the composite name, which will be the ldap // compound name, and init the DN with that. } catch (NamingException e) // should never happen :-) (ROTFL) { log.log( Level.WARNING, "unexpected error: bad name back from jndi ftn in CBOps.postParse(" + name + ")?\n" + e.toString()); e.printStackTrace(); // System.exit(-1); return new DN(name); // bad server response? return (possibly) corrupt name anyway... } }
public void rebind(Name name, Object obj) throws NamingException { if (name.size() == 0) throw new NamingException(L.l("can't bind root")); AbstractModel model = _model; int i = 0; for (; i + 1 < name.size(); i++) { String first = name.get(i); Object value = model.lookup(first); if (value instanceof AbstractModel) { model = (AbstractModel) value; continue; } value = dereference(value, null, model); if (value instanceof Context) { ((Context) value).bind(name.getSuffix(i + 1), obj); return; } else if (value != null) throw new NotContextException( L.l("{0}: expected intermediate context at `{1}'", getFullPath(name), value)); else throw new NameNotFoundException(getFullPath(name)); } String first = name.get(i); if (obj == null) obj = NullValue.NULL; model.bind(first, getReference(model, obj)); }
/** Looks up an object with the given parsed JNDI name, but don't dereference the final object. */ public Object lookupLink(Name name) throws NamingException { if (name == null) return create(_model, _env); AbstractModel model = _model; for (int i = 0; i < name.size(); i++) { String first = name.get(i); Object value = model.lookup(first); if (value instanceof AbstractModel) { model = (AbstractModel) value; continue; } if (i + 1 == name.size()) { if (value == NullValue.NULL) return null; else if (value != null) return value; else throw new NameNotFoundException(getFullPath(name)); } value = dereference(value, null, model); if (value instanceof Context) return ((Context) value).lookupLink(name.getSuffix(i + 1)); else if (value != null) throw new NotContextException( L.l("{0}: expected intermediate context at `{1}'", getFullPath(name), value)); else throw new NameNotFoundException(getFullPath(name)); } return create(getFullPath(name), model, _env); }
public void rename(Name name, Name newName) throws NamingException { if (name.size() == 1) { if (newName.size() != 1) { throw new OperationNotSupportedException( "Renaming to a Name with more components not supported: " + newName); } rename(name.get(0), newName.get(0)); } else { // > 1 component with 1st one being URL // URLs must be identical; cannot deal with diff URLs if (!urlEquals(name.get(0), newName.get(0))) { throw new OperationNotSupportedException( "Renaming using different URLs as first components not supported: " + name + " " + newName); } Context ctx = getContinuationContext(name); try { ctx.rename(name.getSuffix(1), newName.getSuffix(1)); } finally { ctx.close(); } } }
public void destroySubcontext(Name name) throws NamingException { if (name.size() == 0) throw new NamingException(L.l("can't destroy root subcontext")); AbstractModel model = _model; int i = 0; for (; i + 1 < name.size(); i++) { String first = name.get(i); Object value = model.lookup(first); if (value instanceof AbstractModel) { model = (AbstractModel) value; continue; } value = dereference(value, null, model); if (value instanceof Context) { ((Context) value).destroySubcontext(name.getSuffix(i + 1)); return; } else if (value != null) throw new NotContextException( L.l("{0}: expected intermediate context at `{1}'", getFullPath(name), value)); else throw new NameNotFoundException(getFullPath(name)); } String first = name.get(i); model.unbind(first); }
/** * 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"); } }
public Object getSpecialObject(Name name) throws NamingException { if (name.size() > 0 && "java:comp".equals(name.get(0))) { if (name.size() == 2 && "ORB".equals(name.get(1))) return ORBFactory.getORB(); else if (name.size() == 2 && "HandleDelegate".equals(name.get(1))) return HandleDelegateFactory.getHandleDelegateSingleton(); } throw new NamingException("Name not found " + name); }
/** * 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); } 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"); }
public int compareTo(PermissionName pn) { /* Each level must be compared. The first level to not be equals determines the ordering of the names. */ int compare = name.size() - pn.name.size(); int length = Math.min(name.size(), pn.name.size()); for (int n = 0; compare == 0 && n < length; n++) { String atom0 = name.get(n); String atom1 = pn.name.get(n); compare = atom0.compareTo(atom1); } return compare; }
private void bind(Name name, Object value, Context context) { try { LOG.tracef("Binding : %s", name); context.rebind(name, value); } catch (Exception initialException) { // We had problems doing a simple bind operation. if (name.size() == 1) { // if the jndi name had only 1 component there is nothing more we can do... throw new JndiException("Error performing bind [" + name + "]", initialException); } // Otherwise, there is a good chance this may have been caused by missing intermediate // contexts. So we // attempt to create those missing intermediate contexts and bind again Context intermediateContextBase = context; while (name.size() > 1) { final String intermediateContextName = name.get(0); Context intermediateContext = null; try { LOG.tracev("Intermediate lookup: {0}", intermediateContextName); intermediateContext = (Context) intermediateContextBase.lookup(intermediateContextName); } catch (NameNotFoundException handledBelow) { // ok as we will create it below if not found } catch (NamingException e) { throw new JndiException("Unanticipated error doing intermediate lookup", e); } if (intermediateContext != null) { LOG.tracev("Found intermediate context: {0}", intermediateContextName); } else { LOG.tracev("Creating sub-context: {0}", intermediateContextName); try { intermediateContext = intermediateContextBase.createSubcontext(intermediateContextName); } catch (NamingException e) { throw new JndiException( "Error creating intermediate context [" + intermediateContextName + "]", e); } } intermediateContextBase = intermediateContext; name = name.getSuffix(1); } LOG.tracev("Binding : {0}", name); try { intermediateContextBase.rebind(name, value); } catch (NamingException e) { throw new JndiException("Error performing intermediate bind [" + name + "]", e); } } LOG.debugf("Bound name: %s", name); }
/** * 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; }
/** * Bind val to name in ctx, and make sure that all intermediate contexts exist. * * @param ctx the root context * @param name the name as a string * @param val the object to be bound * @throws NamingException */ public static void bind(Context ctx, String name, Object val) throws NamingException { try { log.trace("binding: " + name); ctx.rebind(name, val); } catch (Exception e) { Name n = ctx.getNameParser("").parse(name); while (n.size() > 1) { String ctxName = n.get(0); Context subctx = null; try { log.trace("lookup: " + ctxName); subctx = (Context) ctx.lookup(ctxName); } catch (NameNotFoundException nfe) { } if (subctx != null) { log.debug("Found subcontext: " + ctxName); ctx = subctx; } else { log.info("Creating subcontext: " + ctxName); ctx = ctx.createSubcontext(ctxName); } n = n.getSuffix(1); } log.trace("binding: " + n); ctx.rebind(n, val); } log.debug("Bound name: " + name); }
/** Add the objects passed to the constructor to the JNDI Context addresses specified */ public void setup() { try { InitialContext ic = new InitialContext(); for (Iterator i = this.objectsToCreate.keySet().iterator(); i.hasNext(); ) { String name = (String) i.next(); try { Name fullName = new CompositeName(name); Context currentContext = ic; while (fullName.size() > 1) { // Make contexts that are not already present try { currentContext = currentContext.createSubcontext(fullName.get(0)); } catch (NamingException err) { currentContext = (Context) currentContext.lookup(fullName.get(0)); } fullName = fullName.getSuffix(1); } ic.bind(name, this.objectsToCreate.get(name)); Logger.log(Logger.FULL_DEBUG, JNDI_RESOURCES, "ContainerJNDIManager.BoundResource", name); } catch (NamingException err) { Logger.log( Logger.ERROR, JNDI_RESOURCES, "ContainerJNDIManager.ErrorBindingResource", name, err); } } Logger.log( Logger.DEBUG, JNDI_RESOURCES, "ContainerJNDIManager.SetupComplete", "" + this.objectsToCreate.size()); } catch (NamingException err) { Logger.log( Logger.ERROR, JNDI_RESOURCES, "ContainerJNDIManager.ErrorGettingInitialContext", err); } }
@Override public void applicationStarted(ComponentContext context) { if (namingContext != null) { return; } namingContext = NuxeoContainer.getRootContext(); // allocate datasource sub-contexts Name comp; try { comp = new CompositeName(DataSourceHelper.getDataSourceJNDIPrefix()); } catch (NamingException e) { throw new RuntimeException(e); } Context ctx = namingContext; for (int i = 0; i < comp.size(); i++) { try { ctx = (Context) ctx.lookup(comp.get(i)); } catch (NamingException e) { try { ctx = ctx.createSubcontext(comp.get(i)); } catch (NamingException e1) { throw new RuntimeException(e1); } } } // bind datasources for (DataSourceDescriptor datasourceDesc : datasources.values()) { bindDataSource(datasourceDesc); } // bind links for (DataSourceLinkDescriptor linkDesc : links.values()) { bindDataSourceLink(linkDesc); } }
/** Lists the names for the context. */ public NamingEnumeration list(Name name) throws NamingException { AbstractModel model = _model; if (name == null) { return new QNameClassEnumeration(create(model, _env), model.list()); } for (int i = 0; i < name.size(); i++) { String first = name.get(i); Object value = model.lookup(first); if (value instanceof AbstractModel) { model = (AbstractModel) value; continue; } value = dereference(value, null, model); if (value instanceof Context) return ((Context) value).list(name.getSuffix(i + 1)); else if (value != null) throw new NotContextException( L.l("{0}: expected intermediate context at `{1}'", getFullPath(name), value)); else throw new NameNotFoundException(getFullPath(name)); } return new QNameClassEnumeration(create(model, _env), model.list()); }
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName(); if (methodName.equals("toString") == true) return "Client ENC(" + clientName + ")"; if (methodName.equals("lookup") == false) throw new OperationNotSupportedException("Only lookup is supported, op=" + method); NameParser parser = lookupCtx.getNameParser(""); Name name = null; if (args[0] instanceof String) name = parser.parse((String) args[0]); else name = (Name) args[0]; // Check for special objects not in the env if (name.size() < 2 || "java:comp".equals(name.get(0)) == false || "env".equals(name.get(1)) == false) return getSpecialObject(name); // Lookup the client application context from the server Context clientCtx = (Context) lookupCtx.lookup(clientName); // JBAS-3967: EJB3 Client container hack try { clientCtx = (Context) clientCtx.lookup("env"); } catch (NamingException e) { // ignore log.trace("No env sub context found", e); } // Strip the comp/env prefix Name bindingName = name.getSuffix(2); Object binding = clientCtx.lookup(bindingName); return binding; }
/** * Rebind val to name in ctx, and make sure that all intermediate contexts exist * * @param ctx the parent JNDI Context under which value will be bound * @param name the name relative to ctx where value will be bound * @param value the value to bind. * @throws javax.naming.NamingException for any error */ public static void rebind(final Context ctx, final Name name, final Object value) throws NamingException { final int size = name.size(); final String atom = name.get(size - 1); final Context parentCtx = createSubcontext(ctx, name.getPrefix(size - 1)); parentCtx.rebind(atom, value); }
/** * Destroys the named context and removes it from the namespace. Any attributes associated with * the name are also removed. Intermediate contexts are not destroyed. * * <p>This method is idempotent. It succeeds even if the terminal atomic name is not bound in the * target context, but throws NameNotFoundException if any of the intermediate contexts do not * exist. * * <p>In a federated naming system, a context from one naming system may be bound to a name in * another. One can subsequently look up and perform operations on the foreign context using a * composite name. However, an attempt destroy the context using this composite name will fail * with NotContextException, because the foreign context is not a "subcontext" of the context in * which it is bound. Instead, use unbind() to remove the binding of the foreign context. * Destroying the foreign context requires that the destroySubcontext() be performed on a context * from the foreign context's "native" naming system. * * @param name the name of the context to be destroyed; may not be empty * @exception NameNotFoundException if an intermediate context does not exist * @exception NotContextException if the name is bound but does not name a context, or does not * name a context of the appropriate type */ @Override public void destroySubcontext(Name name) 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 (entry == null) { throw new NameNotFoundException( sm.getString("namingContext.nameNotBound", name, name.get(0))); } if (name.size() > 1) { if (entry.type == NamingEntry.CONTEXT) { ((Context) entry.value).destroySubcontext(name.getSuffix(1)); } else { throw new NamingException(sm.getString("namingContext.contextExpected")); } } else { if (entry.type == NamingEntry.CONTEXT) { ((Context) entry.value).close(); bindings.remove(name.get(0)); } else { throw new NotContextException(sm.getString("namingContext.contextExpected")); } } }
/** Returns the full name for the context. */ protected String getFullPath(Name name) { if (_name == null || _name.equals("")) return name.toString(); else if (name == null || name.size() == 0) return _name; String sep = getSeparatorString(); return _name + sep + name; }
public NameParser getNameParser(Name name) throws NamingException { if (name.size() == 0) return new QNameParser(this); Object obj = lookupSingleObject(name.get(0)); if (obj instanceof Context) return ((Context) obj).getNameParser(name.getSuffix(1)); else return new QNameParser(this); }
/** * Retrieves the named object. * * @param name the name of the object to look up * @param resolveLinks If true, the links will be resolved * @return the object bound to name * @exception NamingException if a naming exception is encountered */ protected Object lookup(Name name, boolean resolveLinks) throws NamingException { // Removing empty parts while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) { // If name is empty, a newly allocated naming context is returned return new NamingContext(env, this.name, bindings); } NamingEntry entry = bindings.get(name.get(0)); if (entry == null) { throw new NameNotFoundException( sm.getString("namingContext.nameNotBound", name, name.get(0))); } if (name.size() > 1) { // If the size of the name is greater that 1, then we go through a // number of subcontexts. if (entry.type != NamingEntry.CONTEXT) { throw new NamingException(sm.getString("namingContext.contextExpected")); } return ((Context) entry.value).lookup(name.getSuffix(1)); } else { if ((resolveLinks) && (entry.type == NamingEntry.LINK_REF)) { String link = ((LinkRef) entry.value).getLinkName(); if (link.startsWith(".")) { // Link relative to this context return lookup(link.substring(1)); } else { return (new InitialContext(env)).lookup(link); } } else if (entry.type == NamingEntry.REFERENCE) { try { // TODO ÐèѧϰNamingManager Object obj = NamingManager.getObjectInstance(entry.value, name, this, env); if (entry.value instanceof ResourceRef) { boolean singleton = Boolean.parseBoolean( (String) ((ResourceRef) entry.value).get("singleton").getContent()); if (singleton) { entry.type = NamingEntry.ENTRY; entry.value = obj; } } return obj; } catch (NamingException e) { throw e; } catch (Exception e) { log.warn(sm.getString("namingContext.failResolvingReference"), e); throw new NamingException(e.getMessage()); } } else { return entry.value; } } }
/** Entry tree lookup. */ protected Entry treeLookup(Name name) { if (name.isEmpty() || entries == null) return entries; Entry currentEntry = entries; for (int i = 0; i < name.size(); i++) { if (name.get(i).length() == 0) continue; currentEntry = currentEntry.getChild(name.get(i)); if (currentEntry == null) return null; } return currentEntry; }
protected final String getAtom(String dn) { // need to strip off all but lowest component of dn // so that is relative to current context (currentDN) try { Name parsed = new LdapName(dn); return parsed.get(parsed.size() - 1); } catch (NamingException e) { return dn; } }
public NamingEnumeration listBindings(Name name) throws NamingException { if (name.size() == 1) { return listBindings(name.get(0)); } else { Context ctx = getContinuationContext(name); try { return ctx.listBindings(name.getSuffix(1)); } finally { ctx.close(); } } }
public void destroySubcontext(Name name) throws NamingException { if (name.size() == 1) { destroySubcontext(name.get(0)); } else { Context ctx = getContinuationContext(name); try { ctx.destroySubcontext(name.getSuffix(1)); } finally { ctx.close(); } } }
public Object lookupLink(Name name) throws NamingException { if (name.size() == 1) { return lookupLink(name.get(0)); } else { Context ctx = getContinuationContext(name); try { return ctx.lookupLink(name.getSuffix(1)); } finally { ctx.close(); } } }
public NameParser getNameParser(Name name) throws NamingException { if (name.size() == 1) { return getNameParser(name.get(0)); } else { Context ctx = getContinuationContext(name); try { return ctx.getNameParser(name.getSuffix(1)); } finally { ctx.close(); } } }
public Context createSubcontext(Name name) throws NamingException { if (name.size() == 1) { return createSubcontext(name.get(0)); } else { Context ctx = getContinuationContext(name); try { return ctx.createSubcontext(name.getSuffix(1)); } finally { ctx.close(); } } }
protected String getComponentName(Name name) throws NamingException { if (name instanceof CompositeName) { if (name.size() > 1) { throw new InvalidNameException( name.toString() + " has more components than namespace can handle"); } return name.get(0); } else { // compound name return name.toString(); } }