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:WeblogicDbServlet.java

public void init() throws ServletException {

    Context env = null;//from w  w w . j a v  a 2 s  .c  o m

    Hashtable ht = new Hashtable();
    ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
    //ht.put(Context.PROVIDER_URL,"t3://localhost:7001");

    try {

        env = new InitialContext(ht);
        pool = (javax.sql.DataSource) env.lookup("oracle-8i-athletes");

        if (pool == null)
            throw new ServletException("'oracle-8i-athletes' is an unknown DataSource");

    } catch (NamingException ne) {

        throw new ServletException(ne);

    }

}

From source file:org.mule.transport.ejb.SimpleEjbContextFactory.java

public Object create(Map<?, ?> properties) throws Exception {
    Hashtable<String, Object> env = new Hashtable<String, Object>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, MuleInitialContextFactory.class.getName());

    InitialContext context = new InitialContext(env);
    for (Map.Entry<?, ?> entry : properties.entrySet()) {
        Object key = entry.getKey();
        if (key instanceof String) {
            Object value = entry.getValue();
            logger.debug("Binding " + key + " to " + value);
            context.bind((String) key, value);
        }// w  w w  .  j  a  va2 s.  c o  m
    }

    return context;
}

From source file:JNDIUtil.java

/**
 * Get the JNDI Context.//from   w ww.  ja  va  2s.  co m
 * <p/>
 * Don't forget to close it when done!
 *
 * @param jndiProperties JNDI properties.
 * @return The context.
 * @throws javax.naming.NamingException Error getting context.
 */
public static Context getNamingContext(final Properties jndiProperties) throws NamingException {
    Context context;
    try {
        context = jndiProperties.isEmpty() ? new InitialContext() : new InitialContext(jndiProperties);
    } catch (NamingException e) {
        System.out.println(
                "NamingException while try to create initialContext. jndiProperties are " + jndiProperties + e);
        throw ((NamingException) new NamingException("Failed to load InitialContext: " + jndiProperties)
                .initCause(e));
    }
    if (context == null) {
        throw new NamingException("Failed to create JNDI context.  Check that '" + Context.PROVIDER_URL + "', '"
                + Context.INITIAL_CONTEXT_FACTORY + "', '" + Context.URL_PKG_PREFIXES
                + "' are correctly configured in the supplied JNDI properties.");
    }

    return context;
}

From source file:com.caricah.iotracah.datastore.IotDataSource.java

public void setupDatasource(String driver, String dbUrl, String username, String password)
        throws NamingException {

    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
    System.setProperty(Context.PROVIDER_URL, "file:////tmp");

    Context ctx = new InitialContext();

    // Construct DriverAdapterCPDS reference
    Reference cpdsRef = new Reference("org.apache.commons.dbcp2.cpdsadapter.DriverAdapterCPDS",
            "org.apache.commons.dbcp2.cpdsadapter.DriverAdapterCPDS", null);
    cpdsRef.add(new StringRefAddr("driver", driver));
    cpdsRef.add(new StringRefAddr("url", dbUrl));
    cpdsRef.add(new StringRefAddr("user", username));
    cpdsRef.add(new StringRefAddr("password", password));
    ctx.rebind("jdbc_cpds", cpdsRef);

    Reference ref = new Reference("org.apache.commons.dbcp2.datasources.SharedPoolDataSource",
            "org.apache.commons.dbcp2.datasources.SharedPoolDataSourceFactory", null);
    ref.add(new StringRefAddr("dataSourceName", "jdbc_cpds"));
    ref.add(new StringRefAddr("defaultMaxTotal", "100"));
    ref.add(new StringRefAddr("defaultMaxIdle", "30"));
    ref.add(new StringRefAddr("defaultMaxWaitMillis", "10000"));

    ctx.rebind("jdbc_commonpool", ref);

}

From source file:org.dhatim.util.JNDIUtil.java

/**
 * Get the JNDI Context./* ww  w. j  av  a  2 s  . c  o m*/
 * <p/>
 * Don't forget to close it when done!
 *
 * @param jndiProperties JNDI properties.
 * @return The context.
 * @throws javax.naming.NamingException Error getting context.
 */
