Search in sources :

Example 1 with EnvEntry

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

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

Example 3 with EnvEntry

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

the class ServerWithAnnotations method main.

public static final void main(String[] args) throws Exception {
    // Create the server
    Server server = new Server(8080);
    // Enable parsing of jndi-related parts of web.xml and jetty-env.xml
    Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(server);
    classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration");
    classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration");
    // Create a WebApp
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    File warFile = new File("../../jetty-distribution/target/distribution/demo-base/webapps/test.war");
    webapp.setWar(warFile.getAbsolutePath());
    webapp.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/javax.servlet-[^/]*\\.jar$|.*/servlet-api-[^/]*\\.jar$");
    server.setHandler(webapp);
    // Register new transaction manager in JNDI
    // At runtime, the webapp accesses this as java:comp/UserTransaction
    new Transaction(new com.acme.MockUserTransaction());
    // Define an env entry with webapp scope.
    new EnvEntry(webapp, "maxAmount", new Double(100), true);
    // Register a mock DataSource scoped to the webapp
    new Resource(webapp, "jdbc/mydatasource", new com.acme.MockDataSource());
    // Configure a LoginService
    HashLoginService loginService = new HashLoginService();
    loginService.setName("Test Realm");
    loginService.setConfig("src/test/resources/realm.properties");
    server.addBean(loginService);
    server.start();
    server.join();
}
Also used : Server(org.eclipse.jetty.server.Server) Configuration(org.eclipse.jetty.webapp.Configuration) Resource(org.eclipse.jetty.plus.jndi.Resource) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) HashLoginService(org.eclipse.jetty.security.HashLoginService) Transaction(org.eclipse.jetty.plus.jndi.Transaction) File(java.io.File) EnvEntry(org.eclipse.jetty.plus.jndi.EnvEntry)

Example 4 with EnvEntry

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

the class EnvConfiguration method bindEnvEntries.

/**
     * Bind all EnvEntries that have been declared, so that the processing of the
     * web.xml file can potentially override them.
     *
     * We first bind EnvEntries declared in Server scope, then WebAppContext scope.
     * @param context the context to use for the object scope
     * @throws NamingException if unable to bind env entries
     */
public void bindEnvEntries(WebAppContext context) throws NamingException {
    LOG.debug("Binding env entries from the jvm scope");
    InitialContext ic = new InitialContext();
    Context envCtx = (Context) ic.lookup("java:comp/env");
    Object scope = null;
    List<Object> list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
    Iterator<Object> itor = list.iterator();
    while (itor.hasNext()) {
        EnvEntry ee = (EnvEntry) itor.next();
        ee.bindToENC(ee.getJndiName());
        Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
        //also save the EnvEntry in the context so we can check it later
        NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
    }
    LOG.debug("Binding env entries from the server scope");
    scope = context.getServer();
    list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
    itor = list.iterator();
    while (itor.hasNext()) {
        EnvEntry ee = (EnvEntry) itor.next();
        ee.bindToENC(ee.getJndiName());
        Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
        //also save the EnvEntry in the context so we can check it later
        NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
    }
    LOG.debug("Binding env entries from the context scope");
    scope = context;
    list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
    itor = list.iterator();
    while (itor.hasNext()) {
        EnvEntry ee = (EnvEntry) itor.next();
        ee.bindToENC(ee.getJndiName());
        Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
        //also save the EnvEntry in the context so we can check it later
        NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
    }
}
Also used : InitialContext(javax.naming.InitialContext) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) NamingContext(org.eclipse.jetty.jndi.NamingContext) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) EnvEntry(org.eclipse.jetty.plus.jndi.EnvEntry) Name(javax.naming.Name)

Aggregations

EnvEntry (org.eclipse.jetty.plus.jndi.EnvEntry)4 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)4 Context (javax.naming.Context)3 InitialContext (javax.naming.InitialContext)3 NamingEntry (org.eclipse.jetty.plus.jndi.NamingEntry)2 Server (org.eclipse.jetty.server.Server)2 File (java.io.File)1 Name (javax.naming.Name)1 NameNotFoundException (javax.naming.NameNotFoundException)1 NamingContext (org.eclipse.jetty.jndi.NamingContext)1 Resource (org.eclipse.jetty.plus.jndi.Resource)1 Transaction (org.eclipse.jetty.plus.jndi.Transaction)1 HashLoginService (org.eclipse.jetty.security.HashLoginService)1 Configuration (org.eclipse.jetty.webapp.Configuration)1 MetaData (org.eclipse.jetty.webapp.MetaData)1 WebAppClassLoader (org.eclipse.jetty.webapp.WebAppClassLoader)1 Test (org.junit.Test)1