Search in sources :

Example 6 with ContextResource

use of org.apache.catalina.deploy.ContextResource in project Payara by payara.

the class StandardContext method addResource.

/**
 * Add a resource reference for this web application.
 *
 * @param resourceName New resource reference name
 */
public String addResource(String resourceName, String type) throws MalformedObjectNameException {
    NamingResources nresources = getNamingResources();
    if (nresources == null) {
        return null;
    }
    ContextResource resource = nresources.findResource(resourceName);
    if (resource != null) {
        throw new IllegalArgumentException("Invalid resource name - already exists'" + resourceName + "'");
    }
    resource = new ContextResource();
    resource.setName(resourceName);
    resource.setType(type);
    nresources.addResource(resource);
    // Return the corresponding MBean name
    return createObjectName(resource).toString();
}
Also used : NamingResources(org.apache.catalina.deploy.NamingResources) ContextResource(org.apache.catalina.deploy.ContextResource)

Example 7 with ContextResource

use of org.apache.catalina.deploy.ContextResource in project Payara by payara.

the class StandardContext method getResourceNames.

/**
 * Return the MBean Names of all the defined resource references for this
 * application.
 */
public String[] getResourceNames() {
    ContextResource[] resources = getNamingResources().findResources();
    List<String> results = new ArrayList<>();
    for (ContextResource resource : resources) {
        try {
            ObjectName oname = createObjectName(resource);
            results.add(oname.toString());
        } catch (MalformedObjectNameException e) {
            throw new IllegalArgumentException("Cannot create object name for resource " + resource, e);
        }
    }
    return results.toArray(new String[results.size()]);
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) ArrayList(java.util.ArrayList) ContextResource(org.apache.catalina.deploy.ContextResource) ObjectName(javax.management.ObjectName)

Example 8 with ContextResource

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

the class TestNamingContext method testListBindings.

@Test
public void testListBindings() 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");
    ctx.getNamingResources().addResource(cr);
    // Map the test Servlet
    Bug23950Servlet bug23950Servlet = new Bug23950Servlet();
    Tomcat.addServlet(ctx, "bug23950Servlet", bug23950Servlet);
    ctx.addServletMapping("/", "bug23950Servlet");
    tomcat.start();
    ByteChunk bc = getUrl("http://localhost:" + getPort() + "/");
    Assert.assertEquals("org.apache.naming.resources.TesterObject", 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 9 with ContextResource

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

the class NamingContextListener method createNamingContext.

/**
 * Create and initialize the JNDI naming context.
 */
private void createNamingContext() throws NamingException {
    // Creating the comp subcontext
    if (container instanceof Server) {
        compCtx = namingContext;
        envCtx = namingContext;
    } else {
        compCtx = namingContext.createSubcontext("comp");
        envCtx = compCtx.createSubcontext("env");
    }
    int i;
    if (log.isDebugEnabled())
        log.debug("Creating JNDI naming context");
    if (namingResources == null) {
        namingResources = new NamingResources();
        namingResources.setContainer(container);
    }
    // Resource links
    ContextResourceLink[] resourceLinks = namingResources.findResourceLinks();
    for (i = 0; i < resourceLinks.length; i++) {
        addResourceLink(resourceLinks[i]);
    }
    // Resources
    ContextResource[] resources = namingResources.findResources();
    for (i = 0; i < resources.length; i++) {
        addResource(resources[i]);
    }
    // Resources Env
    ContextResourceEnvRef[] resourceEnvRefs = namingResources.findResourceEnvRefs();
    for (i = 0; i < resourceEnvRefs.length; i++) {
        addResourceEnvRef(resourceEnvRefs[i]);
    }
    // Environment entries
    ContextEnvironment[] contextEnvironments = namingResources.findEnvironments();
    for (i = 0; i < contextEnvironments.length; i++) {
        addEnvironment(contextEnvironments[i]);
    }
    // EJB references
    ContextEjb[] ejbs = namingResources.findEjbs();
    for (i = 0; i < ejbs.length; i++) {
        addEjb(ejbs[i]);
    }
    // WebServices references
    ContextService[] services = namingResources.findServices();
    for (i = 0; i < services.length; i++) {
        addService(services[i]);
    }
    // Binding a User Transaction reference
    if (container instanceof Context) {
        try {
            Reference ref = new TransactionRef();
            compCtx.bind("UserTransaction", ref);
            ContextTransaction transaction = namingResources.getTransaction();
            if (transaction != null) {
                Iterator<String> params = transaction.listProperties();
                while (params.hasNext()) {
                    String paramName = params.next();
                    String paramValue = (String) transaction.getProperty(paramName);
                    StringRefAddr refAddr = new StringRefAddr(paramName, paramValue);
                    ref.add(refAddr);
                }
            }
        } catch (NameAlreadyBoundException e) {
        // Ignore because UserTransaction was obviously
        // added via ResourceLink
        } catch (NamingException e) {
            logger.error(sm.getString("naming.bindFailed", e));
        }
    }
    // Binding the resources directory context
    if (container instanceof Context) {
        try {
            compCtx.bind("Resources", ((Container) container).getResources());
        } catch (NamingException e) {
            logger.error(sm.getString("naming.bindFailed", e));
        }
    }
}
Also used : ContextEnvironment(org.apache.catalina.deploy.ContextEnvironment) NamingContext(org.apache.naming.NamingContext) Context(org.apache.catalina.Context) ContextService(org.apache.catalina.deploy.ContextService) Server(org.apache.catalina.Server) ContextResourceLink(org.apache.catalina.deploy.ContextResourceLink) Reference(javax.naming.Reference) NamingResources(org.apache.catalina.deploy.NamingResources) ContextTransaction(org.apache.catalina.deploy.ContextTransaction) ContextEjb(org.apache.catalina.deploy.ContextEjb) ContextResource(org.apache.catalina.deploy.ContextResource) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) StringRefAddr(javax.naming.StringRefAddr) TransactionRef(org.apache.naming.TransactionRef) NamingException(javax.naming.NamingException) ContextResourceEnvRef(org.apache.catalina.deploy.ContextResourceEnvRef)

Example 10 with ContextResource

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

the class NamingResourcesMBean method addResource.

/**
 * Add a resource reference for this web application.
 *
 * @param resourceName New resource reference name
 * @param type New resource reference type
 */
public String addResource(String resourceName, String type) throws MalformedObjectNameException {
    NamingResources nresources = (NamingResources) this.resource;
    if (nresources == null) {
        return null;
    }
    ContextResource resource = nresources.findResource(resourceName);
    if (resource != null) {
        throw new IllegalArgumentException("Invalid resource name - already exists'" + resourceName + "'");
    }
    resource = new ContextResource();
    resource.setName(resourceName);
    resource.setType(type);
    nresources.addResource(resource);
    // Return the corresponding MBean name
    ManagedBean managed = registry.findManagedBean("ContextResource");
    ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), resource);
    return (oname.toString());
}
Also used : NamingResources(org.apache.catalina.deploy.NamingResources) ManagedBean(org.apache.tomcat.util.modeler.ManagedBean) ContextResource(org.apache.catalina.deploy.ContextResource) ObjectName(javax.management.ObjectName)

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