Search in sources :

Example 1 with ServerApplicationConfig

use of javax.websocket.server.ServerApplicationConfig in project wildfly by wildfly.

the class UndertowJSRWebSocketDeploymentProcessor method deploy.

@Override
public void deploy(final DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final Module module = deploymentUnit.getAttachment(Attachments.MODULE);
    if (module == null) {
        return;
    }
    ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(module.getClassLoader());
        WarMetaData metaData = deploymentUnit.getAttachment(WarMetaData.ATTACHMENT_KEY);
        if (metaData == null || metaData.getMergedJBossWebMetaData() == null) {
            return;
        }
        if (!metaData.getMergedJBossWebMetaData().isEnableWebSockets()) {
            return;
        }
        final Set<Class<?>> annotatedEndpoints = new HashSet<>();
        final Set<Class<? extends Endpoint>> endpoints = new HashSet<>();
        final Set<Class<? extends ServerApplicationConfig>> config = new HashSet<>();
        final CompositeIndex index = deploymentUnit.getAttachment(Attachments.COMPOSITE_ANNOTATION_INDEX);
        final List<AnnotationInstance> serverEndpoints = index.getAnnotations(SERVER_ENDPOINT);
        if (serverEndpoints != null) {
            for (AnnotationInstance endpoint : serverEndpoints) {
                if (endpoint.target() instanceof ClassInfo) {
                    ClassInfo clazz = (ClassInfo) endpoint.target();
                    try {
                        Class<?> moduleClass = ClassLoadingUtils.loadClass(clazz.name().toString(), module);
                        if (!Modifier.isAbstract(moduleClass.getModifiers())) {
                            annotatedEndpoints.add(moduleClass);
                        }
                    } catch (ClassNotFoundException e) {
                        UndertowLogger.ROOT_LOGGER.couldNotLoadWebSocketEndpoint(clazz.name().toString(), e);
                    }
                }
            }
        }
        final List<AnnotationInstance> clientEndpoints = index.getAnnotations(CLIENT_ENDPOINT);
        if (clientEndpoints != null) {
            for (AnnotationInstance endpoint : clientEndpoints) {
                if (endpoint.target() instanceof ClassInfo) {
                    ClassInfo clazz = (ClassInfo) endpoint.target();
                    try {
                        Class<?> moduleClass = ClassLoadingUtils.loadClass(clazz.name().toString(), module);
                        if (!Modifier.isAbstract(moduleClass.getModifiers())) {
                            annotatedEndpoints.add(moduleClass);
                        }
                    } catch (ClassNotFoundException e) {
                        UndertowLogger.ROOT_LOGGER.couldNotLoadWebSocketEndpoint(clazz.name().toString(), e);
                    }
                }
            }
        }
        final Set<ClassInfo> subclasses = index.getAllKnownImplementors(SERVER_APPLICATION_CONFIG);
        if (subclasses != null) {
            for (final ClassInfo clazz : subclasses) {
                try {
                    Class<?> moduleClass = ClassLoadingUtils.loadClass(clazz.name().toString(), module);
                    if (!Modifier.isAbstract(moduleClass.getModifiers())) {
                        config.add((Class) moduleClass);
                    }
                } catch (ClassNotFoundException e) {
                    UndertowLogger.ROOT_LOGGER.couldNotLoadWebSocketConfig(clazz.name().toString(), e);
                }
            }
        }
        final Set<ClassInfo> epClasses = index.getAllKnownSubclasses(ENDPOINT);
        if (epClasses != null) {
            for (final ClassInfo clazz : epClasses) {
                try {
                    Class<?> moduleClass = ClassLoadingUtils.loadClass(clazz.name().toString(), module);
                    if (!Modifier.isAbstract(moduleClass.getModifiers())) {
                        endpoints.add((Class) moduleClass);
                    }
                } catch (ClassNotFoundException e) {
                    UndertowLogger.ROOT_LOGGER.couldNotLoadWebSocketConfig(clazz.name().toString(), e);
                }
            }
        }
        WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo();
        doDeployment(deploymentUnit, webSocketDeploymentInfo, annotatedEndpoints, config, endpoints);
        installWebsockets(phaseContext, webSocketDeploymentInfo);
    } finally {
        Thread.currentThread().setContextClassLoader(oldCl);
    }
}
Also used : WarMetaData(org.jboss.as.web.common.WarMetaData) CompositeIndex(org.jboss.as.server.deployment.annotation.CompositeIndex) ServerApplicationConfig(javax.websocket.server.ServerApplicationConfig) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) Endpoint(javax.websocket.Endpoint) ServerEndpoint(javax.websocket.server.ServerEndpoint) ClientEndpoint(javax.websocket.ClientEndpoint) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) AnnotationInstance(org.jboss.jandex.AnnotationInstance) HashSet(java.util.HashSet) ClassInfo(org.jboss.jandex.ClassInfo)

