Search in sources :

Example 76 with Container

use of org.apache.catalina.Container in project Payara by payara.

the class StandardContext method addServlet.

/**
 * Adds the given servlet instance with the given name and URL patterns
 * to this servlet context, and initializes it.
 *
 * @param servletName the servlet name
 * @param servlet the servlet instance
 * @param initParams Map containing the initialization parameters for
 * the servlet
 * @param urlPatterns the URL patterns that will be mapped to the servlet
 *
 * @return the ServletRegistration through which the servlet may be
 * further configured
 */
@Override
public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet, Map<String, String> initParams, String... urlPatterns) {
    if (isContextInitializedCalled) {
        String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_CONTEXT_ALREADY_INIT_EXCEPTION), new Object[] { "addServlet", getName() });
        throw new IllegalStateException(msg);
    }
    if (servletName == null || servletName.length() == 0) {
        throw new IllegalArgumentException(rb.getString(LogFacade.NULL_EMPTY_SERVLET_NAME_EXCEPTION));
    }
    if (servlet == null) {
        throw new NullPointerException(rb.getString(LogFacade.NULL_SERVLET_INSTANCE_EXCEPTION));
    }
    if (servlet instanceof SingleThreadModel) {
        throw new IllegalArgumentException("Servlet implements " + SingleThreadModel.class.getName());
    }
    /*
         * Make sure the given Servlet instance is unique across all deployed
         * contexts
         */
    Container host = getParent();
    if (host != null) {
        for (Container child : host.findChildren()) {
            if (child == this) {
                // Our own context will be checked further down
                continue;
            }
            if (((StandardContext) child).hasServlet(servlet)) {
                return null;
            }
        }
    }
    /*
         * Make sure the given Servlet name and instance are unique within
         * this context
         */
    synchronized (children) {
        for (Map.Entry<String, Container> e : children.entrySet()) {
            if (servletName.equals(e.getKey()) || servlet == ((StandardWrapper) e.getValue()).getServlet()) {
                return null;
            }
        }
        DynamicServletRegistrationImpl regis = (DynamicServletRegistrationImpl) servletRegisMap.get(servletName);
        StandardWrapper wrapper = null;
        if (regis == null) {
            wrapper = (StandardWrapper) createWrapper();
        } else {
            // Complete preliminary servlet registration
            wrapper = regis.getWrapper();
        }
        wrapper.setName(servletName);
        wrapper.setServlet(servlet);
        if (initParams != null) {
            for (Map.Entry<String, String> e : initParams.entrySet()) {
                wrapper.addInitParameter(e.getKey(), e.getValue());
            }
        }
        addChild(wrapper, true, (null == regis));
        if (null == regis) {
            regis = (DynamicServletRegistrationImpl) servletRegisMap.get(servletName);
        }
        if (urlPatterns != null) {
            for (String urlPattern : urlPatterns) {
                addServletMapping(urlPattern, servletName, false);
            }
        }
        return regis;
    }
}
Also used : Container(org.apache.catalina.Container) SingleThreadModel(javax.servlet.SingleThreadModel) FilterMap(org.apache.catalina.deploy.FilterMap) ServletMap(org.apache.catalina.deploy.ServletMap) Map(java.util.Map) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 77 with Container

use of org.apache.catalina.Container in project Payara by payara.

the class StandardContext method loadOnStartup.

/**
 * Load and initialize all servlets marked "load on startup" in the
 * web application deployment descriptor.
 *
 * @param children Array of wrappers for all currently defined
 *  servlets (including those not declared load on startup)
 */
/* SJSAS 6377790
    public void loadOnStartup(Container children[]){
    */
