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:com.dattack.naming.StandaloneJndiTest.java

@Test
public void testCreateMultiContext() {
    try {//w  ww . j  av  a  2  s .c om
        final InitialContext context = new InitialContext();
        final String name = getCompositeName(VALID_CONTEXT, "testCreateMultiContext");
        final Context subcontext = context.createSubcontext(name);
        assertNotNull(subcontext);
    } catch (final NamingException e) {
        fail(e.getMessage());
    }
}

From source file:org.settings4j.connector.JNDIConnectorTest.java

/**
 * SmokeTest: tests if the {@link #setTomcatJNDIContextProperties()} works which is required for all other
 * TestCases.//from  www. jav  a 2s  .  c om
 *
 * @throws Exception in case of an error
 */
@Test
public void testSmokeTest() throws Exception {
    LOG.info("START: testJNDIConnectorWithoutJNDI");
    // Set JNDI Tomcat Configs
    setTomcatJNDIContextProperties();
    final InitialContext initialContext = new InitialContext();
    Context tmpCtx = initialContext.createSubcontext("java:");
    tmpCtx = tmpCtx.createSubcontext("comp");
    tmpCtx = tmpCtx.createSubcontext("env");
    tmpCtx.bind("testKey", "testValue");

    // start getting the JNDI Resource
    final Context envCtx = (Context) new InitialContext().lookup("java:/comp/env");
    final Object testValue = envCtx.lookup("testKey");

    assertThat(testValue, is((Object) "testValue"));

}

From source file:com.redhat.lightblue.rest.crud.ITCaseCrudResourceRDBMSTest.java

@Before
public void setup() throws Exception {
    File folder = new File("/tmp");
    File[] files = folder.listFiles(new FilenameFilter() {
        @Override//from w w w .  jav  a  2s .  com
        public boolean accept(final File dir, final String name) {
            return name.startsWith("test.db");
        }
    });
    for (final File file : files) {
        if (!file.delete()) {
            System.out.println("Failed to remove " + file.getAbsolutePath());
        }
    }

    mongo.dropDatabase(DB_NAME);
    mongo.dropDatabase("local");
    mongo.dropDatabase("admin");
    mongo.dropDatabase("mongo");
    mongo.getDB(DB_NAME).dropDatabase();
    mongo.getDB("local").dropDatabase();
    mongo.getDB("admin").dropDatabase();
    mongo.getDB("mongo").dropDatabase();

    db.getCollection(MongoMetadata.DEFAULT_METADATA_COLLECTION).remove(new BasicDBObject());
    mongo.getDB("mongo").getCollection("metadata").remove(new BasicDBObject());

    db.createCollection(MongoMetadata.DEFAULT_METADATA_COLLECTION, null);
    BasicDBObject index = new BasicDBObject("name", 1);
    index.put("version.value", 1);
    db.getCollection(MongoMetadata.DEFAULT_METADATA_COLLECTION).ensureIndex(index, "name", true);

    if (notRegistered) {
        notRegistered = false;
        try {
            // Create initial context
            System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
            System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
            // already tried System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.as.naming.InitialContextFactory");
            InitialContext ic = new InitialContext();

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

            JdbcConnectionPool ds = JdbcConnectionPool.create(
                    "jdbc:h2:file:/tmp/test.db;FILE_LOCK=NO;MVCC=TRUE;DB_CLOSE_ON_EXIT=TRUE", "sa", "sasasa");

            ic.bind("java:/mydatasource", ds);
        } catch (NamingException ex) {
            throw new IllegalStateException(ex);
        }
    } else {
        Context initCtx = new InitialContext();
        DataSource ds = (DataSource) initCtx.lookup("java:/mydatasource");
        Connection conn = ds.getConnection();
        Statement stmt = conn.createStatement();
        stmt.execute("DROP ALL OBJECTS ");
        stmt.close();
    }
}

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

private void initializeDatabase(String configFilePath) {

    InputStream in = null;//from   w w  w .j  a va2s  . c o m
    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 e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

From source file:org.mifos.framework.ApplicationInitializer.java

private void initJNDIforPentaho(ApplicationContext applicationContext) {
    try {//from w ww. j  a  va  2  s.  co m
        InitialContext ic = new InitialContext();

        //check if ds is already bound
        boolean dataSourceBound = true;
        boolean dataSourceBoundDw = true;
        try {
            DataSource datasource = (DataSource) ic.lookup("jdbc/SourceDB");

            if (datasource != null) {
                dataSourceBound = true;
            }
        } catch (Exception ex) {
            dataSourceBound = false;
        }
        try {
            DataSource datasourcedw = (DataSource) ic.lookup("jdbc/DestinationDB");
            if (datasourcedw != null) {
                dataSourceBoundDw = true;
            }
        } catch (Exception ex) {
            dataSourceBoundDw = false;
        }

        if (!dataSourceBound) {
            Object dataSource = applicationContext.getBean("dataSource");

            try {
                ic.createSubcontext("jdbc");
            } catch (NameAlreadyBoundException ex) {
                logger.info("Subcontext jdbc was already bound");
            }

            ic.bind("jdbc/SourceDB", dataSource);
            logger.info("Bound datasource to jdbc/SourceDB");
        } else {
            logger.info("jdbc/SourceDB is already bound");
        }

        if (!dataSourceBoundDw) {

            Object dataSourcedw = applicationContext.getBean("dataSourcePentahoDW");

            try {
                ic.createSubcontext("jdbc");
            } catch (NameAlreadyBoundException ex) {
                logger.info("Subcontext jdbc was already bound");
            }

            ic.bind("jdbc/DestinationDB", dataSourcedw);
            logger.info("Bound datasource to jdbc/DestinationDB");
        } else {
            logger.info("jdbc/DestinationDB is already bound");
        }
    } catch (Exception ex) {
        logger.error("Unable to bind dataSource to JNDI");
    }
}