Example usage for javax.naming Context INITIAL_CONTEXT_FACTORY

List of usage examples for javax.naming Context INITIAL_CONTEXT_FACTORY

Introduction

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

Prototype

String INITIAL_CONTEXT_FACTORY

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

Click Source Link

Document

Constant that holds the name of the environment property for specifying the initial context factory to use.

Usage

From source file:no.smint.anthropos.ldap.LDAP.java

/**
 * Binds anonymously to the LDAP server. Returns a <code>Hashtable</code> to use for searching etc.
 * @return <code>Hashtable</code> with the binding to the server.
 *//*from w  w  w. j  a va  2 s.co  m*/
public static Hashtable<String, Object> config() {
    Hashtable<String, Object> env = new Hashtable<String, Object>();

    //Connection details
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, host);
    env.put(Context.SECURITY_AUTHENTICATION, "none");

    //Optional for testing purposes
    //env.put(Context.SECURITY_AUTHENTICATION, "simple");
    //env.put(Context.SECURITY_PRINCIPAL, "uid=birgith.do,ou=System Users,dc=studentmeidene,dc=no");
    //env.put(Context.SECURITY_CREDENTIALS, "overrated rapid machine");
    return env;
}

From source file:org.jboss.adminclient.connection.RemoteProfileServiceConnectionProvider.java

protected ProfileServiceConnectionImpl doConnect() {
    Properties env = new Properties();
    env.setProperty(Context.PROVIDER_URL, this.providerURL);
    ProfileService profileService;//from  w w w . j  a  v  a 2  s. co m
    ManagementView managementView;
    DeploymentManager deploymentManager;
    ClassLoader originalContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
        if (this.principal != null) {
            env.setProperty(Context.INITIAL_CONTEXT_FACTORY, JNDI_LOGIN_INITIAL_CONTEXT_FACTORY);
            env.setProperty(Context.SECURITY_PRINCIPAL, this.principal);
            env.setProperty(Context.SECURITY_CREDENTIALS, this.credentials);
            log.debug("Connecting to Profile Service via remote JNDI using env [" + env + "]...");
            InitialContext initialContext = createInitialContext(env);
            profileService = (ProfileService) lookup(initialContext, SECURE_PROFILE_SERVICE_JNDI_NAME);
            managementView = (ManagementView) lookup(initialContext, SECURE_MANAGEMENT_VIEW_JNDI_NAME);
            deploymentManager = (DeploymentManager) lookup(initialContext, SECURE_DEPLOYMENT_MANAGER_JNDI_NAME);
        } else {
            env.setProperty(Context.INITIAL_CONTEXT_FACTORY, NAMING_CONTEXT_FACTORY);
            env.setProperty(JNP_DISABLE_DISCOVERY_JNP_INIT_PROP, "true");
            // Make sure the timeout always happens, even if the JBoss server is hung.
            env.setProperty("jnp.timeout", String.valueOf(JNP_TIMEOUT));
            env.setProperty("jnp.sotimeout", String.valueOf(JNP_SO_TIMEOUT));
            log.debug("Connecting to Profile Service via remote JNDI using env [" + env + "]...");
            InitialContext initialContext = createInitialContext(env);
            profileService = (ProfileService) lookup(initialContext, PROFILE_SERVICE_JNDI_NAME);
            managementView = profileService.getViewManager();
            deploymentManager = profileService.getDeploymentManager();
        }
    } finally {
        Thread.currentThread().setContextClassLoader(originalContextClassLoader);
    }
    return new ProfileServiceConnectionImpl(this, profileService, managementView, deploymentManager);
}

From source file:org.jamwiki.ldap.LdapUserHandler.java

/**
 * Connect to the LDAP server and return a context.
 *
 * @return The LDAP context to use when retrieving user information.
 *///from w  w  w .  jav a2s. com
private InitialDirContext getContext(String username, String password) throws Exception {
    // Set up the environment for creating the initial context
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, Environment.getValue(Environment.PROP_LDAP_FACTORY_CLASS));
    env.put(Context.PROVIDER_URL, Environment.getValue(Environment.PROP_LDAP_URL));
    if (!StringUtils.isBlank(username)) {
        // "simple" "DIGEST-MD5"
        env.put(Context.SECURITY_AUTHENTICATION,
                Environment.getValue(Environment.PROP_LDAP_SECURITY_AUTHENTICATION));
        // cn=login, ou=NewHires, o=JNDITutorial
        env.put(Context.SECURITY_PRINCIPAL, username);
        env.put(Context.SECURITY_CREDENTIALS, password);
    }
    InitialDirContext ctx = new InitialDirContext(env);
    return ctx;
}

From source file:it.doqui.index.ecmengine.client.engine.EcmEngineDirectDelegateImpl.java

