Search in sources :

Example 1 with ContextResource

use of org.apache.catalina.deploy.ContextResource in project tomcat70 by apache.

the class TestNamingContext method testBeanFactory.

@Test
public void testBeanFactory() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();
    // No file system docBase required
    StandardContext ctx = (StandardContext) 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.addServletMapping("/", "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) StandardContext(org.apache.catalina.core.StandardContext) ContextResource(org.apache.catalina.deploy.ContextResource) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 2 with ContextResource

use of org.apache.catalina.deploy.ContextResource in project tomcat70 by apache.

the class TestNamingContext method doTestLookup.

public void doTestLookup(boolean useSingletonResource) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();
    // No file system docBase required
    StandardContext ctx = (StandardContext) 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.addServletMapping("/", "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) StandardContext(org.apache.catalina.core.StandardContext) ContextResource(org.apache.catalina.deploy.ContextResource)

Example 3 with ContextResource

use of org.apache.catalina.deploy.ContextResource in project tomcat70 by apache.

the class ContextResourceMBean method getAttribute.

// ----------------------------------------------------- Instance Variables
// ------------------------------------------------------------- Attributes
/**
 * 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("Attribute name is null"), "Attribute name is null");
    ContextResource cr = null;
    try {
        cr = (ContextResource) getManagedResource();
    } catch (InstanceNotFoundException e) {
        throw new MBeanException(e);
    } catch (InvalidTargetObjectTypeException e) {
        throw new MBeanException(e);
    }
    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("Cannot find attribute " + name);
        }
    }
    return value;
}
Also used : AttributeNotFoundException(javax.management.AttributeNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanException(javax.management.MBeanException) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) RuntimeOperationsException(javax.management.RuntimeOperationsException) ContextResource(org.apache.catalina.deploy.ContextResource)

Example 4 with ContextResource

use of org.apache.catalina.deploy.ContextResource in project tomcat70 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("Attribute is null"), "Attribute is null");
    String name = attribute.getName();
    Object value = attribute.getValue();
    if (name == null)
        throw new RuntimeOperationsException(new IllegalArgumentException("Attribute name is null"), "Attribute name is null");
    ContextResource cr = null;
    try {
        cr = (ContextResource) getManagedResource();
    } catch (InstanceNotFoundException e) {
        throw new MBeanException(e);
    } catch (InvalidTargetObjectTypeException e) {
        throw new MBeanException(e);
    }
    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 : InstanceNotFoundException(javax.management.InstanceNotFoundException) NamingResources(org.apache.catalina.deploy.NamingResources) MBeanException(javax.management.MBeanException) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) RuntimeOperationsException(javax.management.RuntimeOperationsException) ContextResource(org.apache.catalina.deploy.ContextResource)

Example 5 with ContextResource

use of org.apache.catalina.deploy.ContextResource in project tomcat70 by apache.

the class WebAnnotationSet method addResource.

protected static void addResource(Context context, Resource annotation, String defaultName, Class<?> defaultType) {
    String name = getName(annotation, defaultName);
    String type = getType(annotation, defaultType);
    if (type.equals("java.lang.String") || type.equals("java.lang.Character") || type.equals("java.lang.Integer") || type.equals("java.lang.Boolean") || type.equals("java.lang.Double") || type.equals("java.lang.Byte") || type.equals("java.lang.Short") || type.equals("java.lang.Long") || type.equals("java.lang.Float")) {
        // env-ref element
        ContextEnvironment resource = new ContextEnvironment();
        resource.setName(name);
        resource.setType(type);
        resource.setDescription(annotation.description());
        resource.setValue(annotation.mappedName());
        resource.setLookupName(annotation.lookup());
        context.getNamingResources().addEnvironment(resource);
    } else if (type.equals("javax.xml.rpc.Service")) {
        // service-ref element
        ContextService service = new ContextService();
        service.setName(name);
        service.setWsdlfile(annotation.mappedName());
        service.setType(type);
        service.setDescription(annotation.description());
        service.setLookupName(annotation.lookup());
        context.getNamingResources().addService(service);
    } else if (type.equals("javax.sql.DataSource") || type.equals("javax.jms.ConnectionFactory") || type.equals("javax.jms.QueueConnectionFactory") || type.equals("javax.jms.TopicConnectionFactory") || type.equals("javax.mail.Session") || type.equals("java.net.URL") || type.equals("javax.resource.cci.ConnectionFactory") || type.equals("org.omg.CORBA_2_3.ORB") || type.endsWith("ConnectionFactory")) {
        // resource-ref element
        ContextResource resource = new ContextResource();
        resource.setName(name);
        resource.setType(type);
        if (annotation.authenticationType() == Resource.AuthenticationType.CONTAINER) {
            resource.setAuth("Container");
        } else if (annotation.authenticationType() == Resource.AuthenticationType.APPLICATION) {
            resource.setAuth("Application");
        }
        resource.setScope(annotation.shareable() ? "Shareable" : "Unshareable");
        resource.setProperty("mappedName", annotation.mappedName());
        resource.setDescription(annotation.description());
        resource.setLookupName(annotation.lookup());
        context.getNamingResources().addResource(resource);
    } else if (type.equals("javax.jms.Queue") || type.equals("javax.jms.Topic")) {
        // message-destination-ref
        MessageDestinationRef resource = new MessageDestinationRef();
        resource.setName(name);
        resource.setType(type);
        resource.setUsage(annotation.mappedName());
        resource.setDescription(annotation.description());
        resource.setLookupName(annotation.lookup());
        context.getNamingResources().addMessageDestinationRef(resource);
    } else {
        /*
             * General case. Also used for:
             * - javax.resource.cci.InteractionSpec
             * - javax.transaction.UserTransaction
             */
        // resource-env-ref
        ContextResourceEnvRef resource = new ContextResourceEnvRef();
        resource.setName(name);
        resource.setType(type);
        resource.setProperty("mappedName", annotation.mappedName());
        resource.setDescription(annotation.description());
        resource.setLookupName(annotation.lookup());
        context.getNamingResources().addResourceEnvRef(resource);
    }
}
Also used : ContextEnvironment(org.apache.catalina.deploy.ContextEnvironment) MessageDestinationRef(org.apache.catalina.deploy.MessageDestinationRef) ContextService(org.apache.catalina.deploy.ContextService) ContextResourceEnvRef(org.apache.catalina.deploy.ContextResourceEnvRef) ContextResource(org.apache.catalina.deploy.ContextResource)

Aggregations

ContextResource (org.apache.catalina.deploy.ContextResource)14 NamingResources (org.apache.catalina.deploy.NamingResources)6 ObjectName (javax.management.ObjectName)3 StandardContext (org.apache.catalina.core.StandardContext)3 ContextEnvironment (org.apache.catalina.deploy.ContextEnvironment)3 Tomcat (org.apache.catalina.startup.Tomcat)3 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)3 ArrayList (java.util.ArrayList)2 InstanceNotFoundException (javax.management.InstanceNotFoundException)2 MBeanException (javax.management.MBeanException)2 MalformedObjectNameException (javax.management.MalformedObjectNameException)2 RuntimeOperationsException (javax.management.RuntimeOperationsException)2 InvalidTargetObjectTypeException (javax.management.modelmbean.InvalidTargetObjectTypeException)2 ContextResourceEnvRef (org.apache.catalina.deploy.ContextResourceEnvRef)2 ContextService (org.apache.catalina.deploy.ContextService)2 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)2 Test (org.junit.Test)2 ContextParameter (com.sun.enterprise.deployment.web.ContextParameter)1 AttributeNotFoundException (javax.management.AttributeNotFoundException)1 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)1