Example usage for javax.naming.directory DirContext listBindings

List of usage examples for javax.naming.directory DirContext listBindings

Introduction

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

Prototype

public NamingEnumeration<Binding> listBindings(Name 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:com.ktds.ldap.populator.LdapTestUtils.java

/**
 * Clear the directory sub-tree starting with the node represented by the
 * supplied distinguished name.//from www  .ja v  a 2  s. com
 *
 * @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:org.apache.catalina.util.ExtensionValidator.java

/**
 * Runtime validation of a Web Applicaiton.
 *
 * This method uses JNDI to look up the resources located under a 
 * <code>DirContext</code>. It locates Web Application MANIFEST.MF 
 * file in the /META-INF/ directory of the application and all 
 * MANIFEST.MF files in each JAR file located in the WEB-INF/lib 
 * directory and creates an <code>ArrayList</code> of 
 * <code>ManifestResorce<code> objects. These objects are then passed 
 * to the validateManifestResources method for validation.
 *
 * @param dirContext The JNDI root of the Web Application
 * @param context The context from which the Logger and path to the
 *                application//  ww  w . j ava  2s . c  o  m
 *
 * @return true if all required extensions satisfied
 */
public static synchronized boolean validateApplication(DirContext dirContext, StandardContext context)
        throws IOException {

    String appName = context.getPath();
    ArrayList appManifestResources = new ArrayList();
    ManifestResource appManifestResource = null;
    // If the application context is null it does not exist and 
    // therefore is not valid
    if (dirContext == null)
        return false;
    // Find the Manifest for the Web Applicaiton
    InputStream inputStream = null;
    try {
        NamingEnumeration wne = dirContext.listBindings("/META-INF/");
        Binding binding = (Binding) wne.nextElement();
        if (binding.getName().toUpperCase().equals("MANIFEST.MF")) {
            Resource resource = (Resource) dirContext.lookup("/META-INF/" + binding.getName());
            inputStream = resource.streamContent();
            Manifest manifest = new Manifest(inputStream);
            inputStream.close();
            inputStream = null;
            ManifestResource mre = new ManifestResource(
                    sm.getString("extensionValidator.web-application-manifest"), manifest,
                    ManifestResource.WAR);
            appManifestResources.add(mre);
        }
    } catch (NamingException nex) {
        // Application does not contain a MANIFEST.MF file
    } catch (NoSuchElementException nse) {
        // Application does not contain a MANIFEST.MF file
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Throwable t) {
                // Ignore
            }
        }
    }

    // Locate the Manifests for all bundled JARs
    NamingEnumeration ne = null;
    try {
        if (dirContext != null) {
            ne = dirContext.listBindings("WEB-INF/lib/");
        }
        while ((ne != null) && ne.hasMoreElements()) {
            Binding binding = (Binding) ne.nextElement();
            if (!binding.getName().toLowerCase().endsWith(".jar")) {
                continue;
            }
            Resource resource = (Resource) dirContext.lookup("/WEB-INF/lib/" + binding.getName());
            Manifest jmanifest = getManifest(resource.streamContent());
            if (jmanifest != null) {
                ManifestResource mre = new ManifestResource(binding.getName(), jmanifest,
                        ManifestResource.APPLICATION);
                appManifestResources.add(mre);
            }
        }
    } catch (NamingException nex) {
        // Jump out of the check for this application because it 
        // has no resources
    }

    return validateManifestResources(appName, appManifestResources);
}

From source file:org.geoserver.security.ldap.LDAPTestUtils.java

/**
 * Clear the directory sub-tree starting with the node represented by the
 * supplied distinguished name.//from  ww  w. java  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();
            DistinguishedName childName = new DistinguishedName(element.getName());
            childName.prepend((DistinguishedName) 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
        }
    }
}

From source file:org.springframework.ldap.core.LdapTemplate.java

public void listBindings(final String base, NameClassPairCallbackHandler handler) {
    SearchExecutor searchExecutor = new SearchExecutor() {
        public NamingEnumeration executeSearch(DirContext ctx) throws javax.naming.NamingException {
            return ctx.listBindings(base);
        }/*from   w w w .j a  va 2  s. c om*/
    };

    search(searchExecutor, handler);
}

From source file:org.springframework.ldap.core.LdapTemplate.java

public void listBindings(final Name base, NameClassPairCallbackHandler handler) {
    SearchExecutor searchExecutor = new SearchExecutor() {
        public NamingEnumeration executeSearch(DirContext ctx) throws javax.naming.NamingException {
            return ctx.listBindings(base);
        }//from www.  j  a v a 2s  .c  om
    };

    search(searchExecutor, handler);
}

From source file:org.springframework.ldap.core.LdapTemplate.java

/**
 * Delete all subcontexts including the current one recursively.
 * //  w w  w. j a  va 2  s .  co  m
 * @param ctx The context to use for deleting.
 * @param name The starting point to delete recursively.
 * @throws NamingException if any error occurs
 */
protected void deleteRecursively(DirContext ctx, DistinguishedName name) {

    NamingEnumeration enumeration = null;
    try {
        enumeration = ctx.listBindings(name);
        while (enumeration.hasMore()) {
            Binding binding = (Binding) enumeration.next();
            DistinguishedName childName = new DistinguishedName(binding.getName());
            childName.prepend((DistinguishedName) name);
            deleteRecursively(ctx, childName);
        }
        ctx.unbind(name);
        if (log.isDebugEnabled()) {
            log.debug("Entry " + name + " deleted");
        }
    } catch (javax.naming.NamingException e) {
        throw LdapUtils.convertLdapException(e);
    } finally {
        try {
            enumeration.close();
        } catch (Exception e) {
            // Never mind this
        }
    }
}

From source file:org.springframework.ldap.test.unboundid.LdapTestUtils.java

/**
 * Clear the directory sub-tree starting with the node represented by the
 * supplied distinguished name./*from   ww  w. j av  a2  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
        }
    }
}