Example usage for javax.naming.directory DirContext getEnvironment

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

Introduction

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

Prototype

public Hashtable<?, ?> getEnvironment() throws NamingException;

Source Link

Document

Retrieves the environment in effect for this context.

Usage

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

private static void loadLdif(DirContext context, Name rootNode, Resource ldifFile) {
    try {/*from  w ww.j a v  a2 s  . c om*/
        LdapName baseDn = (LdapName) context.getEnvironment()
                .get(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY);

        LdifParser parser = new LdifParser(ldifFile);
        parser.open();
        while (parser.hasMoreRecords()) {
            LdapAttributes record = parser.getRecord();

            LdapName dn = record.getName();

            if (baseDn != null) {
                dn = LdapUtils.removeFirst(dn, baseDn);
            }

            if (!rootNode.isEmpty()) {
                dn = LdapUtils.prepend(dn, rootNode);
            }
            context.bind(dn, null, record);
        }
    } catch (Exception e) {
        throw new UncategorizedLdapException("Failed to populate LDIF", e);
    }
}

From source file:net.grinder.util.NetworkUtils.java

public static List<String> getDnsServers() throws NamingException {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
    DirContext ctx = null;
    List<String> dnsServers = new ArrayList<String>();
    try {/*from  w  w w . ja  va2  s. c  o m*/
        ctx = new InitialDirContext(env);
        String dnsString = (String) ctx.getEnvironment().get("java.naming.provider.url");
        for (String each : dnsString.split(" ")) {
            dnsServers.add(each.replace("dns://", ""));
        }
    } catch (Exception e) {
        NoOp.noOp();
    } finally {
        if (ctx != null) {
            ctx.close();
        }
    }
    return dnsServers;
}

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

private static void loadLdif(DirContext context, Resource ldifFile) throws IOException {
    try {/*from w ww .  j  ava 2s  .  c  o m*/
        DistinguishedName baseDn = (DistinguishedName) context.getEnvironment()
                .get(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY);

        LdifParser parser = new LdifParser(ldifFile);
        parser.open();
        while (parser.hasMoreRecords()) {
            LdapAttributes record = parser.getRecord();

            DistinguishedName dn = record.getDN();
            if (baseDn != null) {
                dn.removeFirst(baseDn);
            }
            context.bind(dn, null, record);
        }
    } catch (NamingException e) {
        throw new RuntimeException("Failed to populate LDIF", e);
    }
}

From source file:org.nuxeo.ecm.directory.ldap.LDAPDirectoryTestCase.java

protected void destroyRecursively(String dn, DirContext ctx, int limit) throws NamingException {
    if (limit == 0) {
        log.warn("Reach recursion limit, stopping deletion at" + dn);
        return;//from   w  w  w . j av  a  2s  . c  om
    }
    SearchControls scts = new SearchControls();
    scts.setSearchScope(SearchControls.ONELEVEL_SCOPE);
    String providerUrl = (String) ctx.getEnvironment().get(Context.PROVIDER_URL);
    NamingEnumeration<SearchResult> children = ctx.search(dn, "(objectClass=*)", scts);
    try {
        while (children.hasMore()) {
            SearchResult child = children.next();
            String subDn = child.getName();
            if (!USE_EXTERNAL_TEST_LDAP_SERVER && subDn.endsWith(providerUrl)) {
                subDn = subDn.substring(0, subDn.length() - providerUrl.length() - 1);
            } else {
                subDn = subDn + ',' + dn;
            }
            destroyRecursively(subDn, ctx, limit);
        }
    } catch (SizeLimitExceededException e) {
        log.warn("SizeLimitExceededException: trying again on partial results " + dn);
        if (limit == -1) {
            limit = 100;
        }
        destroyRecursively(dn, ctx, limit - 1);
    }
    ctx.destroySubcontext(dn);
}

From source file:org.springframework.ldap.core.support.AbstractContextSource.java

/**
 * Create a DirContext using the supplied environment.
 * /*from ww w  . ja  va2s  .co  m*/
 * @param environment the LDAP environment to use when creating the
 * <code>DirContext</code>.
 * @return a new DirContext implementation initialized with the supplied
 * environment.
 */
protected DirContext createContext(Hashtable environment) {
    DirContext ctx = null;

    try {
        ctx = getDirContextInstance(environment);

        if (log.isInfoEnabled()) {
            Hashtable ctxEnv = ctx.getEnvironment();
            String ldapUrl = (String) ctxEnv.get(Context.PROVIDER_URL);
            log.debug("Got Ldap context on server '" + ldapUrl + "'");
        }

        return ctx;
    } catch (NamingException e) {
        closeContext(ctx);
        throw LdapUtils.convertLdapException(e);
    }
}

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

@SuppressWarnings("deprecation")
private static void loadLdif(DirContext context, Name rootNode, Resource ldifFile) {
    try {/*from  w  w  w  .  jav  a2s .c om*/
        LdapName baseDn = (LdapName) context.getEnvironment()
                .get(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY);

        LdifParser parser = new LdifParser(ldifFile);
        parser.open();
        while (parser.hasMoreRecords()) {
            LdapAttributes record = parser.getRecord();

            LdapName dn = record.getName();

            if (baseDn != null) {
                dn = LdapUtils.removeFirst(dn, baseDn);
            }

            if (!rootNode.isEmpty()) {
                dn = LdapUtils.prepend(dn, rootNode);
            }
            context.bind(dn, null, record);
        }
    } catch (Exception e) {
        throw new UncategorizedLdapException("Failed to populate LDIF", e);
    }
}