Example usage for javax.naming Context PROVIDER_URL

List of usage examples for javax.naming Context PROVIDER_URL

Introduction

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

Prototype

String PROVIDER_URL

To view the source code for javax.naming Context PROVIDER_URL.

Click Source Link

Document

Constant that holds the name of the environment property for specifying configuration information for the service provider to use.

Usage

From source file:xc.mst.manager.user.DefaultUserService.java

/**
 * Sets up the Properties used to create an LDAP connection
 * /*  ww w  .j a  va  2 s . c om*/
 * @return The Properties for an LDAP connection
 */
private static Properties getGenericLDAPProperties(Server loginserver) {

    // Get important values from the configuration file for connecting to the LDAP server.
    String url = loginserver.getUrl();
    int port = loginserver.getPort();

    // Set up the environment for creating the initial context
    Properties ldapProperties = new Properties();
    ldapProperties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    ldapProperties.setProperty(Context.PROVIDER_URL, url + ":" + port);

    return ldapProperties;
}

From source file:org.acegisecurity.ldap.DefaultInitialDirContextFactory.java

/**
 * Sets up the environment parameters for creating a new context.
 *
 * @return the Hashtable describing the base DirContext that will be created, minus the username/password if any.
 *//*  w  w w . j  av  a2 s  .  co  m*/
protected Hashtable getEnvironment() {
    Hashtable env = new Hashtable();

    env.put(Context.SECURITY_AUTHENTICATION, authenticationType);
    env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
    env.put(Context.PROVIDER_URL, getProviderUrl());

    if (useConnectionPool) {
        env.put(CONNECTION_POOL_KEY, "true");
    }

    if ((extraEnvVars != null) && (extraEnvVars.size() > 0)) {
        env.putAll(extraEnvVars);
    }

    return env;
}

From source file:org.rhq.enterprise.server.core.CustomJaasDeploymentService.java

private void validateLdapOptions(Map<String, String> options) throws NamingException {
    Properties env = new Properties();

    String factory = options.get(Context.INITIAL_CONTEXT_FACTORY);
    if (factory == null) {
        throw new NamingException("No initial context factory");
    }/* ww w  .j a  v  a2s  .  c  om*/

    String url = options.get(Context.PROVIDER_URL);
    if (url == null) {
        throw new NamingException("Naming provider url not set");
    }

    String protocol = options.get(Context.SECURITY_PROTOCOL);
    if ("ssl".equals(protocol)) {
        String ldapSocketFactory = env.getProperty("java.naming.ldap.factory.socket");
        if (ldapSocketFactory == null) {
            env.put("java.naming.ldap.factory.socket", UntrustedSSLSocketFactory.class.getName());
        }
        env.put(Context.SECURITY_PROTOCOL, "ssl");
    }

    env.setProperty(Context.INITIAL_CONTEXT_FACTORY, factory);
    env.setProperty(Context.PROVIDER_URL, url);

    // Load any information we may need to bind
    String bindDN = options.get("BindDN");
    String bindPW = options.get("BindPW");
    if ((bindDN != null) && (bindDN.length() != 0) && (bindPW != null) && (bindPW.length() != 0)) {
        env.setProperty(Context.SECURITY_PRINCIPAL, bindDN);
        env.setProperty(Context.SECURITY_CREDENTIALS, bindPW);
        env.setProperty(Context.SECURITY_AUTHENTICATION, "simple");
    }

    log.debug("Validating LDAP properties. Initializing context...");
    new InitialLdapContext(env, null).close();

    return;
}

From source file:org.apache.directory.server.operations.bind.MiscBindIT.java

/**
 * Test to make sure that if anonymous binds are allowed a user may search
 * within a a partition./* w  ww  .j  a  va  2 s. c om*/
 *
 * @throws Exception if anything goes wrong
 */
