Search in sources :

Example 86 with Context

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();
}
Also used : Context(org.apache.catalina.Context) Wrapper(org.apache.catalina.Wrapper) Container(org.apache.catalina.Container) Host(org.apache.catalina.Host) Engine(org.apache.catalina.Engine) ContextName(org.apache.catalina.util.ContextName)

Example 87 with Context

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;
    }
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) Container(org.apache.catalina.Container) MessageBytes(org.apache.tomcat.util.buf.MessageBytes) MappingData(org.apache.catalina.mapper.MappingData) Engine(org.apache.catalina.Engine)

Example 88 with Context

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);
}
Also used : NamingContext(org.apache.naming.NamingContext) Context(org.apache.catalina.Context) Server(org.apache.catalina.Server) Host(org.apache.catalina.Host) ObjectName(javax.management.ObjectName)

Example 89 with Context

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);
            }
        }
    }
}
Also used : NamingContext(org.apache.naming.NamingContext) Context(org.apache.catalina.Context) StringTokenizer(java.util.StringTokenizer) NamingException(javax.naming.NamingException)

Example 90 with Context

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));
    }
}
Also used : NamingContext(org.apache.naming.NamingContext) Context(org.apache.catalina.Context) ContextHandler(org.apache.tomcat.util.descriptor.web.ContextHandler) MalformedURLException(java.net.MalformedURLException) StringRefAddr(javax.naming.StringRefAddr) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) ServiceRef(org.apache.naming.ServiceRef) URL(java.net.URL) HandlerRef(org.apache.naming.HandlerRef)

Aggregations

Context (org.apache.catalina.Context)376 Tomcat (org.apache.catalina.startup.Tomcat)212 Test (org.junit.Test)180 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)127 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)96 File (java.io.File)77 ServletContext (javax.servlet.ServletContext)74 AsyncContext (javax.servlet.AsyncContext)73 StandardContext (org.apache.catalina.core.StandardContext)65 Wrapper (org.apache.catalina.Wrapper)53 IOException (java.io.IOException)40 TesterContext (org.apache.tomcat.unittest.TesterContext)39 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)37 URI (java.net.URI)33 WebSocketContainer (javax.websocket.WebSocketContainer)32 Session (javax.websocket.Session)31 Host (org.apache.catalina.Host)30 Container (org.apache.catalina.Container)26 ArrayList (java.util.ArrayList)25 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)24