Search in sources :

Example 1 with ContextResourceLink

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

the class StandardContext method getResourceLinks.

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

Example 2 with ContextResourceLink

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

the class NamingResourcesMBean method addResourceLink.

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

Example 3 with ContextResourceLink

use of org.apache.catalina.deploy.ContextResourceLink in project tomcat70 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.catalina.deploy.ContextEnvironment) Tomcat(org.apache.catalina.startup.Tomcat) ContextResourceLink(org.apache.catalina.deploy.ContextResourceLink) ResourceLinkFactory(org.apache.naming.factory.ResourceLinkFactory) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 4 with ContextResourceLink

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

the class ContextResourceLinkMBean 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");
    ContextResourceLink crl = null;
    try {
        crl = (ContextResourceLink) getManagedResource();
    } catch (InstanceNotFoundException e) {
        throw new MBeanException(e);
    } catch (InvalidTargetObjectTypeException e) {
        throw new MBeanException(e);
    }
    if ("global".equals(name)) {
        crl.setGlobal((String) value);
    } else if ("description".equals(name)) {
        crl.setDescription((String) value);
    } else if ("name".equals(name)) {
        crl.setName((String) value);
    } else if ("type".equals(name)) {
        crl.setType((String) value);
    } else {
        crl.setProperty(name, "" + value);
    }
    // cannot use side-effects.  It's removed and added back each time
    // there is a modification in a resource.
    NamingResources nr = crl.getNamingResources();
    nr.removeResourceLink(crl.getName());
    nr.addResourceLink(crl);
}
Also used : ContextResourceLink(org.apache.catalina.deploy.ContextResourceLink) InstanceNotFoundException(javax.management.InstanceNotFoundException) NamingResources(org.apache.catalina.deploy.NamingResources) MBeanException(javax.management.MBeanException) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 5 with ContextResourceLink

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

the class TestTomcat method testEnableNamingGlobal.

/**
 * Test for enabling JNDI and using global resources.
 */
@Test
public void testEnableNamingGlobal() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    // You can customise the context by calling its API
    // Enable JNDI - it is disabled by default
    tomcat.enableNaming();
    ContextEnvironment environment = new ContextEnvironment();
    environment.setType("java.lang.String");
    environment.setName("globalTest");
    environment.setValue("Tomcat User");
    tomcat.getServer().getGlobalNamingResources().addEnvironment(environment);
    ContextResourceLink link = new ContextResourceLink();
    link.setGlobal("globalTest");
    link.setName(HelloWorldJndi.JNDI_ENV_NAME);
    link.setType("java.lang.String");
    ctx.getNamingResources().addResourceLink(link);
    Tomcat.addServlet(ctx, "jndiServlet", new HelloWorldJndi());
    ctx.addServletMapping("/", "jndiServlet");
    tomcat.start();
    ByteChunk res = getUrl("http://localhost:" + getPort() + "/");
    Assert.assertEquals("Hello, Tomcat User", res.toString());
}
Also used : ReplicatedContext(org.apache.catalina.ha.context.ReplicatedContext) InitialContext(javax.naming.InitialContext) Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) ContextEnvironment(org.apache.catalina.deploy.ContextEnvironment) ContextResourceLink(org.apache.catalina.deploy.ContextResourceLink) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) Test(org.junit.Test)

Aggregations

ContextResourceLink (org.apache.catalina.deploy.ContextResourceLink)11 NamingResources (org.apache.catalina.deploy.NamingResources)6 ObjectName (javax.management.ObjectName)3 ContextEnvironment (org.apache.catalina.deploy.ContextEnvironment)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 Context (org.apache.catalina.Context)2 Test (org.junit.Test)2 AttributeNotFoundException (javax.management.AttributeNotFoundException)1 Context (javax.naming.Context)1 InitialContext (javax.naming.InitialContext)1 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)1 NamingException (javax.naming.NamingException)1 Reference (javax.naming.Reference)1 StringRefAddr (javax.naming.StringRefAddr)1 Server (org.apache.catalina.Server)1