Search in sources :

Example 11 with InstanceManager

use of org.apache.tomcat.InstanceManager in project tomcat by apache.

the class WsSession method fireEndpointOnClose.

private void fireEndpointOnClose(CloseReason closeReason) {
    // Fire the onClose event
    Throwable throwable = null;
    InstanceManager instanceManager = getInstanceManager();
    Thread t = Thread.currentThread();
    ClassLoader cl = t.getContextClassLoader();
    t.setContextClassLoader(applicationClassLoader);
    try {
        localEndpoint.onClose(this, closeReason);
    } catch (Throwable t1) {
        ExceptionUtils.handleThrowable(t1);
        throwable = t1;
    } finally {
        if (instanceManager != null) {
            try {
                instanceManager.destroyInstance(localEndpoint);
            } catch (Throwable t2) {
                ExceptionUtils.handleThrowable(t2);
                if (throwable == null) {
                    throwable = t2;
                }
            }
        }
        t.setContextClassLoader(cl);
    }
    if (throwable != null) {
        fireEndpointOnError(throwable);
    }
}
Also used : InstanceManager(org.apache.tomcat.InstanceManager)

Example 12 with InstanceManager

use of org.apache.tomcat.InstanceManager in project tomcat by apache.

the class WsRemoteEndpointImplBase method setEncoders.

protected void setEncoders(EndpointConfig endpointConfig) throws DeploymentException {
    encoderEntries.clear();
    for (Class<? extends Encoder> encoderClazz : endpointConfig.getEncoders()) {
        Encoder instance;
        InstanceManager instanceManager = wsSession.getInstanceManager();
        try {
            if (instanceManager == null) {
                instance = encoderClazz.getConstructor().newInstance();
            } else {
                instance = (Encoder) instanceManager.newInstance(encoderClazz);
            }
            instance.init(endpointConfig);
        } catch (ReflectiveOperationException | NamingException e) {
            throw new DeploymentException(sm.getString("wsRemoteEndpoint.invalidEncoder", encoderClazz.getName()), e);
        }
        EncoderEntry entry = new EncoderEntry(Util.getEncoderType(encoderClazz), instance);
        encoderEntries.add(entry);
    }
}
Also used : Utf8Encoder(org.apache.tomcat.util.buf.Utf8Encoder) Encoder(jakarta.websocket.Encoder) CharsetEncoder(java.nio.charset.CharsetEncoder) InstanceManager(org.apache.tomcat.InstanceManager) NamingException(javax.naming.NamingException) DeploymentException(jakarta.websocket.DeploymentException)

Example 13 with InstanceManager

use of org.apache.tomcat.InstanceManager in project tomcat by apache.

the class JspServletWrapper method getServlet.