Example 2 with ServerApplicationConfig

use of javax.websocket.server.ServerApplicationConfig in project jetty.project by eclipse.

the class WebSocketServerContainerInitializer method onStartup.

@Override
public void onStartup(Set<Class<?>> c, ServletContext context) throws ServletException {
    if (!isEnabledViaContext(context, ENABLE_KEY, true)) {
        LOG.info("JSR-356 is disabled by configuration");
        return;
    }
    ContextHandler handler = ContextHandler.getContextHandler(context);
    if (handler == null) {
        throw new ServletException("Not running on Jetty, JSR-356 support unavailable");
    }
    if (!(handler instanceof ServletContextHandler)) {
        throw new ServletException("Not running in Jetty ServletContextHandler, JSR-356 support unavailable");
    }
    ServletContextHandler jettyContext = (ServletContextHandler) handler;
    ClassLoader old = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(context.getClassLoader());
        // Create the Jetty ServerContainer implementation
        ServerContainer jettyContainer = configureContext(jettyContext);
        // make sure context is cleaned up when the context stops
        context.addListener(new ContextDestroyListener());
        if (c.isEmpty()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("No JSR-356 annotations or interfaces discovered");
            }
            return;
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Found {} classes", c.size());
        }
        // Now process the incoming classes
        Set<Class<? extends Endpoint>> discoveredExtendedEndpoints = new HashSet<>();
        Set<Class<?>> discoveredAnnotatedEndpoints = new HashSet<>();
        Set<Class<? extends ServerApplicationConfig>> serverAppConfigs = new HashSet<>();
        filterClasses(c, discoveredExtendedEndpoints, discoveredAnnotatedEndpoints, serverAppConfigs);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Discovered {} extends Endpoint classes", discoveredExtendedEndpoints.size());
            LOG.debug("Discovered {} @ServerEndpoint classes", discoveredAnnotatedEndpoints.size());
            LOG.debug("Discovered {} ServerApplicationConfig classes", serverAppConfigs.size());
        }
        // Process the server app configs to determine endpoint filtering
        boolean wasFiltered = false;
        Set<ServerEndpointConfig> deployableExtendedEndpointConfigs = new HashSet<>();
        Set<Class<?>> deployableAnnotatedEndpoints = new HashSet<>();
        for (Class<? extends ServerApplicationConfig> clazz : serverAppConfigs) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Found ServerApplicationConfig: {}", clazz);
            }
            try {
                ServerApplicationConfig config = clazz.newInstance();
                Set<ServerEndpointConfig> seconfigs = config.getEndpointConfigs(discoveredExtendedEndpoints);
                if (seconfigs != null) {
                    wasFiltered = true;
                    deployableExtendedEndpointConfigs.addAll(seconfigs);
                }
                Set<Class<?>> annotatedClasses = config.getAnnotatedEndpointClasses(discoveredAnnotatedEndpoints);
                if (annotatedClasses != null) {
                    wasFiltered = true;
                    deployableAnnotatedEndpoints.addAll(annotatedClasses);
                }
            } catch (InstantiationException | IllegalAccessException e) {
                throw new ServletException("Unable to instantiate: " + clazz.getName(), e);
            }
        }
        // Default behavior if nothing filtered
        if (!wasFiltered) {
            deployableAnnotatedEndpoints.addAll(discoveredAnnotatedEndpoints);
            // Note: it is impossible to determine path of "extends Endpoint" discovered classes
            deployableExtendedEndpointConfigs = new HashSet<>();
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Deploying {} ServerEndpointConfig(s)", deployableExtendedEndpointConfigs.size());
        }
        // Deploy what should be deployed.
        for (ServerEndpointConfig config : deployableExtendedEndpointConfigs) {
            try {
                jettyContainer.addEndpoint(config);
            } catch (DeploymentException e) {
                throw new ServletException(e);
            }
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Deploying {} @ServerEndpoint(s)", deployableAnnotatedEndpoints.size());
        }
        for (Class<?> annotatedClass : deployableAnnotatedEndpoints) {
            try {
                jettyContainer.addEndpoint(annotatedClass);
            } catch (DeploymentException e) {
                throw new ServletException(e);
            }
        }
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) ServerApplicationConfig(javax.websocket.server.ServerApplicationConfig) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ServletException(javax.servlet.ServletException) ServerEndpoint(javax.websocket.server.ServerEndpoint) Endpoint(javax.websocket.Endpoint) DeploymentException(javax.websocket.DeploymentException) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ServerContainer(org.eclipse.jetty.websocket.jsr356.server.ServerContainer) HashSet(java.util.HashSet)

