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