Example usage for javax.naming InitialContext createSubcontext

List of usage examples for javax.naming InitialContext createSubcontext

Introduction

In this page you can find the example usage for javax.naming InitialContext createSubcontext.

Prototype

public Context createSubcontext(Name name) throws NamingException 

Source Link

Usage

From source file:org.wso2.carbon.metrics.jdbc.core.BaseReporterTest.java

@BeforeSuite
protected static void init() throws Exception {
    if (logger.isInfoEnabled()) {
        logger.info("Initializing the data source and populating data");
    }/*from  ww w .  java 2  s .  c  om*/
    // Setup 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);

    if (logger.isInfoEnabled()) {
        logger.info("Creating Metrics");
    }
    metrics = new Metrics(TestUtils.getConfigProvider("metrics.yaml"));
    metrics.activate();
    metricService = metrics.getMetricService();
    metricManagementService = metrics.getMetricManagementService();
}

From source file:org.modeshape.connector.meta.jdbc.DatasourceHelper.java

public static void bindInJNDI(String name) throws NamingException {
    // Create initial context
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.commons.naming.java.javaURLContextFactory");
    System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
    InitialContext ic = new InitialContext();

    ic.createSubcontext("java:");
    ic.bind("java:/" + name, dataSource);
}

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.j  ava2  s  . c  om*/

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

From source file:org.wso2.carbon.metrics.impl.ReporterTest.java

public static Test suite() {
    return new TestSetup(new TestSuite(ReporterTest.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);

            // Set setup system property to cover database creator logic
            System.setProperty("setup", "");
        }//from www .j  a  v  a  2 s  .  c om

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

From source file:org.wso2.carbon.apimgt.hybrid.gateway.usage.publisher.dao.UploadedUsageFileInfoDAOTest.java

private static void initializeDatabase(String configFilePath) {
    InputStream in;/*from  w  w  w  .j a v a 2s  .  c  om*/
    try {
        in = FileUtils.openInputStream(new File(configFilePath));
        StAXOMBuilder builder = new StAXOMBuilder(in);
        String dataSource = builder.getDocumentElement().getFirstChildWithName(new QName("DataSourceName"))
                .getText();
        OMElement databaseElement = builder.getDocumentElement().getFirstChildWithName(new QName("Database"));
        String databaseURL = databaseElement.getFirstChildWithName(new QName("URL")).getText();
        String databaseUser = databaseElement.getFirstChildWithName(new QName("Username")).getText();
        String databasePass = databaseElement.getFirstChildWithName(new QName("Password")).getText();
        String databaseDriver = databaseElement.getFirstChildWithName(new QName("Driver")).getText();

        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName(databaseDriver);
        basicDataSource.setUrl(databaseURL);
        basicDataSource.setUsername(databaseUser);
        basicDataSource.setPassword(databasePass);

        // Create initial context
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
        System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
        try {
            InitialContext.doLookup("java:/comp/env/jdbc/WSO2AM_DB");
        } catch (NamingException e) {
            InitialContext ic = new InitialContext();
            ic.createSubcontext("java:");
            ic.createSubcontext("java:/comp");
            ic.createSubcontext("java:/comp/env");
            ic.createSubcontext("java:/comp/env/jdbc");
            ic.bind("java:/comp/env/jdbc/WSO2AM_DB", basicDataSource);
        }
    } catch (XMLStreamException | IOException | NamingException e) {
        log.error(e);
    }
}

From source file:org.wso2.extension.siddhi.store.rdbms.util.RDBMSTableTestUtils.java

public static void setupJNDIDatasource(String url, String driverClassName) {
    try {/*from   w  w  w  .  j  a  v  a  2 s .  c  om*/
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
        System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
        InitialContext context = new InitialContext();
        context.createSubcontext("java:");
        context.createSubcontext("java:comp");
        context.createSubcontext("java:comp/env");
        context.createSubcontext("java:comp/env/jdbc");
        Properties connectionProperties = new Properties();
        connectionProperties.setProperty("jdbcUrl", url);
        connectionProperties.setProperty("dataSource.user", user);
        connectionProperties.setProperty("dataSource.password", password);
        connectionProperties.setProperty("driverClassName", driverClassName);
        connectionProperties.setProperty("poolName", "JNDI_Pool");
        HikariConfig config = new HikariConfig(connectionProperties);
        DataSource testDataSourceJNDI = new HikariDataSource(config);
        context.bind(JNDI_RESOURCE, testDataSourceJNDI);
    } catch (NamingException e) {
        log.error("Error while bind the datasource as JNDI resource." + e.getMessage(), e);
    }
}

From source file:org.grouter.common.jndi.JNDIUtils.java

public static void createInMemoryJndiProvider(List<BindingItem> bindings) {
    if (bindings == null) {
        throw new IllegalArgumentException("Can not bind null to jndi tree.");
    }//from   ww  w  . j  a va2 s  . c o m
    try {
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                "org.apache.commons.naming.java.javaURLContextFactory");
        System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");

        Iterator<BindingItem> bindingItemIterator = bindings.iterator();
        while (bindingItemIterator.hasNext()) {
            BindingItem item = bindingItemIterator.next();
            InitialContext initialContext = new InitialContext();
            logger.debug("Creating component context : " + item.getJndipath()[0]);
            Context compContext = initialContext.createSubcontext(item.getJndipath()[0]);
            logger.debug("Creating environment context : " + item.getJndipath()[1]);
            Context envContext = compContext.createSubcontext(item.getJndipath()[1]);
            logger.debug("Bidning : " + item.getJndiName() + " to implementation : " + item.getImplemenation());
            envContext.bind(item.getJndiName(), item.getImplemenation());
            printJNDI(envContext, logger);
            //logger.debug(envContext.listBindings(initialContext.getNameInNamespace()));
        }

    } catch (NamingException e) {
        logger.error(e, e);
    }

}

