/** * Retrieves the named object. If name is empty, returns a new instance * of this context (which represents the same naming context as this * context, but its environment may be modified independently and it may * be accessed concurrently). * * @param name the name of the object to look up * @return the object bound to name * @exception NamingException if a naming exception is encountered */ public Object lookup(Name name) throws NamingException { CacheEntry entry = cacheLookup(name.toString()); if (entry != null) { if (!entry.exists) { throw notFoundException; } if (entry.resource != null) { // Check content caching. return entry.resource; } else { return entry.context; } } Object object = dirContext.lookup(parseName(name)); if (object instanceof InputStream) return new Resource((InputStream) object); else return object; }
/** * Enumerates the names bound in the named context, along with the class * names of objects bound to them. The contents of any subcontexts are * not included. * <p> * If a binding is added to or removed from this context, its effect on * an enumeration previously returned is undefined. * * @param name the name of the context to list * @return an enumeration of the names and class names of the bindings in * this context. Each element of the enumeration is of type NameClassPair. * @exception NamingException if a naming exception is encountered */ public NamingEnumeration list(Name name) throws NamingException { // Removing empty parts while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) { return new NamingContextEnumeration(bindings.values().iterator()); } NamingEntry entry = (NamingEntry) bindings.get(name.get(0)); if (entry == null) { throw new NameNotFoundException (sm.getString("namingContext.nameNotBound", name.get(0))); } if (entry.type != NamingEntry.CONTEXT) { throw new NamingException (sm.getString("namingContext.contextExpected")); } return ((Context) entry.value).list(name.getSuffix(1)); }
private boolean matches(int beg, int end, Name n) { if (n instanceof LdapName) { LdapName ln = (LdapName) n; return doesListMatch(beg, end, ln.rdns); } else { for (int i = beg; i < end; i++) { Rdn rdn; String rdnString = n.get(i - beg); try { rdn = (new Rfc2253Parser(rdnString)).parseRdn(); } catch (InvalidNameException e) { return false; } if (!rdn.equals(rdns.get(i))) { return false; } } } return true; }
/** * Retrieves all of the attributes associated with a named object. * * @return the set of attributes associated with name. * Returns an empty attribute set if name has no attributes; never null. * @param name the name of the object from which to retrieve attributes * @exception NamingException if a naming exception is encountered */ public Attributes getAttributes(Name name, String[] attrIds) throws NamingException { Entry entry = null; if (name.isEmpty()) entry = entries; else entry = treeLookup(name); if (entry == null) throw new NamingException (sm.getString("resources.notFound", name)); ZipEntry zipEntry = entry.getEntry(); ResourceAttributes attrs = new ResourceAttributes(); attrs.setCreationDate(new Date(zipEntry.getTime())); attrs.setName(entry.getName()); if (!zipEntry.isDirectory()) attrs.setResourceType(""); attrs.setContentLength(zipEntry.getSize()); attrs.setLastModified(new Date(zipEntry.getTime())); return attrs; }
/** * Enumerates the names bound in the named context, along with the * objects bound to them. The contents of any subcontexts are not * included. * <p> * If a binding is added to or removed from this context, its effect on * an enumeration previously returned is undefined. * * @param strName the name of the context to list * @return an enumeration of the bindings in this context. * Each element of the enumeration is of type Binding. * @exception NamingException if a naming exception is encountered */ @Override protected List<NamingEntry> doListBindings(String strName) throws NamingException { Name name = getEscapedJndiName(strName); if (name.isEmpty()) return list(entries); Entry entry = treeLookup(name); if (entry == null) return null; return list(entry); }
public LDAPResult getGroupResult(DirContext ctx, String groupID, String[] attrs) { if( !Check.isEmpty(groupIdField) ) { SingleFilter nv1 = new SingleFilter(OBJECTCLASS, getGroupObject()); SingleFilter nv2 = new SingleFilter(groupIdField, groupID); return searchFirstResultAllBases(ctx, new AndFilter(nv1, nv2), new LdapResultHitsCollector(attrs), true); } try { Name name = LDAP.parse(groupID); return new LDAPResult(name, getAttributes(ctx, name, attrs)); } catch( InvalidNameException e ) { LOGGER.debug(e, e); return null; } }
private void collectAllSubGroupFullNames(Set<String> results, DirContext ctx, Name parent) { final String fullname = parent.toString(); if( results.add(fullname) ) { for( Name child : ldap.searchAllBases(ctx, getSubgroupsByMemberOfFilter(fullname), new FullNameHitsCollector(), true) ) { collectAllSubGroupFullNames(results, ctx, child); } } }
/** * Creates and binds a new context. Creates a new context with the given * name and binds it in the target context (that named by all but * terminal atomic component of the name). All intermediate contexts and * the target context must already exist. * * @param name the name of the context to create; may not be empty * @return the newly created context * @exception NameAlreadyBoundException if name is already bound * @exception javax.naming.directory.InvalidAttributesException if creation * of the sub-context requires specification of mandatory attributes * @exception NamingException if a naming exception is encountered */ @Override public Context createSubcontext(Name name) throws NamingException { if (!checkWritable()) { return null; } NamingContext newContext = new NamingContext(env, this.name); bind(name, newContext); newContext.setExceptionOnFailedWrite(getExceptionOnFailedWrite()); return newContext; }
@Override public Object lookup(Name name) throws NamingException { if (name.isEmpty()) return cloneCtx(); Name nm = getMyComponents(name); String atom = nm.get(0); Object inter = iBindings.get(atom); if (nm.size() == 1) { if (inter == null) throw new NameNotFoundException(name + " not found"); try { return NamingManager.getObjectInstance(inter, new CompositeName().add(atom), this, iEnv); } catch (Exception e) { NamingException ne = new NamingException("getObjectInstance failed"); ne.setRootCause(e); throw ne; } } else { if (!(inter instanceof Context)) throw new NotContextException(atom + " does not name a context"); return ((Context) inter).lookup(nm.getSuffix(1)); } }
@Override public void rename(Name oldname, Name newname) throws NamingException { if (oldname.isEmpty() || newname.isEmpty()) throw new InvalidNameException("Cannot rename empty name"); Name oldnm = getMyComponents(oldname); Name newnm = getMyComponents(newname); if (oldnm.size() != newnm.size()) throw new OperationNotSupportedException("Do not support rename across different contexts"); String oldatom = oldnm.get(0); String newatom = newnm.get(0); if (oldnm.size() == 1) { if (iBindings.get(newatom) != null) throw new NameAlreadyBoundException(newname.toString() + " is already bound"); Object oldBinding = iBindings.remove(oldatom); if (oldBinding == null) throw new NameNotFoundException(oldname.toString() + " not bound"); iBindings.put(newatom, oldBinding); } else { if (!oldatom.equals(newatom)) throw new OperationNotSupportedException("Do not support rename across different contexts"); Object inter = iBindings.get(oldatom); if (!(inter instanceof Context)) throw new NotContextException(oldatom + " does not name a context"); ((Context) inter).rename(oldnm.getSuffix(1), newnm.getSuffix(1)); } }
/** * Retrieves the named object. If name is empty, returns a new instance * of this context (which represents the same naming context as this * context, but its environment may be modified independently and it may * be accessed concurrently). * * @param name the name of the object to look up * @return the object bound to name * @exception NamingException if a naming exception is encountered */ public Object lookup(Name name) throws NamingException { if (name.isEmpty()) return this; Entry entry = treeLookup(name); if (entry == null) throw new NamingException (sm.getString("resources.notFound", name)); ZipEntry zipEntry = entry.getEntry(); if (zipEntry.isDirectory()) return new WARDirContext(base, entry); else return new WARResource(entry.getEntry()); }
@Override public NamingEnumeration list(Name name) throws NamingException { if (name.isEmpty()) return new ListOfNames(iBindings.keys()); Object target = lookup(name); if (target instanceof Context) return ((Context) target).list(""); throw new NotContextException(name + " cannot be listed"); }
@Override public NamingEnumeration listBindings(Name name) throws NamingException { if (name.isEmpty()) return new ListOfBindings(iBindings.keys()); Object target = lookup(name); if (target instanceof Context) return ((Context) target).listBindings(""); throw new NotContextException(name + " cannot be listed"); }
/** * Entry tree lookup. */ protected Entry treeLookup(Name name) { if (name.isEmpty()) 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; }
/** * 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 DirContextNamePair getTargetContext(Name name) throws NamingException { if (cpe.getResolvedObj() == null) throw (NamingException)cpe.fillInStackTrace(); Context ctx = NamingManager.getContext(cpe.getResolvedObj(), cpe.getAltName(), cpe.getAltNameCtx(), env); if (ctx == null) throw (NamingException)cpe.fillInStackTrace(); if (ctx instanceof DirContext) return new DirContextNamePair((DirContext)ctx, name); if (ctx instanceof Resolver) { Resolver res = (Resolver)ctx; ResolveResult rr = res.resolveToClass(name, DirContext.class); // Reached a DirContext; return result. DirContext dctx = (DirContext)rr.getResolvedObj(); return (new DirContextNamePair(dctx, rr.getRemainingName())); } // Resolve all the way using lookup(). This may allow the operation // to succeed if it doesn't require the penultimate context. Object ultimate = ctx.lookup(name); if (ultimate instanceof DirContext) { return (new DirContextNamePair((DirContext)ultimate, new CompositeName())); } throw (NamingException)cpe.fillInStackTrace(); }
@Override public NameParser getNameParser(Name name) throws NamingException { Object obj = lookup(name); if (obj instanceof Context) ((Context) obj).close(); return this; }
@Override public void unbind(String jndiName) { final InitialContext initialContext = buildInitialContext(); final Name name = parseName( jndiName, initialContext ); try { initialContext.unbind( name ); } catch (Exception e) { throw new JndiException( "Error performing unbind [" + name + "]", e ); } finally { cleanUp( initialContext ); } }
/** * Create a new EJB instance using OpenEJB. * * @param obj * The reference object describing the DataSource */ @Override public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception { Object beanObj = null; if (obj instanceof EjbRef) { Reference ref = (Reference) obj; String factory = DEFAULT_OPENEJB_FACTORY; RefAddr factoryRefAddr = ref.get("openejb.factory"); if (factoryRefAddr != null) { // Retrieving the OpenEJB factory factory = factoryRefAddr.getContent().toString(); } Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, factory); RefAddr linkRefAddr = ref.get("openejb.link"); if (linkRefAddr != null) { String ejbLink = linkRefAddr.getContent().toString(); beanObj = (new InitialContext(env)).lookup(ejbLink); } } return beanObj; }
/** * <p> * Create and return a new <code>MemoryUserDatabase</code> instance that has * been configured according to the properties of the specified * <code>Reference</code>. If you instance can be created, return * <code>null</code> instead. * </p> * * @param obj * The possibly null object containing location or reference * information that can be used in creating an object * @param name * The name of this object relative to <code>nameCtx</code> * @param nameCtx * The context relative to which the <code>name</code> parameter * is specified, or <code>null</code> if <code>name</code> is * relative to the default initial context * @param environment * The possibly null environment that is used in creating this * object */ @Override public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception { // We only know how to deal with <code>javax.naming.Reference</code>s // that specify a class name of "org.apache.catalina.UserDatabase" if ((obj == null) || !(obj instanceof Reference)) { return (null); } Reference ref = (Reference) obj; if (!"org.apache.catalina.UserDatabase".equals(ref.getClassName())) { return (null); } // Create and configure a MemoryUserDatabase instance based on the // RefAddr values associated with this Reference MemoryUserDatabase database = new MemoryUserDatabase(name.toString()); RefAddr ra = null; ra = ref.get("pathname"); if (ra != null) { database.setPathname(ra.getContent().toString()); } ra = ref.get("readonly"); if (ra != null) { database.setReadonly(Boolean.parseBoolean(ra.getContent().toString())); } // Return the configured database instance database.open(); // Don't try something we know won't work if (!database.getReadonly()) database.save(); return (database); }
/** * Creates subcontext with name, relative to this Context. * * @param name subcontext name. * @return new subcontext named name relative to this context. * @throws NoPermissionException if this context has been destroyed. * @throws InvalidNameException if name is empty or is CompositeName that spans more than one * naming system. * @throws NameAlreadyBoundException if name is already bound in this Context * @throws NotContextException if any intermediate name from name is not bound to instance of * javax.naming.Context. * */ public Context createSubcontext(Name name) throws NamingException { checkIsDestroyed(); Name parsedName = getParsedName(name); if (parsedName.size() == 0 || parsedName.get(0).length() == 0) { throw new InvalidNameException( LocalizedStrings.ContextImpl_NAME_CAN_NOT_BE_EMPTY.toLocalizedString()); } String subContextName = parsedName.get(0); Object boundObject = ctxMaps.get(parsedName.get(0)); if (parsedName.size() == 1) { // Check if name is already in use if (boundObject == null) { Context subContext = new ContextImpl(this, subContextName); ctxMaps.put(subContextName, subContext); return subContext; } else { throw new NameAlreadyBoundException( LocalizedStrings.ContextImpl_NAME_0_IS_ALREADY_BOUND.toLocalizedString(subContextName)); } } else { if (boundObject instanceof Context) { // Let the subcontext create new subcontext // lets consider a scenerio a/b/c // case a/b exists : c will be created // case a exists : b/c will not be created // an exception will be thrown in that case. return ((Context) boundObject).createSubcontext(parsedName.getSuffix(1)); } else { throw new NotContextException(LocalizedStrings.ContextImpl_EXPECTED_CONTEXT_BUT_FOUND_0 .toLocalizedString(boundObject)); } } }
public LDAPResult(SearchResult sr, Name base) throws InvalidNameException { attributes = sr.getAttributes(); // getNameInNamespace, otherwise you get parsing problems fullName = parse(sr.getNameInNamespace()); this.baseName = base; }
@Override public NamingEnumeration<NameClassPair> list(Name name) throws NamingException { return new NamingEnumerationStub(); }
DirContextNamePair(DirContext ctx, Name name) { this.ctx = ctx; this.name = name; }
public void rebind(Name name, Object obj, Attributes attrs) throws NamingException { DirContextNamePair res = getTargetContext(name); res.getDirContext().rebind(res.getName(), obj, attrs); }
public Attributes getAttributes(Name name) throws NamingException { DirContextNamePair res = getTargetContext(name); return res.getDirContext().getAttributes(res.getName()); }
void search(Name base, String filter, SearchControls controls, NameClassPairCallbackHandler handler, DirContextProcessor processor) throws NamingException;
@Override @SuppressWarnings("unchecked") public NamingEnumeration<SearchResult> search(Name name, String filterExpr, Object[] filterArgs, SearchControls cons) throws NamingException { return emptyEnum; }
@Override @SuppressWarnings("unchecked") public NamingEnumeration<SearchResult> search(Name name, Attributes matchingAttributes, String[] attributesToReturn) throws NamingException { return emptyEnum; }
@Override public NamingEnumeration<NameClassPair> list(Name name) throws NamingException { return null; }
public DirContext createSubcontext(Name name, Attributes attrs) throws NamingException { DirContextNamePair res = getTargetContext(name); return res.getDirContext().createSubcontext(res.getName(), attrs); }
/** * <p>Create and return a new <code>BasicDataSource</code> instance. If no * instance can be created, return <code>null</code> instead.</p> * * @param obj The possibly null object containing location or * reference information that can be used in creating an object * @param name The name of this object relative to <code>nameCtx</code> * @param nameCtx The context relative to which the <code>name</code> * parameter is specified, or <code>null</code> if <code>name</code> * is relative to the default initial context * @param environment The possibly null environment that is used in * creating this object * * @exception Exception if an exception occurs creating the instance */ @Override public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?,?> environment) throws Exception { // We only know how to deal with <code>javax.naming.Reference</code>s // that specify a class name of "javax.sql.DataSource" if ((obj == null) || !(obj instanceof Reference)) { return null; } Reference ref = (Reference) obj; boolean XA = false; boolean ok = false; if ("javax.sql.DataSource".equals(ref.getClassName())) { ok = true; } if ("javax.sql.XADataSource".equals(ref.getClassName())) { ok = true; XA = true; } if (org.apache.tomcat.jdbc.pool.DataSource.class.getName().equals(ref.getClassName())) { ok = true; } if (!ok) { log.warn(ref.getClassName()+" is not a valid class name/type for this JNDI factory."); return null; } Properties properties = new Properties(); for (int i = 0; i < ALL_PROPERTIES.length; i++) { String propertyName = ALL_PROPERTIES[i]; RefAddr ra = ref.get(propertyName); if (ra != null) { String propertyValue = ra.getContent().toString(); properties.setProperty(propertyName, propertyValue); } } return createDataSource(properties,nameCtx,XA); }
/** * Create a new DataSource instance. * * @param obj * The reference object describing the DataSource */ @Override public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws NamingException { if (!(obj instanceof ResourceLinkRef)) { return null; } // Can we process this request? Reference ref = (Reference) obj; // Read the global ref addr String globalName = null; RefAddr refAddr = ref.get(ResourceLinkRef.GLOBALNAME); if (refAddr != null) { globalName = refAddr.getContent().toString(); // Confirm that the current web application is currently configured // to access the specified global resource if (!validateGlobalResourceAccess(globalName)) { return null; } Object result = null; result = globalContext.lookup(globalName); // Check the expected type String expectedClassName = ref.getClassName(); if (expectedClassName == null) { throw new IllegalArgumentException(sm.getString("resourceLinkFactory.nullType", name, globalName)); } try { Class<?> expectedClazz = Class.forName(expectedClassName, true, Thread.currentThread().getContextClassLoader()); if (!expectedClazz.isAssignableFrom(result.getClass())) { throw new IllegalArgumentException(sm.getString("resourceLinkFactory.wrongType", name, globalName, expectedClassName, result.getClass().getName())); } } catch (ClassNotFoundException e) { throw new IllegalArgumentException( sm.getString("resourceLinkFactory.unknownType", name, globalName, expectedClassName), e); } return result; } return null; }
@Override @SuppressWarnings("unchecked") public NamingEnumeration<SearchResult> search(Name name, Attributes matchingAttributes) throws NamingException { return emptyEnum; }
public Name getHead() { return this.head; }
public DirContext getSchemaClassDefinition(Name name) throws NamingException { DirContextNamePair res = getTargetContext(name); return res.getDirContext().getSchemaClassDefinition(res.getName()); }
@Override public DirContext getSchema(Name name) throws NamingException { return this; }
@Override public DirContext getSchemaClassDefinition(Name name) throws NamingException { return this; }
@Override public void rename(Name oldName, Name newName) throws NamingException { // no op }
/** * 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 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 { checkWritable(); while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) throw new NamingException (sm.getString("namingContext.invalidName")); NamingEntry entry = (NamingEntry) bindings.get(name.get(0)); if (name.size() > 1) { if (entry == null) { throw new NameNotFoundException (sm.getString("namingContext.nameNotBound", 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); } } }