Search in sources :

Example 1 with ContainerServlet

use of org.apache.catalina.ContainerServlet in project tomcat by apache.

the class StandardWrapper method loadServlet.

/**
 * Load and initialize an instance of this servlet, if there is not already
 * an initialized instance.  This can be used, for example, to load servlets
 * that are marked in the deployment descriptor to be loaded at server
 * startup time.
 *
 * @return the loaded Servlet instance
 * @throws ServletException for a Servlet load error
 */
public synchronized Servlet loadServlet() throws ServletException {
    // Nothing to do if we already have an instance or an instance pool
    if (instance != null) {
        return instance;
    }
    PrintStream out = System.out;
    if (swallowOutput) {
        SystemLogHandler.startCapture();
    }
    Servlet servlet;
    try {
        long t1 = System.currentTimeMillis();
        // Complain if no servlet class has been specified
        if (servletClass == null) {
            unavailable(null);
            throw new ServletException(sm.getString("standardWrapper.notClass", getName()));
        }
        InstanceManager instanceManager = ((StandardContext) getParent()).getInstanceManager();
        try {
            servlet = (Servlet) instanceManager.newInstance(servletClass);
        } catch (ClassCastException e) {
            unavailable(null);
            // Restore the context ClassLoader
            throw new ServletException(sm.getString("standardWrapper.notServlet", servletClass), e);
        } catch (Throwable e) {
            e = ExceptionUtils.unwrapInvocationTargetException(e);
            ExceptionUtils.handleThrowable(e);
            unavailable(null);
            // https://bz.apache.org/bugzilla/show_bug.cgi?id=36630
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("standardWrapper.instantiate", servletClass), e);
            }
            // Restore the context ClassLoader
            throw new ServletException(sm.getString("standardWrapper.instantiate", servletClass), e);
        }
        if (multipartConfigElement == null) {
            MultipartConfig annotation = servlet.getClass().getAnnotation(MultipartConfig.class);
            if (annotation != null) {
                multipartConfigElement = new MultipartConfigElement(annotation);
            }
        }
        // to load ContainerServlets
        if (servlet instanceof ContainerServlet) {
            ((ContainerServlet) servlet).setWrapper(this);
        }
        classLoadTime = (int) (System.currentTimeMillis() - t1);
        initServlet(servlet);
        fireContainerEvent("load", this);
        loadTime = System.currentTimeMillis() - t1;
    } finally {
        if (swallowOutput) {
            String log = SystemLogHandler.stopCapture();
            if (log != null && log.length() > 0) {
                if (getServletContext() != null) {
                    getServletContext().log(log);
                } else {
                    out.println(log);
                }
            }
        }
    }
    return servlet;
}
Also used : ServletException(jakarta.servlet.ServletException) PrintStream(java.io.PrintStream) MultipartConfigElement(jakarta.servlet.MultipartConfigElement) MultipartConfig(jakarta.servlet.annotation.MultipartConfig) InstanceManager(org.apache.tomcat.InstanceManager) Servlet(jakarta.servlet.Servlet) ContainerServlet(org.apache.catalina.ContainerServlet) ContainerServlet(org.apache.catalina.ContainerServlet)

Example 2 with ContainerServlet

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

the class StandardWrapper method loadServlet.

/**
 * Creates an instance of the servlet, if there is not already
 * at least one initialized instance.
 */
private synchronized Servlet loadServlet() throws ServletException {
    // Nothing to do if we already have an instance or an instance pool
    if (!singleThreadModel && instance != null) {
        return instance;
    }
    long t1 = System.currentTimeMillis();
    loadServletClass();
    // Instantiate the servlet class
    Servlet servlet = null;
    try {
        servlet = ((StandardContext) getParent()).createServletInstance(servletClass);
    } catch (ClassCastException e) {
        unavailable(null);
        // Restore the context ClassLoader
        throw new ServletException(createMsg(CLASS_IS_NOT_SERVLET_EXCEPTION, servletClass.getName()), e);
    } catch (Throwable e) {
        unavailable(null);
        // Restore the context ClassLoader
        throw new ServletException(createMsg(ERROR_INSTANTIATE_SERVLET_CLASS_EXCEPTION, servletClass.getName()), e);
    }
    // Check if loading the servlet in this web application should be allowed
    if (!isServletAllowed(servlet)) {
        throw new SecurityException(createMsg(PRIVILEGED_SERVLET_CANNOT_BE_LOADED_EXCEPTION, servletClass.getName()));
    }
    // Special handling for ContainerServlet instances
    if ((servlet instanceof ContainerServlet) && (isContainerProvidedServlet(servletClass.getName()) || ((Context) getParent()).getPrivileged())) {
        ((ContainerServlet) servlet).setWrapper(this);
    }
    classLoadTime = (int) (System.currentTimeMillis() - t1);
    // Register our newly initialized instance
    singleThreadModel = servlet instanceof SingleThreadModel;
    if (singleThreadModel) {
        if (instancePool == null)
            instancePool = new Stack<Servlet>();
    }
    if (notifyContainerListeners) {
        fireContainerEvent("load", this);
    }
    loadTime = System.currentTimeMillis() - t1;
    return servlet;
}
Also used : ServletException(javax.servlet.ServletException) HttpServlet(javax.servlet.http.HttpServlet) Servlet(javax.servlet.Servlet) ContainerServlet(org.apache.catalina.ContainerServlet) ContainerServlet(org.apache.catalina.ContainerServlet) SingleThreadModel(javax.servlet.SingleThreadModel) Stack(java.util.Stack)

