use of javax.naming.Context in project jetty.project by eclipse.
the class PlusDescriptorProcessor method bindEnvEntry.
/**
* @param name the jndi name
* @param value the value
* @throws Exception if unable to bind entry
*/
public void bindEnvEntry(String name, Object value) throws Exception {
InitialContext ic = null;
boolean bound = false;
//check to see if we bound a value and an EnvEntry with this name already
//when we processed the server and the webapp's naming environment
//@see EnvConfiguration.bindEnvEntries()
ic = new InitialContext();
try {
NamingEntry ne = (NamingEntry) ic.lookup("java:comp/env/" + NamingEntryUtil.makeNamingEntryName(ic.getNameParser(""), name));
if (ne != null && ne instanceof EnvEntry) {
EnvEntry ee = (EnvEntry) ne;
bound = ee.isOverrideWebXml();
}
} catch (NameNotFoundException e) {
bound = false;
}
if (!bound) {
//either nothing was bound or the value from web.xml should override
Context envCtx = (Context) ic.lookup("java:comp/env");
NamingUtil.bind(envCtx, name, value);
}
}
use of javax.naming.Context in project jetty.project by eclipse.
the class TestNamingEntries method testEnvEntryNonOverride.
@Test
public void testEnvEntryNonOverride() throws Exception {
ScopeA scope = new ScopeA();
EnvEntry ee = new EnvEntry(scope, "nameA", someObject, false);
NamingEntry ne = NamingEntryUtil.lookupNamingEntry(scope, "nameA");
assertNotNull(ne);
assertTrue(ne instanceof EnvEntry);
assertFalse(((EnvEntry) ne).isOverrideWebXml());
Context scopeContext = NamingEntryUtil.getContextForScope(scope);
assertNotNull(scopeContext);
Context namingEntriesContext = NamingEntryUtil.getContextForNamingEntries(scope);
assertNotNull(namingEntriesContext);
assertEquals(someObject, scopeContext.lookup("nameA"));
}
use of javax.naming.Context in project jetty.project by eclipse.
the class TestNamingEntryUtil method testGetContextForScope.
@Test
public void testGetContextForScope() throws Exception {
ScopeA scope = new ScopeA();
try {
Context c = NamingEntryUtil.getContextForScope(scope);
fail("context should not exist");
} catch (NameNotFoundException e) {
//expected
}
InitialContext ic = new InitialContext();
Context scopeContext = ic.createSubcontext(NamingEntryUtil.getNameForScope(scope));
assertNotNull(scopeContext);
try {
Context c = NamingEntryUtil.getContextForScope(scope);
assertNotNull(c);
} catch (NameNotFoundException e) {
fail(e.getMessage());
}
}
use of javax.naming.Context 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.Context 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);
}
}
Aggregations