Example 3 with ServerApplicationConfig

use of javax.websocket.server.ServerApplicationConfig in project tomcat70 by apache.

the class WsSci method onStartup.

@Override
public void onStartup(Set<Class<?>> clazzes, ServletContext ctx) throws ServletException {
    if (!isJava7OrLater()) {
        // it if Java 7 is not available.
        if (!logMessageWritten) {
            logMessageWritten = true;
            log.info(sm.getString("sci.noWebSocketSupport"));
        }
        return;
    }
    WsServerContainer sc = init(ctx, true);
    if (clazzes == null || clazzes.size() == 0) {
        return;
    }
    // Group the discovered classes by type
    Set<ServerApplicationConfig> serverApplicationConfigs = new HashSet<ServerApplicationConfig>();
    Set<Class<? extends Endpoint>> scannedEndpointClazzes = new HashSet<Class<? extends Endpoint>>();
    Set<Class<?>> scannedPojoEndpoints = new HashSet<Class<?>>();
    try {
        // wsPackage is "javax.websocket."
        String wsPackage = ContainerProvider.class.getName();
        wsPackage = wsPackage.substring(0, wsPackage.lastIndexOf('.') + 1);
        for (Class<?> clazz : clazzes) {
            int modifiers = clazz.getModifiers();
            if (!Modifier.isPublic(modifiers) || Modifier.isAbstract(modifiers)) {
                // Non-public or abstract - skip it.
                continue;
            }
            // Protect against scanning the WebSocket API JARs
            if (clazz.getName().startsWith(wsPackage)) {
                continue;
            }
            if (ServerApplicationConfig.class.isAssignableFrom(clazz)) {
                serverApplicationConfigs.add((ServerApplicationConfig) clazz.newInstance());
            }
            if (Endpoint.class.isAssignableFrom(clazz)) {
                @SuppressWarnings("unchecked") Class<? extends Endpoint> endpoint = (Class<? extends Endpoint>) clazz;
                scannedEndpointClazzes.add(endpoint);
            }
            if (clazz.isAnnotationPresent(ServerEndpoint.class)) {
                scannedPojoEndpoints.add(clazz);
            }
        }
    } catch (InstantiationException e) {
        throw new ServletException(e);
    } catch (IllegalAccessException e) {
        throw new ServletException(e);
    }
    // Filter the results
    Set<ServerEndpointConfig> filteredEndpointConfigs = new HashSet<ServerEndpointConfig>();
    Set<Class<?>> filteredPojoEndpoints = new HashSet<Class<?>>();
    if (serverApplicationConfigs.isEmpty()) {
        filteredPojoEndpoints.addAll(scannedPojoEndpoints);
    } else {
        for (ServerApplicationConfig config : serverApplicationConfigs) {
            Set<ServerEndpointConfig> configFilteredEndpoints = config.getEndpointConfigs(scannedEndpointClazzes);
            if (configFilteredEndpoints != null) {
                filteredEndpointConfigs.addAll(configFilteredEndpoints);
            }
            Set<Class<?>> configFilteredPojos = config.getAnnotatedEndpointClasses(scannedPojoEndpoints);
            if (configFilteredPojos != null) {
                filteredPojoEndpoints.addAll(configFilteredPojos);
            }
        }
    }
    try {
        // Deploy endpoints
        for (ServerEndpointConfig config : filteredEndpointConfigs) {
            sc.addEndpoint(config);
        }
        // Deploy POJOs
        for (Class<?> clazz : filteredPojoEndpoints) {
            sc.addEndpoint(clazz);
        }
    } catch (DeploymentException e) {
        throw new ServletException(e);
    }
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) ServerApplicationConfig(javax.websocket.server.ServerApplicationConfig) ServerEndpoint(javax.websocket.server.ServerEndpoint) Endpoint(javax.websocket.Endpoint) ServletException(javax.servlet.ServletException) ServerEndpoint(javax.websocket.server.ServerEndpoint) Endpoint(javax.websocket.Endpoint) DeploymentException(javax.websocket.DeploymentException) HashSet(java.util.HashSet)