public Servlet getServlet() throws ServletException {
    /*
         * DCL on 'reload' requires that 'reload' be volatile
         * (this also forces a read memory barrier, ensuring the new servlet
         * object is read consistently).
         *
         * When running in non development mode with a checkInterval it is
         * possible (see BZ 62603) for a race condition to cause failures
         * if a Servlet or tag is reloaded while a compile check is running
         */
    if (getReloadInternal() || theServlet == null) {
        synchronized (this) {
            // of different pages, but not the same page.
            if (getReloadInternal() || theServlet == null) {
                // This is to maintain the original protocol.
                destroy();
                final Servlet servlet;
                try {
                    InstanceManager instanceManager = InstanceManagerFactory.getInstanceManager(config);
                    servlet = (Servlet) instanceManager.newInstance(ctxt.getFQCN(), ctxt.getJspLoader());
                } catch (Exception e) {
                    Throwable t = ExceptionUtils.unwrapInvocationTargetException(e);
                    ExceptionUtils.handleThrowable(t);
                    throw new JasperException(t);
                }
                servlet.init(config);
                if (theServlet != null) {
                    ctxt.getRuntimeContext().incrementJspReloadCount();
                }
                theServlet = servlet;
                reload = false;
            // Volatile 'reload' forces in order write of 'theServlet' and new servlet object
            }
        }
    }
    return theServlet;
}
Also used : JasperException(org.apache.jasper.JasperException) InstanceManager(org.apache.tomcat.InstanceManager) Servlet(jakarta.servlet.Servlet) UnavailableException(jakarta.servlet.UnavailableException) ServletException(jakarta.servlet.ServletException) JasperException(org.apache.jasper.JasperException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 14 with InstanceManager

use of org.apache.tomcat.InstanceManager in project tomcat by apache.

the class TestDefaultInstanceManager method testConcurrency.

/*
     * Performance test. Comment out @Ignore to run the test.
     */
@Ignore
@Test
public void testConcurrency() throws Exception {
    // Create a populated InstanceManager
    Tomcat tomcat = getTomcatInstance();
    Context ctx = tomcat.addContext(null, "", null);
    tomcat.start();
    InstanceManager im = ctx.getInstanceManager();
    for (int i = 1; i < 9; i++) {
        doTestConcurrency(im, i);
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) InstanceManager(org.apache.tomcat.InstanceManager) Ignore(org.junit.Ignore) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 15 with InstanceManager

use of org.apache.tomcat.InstanceManager in project tomcat by apache.

the class Request method upgrade.

@SuppressWarnings("unchecked")
@Override
public <T extends HttpUpgradeHandler> T upgrade(Class<T> httpUpgradeHandlerClass) throws java.io.IOException, ServletException {
    T handler;
    InstanceManager instanceManager = null;
    try {
        // need injection
        if (InternalHttpUpgradeHandler.class.isAssignableFrom(httpUpgradeHandlerClass)) {
            handler = httpUpgradeHandlerClass.getConstructor().newInstance();
        } else {
            instanceManager = getContext().getInstanceManager();
            handler = (T) instanceManager.newInstance(httpUpgradeHandlerClass);
        }
    } catch (ReflectiveOperationException | NamingException | IllegalArgumentException | SecurityException e) {
        throw new ServletException(e);
    }
    UpgradeToken upgradeToken = new UpgradeToken(handler, getContext(), instanceManager, getUpgradeProtocolName(httpUpgradeHandlerClass));
    coyoteRequest.action(ActionCode.UPGRADE, upgradeToken);
    // Output required by RFC2616. Protocol specific headers should have
    // already been set.
    response.setStatus(HttpServletResponse.SC_SWITCHING_PROTOCOLS);
    return handler;
}
Also used : ServletException(jakarta.servlet.ServletException) UpgradeToken(org.apache.coyote.UpgradeToken) InstanceManager(org.apache.tomcat.InstanceManager) NamingException(javax.naming.NamingException)

Aggregations

InstanceManager (org.apache.tomcat.InstanceManager)16 IOException (java.io.IOException)5 NamingException (javax.naming.NamingException)5 ServletException (jakarta.servlet.ServletException)4 ServletException (javax.servlet.ServletException)3 Context (org.apache.catalina.Context)3 LifecycleException (org.apache.catalina.LifecycleException)3 Servlet (jakarta.servlet.Servlet)2 FileNotFoundException (java.io.FileNotFoundException)2 PrintStream (java.io.PrintStream)2 MalformedURLException (java.net.MalformedURLException)2 ListenerNotFoundException (javax.management.ListenerNotFoundException)2 Servlet (javax.servlet.Servlet)2 ContainerServlet (org.apache.catalina.ContainerServlet)2 JasperException (org.apache.jasper.JasperException)2 MultipartConfigElement (jakarta.servlet.MultipartConfigElement)1 UnavailableException (jakarta.servlet.UnavailableException)1 MultipartConfig (jakarta.servlet.annotation.MultipartConfig)1 DeploymentException (jakarta.websocket.DeploymentException)1 Encoder (jakarta.websocket.Encoder)1