Search in sources :

Example 26 with Context

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);
    }
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) NameNotFoundException(javax.naming.NameNotFoundException) NamingEntry(org.eclipse.jetty.plus.jndi.NamingEntry) InitialContext(javax.naming.InitialContext) EnvEntry(org.eclipse.jetty.plus.jndi.EnvEntry)

Example 27 with Context

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"));
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) Test(org.junit.Test)

Example 28 with Context

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());
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NameNotFoundException(javax.naming.NameNotFoundException) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 29 with Context

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);
}
Also used : InitialContext(javax.naming.InitialContext) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Context(javax.naming.Context) WebAppClassLoader(org.eclipse.jetty.webapp.WebAppClassLoader) InitialContext(javax.naming.InitialContext) After(org.junit.After)

Example 30 with Context

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);
    }
}
Also used : InitialContext(javax.naming.InitialContext) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Context(javax.naming.Context) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Server(org.eclipse.jetty.server.Server) MetaData(org.eclipse.jetty.webapp.MetaData) WebAppClassLoader(org.eclipse.jetty.webapp.WebAppClassLoader) NamingEntry(org.eclipse.jetty.plus.jndi.NamingEntry) WebAppClassLoader(org.eclipse.jetty.webapp.WebAppClassLoader) InitialContext(javax.naming.InitialContext) EnvEntry(org.eclipse.jetty.plus.jndi.EnvEntry) Test(org.junit.Test)

Aggregations

Context (javax.naming.Context)507 InitialContext (javax.naming.InitialContext)250 Test (org.junit.Test)173 NamingException (javax.naming.NamingException)156 DataSource (javax.sql.DataSource)72 Properties (java.util.Properties)67 Connection (java.sql.Connection)62 NameNotFoundException (javax.naming.NameNotFoundException)52 UserTransaction (javax.transaction.UserTransaction)48 SQLException (java.sql.SQLException)46 IOException (java.io.IOException)44 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)44 Statement (java.sql.Statement)42 Name (javax.naming.Name)38 Hashtable (java.util.Hashtable)35 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)27 BeanContext (org.apache.openejb.BeanContext)27 Binding (javax.naming.Binding)25 Reference (javax.naming.Reference)25 NotContextException (javax.naming.NotContextException)23