protected EcmEngineManagementBusinessInterface createManagementService() throws Throwable {
    this.log.debug("[" + getClass().getSimpleName() + "::createManagementService] BEGIN ");
    Properties properties = new Properties();

    /* Caricamento del file contenenti le properties su cui fare il binding */
    rb = ResourceBundle.getBundle(ECMENGINE_PROPERTIES_FILE);

    /*/*from  w w  w.ja v  a 2  s .co m*/
    * Caricamento delle proprieta' su cui fare il binding all'oggetto di business delle funzionalita'
    * implementate per il management.
    */
    try {
        this.log.debug(
                "[" + getClass().getSimpleName() + "::createManagementService] P-Delegata di backoffice.");

        this.log.debug("[" + getClass().getSimpleName() + "::createManagementService] context factory vale : "
                + rb.getString(ECMENGINE_CONTEXT_FACTORY));
        properties.put(Context.INITIAL_CONTEXT_FACTORY, rb.getString(ECMENGINE_CONTEXT_FACTORY));
        this.log.debug("[" + getClass().getSimpleName() + "::createManagementService] url to connect vale : "
                + rb.getString(ECMENGINE_URL_TO_CONNECT));
        properties.put(Context.PROVIDER_URL, rb.getString(ECMENGINE_URL_TO_CONNECT));

        /* Controllo che la property cluster partition sia valorizzata per capire se
         * sto lavorando in una configurazione in cluster oppure no */
        String clusterPartition = rb.getString(ECMENGINE_CLUSTER_PARTITION);
        this.log.debug("[" + getClass().getSimpleName() + "::createManagementService] clusterPartition vale : "
                + clusterPartition);
        if (clusterPartition != null && clusterPartition.length() > 0) {
            properties.put("jnp.partitionName", clusterPartition);
            this.log.debug(
                    "[" + getClass().getSimpleName() + "::createManagementService] disable discovery vale : "
                            + rb.getString(ECMENGINE_DISABLE_DISCOVERY));
            properties.put("jnp.disableDiscovery", rb.getString(ECMENGINE_DISABLE_DISCOVERY));
        }

        // Get an initial context
        InitialContext jndiContext = new InitialContext(properties);
        log.debug("[" + getClass().getSimpleName() + "::createManagementService] context istanziato");

        // Get a reference to the Bean
        Object ref = jndiContext.lookup(ECMENGINE_MANAGEMENT_JNDI_NAME);

        // Get a reference from this to the Bean's Home interface
        EcmEngineManagementHome home = (EcmEngineManagementHome) PortableRemoteObject.narrow(ref,
                EcmEngineManagementHome.class);

        // Create an Adder object from the Home interface
        return home.create();

    } catch (Throwable e) {
        this.log.error("[" + getClass().getSimpleName() + "::createManagementService] "
                + "Impossibile istanziare la P-Delegata di management: " + e.getMessage());
        throw e;
    } finally {
        this.log.debug("[" + getClass().getSimpleName() + "::createManagementService] END ");
    }
}

From source file:org.hyperic.hq.plugin.jboss.JBossUtil.java

public static MBeanServerConnection getMBeanServerConnection(Properties config)
        throws NamingException, RemoteException {
    MBeanServerConnection adaptor;

    Properties props = new Properties();

    for (int i = 0; i < NAMING_PROPS.length; i++) {
        props.setProperty(NAMING_PROPS[i][0], NAMING_PROPS[i][1]);
    }/* w  w w. j  av  a  2 s .  co m*/

    props.putAll(config);

    if (props.getProperty(Context.SECURITY_PRINCIPAL) != null) {
        props.setProperty(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
    }

    InitialContext ctx = new InitialContext(props);

    try {
        Object o = ctx.lookup(props.getProperty(PROP_NAMING_CONNECTOR));
        log.debug("=> " + Arrays.asList(o.getClass().getInterfaces()));
        adaptor = (MBeanServerConnection) o;
    } finally {
        ctx.close();
    }

    return adaptor;
}

From source file:de.interseroh.report.test.security.LdapServerTest.java

@Test
public void testJndiSun() throws NamingException {
    Hashtable<String, String> contextParams = new Hashtable<String, String>();
    contextParams.put(Context.PROVIDER_URL, "ldap://ldap.xxx:389");
    contextParams.put(Context.SECURITY_PRINCIPAL, USER_LDAP);
    contextParams.put(Context.SECURITY_CREDENTIALS, PASSWORD_LDAP);
    contextParams.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

    DirContext dirContext = new InitialDirContext(contextParams);

    Attributes attributes = dirContext.getAttributes("", new String[] { "namingContexts" });
    Attribute attribute = attributes.get("namingContexts");
    NamingEnumeration<?> all = attribute.getAll();
    while (all.hasMore()) {
        String next = (String) all.next();
        logger.info(next);/*  www  .ja va  2 s . c om*/
    }
}

From source file:net.sf.ehcache.distribution.JNDIRMICacheManagerPeerListener.java

/**
 * Gets the initial context/*  www .  j  a  v a2  s.  c o  m*/
 *
 * @return an initial context
 * @throws NamingException if JNDI goes wrong
 */
private Context getInitialContext() throws NamingException {
    String initialContextFactory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);

    if (initialContextFactory != null && initialContextFactory.startsWith("net.sf.ehcache")) {
        // Put Context.PROVIDER_URL so unit tests can work
        Hashtable hashTable = new Hashtable();
        hashTable.put(Context.PROVIDER_URL, "//localhost:" + port);
        return new InitialContext(hashTable);
    }
    return new InitialContext();
}

