Example usage for javax.naming InitialContext InitialContext

List of usage examples for javax.naming InitialContext InitialContext

Introduction

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

Prototype

public InitialContext() throws NamingException 

Source Link

Document

Constructs an initial context.

Usage

From source file:com.nabla.wapp.server.database.TomcatConnectionPool.java

/**
 * Constructor/*from   w  ww.j  a  v a  2 s  .c  om*/
 * @param dbName         - database name as defined in pool
 * @throws SQLException 
 */
public TomcatConnectionPool(final String dbName) throws SQLException {
    Assert.argumentNotNull(dbName, "Have you set the database name in your web.xml file?");

    try {
        final Context initContext = new InitialContext();
        final Context ctx = (Context) initContext.lookup("java:/comp/env");
        dataSource = (DataSource) ctx.lookup("jdbc/" + dbName);
    } catch (NamingException e) {
        if (log.isDebugEnabled())
            log.debug("fail to get datasource '" + dbName + "'", e);
        throw new SQLException("fail to get database '" + dbName + "'");
    }
}

From source file:com.alliander.osgp.adapter.protocol.oslp.application.config.OsgpProtocolAdapterOslpInitializer.java

@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
    try {//  w w w  . j  a  va2s . c om
        // Force the timezone of application to UTC (required for
        // Hibernate/JDBC)
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

        final Context initialContext = new InitialContext();

        final String logLocation = (String) initialContext
                .lookup("java:comp/env/osp/osgpAdapterProtocolOslp/log-config");
        LogbackConfigurer.initLogging(logLocation);

        final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(ApplicationContext.class);

        servletContext.addListener(new ContextLoaderListener(rootContext));

    } catch (final NamingException e) {
        throw new ServletException("naming exception", e);
    } catch (final FileNotFoundException e) {
        throw new ServletException("Logging file not found", e);
    } catch (final JoranException e) {
        throw new ServletException("Logback exception", e);
    }
}

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

public void create() throws Exception {
    logger.debug("Inside ServicePOJOImpl.create()");
    InitialContext ctx = new InitialContext();
    bind(ctx, "PlaceOrderService", PlaceOrderService.class, "PlaceOrderService");
    bind(ctx, "RestaurantRepository", RestaurantRepository.class, "RestaurantRepositoryImpl");
    bind(ctx, "PlaceOrderFacadeResultFactory", PlaceOrderFacadeResultFactory.class,
            "PlaceOrderFacadeResultFactoryImpl");

}

From source file:com.alliander.osgp.adapter.protocol.oslp.elster.application.config.OsgpProtocolAdapterOslpInitializer.java

@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
    try {//from ww w  .  ja  va  2  s .  c  om
        // Force the timezone of application to UTC (required for
        // Hibernate/JDBC)
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

        final Context initialContext = new InitialContext();

        final String logLocation = (String) initialContext
                .lookup("java:comp/env/osp/osgpAdapterProtocolOslpElster/log-config");
        LogbackConfigurer.initLogging(logLocation);

        final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(ApplicationContext.class);

        servletContext.addListener(new ContextLoaderListener(rootContext));

    } catch (final NamingException e) {
        throw new ServletException("naming exception", e);
    } catch (final FileNotFoundException e) {
        throw new ServletException("Logging file not found", e);
    } catch (final JoranException e) {
        throw new ServletException("Logback exception", e);
    }
}

From source file:ca.n4dev.dev.worktime.config.SpringHibernateJPAConfig.java

