use of javax.naming.InitialContext in project jetty.project by eclipse.
the class PlusDescriptorProcessorTest method tearDown.
@After
public void tearDown() throws Exception {
ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(context.getClassLoader());
Context ic = new InitialContext();
Context compCtx = (Context) ic.lookup("java:comp");
compCtx.destroySubcontext("env");
Thread.currentThread().setContextClassLoader(oldLoader);
}
use of javax.naming.InitialContext in project jetty.project by eclipse.
the class TestConfiguration method testIt.
@Test
public void testIt() throws Exception {
ClassLoader old_loader = Thread.currentThread().getContextClassLoader();
try {
InitialContext ic = new InitialContext();
Server server = new Server();
WebAppContext wac = new WebAppContext();
wac.setServer(server);
wac.setClassLoader(new WebAppClassLoader(Thread.currentThread().getContextClassLoader(), wac));
MetaData metaData = new MetaData();
PlusDescriptorProcessor plusProcessor = new PlusDescriptorProcessor();
//bind some EnvEntrys at the server level
EnvEntry ee1 = new EnvEntry(server, "xxx/a", "100", true);
EnvEntry ee2 = new EnvEntry(server, "yyy/b", "200", false);
EnvEntry ee3 = new EnvEntry(server, "zzz/c", "300", false);
EnvEntry ee4 = new EnvEntry(server, "zzz/d", "400", false);
EnvEntry ee5 = new EnvEntry(server, "zzz/f", "500", true);
//bind some EnvEntrys at the webapp level
EnvEntry ee6 = new EnvEntry(wac, "xxx/a", "900", true);
EnvEntry ee7 = new EnvEntry(wac, "yyy/b", "910", true);
EnvEntry ee8 = new EnvEntry(wac, "zzz/c", "920", false);
EnvEntry ee9 = new EnvEntry(wac, "zzz/e", "930", false);
assertNotNull(NamingEntryUtil.lookupNamingEntry(server, "xxx/a"));
assertNotNull(NamingEntryUtil.lookupNamingEntry(server, "yyy/b"));
assertNotNull(NamingEntryUtil.lookupNamingEntry(server, "zzz/c"));
assertNotNull(NamingEntryUtil.lookupNamingEntry(server, "zzz/d"));
assertNotNull(NamingEntryUtil.lookupNamingEntry(wac, "xxx/a"));
assertNotNull(NamingEntryUtil.lookupNamingEntry(wac, "yyy/b"));
assertNotNull(NamingEntryUtil.lookupNamingEntry(wac, "zzz/c"));
assertNotNull(NamingEntryUtil.lookupNamingEntry(wac, "zzz/e"));
//make a new env configuration
EnvConfiguration envConfig = new EnvConfiguration();
Thread.currentThread().setContextClassLoader(wac.getClassLoader());
MetaData metadata = new MetaData();
envConfig.preConfigure(wac);
envConfig.configure(wac);
envConfig.bindEnvEntries(wac);
String val = (String) ic.lookup("java:comp/env/xxx/a");
//webapp naming overrides server
assertEquals("900", val);
val = (String) ic.lookup("java:comp/env/yyy/b");
//webapp overrides server
assertEquals("910", val);
val = (String) ic.lookup("java:comp/env/zzz/c");
//webapp overrides server
assertEquals("920", val);
val = (String) ic.lookup("java:comp/env/zzz/d");
//from server naming
assertEquals("400", val);
val = (String) ic.lookup("java:comp/env/zzz/e");
//from webapp naming
assertEquals("930", val);
NamingEntry ne = (NamingEntry) ic.lookup("java:comp/env/" + NamingEntry.__contextName + "/xxx/a");
assertNotNull(ne);
ne = (NamingEntry) ic.lookup("java:comp/env/" + NamingEntry.__contextName + "/yyy/b");
assertNotNull(ne);
ne = (NamingEntry) ic.lookup("java:comp/env/" + NamingEntry.__contextName + "/zzz/c");
assertNotNull(ne);
ne = (NamingEntry) ic.lookup("java:comp/env/" + NamingEntry.__contextName + "/zzz/d");
assertNotNull(ne);
ne = (NamingEntry) ic.lookup("java:comp/env/" + NamingEntry.__contextName + "/zzz/e");
assertNotNull(ne);
plusProcessor.bindEnvEntry("foo", "99");
assertEquals("99", ic.lookup("java:comp/env/foo"));
plusProcessor.bindEnvEntry("xxx/a", "7");
//webapp overrides web.xml
assertEquals("900", ic.lookup("java:comp/env/xxx/a"));
plusProcessor.bindEnvEntry("yyy/b", "7");
//webapp overrides web.xml
assertEquals("910", ic.lookup("java:comp/env/yyy/b"));
plusProcessor.bindEnvEntry("zzz/c", "7");
//webapp does NOT override web.xml
assertEquals("7", ic.lookup("java:comp/env/zzz/c"));
plusProcessor.bindEnvEntry("zzz/d", "7");
//server does NOT override web.xml
assertEquals("7", ic.lookup("java:comp/env/zzz/d"));
plusProcessor.bindEnvEntry("zzz/e", "7");
//webapp does NOT override web.xml
assertEquals("7", ic.lookup("java:comp/env/zzz/e"));
plusProcessor.bindEnvEntry("zzz/f", "7");
//server overrides web.xml
assertEquals("500", ic.lookup("java:comp/env/zzz/f"));
((Context) ic.lookup("java:comp")).destroySubcontext("env");
ic.destroySubcontext("xxx");
ic.destroySubcontext("yyy");
ic.destroySubcontext("zzz");
} finally {
Thread.currentThread().setContextClassLoader(old_loader);
}
}
use of javax.naming.InitialContext in project jetty.project by eclipse.
the class Transaction method unbindENC.
/**
* Unbind this Transaction from a java:comp
*/
public void unbindENC() {
try {
InitialContext ic = new InitialContext();
Context env = (Context) ic.lookup("java:comp");
__log.debug("Unbinding java:comp/" + getJndiName());
env.unbind(getJndiName());
} catch (NamingException e) {
__log.warn(e);
}
}
use of javax.naming.InitialContext in project jetty.project by eclipse.
the class DataSourceLoginService method initDb.
/* ------------------------------------------------------------ */
/**
* Lookup the datasource for the jndiName and formulate the
* necessary sql query strings based on the configured table
* and column names.
*
* @throws NamingException if unable to init jndi
* @throws SQLException if unable to init database
*/
public void initDb() throws NamingException, SQLException {
if (_datasource != null)
return;
@SuppressWarnings("unused") InitialContext ic = new InitialContext();
assert ic != null;
// try finding the datasource in the Server scope
if (_server != null) {
try {
_datasource = (DataSource) NamingEntryUtil.lookup(_server, _jndiName);
} catch (NameNotFoundException e) {
//next try the jvm scope
}
}
//try finding the datasource in the jvm scope
if (_datasource == null) {
_datasource = (DataSource) NamingEntryUtil.lookup(null, _jndiName);
}
// set up the select statements based on the table and column names configured
_userSql = "select " + _userTableKey + "," + _userTablePasswordField + " from " + _userTableName + " where " + _userTableUserField + " = ?";
_roleSql = "select r." + _roleTableRoleField + " from " + _roleTableName + " r, " + _userRoleTableName + " u where u." + _userRoleTableUserKey + " = ?" + " and r." + _roleTableKey + " = u." + _userRoleTableRoleKey;
prepareTables();
}
use of javax.naming.InitialContext in project jetty.project by eclipse.
the class EnvConfiguration method deconfigure.
/**
* Remove jndi setup from start
* @throws Exception if unable to deconfigure
*/
@Override
public void deconfigure(WebAppContext context) throws Exception {
//get rid of any bindings for comp/env for webapp
ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(context.getClassLoader());
ContextFactory.associateClassLoader(context.getClassLoader());
try {
Context ic = new InitialContext();
Context compCtx = (Context) ic.lookup("java:comp");
compCtx.destroySubcontext("env");
//unbind any NamingEntries that were configured in this webapp's name space
@SuppressWarnings("unchecked") List<Bound> bindings = (List<Bound>) context.getAttribute(JETTY_ENV_BINDINGS);
context.setAttribute(JETTY_ENV_BINDINGS, null);
if (bindings != null) {
Collections.reverse(bindings);
for (Bound b : bindings) b._context.destroySubcontext(b._name);
}
} catch (NameNotFoundException e) {
LOG.warn(e);
} finally {
ContextFactory.disassociateClassLoader();
Thread.currentThread().setContextClassLoader(oldLoader);
}
}
Aggregations