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");
}
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));
}
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.");
}
}
}
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;
}
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;
}
Aggregations