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:org.easy.ldap.LdapContextFactory.java

/**
 * @param orgId//from   w w  w .j a  v  a  2  s .  c om
 * @return
 * @throws NamingException
 */
public DirContext createUsersContext(String orgId) throws NamingException {
    Hashtable<String, String> properties = getEnviroment();
    properties.put(Context.PROVIDER_URL, createProviderUrl(namingFactory.createTenantDn(orgId).toString()));

    return createContext(properties);
}

From source file:alpine.auth.LdapConnectionWrapper.java

/**
 * Asserts a users credentials. Returns an LdapContext if assertion is successful
 * or an exception for any other reason.
 *
 * @param userDn the users DN to assert//from w w w.ja  v  a 2 s .  c  o m
 * @param password the password to assert
 * @return the LdapContext upon a successful connection
 * @throws NamingException when unable to establish a connection
 * @since 1.4.0
 */
public LdapContext createLdapContext(String userDn, String password) throws NamingException {
    if (StringUtils.isEmpty(userDn) || StringUtils.isEmpty(password)) {
        throw new NamingException("Username or password cannot be empty or null");
    }
    final Hashtable<String, String> env = new Hashtable<>();
    if (StringUtils.isNotBlank(LDAP_SECURITY_AUTH)) {
        env.put(Context.SECURITY_AUTHENTICATION, LDAP_SECURITY_AUTH);
    }
    env.put(Context.SECURITY_PRINCIPAL, userDn);
    env.put(Context.SECURITY_CREDENTIALS, password);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, LDAP_URL);
    if (IS_LDAP_SSLTLS) {
        env.put("java.naming.ldap.factory.socket", "alpine.crypto.RelaxedSSLSocketFactory");
    }
    try {
        return new InitialLdapContext(env, null);
    } catch (CommunicationException e) {
        LOGGER.error("Failed to connect to directory server", e);
        throw (e);
    } catch (NamingException e) {
        throw new NamingException("Failed to authenticate user");
    }
}

From source file:org.mc4j.ems.impl.jmx.connection.support.providers.JBossConnectionProvider.java

private InitialContext getInitialContext() throws NamingException {
    Properties props = this.connectionSettings.getAdvancedProperties();
    if (!NAMING_CONTEXT_FACTORY_CLASS_NAME.equals(this.connectionSettings.getInitialContextName())) {
        log.warn("Unsupported initial context factory [" + this.connectionSettings.getInitialContextName()
                + "] - only " + NAMING_CONTEXT_FACTORY_CLASS_NAME
                + " is supported for JBoss connections; using that instead...");
    }// w w  w . j  a v a2  s.c  o  m
    props.put(Context.INITIAL_CONTEXT_FACTORY, NAMING_CONTEXT_FACTORY_CLASS_NAME);
    props.put(Context.PROVIDER_URL, this.connectionSettings.getServerUrl());

    try {
        InitialContext context = new InitialContext(props);
        return context;
    } catch (NoInitialContextException e) {
        // Try to be more helpful, indicating the reason we couldn't make the connection in this
        // common case of missing libraries.
        if (e.getCause() instanceof ClassNotFoundException) {
            throw new ConnectionException(
                    "Necessary classes not found for remote connection, check installation path configuration.",
                    e.getCause());
        }
        throw e;
    }
}

From source file:com.zotoh.maedr.device.JmsIO.java

private void inizConn() throws Exception {

    Hashtable<String, String> vars = new Hashtable<String, String>();
    Context ctx;//from   w  w w  .  j  ava  2s. c  o m
    Object obj;

    if (!isEmpty(_ctxFac)) {
        vars.put(Context.INITIAL_CONTEXT_FACTORY, _ctxFac);
    }

    if (!isEmpty(_url)) {
        vars.put(Context.PROVIDER_URL, _url);
    }

    if (!isEmpty(_JNDIPwd)) {
        vars.put("jndi.password", _JNDIPwd);
    }

    if (!isEmpty(_JNDIUser)) {
        vars.put("jndi.user", _JNDIUser);
    }

    ctx = new InitialContext(vars);
    obj = ctx.lookup(_connFac);

    if (obj instanceof QueueConnectionFactory) {
        inizQueue(ctx, obj);
    } else if (obj instanceof TopicConnectionFactory) {
        inizTopic(ctx, obj);
    } else if (obj instanceof ConnectionFactory) {
        inizFac(ctx, obj);
    } else {
        throw new Exception("JmsIO: unsupported JMS Connection Factory");
    }

    if (_conn != null) {
        _conn.start();
    }
}

From source file:eu.europa.ec.markt.dss.validation.crl.OnlineCRLSource.java

/**
 * Downloads a CRL from given LDAP url, e.g. ldap://ldap.infonotary.com/dc=identity-ca,dc=infonotary,dc=com
 *
 * @throws CertificateException/*from  w ww  .j  ava 2  s. c o m*/
 * @throws CRLException
 */

private static X509CRL downloadCRLFromLDAP_(final String ldapURL) throws DSSException {

    final Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, ldapURL);
    try {

        final DirContext ctx = new InitialDirContext(env);
        final Attributes attributes = ctx.getAttributes("");
        final javax.naming.directory.Attribute attribute = attributes.get("certificateRevocationList;binary");
        final byte[] val = (byte[]) attribute.get();
        if (val == null || val.length == 0) {

            throw new DSSException("Can not download CRL from: " + ldapURL);
        }
        final InputStream inStream = new ByteArrayInputStream(val);
        return DSSUtils.loadCRL(inStream);
    } catch (Exception e) {

        LOG.warning(e.getMessage());
        e.printStackTrace();
    }
    return null;
}

