use of org.apache.catalina.Context in project tomcat by apache.
the class ContainerBase method getMBeanKeyProperties.
@Override
public String getMBeanKeyProperties() {
Container c = this;
StringBuilder keyProperties = new StringBuilder();
int containerCount = 0;
// each container
while (!(c instanceof Engine)) {
if (c instanceof Wrapper) {
keyProperties.insert(0, ",servlet=");
keyProperties.insert(9, c.getName());
} else if (c instanceof Context) {
keyProperties.insert(0, ",context=");
ContextName cn = new ContextName(c.getName(), false);
keyProperties.insert(9, cn.getDisplayName());
} else if (c instanceof Host) {
keyProperties.insert(0, ",host=");
keyProperties.insert(6, c.getName());
} else if (c == null) {
// May happen in unit testing and/or some embedding scenarios
keyProperties.append(",container");
keyProperties.append(containerCount++);
keyProperties.append("=null");
break;
} else {
// Should never happen...
keyProperties.append(",container");
keyProperties.append(containerCount++);
keyProperties.append('=');
keyProperties.append(c.getName());
}
c = c.getParent();
}
return keyProperties.toString();
}
use of org.apache.catalina.Context in project tomcat by apache.
the class ApplicationContext method getContext.
@Override
public ServletContext getContext(String uri) {
// Validate the format of the specified argument
if (uri == null || !uri.startsWith("/")) {
return null;
}
Context child = null;
try {
// Look for an exact match
Container host = context.getParent();
child = (Context) host.findChild(uri);
// Non-running contexts should be ignored.
if (child != null && !child.getState().isAvailable()) {
child = null;
}
// Remove any version information and use the mapper
if (child == null) {
int i = uri.indexOf("##");
if (i > -1) {
uri = uri.substring(0, i);
}
// Note: This could be more efficient with a dedicated Mapper
// method but such an implementation would require some
// refactoring of the Mapper to avoid copy/paste of
// existing code.
MessageBytes hostMB = MessageBytes.newInstance();
hostMB.setString(host.getName());
MessageBytes pathMB = MessageBytes.newInstance();
pathMB.setString(uri);
MappingData mappingData = new MappingData();
((Engine) host.getParent()).getService().getMapper().map(hostMB, pathMB, null, mappingData);
child = mappingData.context;
}
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
return null;
}
if (child == null) {
return null;
}
if (context.getCrossContext()) {
// If crossContext is enabled, can always return the context
return child.getServletContext();
} else if (child == context) {
// Can still return the current context
return context.getServletContext();
} else {
// Nothing to return
return null;
}
}
use of org.apache.catalina.Context in project tomcat by apache.
the class NamingContextListener method createObjectName.
/**
* Create an <code>ObjectName</code> for this
* <code>ContextResource</code> object.
*
* @param resource The resource
* @return ObjectName The object name
* @exception MalformedObjectNameException if a name cannot be created
*/
protected ObjectName createObjectName(ContextResource resource) throws MalformedObjectNameException {
String domain = null;
if (container instanceof StandardServer) {
domain = ((StandardServer) container).getDomain();
} else if (container instanceof ContainerBase) {
domain = ((ContainerBase) container).getDomain();
}
if (domain == null) {
domain = "Catalina";
}
ObjectName name = null;
String quotedResourceName = ObjectName.quote(resource.getName());
if (container instanceof Server) {
name = new ObjectName(domain + ":type=DataSource" + ",class=" + resource.getType() + ",name=" + quotedResourceName);
} else if (container instanceof Context) {
String contextName = ((Context) container).getName();
if (!contextName.startsWith("/"))
contextName = "/" + contextName;
Host host = (Host) ((Context) container).getParent();
name = new ObjectName(domain + ":type=DataSource" + ",host=" + host.getName() + ",context=" + contextName + ",class=" + resource.getType() + ",name=" + quotedResourceName);
}
return (name);
}
use of org.apache.catalina.Context in project tomcat by apache.
the class NamingContextListener method createSubcontexts.
/**
* Create all intermediate subcontexts.
*/
private void createSubcontexts(javax.naming.Context ctx, String name) throws NamingException {
javax.naming.Context currentContext = ctx;
StringTokenizer tokenizer = new StringTokenizer(name, "/");
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
if ((!token.equals("")) && (tokenizer.hasMoreTokens())) {
try {
currentContext = currentContext.createSubcontext(token);
} catch (NamingException e) {
// Silent catch. Probably an object is already bound in
// the context.
currentContext = (javax.naming.Context) currentContext.lookup(token);
}
}
}
}
use of org.apache.catalina.Context in project tomcat by apache.
the class NamingContextListener method addService.
/**
* Set the specified web service in the naming context.
*
* @param service the web service descriptor
*/
public void addService(ContextService service) {
if (service.getWsdlfile() != null) {
URL wsdlURL = null;
try {
wsdlURL = new URL(service.getWsdlfile());
} catch (MalformedURLException e) {
// Ignore and carry on
}
if (wsdlURL == null) {
try {
wsdlURL = ((Context) container).getServletContext().getResource(service.getWsdlfile());
} catch (MalformedURLException e) {
// Ignore and carry on
}
}
if (wsdlURL == null) {
try {
wsdlURL = ((Context) container).getServletContext().getResource("/" + service.getWsdlfile());
log.debug(" Changing service ref wsdl file for /" + service.getWsdlfile());
} catch (MalformedURLException e) {
log.error(sm.getString("naming.wsdlFailed", e));
}
}
if (wsdlURL == null)
service.setWsdlfile(null);
else
service.setWsdlfile(wsdlURL.toString());
}
if (service.getJaxrpcmappingfile() != null) {
URL jaxrpcURL = null;
try {
jaxrpcURL = new URL(service.getJaxrpcmappingfile());
} catch (MalformedURLException e) {
// Ignore and carry on
}
if (jaxrpcURL == null) {
try {
jaxrpcURL = ((Context) container).getServletContext().getResource(service.getJaxrpcmappingfile());
} catch (MalformedURLException e) {
// Ignore and carry on
}
}
if (jaxrpcURL == null) {
try {
jaxrpcURL = ((Context) container).getServletContext().getResource("/" + service.getJaxrpcmappingfile());
log.debug(" Changing service ref jaxrpc file for /" + service.getJaxrpcmappingfile());
} catch (MalformedURLException e) {
log.error(sm.getString("naming.wsdlFailed", e));
}
}
if (jaxrpcURL == null)
service.setJaxrpcmappingfile(null);
else
service.setJaxrpcmappingfile(jaxrpcURL.toString());
}
// Create a reference to the resource.
Reference ref = new ServiceRef(service.getName(), service.getType(), service.getServiceqname(), service.getWsdlfile(), service.getJaxrpcmappingfile());
// Adding the additional port-component-ref, if any
Iterator<String> portcomponent = service.getServiceendpoints();
while (portcomponent.hasNext()) {
String serviceendpoint = portcomponent.next();
StringRefAddr refAddr = new StringRefAddr(ServiceRef.SERVICEENDPOINTINTERFACE, serviceendpoint);
ref.add(refAddr);
String portlink = service.getPortlink(serviceendpoint);
refAddr = new StringRefAddr(ServiceRef.PORTCOMPONENTLINK, portlink);
ref.add(refAddr);
}
// Adding the additional parameters, if any
Iterator<String> handlers = service.getHandlers();
while (handlers.hasNext()) {
String handlername = handlers.next();
ContextHandler handler = service.getHandler(handlername);
HandlerRef handlerRef = new HandlerRef(handlername, handler.getHandlerclass());
Iterator<String> localParts = handler.getLocalparts();
while (localParts.hasNext()) {
String localPart = localParts.next();
String namespaceURI = handler.getNamespaceuri(localPart);
handlerRef.add(new StringRefAddr(HandlerRef.HANDLER_LOCALPART, localPart));
handlerRef.add(new StringRefAddr(HandlerRef.HANDLER_NAMESPACE, namespaceURI));
}
Iterator<String> params = handler.listProperties();
while (params.hasNext()) {
String paramName = params.next();
String paramValue = (String) handler.getProperty(paramName);
handlerRef.add(new StringRefAddr(HandlerRef.HANDLER_PARAMNAME, paramName));
handlerRef.add(new StringRefAddr(HandlerRef.HANDLER_PARAMVALUE, paramValue));
}
for (int i = 0; i < handler.getSoapRolesSize(); i++) {
handlerRef.add(new StringRefAddr(HandlerRef.HANDLER_SOAPROLE, handler.getSoapRole(i)));
}
for (int i = 0; i < handler.getPortNamesSize(); i++) {
handlerRef.add(new StringRefAddr(HandlerRef.HANDLER_PORTNAME, handler.getPortName(i)));
}
((ServiceRef) ref).addHandler(handlerRef);
}
try {
if (log.isDebugEnabled()) {
log.debug(" Adding service ref " + service.getName() + " " + ref);
}
createSubcontexts(envCtx, service.getName());
envCtx.bind(service.getName(), ref);
} catch (NamingException e) {
log.error(sm.getString("naming.bindFailed", e));
}
}
Aggregations