forked from quarkusio/quarkus
/
WebsocketRecorder.java
174 lines (159 loc) · 7.55 KB
/
WebsocketRecorder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package io.quarkus.undertow.websockets.runtime;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.function.Supplier;
import javax.websocket.DeploymentException;
import javax.websocket.Endpoint;
import javax.websocket.server.ServerApplicationConfig;
import javax.websocket.server.ServerEndpointConfig;
import org.jboss.logging.Logger;
import io.netty.channel.EventLoopGroup;
import io.quarkus.arc.Arc;
import io.quarkus.arc.ManagedContext;
import io.quarkus.arc.runtime.BeanContainer;
import io.quarkus.runtime.ExecutorRecorder;
import io.quarkus.runtime.annotations.Recorder;
import io.undertow.websockets.UndertowContainerProvider;
import io.undertow.websockets.WebSocketDeploymentInfo;
import io.undertow.websockets.util.ContextSetupHandler;
import io.undertow.websockets.util.ObjectFactory;
import io.undertow.websockets.util.ObjectHandle;
import io.undertow.websockets.util.ObjectIntrospecter;
import io.undertow.websockets.vertx.VertxServerWebSocketContainer;
import io.undertow.websockets.vertx.VertxWebSocketHandler;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
@Recorder
public class WebsocketRecorder {
private static final Logger log = Logger.getLogger(WebsocketRecorder.class);
public void setupWorker(Executor executor) {
ExecutorSupplier.executor = executor;
}
@SuppressWarnings("unchecked")
public WebSocketDeploymentInfo createDeploymentInfo(Set<String> annotatedEndpoints, Set<String> endpoints,
Set<String> serverApplicationConfigClasses, int maxFrameSize, boolean dispatchToWorker) {
WebSocketDeploymentInfo container = new WebSocketDeploymentInfo();
container.setMaxFrameSize(maxFrameSize);
container.setDispatchToWorkerThread(dispatchToWorker);
container.setExecutor(new ExecutorSupplier());
Set<Class<? extends Endpoint>> allScannedEndpointImplementations = new HashSet<>();
for (String i : endpoints) {
try {
allScannedEndpointImplementations.add(
(Class<? extends Endpoint>) Class.forName(i, true, Thread.currentThread().getContextClassLoader()));
} catch (Exception e) {
log.error("Could not initialize websocket class " + i, e);
}
}
Set<Class<?>> allScannedAnnotatedEndpoints = new HashSet<>();
for (String i : annotatedEndpoints) {
try {
allScannedAnnotatedEndpoints.add(Class.forName(i, true, Thread.currentThread().getContextClassLoader()));
} catch (Exception e) {
log.error("Could not initialize websocket class " + i, e);
}
}
Set<Class<?>> newAnnotatedEndpoints = new HashSet<>();
Set<ServerEndpointConfig> serverEndpointConfigurations = new HashSet<>();
final Set<ServerApplicationConfig> configInstances = new HashSet<>();
for (String clazzName : serverApplicationConfigClasses) {
try {
configInstances.add((ServerApplicationConfig) Class
.forName(clazzName, true, Thread.currentThread().getContextClassLoader()).newInstance());
} catch (Exception e) {
log.error("Could not initialize websocket config class " + clazzName, e);
}
}
if (!configInstances.isEmpty()) {
for (ServerApplicationConfig config : configInstances) {
Set<Class<?>> returnedEndpoints = config.getAnnotatedEndpointClasses(allScannedAnnotatedEndpoints);
if (returnedEndpoints != null) {
newAnnotatedEndpoints.addAll(returnedEndpoints);
}
Set<ServerEndpointConfig> endpointConfigs = config.getEndpointConfigs(allScannedEndpointImplementations);
if (endpointConfigs != null) {
serverEndpointConfigurations.addAll(endpointConfigs);
}
}
} else {
newAnnotatedEndpoints.addAll(allScannedAnnotatedEndpoints);
}
//annotated endpoints first
for (Class<?> endpoint : newAnnotatedEndpoints) {
if (endpoint != null) {
container.addEndpoint(endpoint);
}
}
for (final ServerEndpointConfig endpoint : serverEndpointConfigurations) {
if (endpoint != null) {
container.addEndpoint(endpoint);
}
}
return container;
}
public Handler<RoutingContext> createHandler(BeanContainer beanContainer, Supplier<EventLoopGroup> eventLoopGroupSupplier,
WebSocketDeploymentInfo info) throws DeploymentException {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
ManagedContext requestContext = Arc.container().requestContext();
VertxServerWebSocketContainer container = new VertxServerWebSocketContainer(new ObjectIntrospecter() {
@Override
public <T> ObjectFactory<T> createInstanceFactory(Class<T> clazz) {
BeanContainer.Factory<T> factory = beanContainer.instanceFactory(clazz);
return new ObjectFactory<T>() {
@Override
public ObjectHandle<T> createInstance() {
BeanContainer.Instance<T> instance = factory.create();
return new ObjectHandle<T>() {
@Override
public T getInstance() {
return instance.get();
}
@Override
public void release() {
instance.close();
}
};
}
};
}
}, Thread.currentThread().getContextClassLoader(), eventLoopGroupSupplier,
Collections.singletonList(new ContextSetupHandler() {
@Override
public <T, C> Action<T, C> create(Action<T, C> action) {
return new Action<T, C>() {
@Override
public T call(C context) throws Exception {
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(cl);
requestContext.activate();
try {
return action.call(context);
} finally {
try {
requestContext.terminate();
} finally {
Thread.currentThread().setContextClassLoader(old);
}
}
}
};
}
}), false,
new Supplier<Executor>() {
@Override
public Executor get() {
return ExecutorRecorder.getCurrent();
}
});
for (Class<?> i : info.getAnnotatedEndpoints()) {
container.addEndpoint(i);
}
for (ServerEndpointConfig i : info.getProgramaticEndpoints()) {
container.addEndpoint(i);
}
UndertowContainerProvider.setDefaultContainer(container);
return new VertxWebSocketHandler(container, info);
}
}