Search in sources :

Example 1 with ThreadClassLoaderScope

use of org.eclipse.jetty.util.thread.ThreadClassLoaderScope in project jetty.project by eclipse.

the class WebSocketCdiInitializer method onStartup.

@Override
public void onStartup(Set<Class<?>> c, ServletContext context) throws ServletException {
    ContextHandler handler = ContextHandler.getContextHandler(context);
    if (handler == null) {
        throw new ServletException("Not running on Jetty, WebSocket+CDI support unavailable");
    }
    if (!(handler instanceof ServletContextHandler)) {
        throw new ServletException("Not running in Jetty ServletContextHandler, WebSocket+CDI support unavailable");
    }
    ServletContextHandler jettyContext = (ServletContextHandler) handler;
    try (ThreadClassLoaderScope scope = new ThreadClassLoaderScope(context.getClassLoader())) {
        addListeners(jettyContext);
    }
}
Also used : ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ServletException(javax.servlet.ServletException) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ThreadClassLoaderScope(org.eclipse.jetty.util.thread.ThreadClassLoaderScope)

Example 2 with ThreadClassLoaderScope

use of org.eclipse.jetty.util.thread.ThreadClassLoaderScope in project ligoj-api by ligoj.

the class PluginsClassLoaderTest method getInstance.

@Test
public void getInstance() {
    ThreadClassLoaderScope scope = null;
    try {
        scope = new ThreadClassLoaderScope(new URLClassLoader(new URL[0], Mockito.mock(PluginsClassLoader.class)));
        Assertions.assertNotNull(PluginsClassLoader.getInstance());
    } finally {
        IOUtils.closeQuietly(scope);
    }
}
Also used : URLClassLoader(java.net.URLClassLoader) ThreadClassLoaderScope(org.eclipse.jetty.util.thread.ThreadClassLoaderScope) Test(org.junit.jupiter.api.Test)

Example 3 with ThreadClassLoaderScope

use of org.eclipse.jetty.util.thread.ThreadClassLoaderScope in project jetty.project by eclipse.

the class WebSocketSession method open.

/**
     * Open/Activate the session
     */
public void open() {
    if (LOG_OPEN.isDebugEnabled())
        LOG_OPEN.debug("[{}] {}.open()", policy.getBehavior(), this.getClass().getSimpleName());
    if (remote != null) {
        // already opened
        return;
    }
    try (ThreadClassLoaderScope scope = new ThreadClassLoaderScope(classLoader)) {
        // Upgrade success
        connection.getIOState().onConnected();
        // Connect remote
        remote = remoteEndpointFactory.newRemoteEndpoint(connection, outgoingHandler, getBatchMode());
        if (LOG_OPEN.isDebugEnabled())
            LOG_OPEN.debug("[{}] {}.open() remote={}", policy.getBehavior(), this.getClass().getSimpleName(), remote);
        // Open WebSocket
        websocket.openSession(this);
        // Open connection
        connection.getIOState().onOpened();
        if (LOG.isDebugEnabled()) {
            LOG.debug("open -> {}", dump());
        }
        if (openFuture != null) {
            openFuture.complete(this);
        }
    } catch (CloseException ce) {
        LOG.warn(ce);
        close(ce.getStatusCode(), ce.getMessage());
    } catch (Throwable t) {
        LOG.warn(t);
        // Exception on end-user WS-Endpoint.
        // Fast-fail & close connection with reason.
        int statusCode = StatusCode.SERVER_ERROR;
        if (policy.getBehavior() == WebSocketBehavior.CLIENT) {
            statusCode = StatusCode.POLICY_VIOLATION;
        }
        close(statusCode, t.getMessage());
    }
}
Also used : CloseException(org.eclipse.jetty.websocket.api.CloseException) ThreadClassLoaderScope(org.eclipse.jetty.util.thread.ThreadClassLoaderScope)

Example 4 with ThreadClassLoaderScope

use of org.eclipse.jetty.util.thread.ThreadClassLoaderScope in project plugin-prov by ligoj.

the class TerraformResourceTest method toFile.

@Test
public void toFile() throws IOException {
    ThreadClassLoaderScope scope = null;
    try {
        final PluginsClassLoader classLoader = Mockito.mock(PluginsClassLoader.class);
        scope = new ThreadClassLoaderScope(new URLClassLoader(new URL[0], classLoader));
        final File file = new File("");
        final Subscription entity = new Subscription();
        entity.setId(15);
        Mockito.when(classLoader.toFile(entity, "15", "some")).thenReturn(file);
        Assertions.assertSame(file, resource.toFile(entity, "some"));
        Assertions.assertNotNull(PluginsClassLoader.getInstance());
    } finally {
        IOUtils.closeQuietly(scope);
    }
}
Also used : URLClassLoader(java.net.URLClassLoader) ThreadClassLoaderScope(org.eclipse.jetty.util.thread.ThreadClassLoaderScope) Subscription(org.ligoj.app.model.Subscription) PluginsClassLoader(org.ligoj.app.resource.plugin.PluginsClassLoader) File(java.io.File) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Aggregations

ThreadClassLoaderScope (org.eclipse.jetty.util.thread.ThreadClassLoaderScope)4 URLClassLoader (java.net.URLClassLoader)2 Test (org.junit.jupiter.api.Test)2 File (java.io.File)1 ServletException (javax.servlet.ServletException)1 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)1 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)1 CloseException (org.eclipse.jetty.websocket.api.CloseException)1 AbstractAppTest (org.ligoj.app.AbstractAppTest)1 Subscription (org.ligoj.app.model.Subscription)1 PluginsClassLoader (org.ligoj.app.resource.plugin.PluginsClassLoader)1