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);
}
}
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);
}
}
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);
}
}
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());
}
}
}
Aggregations