From source file:org.apache.juddi.v3.auth.LdapExpandedAuthenticator.java

public void init(String url) throws NamingException, ConfigurationException {
    env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, AppConfig.getConfiguration()
            .getString(Property.JUDDI_AUTHENTICATOR_INITIAL_CONTEXT, "com.sun.jndi.ldap.LdapCtxFactory"));
    env.put(Context.SECURITY_AUTHENTICATION,
            AppConfig.getConfiguration().getString(Property.JUDDI_AUTHENTICATOR_STYLE, "simple"));
    env.put(Context.PROVIDER_URL, url); // organization ldap url, example ldap://localhost:389

    this.url = url;

    try {/*from ww w.  j  ava2s  . c  o  m*/
        ctx = new InitialLdapContext(env, null);
    } catch (NamingException e) {
        logger.error("Naming exception " + e);
        throw e;
    }
}

From source file:org.wso2.mb.integration.common.clients.operations.queue.QueueMessageReceiver.java

public QueueMessageReceiver(String connectionString, String hostName, String port, String userName,
        String password, String queueName, int ackMode, boolean useMessageListener,
        AtomicInteger messageCounter, int delayBetweenMessages, int printNumberOfMessagesPer,
        boolean isToPrintEachMessage, String fileToWriteReceivedMessages, int stopAfter, int ackAfterEach,
        int commitAfterEach, int rollbackAfterEach) {

    this.hostName = hostName;
    this.port = port;
    this.connectionString = connectionString;
    this.useMessageListener = useMessageListener;
    this.delayBetweenMessages = delayBetweenMessages;
    this.messageCounter = messageCounter;
    this.queueName = queueName;
    this.printNumberOfMessagesPer = printNumberOfMessagesPer;
    this.isToPrintEachMessage = isToPrintEachMessage;
    this.fileToWriteReceivedMessages = fileToWriteReceivedMessages;
    this.stopAfter = stopAfter;
    this.ackAfterEach = ackAfterEach;
    this.commitAfterEach = commitAfterEach;
    this.rollbackAfterEach = rollbackAfterEach;

    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, QPID_ICF);
    properties.put(CF_NAME_PREFIX + CF_NAME, getTCPConnectionURL(userName, password));
    properties.put("queue." + queueName, queueName);

    try {/*from  w w w  .  j av a 2  s .c o  m*/
        InitialContext ctx = new InitialContext(properties);
        // Lookup connection factory
        QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup(CF_NAME);
        queueConnection = connFactory.createQueueConnection();
        queueConnection.start();
        if (ackMode == QueueSession.SESSION_TRANSACTED) {
            queueSession = queueConnection.createQueueSession(true, ackMode);
        } else {
            queueSession = queueConnection.createQueueSession(false, ackMode);
        }
        Queue queue = (Queue) ctx.lookup(queueName);
        queueReceiver = queueSession.createReceiver(queue);

    } catch (NamingException e) {
        log.error("Error while looking up for queue", e);
    } catch (JMSException e) {
        log.error("Error while initializing queue connection", e);
    }

}

From source file:org.hyperic.hq.plugin.tomcat.JBossUtil.java

public static MBeanServerConnection getMBeanServerConnection(Properties config)
        throws NamingException, RemoteException {
    MBeanServerConnection adaptor;

    Properties props = new Properties();

    for (int i = 0; i < NAMING_PROPS.length; i++) {
        props.setProperty(NAMING_PROPS[i][0], NAMING_PROPS[i][1]);
    }//from ww  w  .  j ava2 s. co  m

    props.putAll(config);
    props.put("java.naming.provider.url", config.get("jmx.url"));

    if (props.getProperty(Context.SECURITY_PRINCIPAL) != null) {
        props.setProperty(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
    }

    InitialContext ctx = new InitialContext(props);

    try {
        Object o = ctx.lookup(props.getProperty(PROP_NAMING_CONNECTOR));
        log.debug("=> " + Arrays.asList(o.getClass().getInterfaces()));
        adaptor = (MBeanServerConnection) o;
    } finally {
        ctx.close();
    }

    return adaptor;
}