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