Example usage for javax.naming Binding getName

List of usage examples for javax.naming Binding getName

Introduction

In this page you can find the example usage for javax.naming Binding getName.

Prototype

public String getName() 

Source Link

Document

Retrieves the name of this binding.

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 {/*from w  w  w  . ja  v  a2 s  .c om*/
        // 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  w w  w  .j a  v a 2s. co m*/
        c.unbind(name);
    }
}

From source file:com.ktds.ldap.populator.LdapTestUtils.java

/**
 * Clear the directory sub-tree starting with the node represented by the
 * supplied distinguished name./*  w w w. jav a  2 s  .  c  o m*/
 *
 * @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.unbind(childName);
            } catch (ContextNotEmptyException e) {
                clearSubContexts(ctx, childName);
                ctx.unbind(childName);
            }
        }
    } catch (NamingException e) {
        LOGGER.debug("Error cleaning sub-contexts", e);
    } finally {
        try {
            enumeration.close();
        } catch (Exception e) {
            // Never mind this
        }
    }
}

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  . ja  va2s . 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.
 * //  ww w  .j  av a 2 s  .  c  o  m
 * @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.  j a  v  a  2 s  . c  om
        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 {//from   w  w w.ja  v  a  2s.com
            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 {//www  .ja v a  2 s.co m
            names.add(ident + "/" + name);
        }
    }

    return names;
}

From source file:jndi.view.JndiView.java

/**
 * @param ctx/*ww w .j a v  a2  s .  co m*/
 *        the Context we're examining
 * @param path
 *        the path to examine
 * @param bindings
 *        the {@link NamingEnumeration} of {@link Binding}s
 * @return List of {@link JndiEntry}
 * @throws NamingException
 *         on exception
 */
private List<JndiEntry> examineBindings(final Context ctx, final String path,
        final NamingEnumeration<Binding> bindings) throws NamingException {
    if (null == bindings) {
        throw new NullPointerException("bindings is null!");
    }
    final List<JndiEntry> entries = new ArrayList<JndiEntry>();
    while (bindings.hasMore()) {
        final Binding binding = bindings.next();
        final String name = binding.getName();
        final String className = binding.getClassName();

        logger.finest("name: " + name + " [" + className + "]");
        final JndiEntry entry = new JndiEntry(name, className);
        final Object obj = binding.getObject();
        if (obj instanceof Context) {
            entry.setContext(true);
            String link = name;
            if (!path.isEmpty()) {
                link = path + "/" + name;
            }
            entry.setLink(link);
        } else if (obj instanceof Reference) {
            final Reference ref = (Reference) obj;
            entry.setTargetClassName(ref.getClassName());
        } else if ("org.glassfish.javaee.services.ResourceProxy".equals(className)) {
            // SUPPRESS CHECKSTYLE AvoidInlineConditionals
            final Object lookup = ctx.lookup(path.isEmpty() ? name : path + "/" + name);
            if (lookup != null) {
                final String lookedUpClassName = lookup.getClass().getName();
                logger.finest("lookup(\"" + name + "\") returned " + lookedUpClassName);
                entry.setTargetClassName(lookedUpClassName);
            }
        } else if ("com.sun.ejb.containers.JavaGlobalJndiNamingObjectProxy".equals(className)) {
            inspectJndiNamingObjectProxy(entry, obj);
        }
        entries.add(entry);
    }
    return entries;
}

From source file:jndi.view.JndiView.java

/**
 * @param path/*from w  ww.  j a  v  a  2s .  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);
}