Example usage for javax.naming Context listBindings

List of usage examples for javax.naming Context listBindings

Introduction

In this page you can find the example usage for javax.naming Context listBindings.

Prototype

public NamingEnumeration<Binding> listBindings(String name) throws NamingException;

Source Link

Document

Enumerates the names bound in the named context, along with the objects bound to them.

Usage

From source file:ListBindings.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {//w  w w.  j av a 2  s . c  o m
        // Create the initial context
        Context ctx = new InitialContext(env);

        // Get listing of context
        NamingEnumeration bindings = ctx.listBindings("ou=People");

        // Go through each item in list
        while (bindings.hasMore()) {
            Binding bd = (Binding) bindings.next();
            System.out.println(bd.getName() + ": " + bd.getObject());
        }

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        System.out.println("List Bindings failed: " + e);
    }
}

From source file:JNDIUtil.java

public static void tearDownRecursively(Context c) throws Exception {
    for (NamingEnumeration ne = c.listBindings(""); ne.hasMore();) {
        Binding b = (Binding) ne.next();
        String name = b.getName();
        Object object = b.getObject();
        if (object instanceof Context) {
            tearDownRecursively((Context) object);
        }/*from   ww  w . j  av  a  2  s  .c o  m*/
        c.unbind(name);
    }
}

From source file:de.micromata.genome.util.runtime.jndi.JndiDumper.java

public static void dumpJndiBinding(InitialContext initialContext, String parentKey, Binding binding,
        StringBuilder sb, String indent) throws NamingException {
    try {//from w w w  .j  av a  2s.c  o  m
        Object obj = binding.getObject();
        ClassLoader bindClassLoader = obj.getClass().getClassLoader();
        sb.append(indent).append(parentKey + "/" + binding.getName()).append("(")
                .append(System.identityHashCode(binding)).append(", cl: ").append(bindClassLoader).append("): ")
                .append(binding.toString());
        if (obj instanceof Context) {

            sb.append("\n");
            Context ctx = (Context) obj;
            indent += "  ";
            NamingEnumeration<Binding> bindings = ctx.listBindings("");
            while (bindings.hasMore()) {
                Binding cbinding = bindings.next();
                dumpJndiBinding(initialContext, parentKey + "/" + binding.getName(), cbinding, sb, indent);
            }
        } else if (obj instanceof Reference) {
            Reference ref = (Reference) obj;
            //      binding.get
            //      ref.
            sb.append("\n");
        } else if (obj instanceof Session) {
            Session sess = (Session) obj;
            sb.append("\n");
        } else if (obj instanceof DataSource) {
            DataSource ds = (DataSource) obj;
            if (ds instanceof BasicDataSource) {
                BasicDataSource dbds = (BasicDataSource) ds;
                sb.append(" '" + dbds.getUrl() + "'");
            }
            sb.append("\n");
        } else if (obj != null) {
            Class<? extends Object> clazz = obj.getClass();

            sb.append(" unkown type: " + clazz);
            sb.append("\n");
        }
    } catch (NamingException ex) {
        sb.append("Error access binding: " + binding.getName() + ": " + ex.getMessage());
    }
}

From source file:com.ironiacorp.persistence.datasource.HibernateConfigurationUtil.java

/**
 * Retrieve the data sources registered at JNDI in the given context.
 * /*from  w  w w  .j  av a2s  .  c om*/
 * @param namingContext
 *            Start point context.
 * 
 * @return A collection of the data sources found within the context.
 */
private static Collection<String> getAvailableDataSources(Context namingContext) {
    Collection<String> datasources = new ArrayList<String>();
    Class<?> type = null;

    // Acquire global JNDI resources if available
    try {
        type = Class.forName("xyz");
    } catch (ClassNotFoundException e) {
    }

    try {
        NamingEnumeration<Binding> items = namingContext.listBindings("");
        while (items.hasMore()) {
            Binding item = (Binding) items.next();
            if (item.getObject() instanceof Context) {
                datasources.addAll(getAvailableDataSources((Context) item.getObject()));
            } else {
                if (type.isInstance(item.getObject())) {
                    datasources.add(item.getName());
                }
            }
        }
    } catch (Throwable t) {
    }

    return datasources;
}