@Test
public void testAnonymousBindsEnabledBaseSearch() throws Exception {
    getLdapServer().getDirectoryService().setAllowAnonymousAccess(true);

    // Use the SUN JNDI provider to hit server port and bind as anonymous
    Hashtable<String, Object> env = new Hashtable<String, Object>();

    env.put(Context.PROVIDER_URL, Network.ldapLoopbackUrl(getLdapServer().getPort()));
    env.put(Context.SECURITY_AUTHENTICATION, "none");
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

    InitialDirContext ctx = new InitialDirContext(env);
    SearchControls cons = new SearchControls();
    cons.setSearchScope(SearchControls.OBJECT_SCOPE);
    NamingEnumeration<SearchResult> list = ctx.search("dc=apache,dc=org", "(objectClass=*)", cons);
    SearchResult result = null;

    if (list.hasMore()) {
        result = list.next();
    }

    assertFalse(list.hasMore());
    list.close();

    assertNotNull(result);
    assertNotNull(result.getAttributes().get("dc"));
}

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

public void shutdownLdapServer() {

    Hashtable<String, Object> env = new Hashtable<>(new ShutdownConfiguration().toJndiEnvironment());
    env.put(Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName());
    env.put(Context.PROVIDER_URL, BASE_DN);

    try {//from  www.j a va 2  s.c  o m
        new InitialLdapContext(env, null);
    } catch (Exception e) {
        throw new ServerSystemPreferenceException("Failed to shutdown ldap server.", e);
    }
}

From source file:org.springframework.ldap.odm.tools.SchemaToJava.java

private static ObjectSchema readSchema(String url, String user, String pass,
        SyntaxToJavaClass syntaxToJavaClass, Set<String> binarySet, Set<String> objectClasses)
        throws NamingException, ClassNotFoundException {

    // Set up environment 
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.PROVIDER_URL, url);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    if (user != null) {
        env.put(Context.SECURITY_PRINCIPAL, user);
    }//from   w ww . jav  a  2  s  .c  om
    if (pass != null) {
        env.put(Context.SECURITY_CREDENTIALS, pass);
    }

    DirContext context = new InitialDirContext(env);
    DirContext schemaContext = context.getSchema("");
    SchemaReader reader = new SchemaReader(schemaContext, syntaxToJavaClass, binarySet);
    ObjectSchema schema = reader.getObjectSchema(objectClasses);

    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Schema - %1$s", schema.toString()));
    }

    return schema;
}

From source file:org.mule.providers.ldap.util.DSManager.java

/**
 * Sets the system context root to null.
 * //from  ww  w  .j  a v  a 2s  .com
 * @see junit.framework.TestCase#tearDown()
 */
public synchronized void stop() throws Exception {
    logger.debug("DS is stopping ...");

    if (!running) {
        logger.debug("stop() called while is not running");

        if (checkSocketNotConnected()) {
            return;
        } else {
            logger.debug("stop() forced");
        }
    }

    // super.tearDown();
    Hashtable env = new Hashtable();
    env.put(Context.PROVIDER_URL, "ou=system");
    env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.directory.server.jndi.ServerContextFactory");
    env.putAll(new ShutdownConfiguration().toJndiEnvironment());
    env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
    env.put(Context.SECURITY_CREDENTIALS, "secret");

    try {
        new InitialContext(env);
    } catch (Exception e) {
        // ignored
        // dont remove try catch block!!
    }

    sysRoot = null;
    doDelete(configuration.getWorkingDirectory());
    configuration = new MutableServerStartupConfiguration();

    logger.debug("DS waiting for socket release ...");

    // wait for shutdown
    int i = 0;

    while (i < 20 && !checkSocketNotConnected()) {
        Thread.sleep(2000);
        i++;
        logger.debug("Try " + i);
    }

    if (!checkSocketNotConnected()) {
        throw new Exception("Shutdown of DS not successfull, server socket was not freed");
    }

    logger.debug("DS now stopped!");
    running = false;

}

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

