Search in sources :

Example 51 with InitialContext

use of javax.naming.InitialContext in project spring-boot by spring-projects.

the class TomcatServletWebServerFactoryTests method jndiLookupsCanBePerformedDuringApplicationContextRefresh.

@Test
public void jndiLookupsCanBePerformedDuringApplicationContextRefresh() throws NamingException {
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
    TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0) {

        @Override
        protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
            tomcat.enableNaming();
            return super.getTomcatWebServer(tomcat);
        }
    };
    // Server is created in onRefresh
    this.webServer = factory.getWebServer();
    // Lookups should now be possible
    new InitialContext().lookup("java:comp/env");
    // Called in finishRefresh, giving us an opportunity to remove the context binding
    // and avoid a leak
    this.webServer.start();
    // Lookups should no longer be possible
    this.thrown.expect(NamingException.class);
    new InitialContext().lookup("java:comp/env");
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 52 with InitialContext

use of javax.naming.InitialContext in project camel by apache.

the class CamelTestSupport method createJndiContext.

protected Context createJndiContext() throws Exception {
    Properties properties = new Properties();
    // jndi.properties is optional
    InputStream in = getClass().getClassLoader().getResourceAsStream("jndi.properties");
    if (in != null) {
        log.debug("Using jndi.properties from classpath root");
        properties.load(in);
    } else {
        properties.put("java.naming.factory.initial", "org.apache.camel.util.jndi.CamelInitialContextFactory");
    }
    return new InitialContext(new Hashtable<Object, Object>(properties));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext)

Example 53 with InitialContext

use of javax.naming.InitialContext in project liquibase by liquibase.

the class MultiTenantSpringLiquibase method resolveDataSources.

private void resolveDataSources() throws NamingException {
    Context context = new InitialContext();
    int lastIndexOf = jndiBase.lastIndexOf("/");
    String jndiRoot = jndiBase.substring(0, lastIndexOf);
    String jndiParent = jndiBase.substring(lastIndexOf + 1);
    Context base = (Context) context.lookup(jndiRoot);
    NamingEnumeration<NameClassPair> list = base.list(jndiParent);
    while (list.hasMoreElements()) {
        NameClassPair entry = list.nextElement();
        String name = entry.getName();
        String jndiUrl;
        if (entry.isRelative()) {
            jndiUrl = jndiBase + "/" + name;
        } else {
            jndiUrl = name;
        }
        Object lookup = context.lookup(jndiUrl);
        if (lookup instanceof DataSource) {
            dataSources.add((DataSource) lookup);
            log.debug("Added a data source at " + jndiUrl);
        } else {
            log.info("Skipping a resource " + jndiUrl + " not compatible with DataSource.");
        }
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NameClassPair(javax.naming.NameClassPair) InitialContext(javax.naming.InitialContext) DataSource(javax.sql.DataSource)

Example 54 with InitialContext

use of javax.naming.InitialContext in project HikariCP by brettwooldridge.

the class PoolBase method initializeDataSource.

// ***********************************************************************
//                          Private methods
// ***********************************************************************
/**
    * Create/initialize the underlying DataSource.
    */
private void initializeDataSource() {
    final String jdbcUrl = config.getJdbcUrl();
    final String username = config.getUsername();
    final String password = config.getPassword();
    final String dsClassName = config.getDataSourceClassName();
    final String driverClassName = config.getDriverClassName();
    final String dataSourceJNDI = config.getDataSourceJNDI();
    final Properties dataSourceProperties = config.getDataSourceProperties();
    DataSource dataSource = config.getDataSource();
    if (dsClassName != null && dataSource == null) {
        dataSource = createInstance(dsClassName, DataSource.class);
        PropertyElf.setTargetFromProperties(dataSource, dataSourceProperties);
    } else if (jdbcUrl != null && dataSource == null) {
        dataSource = new DriverDataSource(jdbcUrl, driverClassName, dataSourceProperties, username, password);
    } else if (dataSourceJNDI != null && dataSource == null) {
        try {
            InitialContext ic = new InitialContext();
            dataSource = (DataSource) ic.lookup(dataSourceJNDI);
        } catch (NamingException e) {
            throw new PoolInitializationException(e);
        }
    }
    if (dataSource != null) {
        setLoginTimeout(dataSource);
        createNetworkTimeoutExecutor(dataSource, dsClassName, jdbcUrl);
    }
    this.dataSource = dataSource;
}
Also used : DriverDataSource(com.zaxxer.hikari.util.DriverDataSource) NamingException(javax.naming.NamingException) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) DataSource(javax.sql.DataSource) DriverDataSource(com.zaxxer.hikari.util.DriverDataSource) PoolInitializationException(com.zaxxer.hikari.pool.HikariPool.PoolInitializationException)

Example 55 with InitialContext

use of javax.naming.InitialContext in project gitblit by gitblit.

the class GitblitContext method lookupBaseFolderFromJndi.

private String lookupBaseFolderFromJndi() {
    try {
        // try to lookup JNDI env-entry for the baseFolder
        InitialContext ic = new InitialContext();
        Context env = (Context) ic.lookup("java:comp/env");
        return (String) env.lookup("baseFolder");
    } catch (NamingException n) {
        logger.error("Failed to get JNDI env-entry: " + n.getExplanation());
    }
    return null;
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) ServletContext(javax.servlet.ServletContext) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Aggregations

InitialContext (javax.naming.InitialContext)1068 Test (org.junit.Test)325 EJBException (javax.ejb.EJBException)228 Properties (java.util.Properties)213 Context (javax.naming.Context)194 RemoteException (java.rmi.RemoteException)173 TestFailureException (org.apache.openejb.test.TestFailureException)172 NamingException (javax.naming.NamingException)168 AssertionFailedError (junit.framework.AssertionFailedError)168 JMSException (javax.jms.JMSException)167 RemoveException (javax.ejb.RemoveException)66 CreateException (javax.ejb.CreateException)57 DataSource (javax.sql.DataSource)55 Hashtable (java.util.Hashtable)54 Assembler (org.apache.openejb.assembler.classic.Assembler)47 EjbJar (org.apache.openejb.jee.EjbJar)41 NameNotFoundException (javax.naming.NameNotFoundException)40 ConfigurationFactory (org.apache.openejb.config.ConfigurationFactory)38 Connection (java.sql.Connection)37 StatelessBean (org.apache.openejb.jee.StatelessBean)30