Search in sources :

Example 11 with ContextResource

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

the class TestNamingContext method doTestLookup.

public void doTestLookup(boolean useSingletonResource) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();
    // No file system docBase required
    org.apache.catalina.Context ctx = tomcat.addContext("", null);
    // Create the resource
    ContextResource cr = new ContextResource();
    cr.setName("list/foo");
    cr.setType("org.apache.naming.resources.TesterObject");
    cr.setProperty("factory", "org.apache.naming.resources.TesterFactory");
    cr.setSingleton(useSingletonResource);
    ctx.getNamingResources().addResource(cr);
    // Map the test Servlet
    Bug49994Servlet bug49994Servlet = new Bug49994Servlet();
    Tomcat.addServlet(ctx, "bug49994Servlet", bug49994Servlet);
    ctx.addServletMappingDecoded("/", "bug49994Servlet");
    tomcat.start();
    ByteChunk bc = getUrl("http://localhost:" + getPort() + "/");
    String expected;
    if (useSingletonResource) {
        expected = "EQUAL";
    } else {
        expected = "NOTEQUAL";
    }
    Assert.assertEquals(expected, bc.toString());
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource)

Example 12 with ContextResource

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

the class TestNamingContext method testBeanFactory.

@Test
public void testBeanFactory() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();
    // No file system docBase required
    org.apache.catalina.Context ctx = tomcat.addContext("", null);
    // Create the resource
    ContextResource cr = new ContextResource();
    cr.setName("bug50351");
    cr.setType("org.apache.naming.resources.TesterObject");
    cr.setProperty("factory", "org.apache.naming.factory.BeanFactory");
    cr.setProperty("foo", "value");
    ctx.getNamingResources().addResource(cr);
    // Map the test Servlet
    Bug50351Servlet bug50351Servlet = new Bug50351Servlet();
    Tomcat.addServlet(ctx, "bug50351Servlet", bug50351Servlet);
    ctx.addServletMappingDecoded("/", "bug50351Servlet");
    tomcat.start();
    ByteChunk bc = getUrl("http://localhost:" + getPort() + "/");
    Assert.assertEquals("value", bc.toString());
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 13 with ContextResource

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

the class ContextResourceMBean method getAttribute.

/**
 * Obtain and return the value of a specific attribute of this MBean.
 *
 * @param name Name of the requested attribute
 *
 * @exception AttributeNotFoundException if this attribute is not
 *  supported by this MBean
 * @exception MBeanException if the initializer of an object
 *  throws an exception
 * @exception ReflectionException if a Java reflection exception
 *  occurs when invoking the getter
 */
@Override
public Object getAttribute(String name) throws AttributeNotFoundException, MBeanException, ReflectionException {
    // Validate the input parameters
    if (name == null) {
        throw new RuntimeOperationsException(new IllegalArgumentException(sm.getString("mBean.nullName")), sm.getString("mBean.nullName"));
    }
    ContextResource cr = doGetManagedResource();
    String value = null;
    if ("auth".equals(name)) {
        return cr.getAuth();
    } else if ("description".equals(name)) {
        return cr.getDescription();
    } else if ("name".equals(name)) {
        return cr.getName();
    } else if ("scope".equals(name)) {
        return cr.getScope();
    } else if ("type".equals(name)) {
        return cr.getType();
    } else {
        value = (String) cr.getProperty(name);
        if (value == null) {
            throw new AttributeNotFoundException(sm.getString("mBean.attributeNotFound", name));
        }
    }
    return value;
}
Also used : AttributeNotFoundException(javax.management.AttributeNotFoundException) RuntimeOperationsException(javax.management.RuntimeOperationsException) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource)

Example 14 with ContextResource

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

the class ContextResourceMBean method setAttribute.

/**
 * Set the value of a specific attribute of this MBean.
 *
 * @param attribute The identification of the attribute to be set
 *  and the new value
 *
 * @exception AttributeNotFoundException if this attribute is not
 *  supported by this MBean
 * @exception MBeanException if the initializer of an object
 *  throws an exception
 * @exception ReflectionException if a Java reflection exception
 *  occurs when invoking the getter
 */
@Override
public void setAttribute(Attribute attribute) throws AttributeNotFoundException, MBeanException, ReflectionException {
    // Validate the input parameters
    if (attribute == null) {
        throw new RuntimeOperationsException(new IllegalArgumentException(sm.getString("mBean.nullAttribute")), sm.getString("mBean.nullAttribute"));
    }
    String name = attribute.getName();
    Object value = attribute.getValue();
    if (name == null) {
        throw new RuntimeOperationsException(new IllegalArgumentException(sm.getString("mBean.nullName")), sm.getString("mBean.nullName"));
    }
    ContextResource cr = doGetManagedResource();
    if ("auth".equals(name)) {
        cr.setAuth((String) value);
    } else if ("description".equals(name)) {
        cr.setDescription((String) value);
    } else if ("name".equals(name)) {
        cr.setName((String) value);
    } else if ("scope".equals(name)) {
        cr.setScope((String) value);
    } else if ("type".equals(name)) {
        cr.setType((String) value);
    } else {
        cr.setProperty(name, "" + value);
    }
    // cannot use side-effects.  It's removed and added back each time
    // there is a modification in a resource.
    NamingResources nr = cr.getNamingResources();
    nr.removeResource(cr.getName());
    nr.addResource(cr);
}
Also used : NamingResources(org.apache.tomcat.util.descriptor.web.NamingResources) RuntimeOperationsException(javax.management.RuntimeOperationsException) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource)

Example 15 with ContextResource

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

the class NamingResourcesMBean method removeResource.

/**
 * Remove any resource reference with the specified name.
 *
 * @param resourceName Name of the resource reference to remove
 */
public void removeResource(String resourceName) {
    resourceName = ObjectName.unquote(resourceName);
    NamingResourcesImpl nresources = (NamingResourcesImpl) this.resource;
    if (nresources == null) {
        return;
    }
    ContextResource resource = nresources.findResource(resourceName);
    if (resource == null) {
        throw new IllegalArgumentException(sm.getString("namingResourcesMBean.removeNotFound.resource", resourceName));
    }
    nresources.removeResource(resourceName);
}
Also used : NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource)

Aggregations

ContextResource (org.apache.tomcat.util.descriptor.web.ContextResource)28 NamingResourcesImpl (org.apache.catalina.deploy.NamingResourcesImpl)10 ContextEnvironment (org.apache.tomcat.util.descriptor.web.ContextEnvironment)9 NamingException (javax.naming.NamingException)7 ContextResourceLink (org.apache.tomcat.util.descriptor.web.ContextResourceLink)7 ContextResourceEnvRef (org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef)6 ContextEjb (org.apache.tomcat.util.descriptor.web.ContextEjb)5 ContextService (org.apache.tomcat.util.descriptor.web.ContextService)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ArrayList (java.util.ArrayList)3 LifecycleException (org.apache.catalina.LifecycleException)3 Tomcat (org.apache.catalina.startup.Tomcat)3 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)3 ObjectName (javax.management.ObjectName)2 RuntimeOperationsException (javax.management.RuntimeOperationsException)2 Context (javax.naming.Context)2 Reference (javax.naming.Reference)2 StringRefAddr (javax.naming.StringRefAddr)2 EntityManagerFactory (javax.persistence.EntityManagerFactory)2 Server (org.apache.catalina.Server)2