Search in sources :

Example 16 with ContextResourceLink

use of org.apache.tomcat.util.descriptor.web.ContextResourceLink 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)

Aggregations

ContextResourceLink (org.apache.tomcat.util.descriptor.web.ContextResourceLink)16 ContextEnvironment (org.apache.tomcat.util.descriptor.web.ContextEnvironment)10 ContextResource (org.apache.tomcat.util.descriptor.web.ContextResource)7 NamingResourcesImpl (org.apache.catalina.deploy.NamingResourcesImpl)6 NamingException (javax.naming.NamingException)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 LifecycleException (org.apache.catalina.LifecycleException)4 ContextEjb (org.apache.tomcat.util.descriptor.web.ContextEjb)4 ContextResourceEnvRef (org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef)4 ObjectName (javax.management.ObjectName)2 RuntimeOperationsException (javax.management.RuntimeOperationsException)2 Context (org.apache.catalina.Context)2 ContextLocalEjb (org.apache.tomcat.util.descriptor.web.ContextLocalEjb)2 ContextService (org.apache.tomcat.util.descriptor.web.ContextService)2 Test (org.junit.Test)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 JarFile (java.util.jar.JarFile)1 AttributeNotFoundException (javax.management.AttributeNotFoundException)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1