Search in sources :

Example 1 with NamingEntry

use of org.eclipse.jetty.plus.jndi.NamingEntry 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 2 with NamingEntry

use of org.eclipse.jetty.plus.jndi.NamingEntry in project jetty.project by eclipse.

the class PlusDescriptorProcessor method bindEntry.

/**
     * Bind a resource with the given name from web.xml of the given type
     * with a jndi resource from either the server or the webapp's naming
     * environment.
     * <p>
     * As the servlet spec does not cover the mapping of names in web.xml with
     * names from the execution environment, jetty uses the concept of a Link, which is
     * a subclass of the NamingEntry class. A Link defines a mapping of a name
     * from web.xml with a name from the execution environment (ie either the server or the
     * webapp's naming environment).
     * 
     * @param context the context
     * @param name name of the resource from web.xml
     * @param typeClass the type class
     * @throws Exception the exception
     */
protected void bindEntry(WebAppContext context, String name, Class<?> typeClass) throws Exception {
    String nameInEnvironment = name;
    boolean bound = false;
    //check if the name in web.xml has been mapped to something else
    //check a context-specific naming environment first
    Object scope = context;
    NamingEntry ne = NamingEntryUtil.lookupNamingEntry(scope, name);
    if (ne != null && (ne instanceof Link)) {
        //if we found a mapping, get out name it is mapped to in the environment
        nameInEnvironment = ((Link) ne).getLink();
    }
    //try finding that mapped name in the webapp's environment first
    scope = context;
    bound = NamingEntryUtil.bindToENC(scope, name, nameInEnvironment);
    if (bound)
        return;
    //try the server's environment
    scope = context.getServer();
    bound = NamingEntryUtil.bindToENC(scope, name, nameInEnvironment);
    if (bound)
        return;
    //try the jvm environment
    bound = NamingEntryUtil.bindToENC(null, name, nameInEnvironment);
    if (bound)
        return;
    //There is no matching resource so try a default name.
    //The default name syntax is: the [res-type]/default
    //eg       javax.sql.DataSource/default
    nameInEnvironment = typeClass.getName() + "/default";
    //First try the server scope
    NamingEntry defaultNE = NamingEntryUtil.lookupNamingEntry(context.getServer(), nameInEnvironment);
    if (defaultNE == null)
        defaultNE = NamingEntryUtil.lookupNamingEntry(null, nameInEnvironment);
    if (defaultNE != null)
        defaultNE.bindToENC(name);
    else
        throw new IllegalStateException("Nothing to bind for name " + name);
}
Also used : NamingEntry(org.eclipse.jetty.plus.jndi.NamingEntry) Link(org.eclipse.jetty.plus.jndi.Link)

Example 3 with NamingEntry

use of org.eclipse.jetty.plus.jndi.NamingEntry 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

NamingEntry (org.eclipse.jetty.plus.jndi.NamingEntry)3 Context (javax.naming.Context)2 InitialContext (javax.naming.InitialContext)2 EnvEntry (org.eclipse.jetty.plus.jndi.EnvEntry)2 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)2 NameNotFoundException (javax.naming.NameNotFoundException)1 Link (org.eclipse.jetty.plus.jndi.Link)1 Server (org.eclipse.jetty.server.Server)1 MetaData (org.eclipse.jetty.webapp.MetaData)1 WebAppClassLoader (org.eclipse.jetty.webapp.WebAppClassLoader)1 Test (org.junit.Test)1