Search in sources :

Example 1 with ServletHandler

use of io.undertow.servlet.handlers.ServletHandler in project undertow by undertow-io.

the class ServletContextImpl method addServlet.

@Override
public ServletRegistration.Dynamic addServlet(final String servletName, final String className) {
    ensureNotProgramaticListener();
    ensureNotInitialized();
    try {
        if (deploymentInfo.getServlets().containsKey(servletName)) {
            return null;
        }
        Class<? extends Servlet> servletClass = (Class<? extends Servlet>) deploymentInfo.getClassLoader().loadClass(className);
        ServletInfo servlet = new ServletInfo(servletName, servletClass, deploymentInfo.getClassIntrospecter().createInstanceFactory(servletClass));
        readServletAnnotations(servlet);
        deploymentInfo.addServlet(servlet);
        ServletHandler handler = deployment.getServlets().addServlet(servlet);
        return new ServletRegistrationImpl(servlet, handler.getManagedServlet(), deployment);
    } catch (ClassNotFoundException e) {
        throw UndertowServletMessages.MESSAGES.cannotLoadClass(className, e);
    } catch (NoSuchMethodException e) {
        throw UndertowServletMessages.MESSAGES.couldNotCreateFactory(className, e);
    }
}
Also used : ServletInfo(io.undertow.servlet.api.ServletInfo) ServletHandler(io.undertow.servlet.handlers.ServletHandler) Servlet(javax.servlet.Servlet) ManagedServlet(io.undertow.servlet.core.ManagedServlet)

Example 2 with ServletHandler

use of io.undertow.servlet.handlers.ServletHandler in project undertow by undertow-io.

the class ManagedServlets method addServlet.

public ServletHandler addServlet(final ServletInfo servletInfo) {
    ManagedServlet managedServlet = new ManagedServlet(servletInfo, deployment.getServletContext());
    ServletHandler servletHandler = new ServletHandler(managedServlet);
    managedServletMap.put(servletInfo.getName(), servletHandler);
    deployment.addLifecycleObjects(managedServlet);
    this.servletPaths.invalidate();
    return servletHandler;
}
Also used : ServletHandler(io.undertow.servlet.handlers.ServletHandler)

Example 3 with ServletHandler

use of io.undertow.servlet.handlers.ServletHandler in project undertow by undertow-io.

the class DeploymentManagerImpl method start.

@Override
public HttpHandler start() throws ServletException {
    try {
        return deployment.createThreadSetupAction(new ThreadSetupHandler.Action<HttpHandler, Object>() {

            @Override
            public HttpHandler call(HttpServerExchange exchange, Object ignore) throws ServletException {
                deployment.getSessionManager().start();
                //we need to copy before iterating
                //because listeners can add other listeners
                ArrayList<Lifecycle> lifecycles = new ArrayList<>(deployment.getLifecycleObjects());
                for (Lifecycle object : lifecycles) {
                    object.start();
                }
                HttpHandler root = deployment.getHandler();
                final TreeMap<Integer, List<ManagedServlet>> loadOnStartup = new TreeMap<>();
                for (Map.Entry<String, ServletHandler> entry : deployment.getServlets().getServletHandlers().entrySet()) {
                    ManagedServlet servlet = entry.getValue().getManagedServlet();
                    Integer loadOnStartupNumber = servlet.getServletInfo().getLoadOnStartup();
                    if (loadOnStartupNumber != null) {
                        if (loadOnStartupNumber < 0) {
                            continue;
                        }
                        List<ManagedServlet> list = loadOnStartup.get(loadOnStartupNumber);
                        if (list == null) {
                            loadOnStartup.put(loadOnStartupNumber, list = new ArrayList<>());
                        }
                        list.add(servlet);
                    }
                }
                for (Map.Entry<Integer, List<ManagedServlet>> load : loadOnStartup.entrySet()) {
                    for (ManagedServlet servlet : load.getValue()) {
                        servlet.createServlet();
                    }
                }
                if (deployment.getDeploymentInfo().isEagerFilterInit()) {
                    for (ManagedFilter filter : deployment.getFilters().getFilters().values()) {
                        filter.createFilter();
                    }
                }
                state = State.STARTED;
                return root;
            }
        }).call(null, null);
    } catch (ServletException | RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : HttpHandler(io.undertow.server.HttpHandler) ServletHandler(io.undertow.servlet.handlers.ServletHandler) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) ServletException(javax.servlet.ServletException) HttpServerExchange(io.undertow.server.HttpServerExchange) ServletException(javax.servlet.ServletException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 4 with ServletHandler

use of io.undertow.servlet.handlers.ServletHandler in project undertow by undertow-io.

the class ServletContextImpl method addServlet.

@Override
public ServletRegistration.Dynamic addServlet(final String servletName, final Servlet servlet) {
    ensureNotProgramaticListener();
    ensureNotInitialized();
    if (deploymentInfo.getServlets().containsKey(servletName)) {
        return null;
    }
    ServletInfo s = new ServletInfo(servletName, servlet.getClass(), new ImmediateInstanceFactory<>(servlet));
    readServletAnnotations(s);
    deploymentInfo.addServlet(s);
    ServletHandler handler = deployment.getServlets().addServlet(s);
    return new ServletRegistrationImpl(s, handler.getManagedServlet(), deployment);
}
Also used : ServletInfo(io.undertow.servlet.api.ServletInfo) ServletHandler(io.undertow.servlet.handlers.ServletHandler)

Example 5 with ServletHandler

use of io.undertow.servlet.handlers.ServletHandler in project undertow by undertow-io.

the class ServletContextImpl method addServlet.

@Override
public ServletRegistration.Dynamic addServlet(final String servletName, final Class<? extends Servlet> servletClass) {
    ensureNotProgramaticListener();
    ensureNotInitialized();
    if (deploymentInfo.getServlets().containsKey(servletName)) {
        return null;
    }
    try {
        ServletInfo servlet = new ServletInfo(servletName, servletClass, deploymentInfo.getClassIntrospecter().createInstanceFactory(servletClass));
        readServletAnnotations(servlet);
        deploymentInfo.addServlet(servlet);
        ServletHandler handler = deployment.getServlets().addServlet(servlet);
        return new ServletRegistrationImpl(servlet, handler.getManagedServlet(), deployment);
    } catch (NoSuchMethodException e) {
        throw UndertowServletMessages.MESSAGES.couldNotCreateFactory(servletClass.getName(), e);
    }
}
Also used : ServletInfo(io.undertow.servlet.api.ServletInfo) ServletHandler(io.undertow.servlet.handlers.ServletHandler)

Aggregations

ServletHandler (io.undertow.servlet.handlers.ServletHandler)5 ServletInfo (io.undertow.servlet.api.ServletInfo)3 HttpHandler (io.undertow.server.HttpHandler)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 ManagedServlet (io.undertow.servlet.core.ManagedServlet)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Servlet (javax.servlet.Servlet)1 ServletException (javax.servlet.ServletException)1