public static Context getNamingContext(final Properties jndiProperties) throws NamingException {
    Context context;
    try {
        context = jndiProperties.isEmpty() ? new InitialContext() : new InitialContext(jndiProperties);
    } catch (NamingException e) {
        logger.error("NamingException while try to create initialContext. jndiProperties are " + jndiProperties,
                e);
        throw ((NamingException) new NamingException("Failed to load InitialContext: " + jndiProperties)
                .initCause(e));
    }
    if (context == null) {
        throw new NamingException("Failed to create JNDI context.  Check that '" + Context.PROVIDER_URL + "', '"
                + Context.INITIAL_CONTEXT_FACTORY + "', '" + Context.URL_PKG_PREFIXES
                + "' are correctly configured in the supplied JNDI properties.");
    }

    return context;
}

From source file:cyrille.jndi.LdapTest.java

@Test
public void test() 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://localhost:389");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
    env.put(Context.SECURITY_CREDENTIALS, "secret");
    DirContext dirContext = new InitialDirContext(env);

    Attributes attributes = dirContext.getAttributes("uid=aeinstein,ou=Users,dc=example,dc=com");
    for (NamingEnumeration<Attribute> attributesEnumeration = (NamingEnumeration<Attribute>) attributes
            .getAll(); attributesEnumeration.hasMore();) {
        Attribute attribute = attributesEnumeration.next();
        System.out.print(attribute.getID() + "=");

        for (NamingEnumeration<?> attributeValues = attribute.getAll(); attributeValues.hasMore();) {
            Object value = attributeValues.next();
            if (value instanceof byte[] && "userpassword".equals(attribute.getID())) {
                byte[] bytes = (byte[]) value;
                System.out.print(new String(bytes) + ", ");
            } else {
                System.out.print(value + ", ");
            }/*from w  w w. j a  v  a2  s .  c  o  m*/
        }
        System.out.println();
    }
}

From source file:org.wso2.carbon.metrics.data.service.MetricsDataServiceTest.java

public static Test suite() {
    return new TestSetup(new TestSuite(MetricsDataServiceTest.class)) {

        protected void setUp() throws Exception {
            DataSource dataSource = JdbcConnectionPool.create("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "sa", "");
            template = new JdbcTemplate(dataSource);
            ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
            populator.addScript(new ClassPathResource("dbscripts/h2.sql"));
            populator.populate(dataSource.getConnection());

            // Create initial context
            System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
            System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
            InitialContext ic = new InitialContext();
            ic.createSubcontext("jdbc");
            ic.bind("jdbc/WSO2MetricsDB", dataSource);
        }//  w  w w.ja  v  a  2 s .  co m

        protected void tearDown() throws Exception {
            InitialContext ic = new InitialContext();
            ic.unbind("jdbc/WSO2MetricsDB");
            ic.unbind("jdbc");
        }
    };
}

From source file:io.lavagna.service.LdapConnection.java

InitialDirContextCloseable context(String providerUrl, String principal, String password)
        throws NamingException {
    Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, providerUrl);
    env.put(Context.SECURITY_PRINCIPAL, principal);
    env.put(Context.SECURITY_CREDENTIALS, password);
    return new InitialDirContextCloseable(env);
}

From source file:org.rimudb.JNDIPoolTests.java

@Before
public void setUp() throws Exception {
    try {//from  w ww  .  j  av a 2s. c  o m
        // Create initial context
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.osjava.sj.memory.MemoryContextFactory");
        System.setProperty("org.osjava.sj.jndi.shared", "true");
        InitialContext ic = new InitialContext();

        ic.createSubcontext("java:/comp/env/");

        com.mysql.jdbc.jdbc2.optional.MysqlDataSource mysql_datasource = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource();
        mysql_datasource.setServerName("localhost");
        mysql_datasource.setPortNumber(3306);
        mysql_datasource.setDatabaseName("dbtest");
        mysql_datasource.setUser("dbtest");
        mysql_datasource.setPassword("dbtest");

        // Create a connection pool datasource
        org.h2.jdbcx.JdbcDataSource h2_datasource = new org.h2.jdbcx.JdbcDataSource();
        h2_datasource.setURL("jdbc:h2:mem:test");
        h2_datasource.setUser("sa");
        h2_datasource.setPassword("");

        ic.bind("jdbc/dbtest", mysql_datasource);
    } catch (NamingException ex) {
        log.error(ex);
    }
}

From source file:eionet.gdem.dcm.conf.LdapTest.java

protected DirContext getDirContext() throws NamingException {
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, url);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    DirContext ctx = new InitialDirContext(env);
    return ctx;/*w ww  .  j  a  va2  s.co  m*/
}