// START SJSAS 6377790
public void loadOnStartup(Container[] children) throws LifecycleException {
    // END SJSAS 6377790
    // Collect "load on startup" servlets that need to be initialized
    Map<Integer, List<Wrapper>> map = new TreeMap<Integer, List<Wrapper>>();
    for (Container aChildren : children) {
        Wrapper wrapper = (Wrapper) aChildren;
        int loadOnStartup = wrapper.getLoadOnStartup();
        if (loadOnStartup < 0) {
            continue;
        }
        Integer key = loadOnStartup;
        List<Wrapper> list = map.get(key);
        if (list == null) {
            list = new ArrayList<Wrapper>();
            map.put(key, list);
        }
        list.add(wrapper);
    }
    // Load the collected "load on startup" servlets
    for (List<Wrapper> list : map.values()) {
        for (Wrapper wrapper : list) {
            try {
                wrapper.load();
            } catch (ServletException e) {
                String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_LOAD_EXCEPTION), getName());
                getServletContext().log(msg, StandardWrapper.getRootCause(e));
                // START SJSAS 6377790
                throw new LifecycleException(StandardWrapper.getRootCause(e));
            // END SJSAS 6377790
            }
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServletException(javax.servlet.ServletException) Wrapper(org.apache.catalina.Wrapper) Container(org.apache.catalina.Container) LifecycleException(org.apache.catalina.LifecycleException) ArrayList(java.util.ArrayList) List(java.util.List) TreeMap(java.util.TreeMap) SecurityConstraint(org.apache.catalina.deploy.SecurityConstraint)

Example 78 with Container

use of org.apache.catalina.Container in project Payara by payara.

the class ListRestEndpointsCommand method getSpecifiedJerseyApplications.

/**
 * Gets a map of Jersey container to the Jersey container name, from a given component name and list of web modules.
 * If the component name is null, then this will return all Jersey applications.
 * @param componentName the name of the Jersey component.
 * @param modules a list of web modules.
 * @return a map of Jersey containers to their names.
 */
private Map<ServletContainer, String> getSpecifiedJerseyApplications(String componentName, List<WebModule> modules) {
    Map<ServletContainer, String> jerseyApplicationMap = new HashMap<>();
    for (WebModule webModule : modules) {
        // loop through all servlets in the given web module
        for (Container container : webModule.findChildren()) {
            // check that it is actually a servlet
            if (container instanceof StandardWrapper) {
                // cast to a servlet from generic container
                StandardWrapper servlet = (StandardWrapper) container;
                // if it is a jersey application servlet, and if a component name has been specified and this servlet matches
                if (servlet.getServletClass() == ServletContainer.class && (componentName == null ^ servlet.getName().equals(componentName))) {
                    Collection<String> mappings = servlet.getMappings();
                    String servletMapping = null;
                    if (mappings.size() > 0) {
                        // May be represented as "path/to/resource/*", which needs to be removed
                        servletMapping = mappings.toArray()[0].toString().replaceAll("/\\*", "");
                    }
                    jerseyApplicationMap.put((ServletContainer) servlet.getServlet(), servletMapping);
                }
            }
        }
    }
    return jerseyApplicationMap;
}
Also used : ServletContainer(org.glassfish.jersey.servlet.ServletContainer) WebContainer(com.sun.enterprise.web.WebContainer) Container(org.apache.catalina.Container) HashMap(java.util.HashMap) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) WebModule(com.sun.enterprise.web.WebModule) StandardWrapper(org.apache.catalina.core.StandardWrapper)

Example 79 with Container

use of org.apache.catalina.Container in project Payara by payara.

the class WebContainerImpl method init.

// --------------------------------------------------------- Private Methods
private void init() {
    if (initialized) {
        return;
    }
    if (config == null) {
        // use default settings
        config = new WebContainerConfig();
    }
    container = habitat.getServiceHandle(org.glassfish.api.container.Container.class, "com.sun.enterprise.web.WebContainer");
    if (container == null) {
        log.severe("Cannot find webcontainer implementation");
        return;
    }
    ActiveDescriptor<?> activeDescriptor = habitat.getBestDescriptor(BuilderHelper.createContractFilter("com.sun.enterprise.web.EmbeddedWebContainer"));
    if (activeDescriptor == null) {
        log.severe("Cannot find embedded implementation");
        return;
    }
    embeddedInhabitant = habitat.getServiceHandle(activeDescriptor);
    try {
        webContainer = (com.sun.enterprise.web.WebContainer) container.getService();
        embedded = (EmbeddedWebContainer) embeddedInhabitant.getService();
        if ((webContainer == null) || (embedded == null)) {
            log.severe("Cannot find webcontainer implementation");
            return;
        }
        engine = webContainer.getEngine();
        if (engine == null) {
            log.severe("Cannot find engine implementation");
            return;
        }
        initialized = true;
    } catch (Exception e) {
        log.severe("Init exception " + e.getMessage());
    }
}
Also used : EmbeddedWebContainer(com.sun.enterprise.web.EmbeddedWebContainer) WebContainer(org.glassfish.embeddable.web.WebContainer) Container(org.apache.catalina.Container) WebContainerConfig(org.glassfish.embeddable.web.config.WebContainerConfig) PropertyVetoException(java.beans.PropertyVetoException) ConfigException(org.glassfish.embeddable.web.ConfigException) GlassFishException(org.glassfish.embeddable.GlassFishException)

Example 80 with Container

use of org.apache.catalina.Container in project Payara by payara.

the class ReplicationStore method getUniqueId.

protected long getUniqueId() {
    long uniqueId = 0L;
    Container container = this.manager.getContainer();
    if (container instanceof StandardContext) {
        StandardContext ctx = (StandardContext) container;
        uniqueId = ctx.getUniqueId();
    }
    return uniqueId;
}
Also used : Container(org.apache.catalina.Container) StandardContext(org.apache.catalina.core.StandardContext)

Aggregations

Container (org.apache.catalina.Container)163 Context (org.apache.catalina.Context)28 IOException (java.io.IOException)24 Host (org.apache.catalina.Host)22 StandardContext (org.apache.catalina.core.StandardContext)21 Engine (org.apache.catalina.Engine)18 LifecycleException (org.apache.catalina.LifecycleException)17 File (java.io.File)15 ObjectName (javax.management.ObjectName)15 Wrapper (org.apache.catalina.Wrapper)13 StandardHost (org.apache.catalina.core.StandardHost)13 ArrayList (java.util.ArrayList)12 ServletException (javax.servlet.ServletException)11 MalformedURLException (java.net.MalformedURLException)10 Valve (org.apache.catalina.Valve)10 StandardWrapper (org.apache.catalina.core.StandardWrapper)10 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)10 NamingException (javax.naming.NamingException)9 Lifecycle (org.apache.catalina.Lifecycle)9 Realm (org.apache.catalina.Realm)9