Java Code Examples for io.undertow.servlet.api.InstanceHandle#release()

The following examples show how to use io.undertow.servlet.api.InstanceHandle#release() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ManagedServlet.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public InstanceHandle<? extends Servlet> getServlet() throws ServletException {
    final InstanceHandle<? extends Servlet> instanceHandle;
    final Servlet instance;
    //TODO: pooling
    try {
        instanceHandle = factory.createInstance();
    } catch (Exception e) {
        throw UndertowServletMessages.MESSAGES.couldNotInstantiateComponent(servletInfo.getName(), e);
    }
    instance = instanceHandle.getInstance();
    new LifecyleInterceptorInvocation(servletContext.getDeployment().getDeploymentInfo().getLifecycleInterceptors(), servletInfo, instance, new ServletConfigImpl(servletInfo, servletContext)).proceed();

    return new InstanceHandle<Servlet>() {
        @Override
        public Servlet getInstance() {
            return instance;
        }

        @Override
        public void release() {
            try {
                instance.destroy();
            } catch (Throwable t) {
                UndertowServletLogger.REQUEST_LOGGER.failedToDestroy(instance, t);
            }
            instanceHandle.release();
        }
    };

}
 
Example 2
Source File: ManagedServlet.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public InstanceHandle<? extends Servlet> getServlet() throws ServletException {
    final InstanceHandle<? extends Servlet> instanceHandle;
    final Servlet instance;
    //TODO: pooling
    try {
        instanceHandle = factory.createInstance();
    } catch (Exception e) {
        throw UndertowServletMessages.MESSAGES.couldNotInstantiateComponent(servletInfo.getName(), e);
    }
    instance = instanceHandle.getInstance();
    new LifecyleInterceptorInvocation(servletContext.getDeployment().getDeploymentInfo().getLifecycleInterceptors(), servletInfo, instance, new ServletConfigImpl(servletInfo, servletContext)).proceed();

    return new InstanceHandle<Servlet>() {
        @Override
        public Servlet getInstance() {
            return instance;
        }

        @Override
        public void release() {
            try {
                instance.destroy();
            } catch (Throwable t) {
                UndertowServletLogger.REQUEST_LOGGER.failedToDestroy(instance, t);
            }
            instanceHandle.release();
        }
    };

}
 
Example 3
Source File: ServletHandler.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
public void handleRequest(final HttpServerExchange exchange) throws IOException, ServletException {
    if (managedServlet.isPermanentlyUnavailable()) {
        UndertowServletLogger.REQUEST_LOGGER.debugf("Returning 404 for servlet %s due to permanent unavailability", managedServlet.getServletInfo().getName());
        exchange.setStatusCode(StatusCodes.NOT_FOUND);
        return;
    }

    if (managedServlet.isTemporarilyUnavailable()) {
        UndertowServletLogger.REQUEST_LOGGER.debugf("Returning 503 for servlet %s due to temporary unavailability", managedServlet.getServletInfo().getName());
        exchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE);
        return;
    }
    final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    if(!managedServlet.getServletInfo().isAsyncSupported()) {
        servletRequestContext.setAsyncSupported(false);
    }
    ServletRequest request = servletRequestContext.getServletRequest();
    ServletResponse response = servletRequestContext.getServletResponse();
    InstanceHandle<? extends Servlet> servlet = null;
    try {
        servlet = managedServlet.getServlet();
        servlet.getInstance().service(request, response);

        //according to the spec we have to call AsyncContext.complete() at this point
        //straight after the service method
        //not super sure about this, surely it would make more sense to do this when the request has returned to the container, however the spec is quite clear wording wise
        //todo: should we actually enable this? Apparently other containers do not do it
        //if(!request.isAsyncStarted()) {
        //    AsyncContextImpl existingAsyncContext = servletRequestContext.getOriginalRequest().getAsyncContextInternal();
        //    if (existingAsyncContext != null) {
        //        existingAsyncContext.complete();
        //    }
        //}
    } catch (UnavailableException e) {
        managedServlet.handleUnavailableException(e);
        if (e.isPermanent()) {
            exchange.setStatusCode(StatusCodes.NOT_FOUND);
        } else {
            exchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE);
        }
    } finally {
        if(servlet != null) {
            servlet.release();
        }
    }
}
 
Example 4
Source File: ServletHandler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void handleRequest(final HttpServerExchange exchange) throws IOException, ServletException {
    if (managedServlet.isPermanentlyUnavailable()) {
        UndertowServletLogger.REQUEST_LOGGER.debugf("Returning 404 for servlet %s due to permanent unavailability", managedServlet.getServletInfo().getName());
        exchange.setStatusCode(StatusCodes.NOT_FOUND);
        return;
    }

    if (managedServlet.isTemporarilyUnavailable()) {
        UndertowServletLogger.REQUEST_LOGGER.debugf("Returning 503 for servlet %s due to temporary unavailability", managedServlet.getServletInfo().getName());
        exchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE);
        return;
    }
    final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    if(!managedServlet.getServletInfo().isAsyncSupported()) {
        servletRequestContext.setAsyncSupported(false);
    }
    ServletRequest request = servletRequestContext.getServletRequest();
    ServletResponse response = servletRequestContext.getServletResponse();
    InstanceHandle<? extends Servlet> servlet = null;
    try {
        servlet = managedServlet.getServlet();
        servlet.getInstance().service(request, response);

        //according to the spec we have to call AsyncContext.complete() at this point
        //straight after the service method
        //not super sure about this, surely it would make more sense to do this when the request has returned to the container, however the spec is quite clear wording wise
        //todo: should we actually enable this? Apparently other containers do not do it
        //if(!request.isAsyncStarted()) {
        //    AsyncContextImpl existingAsyncContext = servletRequestContext.getOriginalRequest().getAsyncContextInternal();
        //    if (existingAsyncContext != null) {
        //        existingAsyncContext.complete();
        //    }
        //}
    } catch (UnavailableException e) {
        managedServlet.handleUnavailableException(e);
        if (e.isPermanent()) {
            exchange.setStatusCode(StatusCodes.NOT_FOUND);
        } else {
            exchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE);
        }
    } finally {
        if(servlet != null) {
            servlet.release();
        }
    }
}