Example 4 with ServerApplicationConfig

use of javax.websocket.server.ServerApplicationConfig in project wildfly by wildfly.

the class UndertowJSRWebSocketDeploymentProcessor method doDeployment.

private void doDeployment(DeploymentUnit deploymentUnit, final WebSocketDeploymentInfo container, final Set<Class<?>> annotatedEndpoints, final Set<Class<? extends ServerApplicationConfig>> serverApplicationConfigClasses, final Set<Class<? extends Endpoint>> endpoints) throws DeploymentUnitProcessingException {
    Set<Class<? extends Endpoint>> allScannedEndpointImplementations = new HashSet<>(endpoints);
    Set<Class<?>> allScannedAnnotatedEndpoints = new HashSet<>(annotatedEndpoints);
    Set<Class<?>> newAnnotatatedEndpoints = new HashSet<>();
    Set<ServerEndpointConfig> serverEndpointConfigurations = new HashSet<>();
    final Set<ServerApplicationConfig> configInstances = new HashSet<>();
    for (Class<? extends ServerApplicationConfig> clazz : serverApplicationConfigClasses) {
        try {
            configInstances.add(clazz.newInstance());
        } catch (InstantiationException | IllegalAccessException e) {
            JsrWebSocketLogger.ROOT_LOGGER.couldNotInitializeConfiguration(clazz, e);
        }
    }
    if (!configInstances.isEmpty()) {
        for (ServerApplicationConfig config : configInstances) {
            Set<Class<?>> returnedEndpoints = config.getAnnotatedEndpointClasses(allScannedAnnotatedEndpoints);
            if (returnedEndpoints != null) {
                newAnnotatatedEndpoints.addAll(returnedEndpoints);
            }
            Set<ServerEndpointConfig> endpointConfigs = config.getEndpointConfigs(allScannedEndpointImplementations);
            if (endpointConfigs != null) {
                serverEndpointConfigurations.addAll(endpointConfigs);
            }
        }
    } else {
        newAnnotatatedEndpoints.addAll(allScannedAnnotatedEndpoints);
    }
    // annotated endpoints first
    for (Class<?> endpoint : newAnnotatatedEndpoints) {
        if (endpoint != null) {
            container.addEndpoint(endpoint);
            ServerEndpoint annotation = endpoint.getAnnotation(ServerEndpoint.class);
            if (annotation != null) {
                String path = annotation.value();
                addManagementWebsocket(deploymentUnit, endpoint, path);
            }
        }
    }
    for (final ServerEndpointConfig endpoint : serverEndpointConfigurations) {
        if (endpoint != null) {
            container.addEndpoint(endpoint);
            addManagementWebsocket(deploymentUnit, endpoint.getEndpointClass(), endpoint.getPath());
        }
    }
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) ServerApplicationConfig(javax.websocket.server.ServerApplicationConfig) ServerEndpoint(javax.websocket.server.ServerEndpoint) Endpoint(javax.websocket.Endpoint) ServerEndpoint(javax.websocket.server.ServerEndpoint) ClientEndpoint(javax.websocket.ClientEndpoint) HashSet(java.util.HashSet)

Aggregations

HashSet (java.util.HashSet)4 Endpoint (javax.websocket.Endpoint)4 ServerApplicationConfig (javax.websocket.server.ServerApplicationConfig)4 ServerEndpoint (javax.websocket.server.ServerEndpoint)4 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)3 ServletException (javax.servlet.ServletException)2 ClientEndpoint (javax.websocket.ClientEndpoint)2 DeploymentException (javax.websocket.DeploymentException)2 WebSocketDeploymentInfo (io.undertow.websockets.jsr.WebSocketDeploymentInfo)1 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)1 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)1 ServerContainer (org.eclipse.jetty.websocket.jsr356.server.ServerContainer)1 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)1 CompositeIndex (org.jboss.as.server.deployment.annotation.CompositeIndex)1 WarMetaData (org.jboss.as.web.common.WarMetaData)1 AnnotationInstance (org.jboss.jandex.AnnotationInstance)1 ClassInfo (org.jboss.jandex.ClassInfo)1 Module (org.jboss.modules.Module)1