Example usage for javax.naming InitialContext bind

List of usage examples for javax.naming InitialContext bind

Introduction

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

Prototype

public void bind(Name name, Object obj) throws NamingException 

Source Link

Usage

From source file:org.bibsonomy.lucene.util.JNDITestDatabaseBinder.java

private static void bindDatabaseContext(final String contextName, final String fileName) {
    final InitialContext ctx;
    final DataSource ds = getBasicDataSource(fileName);
    try {/*from   ww  w.j ava 2s.  c o m*/
        // create Mock JNDI context
        MockContextFactory.setAsInitial();
        ctx = new InitialContext();
        ctx.bind("java:comp/env/jdbc/" + contextName, ds);
        ctx.bind("java:/comp/env/jdbc/" + contextName, ds);
    } catch (NamingException ex) {
        log.error("Error when trying to bind test database connection '" + contextName + "' via JNDI");
        log.error(ex.getMessage());
    }

}

From source file:net.sf.jabb.util.db.ConnectionUtility.java

/**
 * Create DataSource and bind it to JNDI
 * @param source   configuration//from  w ww.j  av a  2s  .c om
 * @param jndiName   JNDI name that the DataSource needs to be bind to
 * @return   The DataSource created
 */
public static DataSource createDataSource(String source, String jndiName) {
    DataSource ds = createDataSource(source);
    if (ds != null && jndiName != null) {
        InitialContext ic;
        try {
            ic = new InitialContext();
            ic.bind(jndiName, ds);
        } catch (NamingException e) {
            log.error("Failed to bind data source '" + source + "' to JNDI name: " + jndiName, e);
        }
    }
    return ds;
}

From source file:io.apiman.gateway.engine.jdbc.JdbcMetricsTest.java

@BeforeClass
public static void setup() {
    try {//  ww w. j av a2s .co m
        InitialContext ctx = TestUtil.initialContext();
        TestUtil.ensureCtx(ctx, "java:/comp/env");
        TestUtil.ensureCtx(ctx, "java:/comp/env/jdbc");
        ds = createInMemoryDatasource();
        ctx.bind(DB_JNDI_LOC, ds);
        System.out.println("DataSource created and bound to JNDI: " + DB_JNDI_LOC);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}

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

private static void initializeDatabase(String configFilePath) {
    InputStream in;/*from  ww  w  .ja v  a2s . co 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 | IOException | NamingException e) {
        log.error(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 a  va 2  s .  c om*/
    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.wso2.extension.siddhi.store.rdbms.util.RDBMSTableTestUtils.java

public static void setupJNDIDatasource(String url, String driverClassName) {
    try {// w  w  w  . j ava  2s  . 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:net.chrisrichardson.foodToGo.ejb3.jndi.SpringBeanToJndiExporter.java

public void afterPropertiesSet() throws Exception {
    InitialContext ctx = new InitialContext();
    FactoryBean factoryBean = (FactoryBean) beanFactory.getBean(beanName + "&");
    Class type = beanFactory.getType(beanName);
    ctx.bind(jndiName, new ReferenceToSpringBean(type, beanName));
}

From source file:org.overlord.dtgov.devsvr.bpm.DTGovBpmDevServer.java

/**
 * @see org.overlord.commons.dev.server.DevServer#preConfig()
 *//*ww w. j a  va  2  s .c  om*/
@Override
protected void preConfig() {
    // Add JNDI resources
    try {
        InitialContext ctx = new InitialContext();
        InitialContext jbossCtx = new InitialContext();
        ctx.bind("java:jboss", jbossCtx);
        jbossCtx.bind("datasources", new InitialContext());
        ds = createInMemoryDatasource();
        ctx.bind("java:jboss/datasources/jbpmDS", ds);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.exoplatform.services.naming.InitialContextTest.java

public void testCompositeNameUsing() throws Exception {
    Name name = new CompositeName("java:comp/env/jdbc/jcr");
    Enumeration en = name.getAll();
    while (en.hasMoreElements()) {
        en.nextElement();//from   w w  w  .j  a  v a 2  s.c o m
    }
    InitialContext ctx = new InitialContext();
    ctx.bind(name, "foo");
    assertEquals("foo", ctx.lookup(name));
    try {
        ctx.bind(name, "foo2");
        fail("A NameAlreadyBoundException is expected here");
    } catch (NameAlreadyBoundException e) {
        // expected exception
    }
    assertEquals("foo", ctx.lookup(name));
    assertEquals("foo", ctx.lookup("java:comp/env/jdbc/jcr"));
    ctx.unbind(name);
    try {
        ctx.lookup(name);
        fail("A NameNotFoundException is expected here");
    } catch (NameNotFoundException e) {
        // expected exception
    }
}

From source file:net.chrisrichardson.foodToGo.ejb3.facadeWithSpringDI.SpringBeanReferenceInitializer.java

private void bind(InitialContext ctx, String jndiName, Class type, String beanName) throws NamingException {
    Reference reference = new Reference(type.getName(), new StringRefAddr("beanName", beanName),
            SpringObjectFactory.class.getName(), null);
    ctx.bind(jndiName, reference);
    jndiNames.add(jndiName);/*from   www.  j  a v a 2 s  . c  o  m*/
    logger.debug("Bound it: " + jndiName);
}