@Bean
public DataSource dataSource() {
    try {/*  www .  j a  va 2 s  .c o  m*/
        // Obtain our environment naming context
        Context initCtx = new InitialContext();
        Context envCtx = (Context) initCtx.lookup("java:comp/env");

        // Look up our data source
        DataSource ds = (DataSource) envCtx.lookup("jdbc/WorktimeDB");

        //         if (ds == null) {
        //
        //            ds = new DataSource();
        //            PoolProperties p = new PoolProperties();
        //            
        //            p.setUrl(env.getRequiredProperty("")); // "jdbc:mysql://localhost:3306/mysql"
        //            p.setDriverClassName(env.getRequiredProperty("")); // "com.mysql.jdbc.Driver"
        //            p.setUsername(env.getRequiredProperty("")); // "root"
        //            p.setPassword(env.getRequiredProperty("")); // "password"
        //            p.setJmxEnabled(Boolean.valueOf(env.getRequiredProperty(""))); // false
        //            p.setTestWhileIdle(Boolean.valueOf(env.getRequiredProperty(""))); // false
        //            p.setTestOnBorrow(Boolean.valueOf(env.getRequiredProperty(""))); // true
        //            p.setValidationQuery(env.getRequiredProperty("")); // "SELECT 1"
        //            p.setTestOnReturn(Boolean.valueOf(env.getRequiredProperty(""))); // false
        //            p.setValidationInterval(Integer.parseInt(env.getRequiredProperty(""))); // 30000
        //            p.setTimeBetweenEvictionRunsMillis(Integer.parseInt(env.getRequiredProperty(""))); // 30000
        //            p.setMaxActive(Integer.parseInt(env.getRequiredProperty(""))); // 100
        //            p.setInitialSize(Integer.parseInt(env.getRequiredProperty(""))); // 10
        //            p.setMaxWait(Integer.parseInt(env.getRequiredProperty(""))); // 10000
        //            p.setRemoveAbandonedTimeout(Integer.parseInt(env.getRequiredProperty(""))); // 60
        //            p.setMinEvictableIdleTimeMillis(Integer.parseInt(env.getRequiredProperty(""))); // 30000
        //            p.setMinIdle(Integer.parseInt(env.getRequiredProperty(""))); // 10
        //            p.setLogAbandoned(Boolean.valueOf(env.getRequiredProperty(""))); // true
        //            p.setRemoveAbandoned(Boolean.valueOf(env.getRequiredProperty(""))); // true
        //            p.setJdbcInterceptors(env.getRequiredProperty("")); // "org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
        //
        //            
        //            ds.setPoolProperties(p);
        //            
        //         }

        return ds;

    } catch (Exception e) {
        throw new RuntimeException("Unable to get or create the datasource!");
    }
}

From source file:com.alliander.osgp.adapter.protocol.iec61850.application.config.OsgpProtocolAdapterIec61850Initializer.java

@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
    try {//from   ww w.j  a v a2 s .com
        // Force the timezone of application to UTC (required for
        // Hibernate/JDBC)
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

        final Context initialContext = new InitialContext();

        final String logLocation = (String) initialContext
                .lookup("java:comp/env/osp/osgpAdapterProtocolIec61850/log-config");
        LogbackConfigurer.initLogging(logLocation);

        final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(ApplicationContext.class);

        servletContext.addListener(new ContextLoaderListener(rootContext));

    } catch (final NamingException e) {
        throw new ServletException("naming exception", e);
    } catch (final FileNotFoundException e) {
        throw new ServletException("Logging file not found", e);
    } catch (final JoranException e) {
        throw new ServletException("Logback exception", e);
    }
}

From source file:edu.byu.wso2.helper.BYUEntityHelper.java

public BYUEntityHelper() {
    try {/* w  ww. j  a v  a2  s .  c om*/
        if (ds == null) {
            if (log.isDebugEnabled())
                log.debug("CustomTokenGenerator: looking up  datasource");

            ds = (DataSource) new InitialContext().lookup("jdbc/BYUPRODB");

            if (log.isDebugEnabled())
                log.debug("acquired datasource");
        }
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

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);
        }/*from  w ww  .  j  a v  a2 s  .  c  om*/

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

From source file:blueprint.sdk.experimental.florist.db.ConnectionListener.java

public void contextDestroyed(final ServletContextEvent arg0) {
    try {//www.j  a  v a  2s. c om
        Context initContext = new InitialContext();

        for (String s : boundDsrs.keySet()) {
            try {
                DataSource dsr = boundDsrs.get(s);
                initContext.unbind(s);
                // Close all BasicDataSources created by this class.
                // No need to close JNDI DataSources. It's not this class's
                // responsibility.
                if (dsr instanceof BasicDataSource) {
                    ((BasicDataSource) dsr).close();
                }
            } catch (SQLException e) {
                LOGGER.trace(e);
            }
        }

        boundDsrs.clear();
    } catch (NamingException e) {
        LOGGER.trace(e);
    }
}

From source file:edu.byu.wso2.apim.extensions.helpers.BYUEntityHelper.java

public BYUEntityHelper(String dSName) {
    try {/*from w  w  w  .  jav  a  2 s  .  c  o  m*/
        if (ds == null) {
            if (log.isDebugEnabled())
                log.debug("BYUEntityHelper: looking up  datasource");

            ds = (DataSource) new InitialContext().lookup(dSName);

            if (log.isDebugEnabled())
                log.debug("acquired datasource");
        }
    } catch (NamingException e) {
        e.printStackTrace();
    }
}