/**
 * Create a DirContext using the supplied environment.
 * /*from w ww  .  ja  va2 s  . c o 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.openiam.idm.srvc.auth.spi.AbstractLoginModule.java

public LdapContext connect(String userName, String password, ManagedSysDto managedSys) throws NamingException {

    if (keystore != null && !keystore.isEmpty()) {
        System.setProperty("javax.net.ssl.trustStore", keystore);
        System.setProperty("javax.net.ssl.keyStorePassword", keystorePasswd);
    }/*from w w w  .  j  a  v  a2  s. c  om*/

    if (managedSys == null) {
        log.debug("ManagedSys is null");
        return null;
    }

    String hostUrl = managedSys.getHostUrl();
    if (managedSys.getPort() > 0) {
        hostUrl = hostUrl + ":" + String.valueOf(managedSys.getPort());
    }

    log.debug("connect: Connecting to target system: " + managedSys.getId());
    log.debug("connect: Managed System object : " + managedSys);

    log.info(" directory login = " + managedSys.getUserId());
    log.info(" directory login passwrd= *****");
    log.info(" javax.net.ssl.trustStore= " + System.getProperty("javax.net.ssl.trustStore"));
    log.info(" javax.net.ssl.keyStorePassword= " + System.getProperty("javax.net.ssl.keyStorePassword"));

    Hashtable<String, String> envDC = new Hashtable();
    envDC.put(Context.PROVIDER_URL, hostUrl);
    envDC.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    envDC.put(Context.SECURITY_AUTHENTICATION, "simple"); // simple
    envDC.put(Context.SECURITY_PRINCIPAL, userName);
    envDC.put(Context.SECURITY_CREDENTIALS, password);

    // Connections Pool configuration
    envDC.put("com.sun.jndi.ldap.connect.pool", "true");
    // Here is an example of a command line that sets the maximum pool size to 20, the preferred pool size to 10, and the idle timeout to 5 minutes for pooled connections.
    envDC.put("com.sun.jndi.ldap.connect.pool.prefsize", "10");
    envDC.put("com.sun.jndi.ldap.connect.pool.maxsize", "20");
    envDC.put("com.sun.jndi.ldap.connect.pool.timeout", "300000");

    LdapContext ldapContext = null;
    try {
        ldapContext = (LdapContext) new LdapCtxFactory().getInitialContext((Hashtable) envDC);

    } catch (CommunicationException ce) {
        log.error("Throw communication exception.", ce);

    } catch (NamingException ne) {
        log.error(ne.toString(), ne);

    } catch (Throwable e) {
        log.error(e.toString(), e);
    }

    return ldapContext;
}

From source file:edu.internet2.middleware.subject.provider.JNDISourceAdapter.java

/**
 * Setup environment.//ww w  .java2  s.  co  m
 * @param props 
 * @throws SourceUnavailableException
 */
protected void setupEnvironment(Properties props) throws SourceUnavailableException {
    this.environment.put("com.sun.jndi.ldap.connect.pool", "true");

    this.environment.put(Context.INITIAL_CONTEXT_FACTORY, props.getProperty("INITIAL_CONTEXT_FACTORY"));
    this.environment.put(Context.PROVIDER_URL, props.getProperty("PROVIDER_URL"));
    this.environment.put(Context.SECURITY_AUTHENTICATION, props.getProperty("SECURITY_AUTHENTICATION"));
    this.environment.put(Context.SECURITY_PRINCIPAL, props.getProperty("SECURITY_PRINCIPAL"));

    String password = props.getProperty("SECURITY_CREDENTIALS");
    password = Morph.decryptIfFile(password);

    this.environment.put(Context.SECURITY_CREDENTIALS, password);
    if (props.getProperty("SECURITY_PROTOCOL") != null) {
        this.environment.put(Context.SECURITY_PROTOCOL, "ssl");
    }
    Context context = null;
    try {
        log.debug("Creating Directory Context");
        context = new InitialDirContext(this.environment);
    } catch (AuthenticationException ex) {
        log.error("Error with Authentication " + ex.getMessage(), ex);
        throw new SourceUnavailableException("Error with Authentication ", ex);
    } catch (NamingException ex) {
        log.error("Naming Error " + ex.getMessage(), ex);
        throw new SourceUnavailableException("Naming Error", ex);
    } finally {
        if (context != null) {
            try {
                context.close();
            } catch (NamingException ne) {
                // squelch, since it is already closed
            }
        }
    }
    log.info("Success in connecting to LDAP");

    this.nameAttributeName = props.getProperty("Name_AttributeType");
    if (this.nameAttributeName == null) {
        log.error("Name_AttributeType not defined");
    }
    this.subjectIDAttributeName = props.getProperty("SubjectID_AttributeType");
    if (this.subjectIDAttributeName == null) {
        log.error("SubjectID_AttributeType not defined");
    }
    this.descriptionAttributeName = props.getProperty("Description_AttributeType");
    if (this.descriptionAttributeName == null) {
        log.error("Description_AttributeType not defined");
    }

}