From source file:org.kitodo.production.services.data.LdapServerService.java

private Hashtable<String, String> initializeWithLdapConnectionSettings(LdapServer ldapServer) {
    Hashtable<String, String> env = new Hashtable<>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, ldapServer.getUrl());
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, ldapServer.getManagerLogin());

    String encryptedManagerPassword = ldapServer.getManagerPassword();
    String decryptedManagerPassword = passwordEncoder.decrypt(encryptedManagerPassword);

    env.put(Context.SECURITY_CREDENTIALS, decryptedManagerPassword);

    if (ldapServer.isUseSsl()) {
        String keystorepath = ldapServer.getKeystore();
        String keystorepasswd = ldapServer.getKeystorePassword();

        // add all necessary certificates first
        loadCertificates(keystorepath, keystorepasswd, ldapServer);

        // set properties, so that the current keystore is used for SSL
        System.setProperty("javax.net.ssl.keyStore", keystorepath);
        System.setProperty("javax.net.ssl.trustStore", keystorepath);
        System.setProperty("javax.net.ssl.keyStorePassword", keystorepasswd);
        env.put(Context.SECURITY_PROTOCOL, "ssl");
    }/*from   ww w .  ja  va2 s. co m*/
    return env;
}

From source file:com.att.ajsc.csilogging.common.QueueConnector.java

@PostConstruct
public void init() {

    if (csiEnable && StringUtils.isNotEmpty(initialContextFactoryName)
            && StringUtils.isNotEmpty(connectionFactoryName) && StringUtils.isNotEmpty(providerURL)) {

        if (StringUtils.isNotEmpty(System.getenv(("com_att_aft_config_file")))) {
            System.setProperty("com.att.aft.config.file", System.getenv("com_att_aft_config_file"));
        }/*ww w.java 2s  .  c o m*/

        if (StringUtils.isEmpty(System.getProperty("com.att.aft.config.file"))) {
            logger.error("Environment or System properties dont have the property com.att.aft.config.file");
            return;
        }

        QueueConnectionFactory queueConnectionFactory;
        InitialContext jndi = null;
        ConnectionFactory connectionFactory = null;
        try {

            Properties env = new Properties();
            env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName);
            env.put(Context.PROVIDER_URL, providerURL);
            jndi = new InitialContext(env);
            connectionFactory = (ConnectionFactory) jndi.lookup(connectionFactoryName);
            queueConnectionFactory = (QueueConnectionFactory) connectionFactory;
            if (StringUtils.isNotEmpty(auditDestinationName)) {
                auditQueueConnection = queueConnectionFactory.createQueueConnection();
                auditQueueSession = auditQueueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
                Queue auditQueue = (Queue) auditQueueSession.createQueue(auditDestinationName);
                auditQueueSender = auditQueueSession.createSender(auditQueue);
                auditQueueConnection.start();
                logger.info("*************CONNECTED :" + auditDestinationName + "*************");

            }

            if (StringUtils.isNotEmpty(perfDestinationName)) {
                pefQueueConnection = queueConnectionFactory.createQueueConnection();
                pefQueueSession = pefQueueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
                Queue perfQueue = (Queue) pefQueueSession.createQueue(perfDestinationName);
                pefQueueSender = pefQueueSession.createSender(perfQueue);
                pefQueueConnection.start();
                logger.info("*************CONNECTED :" + perfDestinationName + "*************");
            }

        } catch (Exception e) {
            logger.error("Error while connecting to the Queue" + e);
        }
    }

}

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

public LdapContext getLdapContext() throws Exception {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, LDAP_PROVIDER_URL);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, LDAP_PRINCIPAL);
    env.put(Context.SECURITY_CREDENTIALS, LDAP_CREDENTIALS);

    return new InitialLdapContext(env, null);
}

From source file:org.easy.ldap.LdapContextFactory.java

/**
 * @return//  w w w.ja va2 s .  c  om
 */
private Hashtable<String, String> getEnviroment() {
    Hashtable<String, String> properties = new Hashtable<String, String>();

    properties.put(Context.PROVIDER_URL, createProviderUrl(environment.getProperty(PropertyNames.DOMAIN_DN)));

    properties.put(Context.INITIAL_CONTEXT_FACTORY,
            environment.getProperty(PropertyNames.INITIAL_CONTEXT_FACTORY_CLASS));
    properties.put("com.sun.jndi.ldap.connect.pool",
            environment.getProperty(PropertyNames.USE_LDAP_CONNECT_POOL));
    properties.put(Context.SECURITY_AUTHENTICATION,
            environment.getProperty(PropertyNames.ADMIN_AUTHENTICATION_METHOD));
    properties.put(Context.SECURITY_PRINCIPAL, environment.getProperty(PropertyNames.ADMIN_PRINCIPAL));
    properties.put(Context.SECURITY_CREDENTIALS, environment.getProperty(PropertyNames.ADMIN_CREDENTIALS));

    return properties;
}

From source file:com.paxxis.cornerstone.messaging.service.shell.ServiceShell.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private void doUnbind(String[] vals) throws NamingException {
    String url = "rmi://localhost:" + vals[1];
    String serviceName = vals[0];
    System.out.println("Unbinding " + url + "/" + serviceName);
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
    env.put(Context.PROVIDER_URL, url);

    Context ictx = new InitialContext(env);
    try {/*from   w  w  w.  java  2s  .  co  m*/
        ictx.unbind(serviceName);
        System.out.println("Done");
    } catch (Exception ex) {
        System.err.println(ex.getMessage() + ex.getCause().getMessage());
    }
}