From source file:org.wso2.carbon.apimgt.impl.dao.test.CertificateMgtDaoTest.java

private static void initializeDatabase(String configFilePath)
        throws IOException, XMLStreamException, NamingException {

    InputStream in;//from   www  . j  av  a 2  s .c o m
    in = FileUtils.openInputStream(new File(configFilePath));
    StAXOMBuilder builder = new StAXOMBuilder(in);
    String dataSource = builder.getDocumentElement().getFirstChildWithName(new QName("DataSourceName"))
            .getText();
    OMElement databaseElement = builder.getDocumentElement().getFirstChildWithName(new QName("Database"));
    String databaseURL = databaseElement.getFirstChildWithName(new QName("URL")).getText();
    String databaseUser = databaseElement.getFirstChildWithName(new QName("Username")).getText();
    String databasePass = databaseElement.getFirstChildWithName(new QName("Password")).getText();
    String databaseDriver = databaseElement.getFirstChildWithName(new QName("Driver")).getText();

    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName(databaseDriver);
    basicDataSource.setUrl(databaseURL);
    basicDataSource.setUsername(databaseUser);
    basicDataSource.setPassword(databasePass);

    // Create initial context
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
    System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
    try {
        InitialContext.doLookup("java:/comp/env/jdbc/WSO2AM_DB");
    } catch (NamingException e) {
        InitialContext ic = new InitialContext();
        ic.createSubcontext("java:");
        ic.createSubcontext("java:/comp");
        ic.createSubcontext("java:/comp/env");
        ic.createSubcontext("java:/comp/env/jdbc");

        ic.bind("java:/comp/env/jdbc/WSO2AM_DB", basicDataSource);
    }
}

From source file:org.rimudb.JNDIPoolTests.java

@Before
public void setUp() throws Exception {
    try {/*  w  w  w.j a  v  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:com.dattack.naming.StandaloneJndiTest.java

@Test
public void testCreateContext() {
    try {/*from  w w  w  .ja  v  a2  s .  c  o m*/
        final InitialContext context = new InitialContext();
        final String name = "testCreateContext";
        final Context subcontext = context.createSubcontext(name);
        assertNotNull(subcontext);
    } catch (final NamingException e) {
        fail(e.getMessage());
    }
}