From source file:EnvEntry.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();

    try {/* w  w  w.  ja v a2s .co  m*/
        Context initCtx = new InitialContext();
        NamingEnumeration e = initCtx.listBindings("java:comp/env");

        while (e.hasMore()) {
            Binding binding = (Binding) e.next();
            out.println("Name: " + binding.getName());
            out.println("Type: " + binding.getClassName());
            out.println("Value: " + binding.getObject());
            out.println();
        }
    } catch (NamingException e) {
        e.printStackTrace(out);
    }
}

From source file:binky.reportrunner.ui.actions.datasource.SaveDataSource.java

private List<String> listJNDINames(Context ctx, String ident) throws NamingException {
    List<String> names = new LinkedList<String>();

    NamingEnumeration<Binding> list = ctx.listBindings("");
    while (list.hasMore()) {
        Binding item = (Binding) list.next();

        String name = item.getName();

        Object o = item.getObject();
        if (o instanceof javax.naming.Context) {
            names.addAll(listJNDINames((Context) o, name));
        } else {/*w w w . j  a v  a2s .  co  m*/
            names.add(ident + "/" + name);
        }
    }

    return names;
}

From source file:binky.reportrunner.ui.actions.datasource.SetupEditJNDIDataSource.java

private List<String> listJNDINames(Context ctx, String ident) throws NamingException {
    List<String> names = new LinkedList<String>();

    String indent = "";

    NamingEnumeration<Binding> list = ctx.listBindings("");
    while (list.hasMore()) {
        Binding item = (Binding) list.next();
        String className = item.getClassName();
        String name = item.getName();
        logger.debug(indent + className + " " + name);
        Object o = item.getObject();
        if (o instanceof javax.naming.Context) {
            names.addAll(listJNDINames((Context) o, name));
        } else {/* w  w w .j  a va  2s. c  om*/
            names.add(ident + "/" + name);
        }
    }

    return names;
}

From source file:org.picketlink.idm.performance.TestBase.java

public void removeContext(Context mainCtx, String name) throws Exception {
    Context deleteCtx = (Context) mainCtx.lookup(name);
    NamingEnumeration subDirs = mainCtx.listBindings(name);

    while (subDirs.hasMoreElements()) {
        Binding binding = (Binding) subDirs.nextElement();
        String subName = binding.getName();

        removeContext(deleteCtx, subName);
    }//from  w  ww. ja va  2 s.  c o  m

    mainCtx.unbind(name);
}

From source file:jndi.view.JndiView.java

/**
 * @param path/*w  ww  .  j a v a  2  s  .c o m*/
 *        the path to browse
 * @return {@link List} of {@link JndiEntry}s
 * @throws NamingException
 *         on exception
 */
private List<JndiEntry> browse(final String path) throws NamingException {
    final JndiCallback<List<JndiEntry>> contextCallback = new JndiCallback<List<JndiEntry>>() {
        @Override
        public List<JndiEntry> doInContext(final Context context) throws NamingException {
            if (JAVA_GLOBAL.equals(path)) {
                // Do a little trick to handle "java:global"
                final NamingEnumeration<Binding> root = context.listBindings("");
                Context javaGlobalContext = null;
                while (root.hasMore()) {
                    final Binding binding = root.next();
                    if (JAVA_GLOBAL.equals(binding.getName())) {
                        final Object obj = binding.getObject();
                        if (obj instanceof Context) {
                            javaGlobalContext = (Context) obj;
                        }
                        break;
                    }
                }
                if (javaGlobalContext != null) {
                    return examineBindings(javaGlobalContext, path, javaGlobalContext.listBindings(""));
                }
                logger.warning("Unable to browse \"" + JAVA_GLOBAL + "\" namespace!");
                return emptyList();
            }
            return examineBindings(context, path, context.listBindings(path));
        }
    };
    return jndiTemplate.execute(contextCallback);
}

From source file:catalina.mbeans.GlobalResourcesLifecycleListener.java

/**
 * Create the MBeans for the interesting global JNDI resources in
 * the specified naming context.//from  www . j  a  v  a  2  s .  c  o  m
 *
 * @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);
            }
        }
    }

}