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()]);
}
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());
}
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);
}
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);
}
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());
}
Aggregations