Search in sources :

Example 16 with ContextEnvironment

use of org.apache.tomcat.util.descriptor.web.ContextEnvironment in project tomcat by apache.

the class NamingResourcesImpl method removeEnvironment.

/**
     * Remove any environment entry with the specified name.
     *
     * @param name Name of the environment entry to remove
     */
@Override
public void removeEnvironment(String name) {
    entries.remove(name);
    ContextEnvironment environment = null;
    synchronized (envs) {
        environment = envs.remove(name);
    }
    if (environment != null) {
        support.firePropertyChange("environment", environment, null);
        // De-register with JMX
        if (resourceRequireExplicitRegistration) {
            try {
                MBeanUtils.destroyMBean(environment);
            } catch (Exception e) {
                log.warn(sm.getString("namingResources.mbeanDestroyFail", environment.getName()), e);
            }
        }
        environment.setNamingResources(null);
    }
}
Also used : ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) NamingException(javax.naming.NamingException) LifecycleException(org.apache.catalina.LifecycleException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 17 with ContextEnvironment

use of org.apache.tomcat.util.descriptor.web.ContextEnvironment in project tomcat by apache.

the class NamingResourcesImpl method initInternal.

// ------------------------------------------------------- Lifecycle methods
@Override
protected void initInternal() throws LifecycleException {
    super.initInternal();
    // Set this before we register currently known naming resources to avoid
    // timing issues. Duplication registration is not an issue.
    resourceRequireExplicitRegistration = true;
    for (ContextResource cr : resources.values()) {
        try {
            MBeanUtils.createMBean(cr);
        } catch (Exception e) {
            log.warn(sm.getString("namingResources.mbeanCreateFail", cr.getName()), e);
        }
    }
    for (ContextEnvironment ce : envs.values()) {
        try {
            MBeanUtils.createMBean(ce);
        } catch (Exception e) {
            log.warn(sm.getString("namingResources.mbeanCreateFail", ce.getName()), e);
        }
    }
    for (ContextResourceLink crl : resourceLinks.values()) {
        try {
            MBeanUtils.createMBean(crl);
        } catch (Exception e) {
            log.warn(sm.getString("namingResources.mbeanCreateFail", crl.getName()), e);
        }
    }
}
Also used : ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) ContextResourceLink(org.apache.tomcat.util.descriptor.web.ContextResourceLink) NamingException(javax.naming.NamingException) LifecycleException(org.apache.catalina.LifecycleException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource)

Example 18 with ContextEnvironment

use of org.apache.tomcat.util.descriptor.web.ContextEnvironment in project tomcat by apache.

the class TestNamingContextListener method testBug49132.

/*
     * Test JNDI is available to ServletContextListeners.
     */
@Test
public void testBug49132() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    // Enable JNDI - it is disabled by default
    tomcat.enableNaming();
    ContextEnvironment environment = new ContextEnvironment();
    environment.setType(BUG49132_VALUE.getClass().getName());
    environment.setName(BUG49132_NAME);
    environment.setValue(BUG49132_VALUE);
    ctx.getNamingResources().addEnvironment(environment);
    ctx.addApplicationListener(Bug49132Listener.class.getName());
    tomcat.start();
    assertEquals(LifecycleState.STARTED, ctx.getState());
}
Also used : InitialContext(javax.naming.InitialContext) Context(org.apache.catalina.Context) ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) Tomcat(org.apache.catalina.startup.Tomcat) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 19 with ContextEnvironment

use of org.apache.tomcat.util.descriptor.web.ContextEnvironment in project tomcat by apache.

the class TestNamingContext method testGlobalNaming.

@Test
public void testGlobalNaming() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();
    org.apache.catalina.Context ctx = tomcat.addContext("", null);
    tomcat.start();
    Context webappInitial = ContextBindings.getContext(ctx);
    // Nothing added at the moment so should be null
    Object obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
    Assert.assertNull(obj);
    ContextEnvironment ce = new ContextEnvironment();
    ce.setName(GLOBAL_NAME);
    ce.setValue(DATA);
    ce.setType(DATA.getClass().getName());
    tomcat.getServer().getGlobalNamingResources().addEnvironment(ce);
    // No link so still should be null
    obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
    Assert.assertNull(obj);
    // Now add a resource link to the context
    ContextResourceLink crl = new ContextResourceLink();
    crl.setGlobal(GLOBAL_NAME);
    crl.setName(LOCAL_NAME);
    crl.setType(DATA.getClass().getName());
    ctx.getNamingResources().addResourceLink(crl);
    // Link exists so should be OK now
    obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
    Assert.assertEquals(DATA, obj);
    // Try shortcut
    ResourceLinkFactory factory = new ResourceLinkFactory();
    ResourceLinkRef rlr = new ResourceLinkRef(DATA.getClass().getName(), GLOBAL_NAME, null, null);
    obj = factory.getObjectInstance(rlr, null, null, null);
    Assert.assertEquals(DATA, obj);
    // Remove the link
    ctx.getNamingResources().removeResourceLink(LOCAL_NAME);
    // No link so should be null
    obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
    Assert.assertNull(obj);
    // Shortcut should fail too
    obj = factory.getObjectInstance(rlr, null, null, null);
    Assert.assertNull(obj);
}
Also used : Context(javax.naming.Context) ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) Tomcat(org.apache.catalina.startup.Tomcat) ContextResourceLink(org.apache.tomcat.util.descriptor.web.ContextResourceLink) ResourceLinkFactory(org.apache.naming.factory.ResourceLinkFactory) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 20 with ContextEnvironment

use of org.apache.tomcat.util.descriptor.web.ContextEnvironment in project tomcat by apache.

the class TestNamingContext method testBug52830.

@Test
public void testBug52830() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();
    // No file system docBase required
    org.apache.catalina.Context ctx = tomcat.addContext("", null);
    // Create the resource
    ContextEnvironment env = new ContextEnvironment();
    env.setName("boolean");
    env.setType(Boolean.class.getName());
    env.setValue("true");
    ctx.getNamingResources().addEnvironment(env);
    // Map the test Servlet
    Bug52830Servlet bug52830Servlet = new Bug52830Servlet();
    Tomcat.addServlet(ctx, "bug52830Servlet", bug52830Servlet);
    ctx.addServletMappingDecoded("/", "bug52830Servlet");
    tomcat.start();
    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/", bc, null);
    assertEquals(200, rc);
    assertTrue(bc.toString().contains("truetrue"));
}
Also used : ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

ContextEnvironment (org.apache.tomcat.util.descriptor.web.ContextEnvironment)23 ContextResourceLink (org.apache.tomcat.util.descriptor.web.ContextResourceLink)10 ContextResource (org.apache.tomcat.util.descriptor.web.ContextResource)9 ContextResourceEnvRef (org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef)7 Test (org.junit.Test)7 NamingResourcesImpl (org.apache.catalina.deploy.NamingResourcesImpl)6 NamingException (javax.naming.NamingException)5 Context (org.apache.catalina.Context)5 Tomcat (org.apache.catalina.startup.Tomcat)5 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)5 ContextEjb (org.apache.tomcat.util.descriptor.web.ContextEjb)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 InitialContext (javax.naming.InitialContext)4 LifecycleException (org.apache.catalina.LifecycleException)4 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)4 ContextService (org.apache.tomcat.util.descriptor.web.ContextService)4 ContextLocalEjb (org.apache.tomcat.util.descriptor.web.ContextLocalEjb)3 File (java.io.File)2 ObjectName (javax.management.ObjectName)2 StandardContext (org.apache.catalina.core.StandardContext)2