Example usage for org.springframework.mock.jndi SimpleNamingContextBuilder activate

List of usage examples for org.springframework.mock.jndi SimpleNamingContextBuilder activate

Introduction

In this page you can find the example usage for org.springframework.mock.jndi SimpleNamingContextBuilder activate.

Prototype

public void activate() throws IllegalStateException, NamingException 

Source Link

Document

Register the context builder by registering it with the JNDI NamingManager.

Usage

From source file:fi.nls.fileservice.web.IntegrationTest.java

@BeforeClass
public static void setUp() throws IllegalStateException, NamingException {
    // Register in mock JNDI
    // an embedded HSQLDB with PostgreSQL syntax enabled
    DataSource dataSource = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
            .setName("tiepaldb;sql.syntax_pgs=true").addScript("file:../resources/sql/create-tables.sql")
            .addScript("file:../resources/sql/data-hsql.sql").build();

    SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
    builder.bind("java:comp/env/jdbc/tiepaldb", dataSource);
    builder.activate();

}

From source file:com.redhat.rhtracking.persistance.unittest.JPATestUtils.java

public static void mockJndiDatasource() throws PropertyVetoException, NamingException, IllegalStateException {
    SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();

    ComboPooledDataSource pool = new ComboPooledDataSource();
    pool.setDriverClass("org.postgresql.Driver");
    pool.setJdbcUrl("jdbc:postgresql://localhost:5432/rhtracking");
    pool.setUser("dev02");
    pool.setPassword("abcd");
    builder.bind("java:comp/env/jdbc/rhtracking", pool);
    builder.bind("java:/jdbc/rhtracking", pool);
    builder.activate();
}

From source file:org.uimafit.factory.ExternalResourceFactoryTest.java

@BeforeClass
public static void initJNDI() throws Exception {
    // Set up JNDI context to test the JndiResourceLocator
    final SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
    Properties deDict = new Properties();
    deDict.setProperty("Hans", "proper noun");
    builder.bind("dictionaries/german", deDict);
    builder.activate();
}

From source file:sk.lazyman.gizmo.JndiH2DataSource.java

public void init() throws NamingException {
    SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
    builder.bind("java:comp/env/jdbc/GizmoDB", dataSource);
    builder.activate();
}

From source file:org.devgateway.toolkit.persistence.spring.DatabaseConfiguration.java

/**
 * This bean creates the JNDI tree and registers the
 * {@link javax.sql.DataSource} to this tree. This allows Pentaho Classic
 * Engine to use a {@link javax.sql.DataSource} ,in our case backed by a
 * connection pool instead of always opening up JDBC connections. Should
 * significantly improve performance of all classic reports. In PRD use
 * connection type=JNDI and name toolkitDS. To use it in PRD you need to add
 * the configuration to the local PRD. Edit
 * ~/.pentaho/simple-jndi/default.properties and add the following:
 * toolkitDS/type=javax.sql.DataSource/*from w  ww  .  j av  a2  s .  c om*/
 * toolkitDS/driver=org.apache.derby.jdbc.ClientDriver toolkitDS/user=app
 * toolkitDS/password=app
 * toolkitDS/url=jdbc:derby://localhost//derby/toolkit
 *
 * @return
 */
@Bean
public SimpleNamingContextBuilder jndiBuilder() {
    SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
    builder.bind(datasourceJndiName, dataSource());
    try {
        builder.activate();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NamingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return builder;
}

From source file:dk.teachus.backend.test.SpringTestCase.java

@Override
protected String[] getConfigLocations() {
    final List<String> configLocations = new ArrayList<String>();

    configLocations.add("/dk/teachus/backend/applicationContext.xml");
    configLocations.add("/dk/teachus/backend/dao/hibernate/applicationContext-hibernate.xml");
    configLocations.add("/dk/teachus/frontend/applicationContext-frontend.xml");

    DataSource dataSource = null;
    if (SpringTestCase.useMysql) {
        final MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
        ds.setUrl("jdbc:mysql://localhost/teachus_test");
        ds.setUser("teachus_build");
        ds.setPassword("teachus_build");
        dataSource = ds;/*from  ww  w .j a v  a2s. c  o  m*/
        configLocations.add("/dk/teachus/backend/test/applicationContext-test-mysql.xml");
    } else {
        dataSource = new SimpleDriverDataSource(new jdbcDriver(), "jdbc:hsqldb:mem:teachus", "sa", "");
        configLocations.add("/dk/teachus/backend/test/applicationContext-test-hsqldb.xml");
    }

    final SimpleNamingContextBuilder contextBuilder = new SimpleNamingContextBuilder();
    contextBuilder.bind("java:comp/env/jdbc/teachus", dataSource);
    try {
        contextBuilder.activate();
    } catch (final IllegalStateException e) {
        throw new RuntimeException(e);
    } catch (final NamingException e) {
        throw new RuntimeException(e);
    }

    addConfigLocations(configLocations);

    return configLocations.toArray(new String[configLocations.size()]);
}

From source file:org.codehaus.groovy.grails.cli.jndi.JndiBindingSupport.java

/**
 * Bindings a JNDI context./*w  w  w.  ja  v  a 2s.co  m*/
 *
 * @return The bound JNDI context
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
Object bind() {
    SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();

    if (jndiConfig != null) {
        // ensure the commons-dbcp factory is used
        System.setProperty("javax.sql.DataSource.Factory", "org.apache.commons.dbcp.BasicDataSourceFactory");

        for (Object o : jndiConfig.entrySet()) {
            Map.Entry entry = (Map.Entry) o;

            Object propsObj = entry.getValue();

            final String entryName = entry.getKey().toString();
            if (propsObj instanceof Map) {
                Map<String, Object> props = (Map) propsObj;
                Object typeObj = props.get(TYPE);

                if (typeObj != null) {
                    props.remove(TYPE);
                    String type = typeObj.toString();

                    JndiBindingHandler handler = jndiBinders.get(type);
                    if (handler != null) {
                        handler.handleBinding(builder, entryName, props);
                    } else {
                        try {
                            Class<?> c = Class.forName(type, true,
                                    Thread.currentThread().getContextClassLoader());
                            Object beanObj = BeanUtils.instantiate(c);
                            bindProperties(beanObj, props);
                            builder.bind(entryName, beanObj);
                        } catch (BeanInstantiationException e) {
                            // ignore
                        } catch (ClassNotFoundException e) {
                            // ignore
                        }
                    }
                }
            } else {
                builder.bind(entryName, propsObj);
            }
        }
    }

    try {
        builder.activate();
        return builder.createInitialContextFactory(null).getInitialContext(null);
    } catch (Exception e) {
        return null;
    }
}

From source file:org.springframework.mock.jndi.SimpleNamingContextBuilder.java

/**
 * If no SimpleNamingContextBuilder is already configuring JNDI,
 * create and activate one. Otherwise take the existing activate
 * SimpleNamingContextBuilder, clear it and return it.
 * <p>This is mainly intended for test suites that want to
 * reinitialize JNDI bindings from scratch repeatedly.
 * @return an empty SimpleNamingContextBuilder that can be used
 * to control JNDI bindings/*from w ww .j  av a  2  s  . c o m*/
 */
public static SimpleNamingContextBuilder emptyActivatedContextBuilder() throws NamingException {
    SimpleNamingContextBuilder builder = activated;
    if (builder != null) {
        // Clear already activated context builder.
        builder.clear();
    } else {
        // Create and activate new context builder.
        builder = new SimpleNamingContextBuilder();
        // The activate() call will cause an assignment to the activated variable.
        builder.activate();
    }
    return builder;
}