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:com.stratelia.silverpeas.silverStatisticsPeas.control.PerfVolumeTest.java

@BeforeClass
public static void setupDataSource() throws NamingException {
    SimpleMemoryContextFactory.setUpAsInitialContext();
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.postgresql.Driver");
    ds.setUsername("postgres");
    ds.setPassword("postgres");
    ds.setUrl("jdbc:postgresql://localhost:5432/extranet");
    InitialContext ic = new InitialContext();
    ic.bind("java:/datasources/silverpeas-jdbc", ds);
    ic.bind("java:/datasources/DocumentStoreDS", ds);
}

From source file:com.stratelia.silverpeas.silverStatisticsPeas.control.AbstractSpringDatasourceTest.java

private static void configureJNDIDatasource() throws Exception {
    springContext = new ClassPathXmlApplicationContext("/spring-silverpeas.xml",
            "/com/stratelia/silverpeas/silverStatisticsPeas/control/spring-h2-datasource.xml");
    datasource = springContext.getBean("dataSource", DataSource.class);
    InitialContext ic = new InitialContext();
    ic.bind("java:/datasources/silverpeas-jdbc", datasource);
}

From source file:io.apiman.servers.gateway_h2.Starter.java

/**
 * Ensure that the given name is bound to a context.
 * @param ctx/*from   ww  w . java  2 s.  c om*/
 * @param name
 * @throws NamingException
 */
private static void ensureCtx(InitialContext ctx, String name) throws NamingException {
    try {
        ctx.bind(name, new InitialContext());
    } catch (NameAlreadyBoundException e) {
        // this is ok
    }
}

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   w  ww  .j a  v  a2  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:io.apiman.servers.gateway_h2.Starter.java

/**
 * Creates a datasource and binds it to JNDI.
 *//*from   w  ww.j a v  a 2s  .c om*/
private static void createDataSource() {
    HikariConfig config = new HikariConfig("src/main/resources/hikari.properties");
    HikariDataSource ds = new HikariDataSource(config);

    //        Connection connection = null;
    //        try {
    //            connection = ds.getConnection();
    //            connection.setAutoCommit(true);
    //            
    //            PreparedStatement statement = connection.prepareStatement("SELECT * FROM information_schema.tables WHERE table_name = 'APIS'");
    //            ResultSet rs = statement.executeQuery();
    //            if (!rs.next()) {
    //                initDB(connection);
    //            }
    //            
    //            connection.close();
    //        } catch (Exception e1) {
    //            if (connection != null) {
    //                try { connection.close(); } catch (Exception e) {}
    //            }
    //            ds.close();
    //            throw new RuntimeException(e1);
    //        }

    try {
        InitialContext ctx = new InitialContext();
        ensureCtx(ctx, "java:/comp/env");
        ensureCtx(ctx, "java:/comp/env/jdbc");
        ctx.bind("java:/comp/env/jdbc/ApiGatewayDS", ds);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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);
        }//ww w  .  j a 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: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 ww w  .j av a2s .co  m

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

From source file:com.funambol.server.db.DataSourceContextHelper.java

/**
 * Binds the given object with the given name
 * @param obj the object to bind//from ww w .  j  av  a 2  s . co m
 * @param jndiName the name
 * @throws javax.naming.NamingException if an error occurs
 */
private static void bind(Object obj, String jndiName) throws NamingException {
    InitialContext context = new InitialContext();
    createSubcontexts(context, jndiName);
    context.bind(jndiName, obj);
}

From source file:io.apiman.gateway.engine.policies.BasicAuthJDBCTest.java

@BeforeClass
public static void setup() throws Exception {
    // Create a test datasource and bind it to JNDI
    BasicDataSource ds = createInMemoryDatasource();
    InitialContext ctx = TestUtil.initialContext();
    TestUtil.ensureCtx(ctx, "java:comp/env"); //$NON-NLS-1$
    TestUtil.ensureCtx(ctx, "java:comp/env/jdbc"); //$NON-NLS-1$
    ctx.bind("java:comp/env/jdbc/BasicAuthJDBCTest", ds); //$NON-NLS-1$
}