Example 3 with ContainerServlet

use of org.apache.catalina.ContainerServlet in project tomcat70 by apache.

the class StandardWrapper method loadServlet.

/**
 * Load and initialize an instance of this servlet, if there is not already
 * at least one initialized instance.  This can be used, for example, to
 * load servlets that are marked in the deployment descriptor to be loaded
 * at server startup time.
 */
public synchronized Servlet loadServlet() throws ServletException {
    if (unloading) {
        throw new ServletException(sm.getString("standardWrapper.unloading", getName()));
    }
    // Nothing to do if we already have an instance or an instance pool
    if (!singleThreadModel && (instance != null))
        return instance;
    PrintStream out = System.out;
    if (swallowOutput) {
        SystemLogHandler.startCapture();
    }
    Servlet servlet;
    try {
        long t1 = System.currentTimeMillis();
        // Complain if no servlet class has been specified
        if (servletClass == null) {
            unavailable(null);
            throw new ServletException(sm.getString("standardWrapper.notClass", getName()));
        }
        InstanceManager instanceManager = ((StandardContext) getParent()).getInstanceManager();
        try {
            servlet = (Servlet) instanceManager.newInstance(servletClass);
        } catch (ClassCastException e) {
            unavailable(null);
            // Restore the context ClassLoader
            throw new ServletException(sm.getString("standardWrapper.notServlet", servletClass), e);
        } catch (Throwable e) {
            e = ExceptionUtils.unwrapInvocationTargetException(e);
            ExceptionUtils.handleThrowable(e);
            unavailable(null);
            // http://bz.apache.org/bugzilla/show_bug.cgi?id=36630
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("standardWrapper.instantiate", servletClass), e);
            }
            // Restore the context ClassLoader
            throw new ServletException(sm.getString("standardWrapper.instantiate", servletClass), e);
        }
        if (multipartConfigElement == null) {
            MultipartConfig annotation = servlet.getClass().getAnnotation(MultipartConfig.class);
            if (annotation != null) {
                multipartConfigElement = new MultipartConfigElement(annotation);
            }
        }
        // Special handling for ContainerServlet instances
        if ((servlet instanceof ContainerServlet) && (isContainerProvidedServlet(servletClass) || ((Context) getParent()).getPrivileged())) {
            ((ContainerServlet) servlet).setWrapper(this);
        }
        classLoadTime = (int) (System.currentTimeMillis() - t1);
        if (servlet instanceof SingleThreadModel) {
            if (instancePool == null) {
                instancePool = new Stack<Servlet>();
            }
            singleThreadModel = true;
        }
        initServlet(servlet);
        fireContainerEvent("load", this);
        loadTime = System.currentTimeMillis() - t1;
    } finally {
        if (swallowOutput) {
            String log = SystemLogHandler.stopCapture();
            if (log != null && log.length() > 0) {
                if (getServletContext() != null) {
                    getServletContext().log(log);
                } else {
                    out.println(log);
                }
            }
        }
    }
    return servlet;
}
Also used : PrintStream(java.io.PrintStream) InstanceManager(org.apache.tomcat.InstanceManager) ContainerServlet(org.apache.catalina.ContainerServlet) ServletException(javax.servlet.ServletException) MultipartConfigElement(javax.servlet.MultipartConfigElement) MultipartConfig(javax.servlet.annotation.MultipartConfig) Servlet(javax.servlet.Servlet) ContainerServlet(org.apache.catalina.ContainerServlet) SingleThreadModel(javax.servlet.SingleThreadModel)

Aggregations

ContainerServlet (org.apache.catalina.ContainerServlet)3 PrintStream (java.io.PrintStream)2 Servlet (javax.servlet.Servlet)2 ServletException (javax.servlet.ServletException)2 SingleThreadModel (javax.servlet.SingleThreadModel)2 InstanceManager (org.apache.tomcat.InstanceManager)2 MultipartConfigElement (jakarta.servlet.MultipartConfigElement)1 Servlet (jakarta.servlet.Servlet)1 ServletException (jakarta.servlet.ServletException)1 MultipartConfig (jakarta.servlet.annotation.MultipartConfig)1 Stack (java.util.Stack)1 MultipartConfigElement (javax.servlet.MultipartConfigElement)1 MultipartConfig (javax.servlet.annotation.MultipartConfig)1 HttpServlet (javax.servlet.http.HttpServlet)1