/** * Clear the directory sub-tree starting with the node represented by the supplied distinguished * name. * * @param ctx The DirContext to use for cleaning the tree. * @param name the distinguished name of the root node. * @throws NamingException if anything goes wrong removing the sub-tree. */ public static void clearSubContexts(DirContext ctx, Name name) throws NamingException { NamingEnumeration enumeration = null; try { enumeration = ctx.listBindings(name); while (enumeration.hasMore()) { Binding element = (Binding) enumeration.next(); Name childName = LdapUtils.newLdapName(element.getName()); childName = LdapUtils.prepend(childName, name); try { ctx.destroySubcontext(childName); } catch (ContextNotEmptyException e) { clearSubContexts(ctx, childName); ctx.destroySubcontext(childName); } } } catch (NamingException e) { e.printStackTrace(); } finally { try { enumeration.close(); } catch (Exception e) { // Never mind this } } }
/* ------------------------------------------------------------ */ public void dump(Appendable out, String indent) throws IOException { out.append(this.getClass().getSimpleName()) .append("@") .append(Long.toHexString(this.hashCode())) .append("\n"); int size = _bindings.size(); int i = 0; for (Map.Entry<String, Binding> entry : ((Map<String, Binding>) _bindings).entrySet()) { boolean last = ++i == size; out.append(indent).append(" +- ").append(entry.getKey()).append(": "); Binding binding = entry.getValue(); Object value = binding.getObject(); if ("comp".equals(entry.getKey()) && value instanceof Reference && "org.eclipse.jetty.jndi.ContextFactory" .equals(((Reference) value).getFactoryClassName())) { ContextFactory.dump(out, indent + (last ? " " : " | ")); } else if (value instanceof Dumpable) { ((Dumpable) value).dump(out, indent + (last ? " " : " | ")); } else { out.append(value.getClass().getSimpleName()).append("="); out.append(String.valueOf(value).replace('\n', '|').replace('\r', '|')); out.append("\n"); } } }
/** * Create the MBeans for the interesting global JNDI resources in the specified naming context. * * @param prefix Prefix for complete object name paths * @param context Context to be scanned * @exception NamingException if a JNDI exception occurs */ protected void createMBeans(String prefix, Context context) throws NamingException { if (debug >= 1) { log("Creating MBeans for Global JNDI Resources in Context '" + prefix + "'"); } NamingEnumeration bindings = context.listBindings(""); while (bindings.hasMore()) { Binding binding = (Binding) bindings.next(); String name = prefix + binding.getName(); Object value = context.lookup(binding.getName()); if (debug >= 2) { log("Checking resource " + name); } if (value instanceof Context) { createMBeans(name + "/", (Context) value); } else if (value instanceof UserDatabase) { try { createMBeans(name, (UserDatabase) value); } catch (Exception e) { log("Exception creating UserDatabase MBeans for " + name, e); } } } }
/** * Constructs a JNDI Binding object from the COS Naming binding object. * * @exception NameNotFound No objects under the name. * @exception CannotProceed Unable to obtain a continuation context * @exception InvalidName Name not understood. * @exception NamingException One of the above. */ private javax.naming.Binding mapBinding(org.omg.CosNaming.Binding bndg) throws NamingException { java.lang.Object obj = _ctx.callResolve(bndg.binding_name); Name cname = CNNameParser.cosNameToName(bndg.binding_name); try { obj = NamingManager.getObjectInstance(obj, cname, _ctx, _env); } catch (NamingException e) { throw e; } catch (Exception e) { NamingException ne = new NamingException("problem generating object using object factory"); ne.setRootCause(e); throw ne; } // Use cname.toString() instead of bindingName because the name // in the binding should be a composite name String cnameStr = cname.toString(); javax.naming.Binding jbndg = new javax.naming.Binding(cnameStr, obj); NameComponent[] comps = _ctx.makeFullName(bndg.binding_name); String fullName = CNNameParser.cosNameToInsString(comps); jbndg.setNameInNamespace(fullName); return jbndg; }
@SuppressWarnings("unchecked") void jndiInJEE() throws NamingException { @SuppressWarnings("rawtypes") Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); InitialContext context = new InitialContext(env); DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/AccountDS"); NamingEnumeration<Binding> bindings = context.listBindings("java:comp/env/jdbc"); while (bindings.hasMore()) { Binding bd = (Binding) bindings.next(); System.out.println("Name = " + bd.getName() + ", Object = " + bd.getObject()); } context.bind("java:comp/env/jdbc/AccountDS", ds); // Directory search InitialDirContext dirContext = new InitialDirContext(); SearchControls control = new SearchControls(); ds = (DataSource) dirContext.search("java:comp/env/jdbc/AccountDS", "(ver=1.1)", control); // Or... BasicAttributes attrs = new BasicAttributes(); attrs.put(new BasicAttribute("ver", "1.1")); ds = (DataSource) dirContext.search("java:comp/env/jdbc/AccountDS", attrs); }
/** * 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"); } }
/** * 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 static void unbind(Context ctx) throws NamingException { // unbind everything in the context and all of its subdirectories NamingEnumeration ne = ctx.listBindings(ctx.getNameInNamespace()); while (ne.hasMoreElements()) { Binding b = (Binding) ne.nextElement(); if (b.getObject() instanceof Context) { unbind((Context) b.getObject()); } else ctx.unbind(b.getName()); } }
/** List resource paths (recursively), and store all of them in the given Set. */ private static void listPaths(Set set, DirContext resources, String path) throws NamingException { Enumeration childPaths = resources.listBindings(path); while (childPaths.hasMoreElements()) { Binding binding = (Binding) childPaths.nextElement(); String name = binding.getName(); String childPath = path + "/" + name; set.add(childPath); Object object = binding.getObject(); if (object instanceof DirContext) { listPaths(set, resources, childPath); } } }
/** * after each test we should scrape out any lingering bindings to prevent cross test pollution as * observed when running java 7 * * @throws Exception on test failure */ @After public void after() throws Exception { InitialContext icontext = new InitialContext(); NamingEnumeration<Binding> bindings = icontext.listBindings(""); List<String> names = new ArrayList<String>(); while (bindings.hasMore()) { Binding bd = (Binding) bindings.next(); names.add(bd.getName()); } for (String name : names) { icontext.unbind(name); } }
/** List resource paths (recursively), and store all of them in the given Set. */ private static void listCollectionPaths(Set set, DirContext resources, String path) throws NamingException { Enumeration childPaths = resources.listBindings(path); while (childPaths.hasMoreElements()) { Binding binding = (Binding) childPaths.nextElement(); String name = binding.getName(); StringBuffer childPath = new StringBuffer(path); if (!"/".equals(path) && !path.endsWith("/")) childPath.append("/"); childPath.append(name); Object object = binding.getObject(); if (object instanceof DirContext) { childPath.append("/"); } set.add(childPath.toString()); } }
/** * 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.iterator()); } if (cname.size() == 0) { return new BindingEnumeration(_bindings.values().iterator()); } // 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 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.iterator()); } if (cname.size() == 0) { return new NameEnumeration(_bindings.values().iterator()); } // 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)); }
/** * Do a deep listing of the bindings for a context. * * @param ctx the context containing the name for which to list the bindings * @param name the name in the context to list * @return map: key is fully qualified name, value is the bound object * @throws NamingException */ public static Map flattenBindings(Context ctx, String name) throws NamingException { HashMap map = new HashMap(); // the context representation of name arg Context c = (Context) ctx.lookup(name); NameParser parser = c.getNameParser(""); NamingEnumeration enm = ctx.listBindings(name); while (enm.hasMore()) { Binding b = (Binding) enm.next(); if (b.getObject() instanceof Context) { map.putAll(flattenBindings(c, b.getName())); } else { Name compoundName = parser.parse(c.getNameInNamespace()); compoundName.add(b.getName()); map.put(compoundName.toString(), b.getObject()); } } return map; }
@SuppressWarnings("unchecked") void jndiInOsgi() throws NamingException { ServiceReference ref = bundleContext.getServiceReference(JNDIContextManager.class.getName()); JNDIContextManager ctxtMgr = (JNDIContextManager) bundleContext.getService(ref); @SuppressWarnings("rawtypes") Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); Context context = ctxtMgr.newInitialContext(env); DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/AccountDS"); NamingEnumeration<Binding> bindings = context.listBindings("java:comp/env/jdbc"); while (bindings.hasMore()) { Binding bd = (Binding) bindings.next(); System.out.println("Name = " + bd.getName() + ", Object = " + bd.getObject()); } }
private static void listContext(String indent, PrintWriter out, Context ctx, String ctxName) throws NamingException { NamingEnumeration<Binding> ne = ctx.listBindings(""); while (ne.hasMore()) { Binding ncp = ne.next(); String name = ncp.getName(); if (name.startsWith(ctxName)) { name = name.substring(ctxName.length() + 1); } out.println(indent + name + " => " + ncp.getClassName()); if (ncp.getObject() instanceof Context) { listContext(indent + CommonTest.indent, out, (Context) ncp.getObject(), ncp.getName()); } } }
/** * busca todos os arquivos com o mesmo nome * * @param ctx * @param i * @param busca * @throws NamingException */ private static void buscaTodosArquivo(Context ctx, int i, String busca) throws NamingException { NamingEnumeration ne = ctx.listBindings(""); while (ne.hasMore()) { Binding coisa = (Binding) ne.next(); if (coisa.getName().equals(busca)) { System.out.println( "Achou : " + coisa.getName() + " -> " + coisa.getObject().getClass().getName()); achou = true; } if (coisa.getObject().getClass().getName().equals("com.sun.jndi.fscontext.FSContext")) { buscaTodosArquivo((Context) coisa.getObject(), i + 2, busca); } } }
/** * 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"); } }
public Binding next() throws NamingException { Binding b = (Binding) _delegate.next(); return new Binding(b.getName(), b.getClassName(), b.getObject(), true); }
public Binding nextElement() { Binding b = (Binding) _delegate.next(); return new Binding(b.getName(), b.getClassName(), b.getObject(), true); }
/** * 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 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)); }