use of io.undertow.websockets.util.ObjectFactory in project quarkus by quarkusio.
the class WebsocketCoreRecorder method createServerContainer.
public RuntimeValue<ServerWebSocketContainer> createServerContainer(BeanContainer beanContainer, RuntimeValue<WebSocketDeploymentInfo> infoVal, ServerWebSocketContainerFactory serverContainerFactory) throws DeploymentException {
WebSocketDeploymentInfo info = infoVal.getValue();
ClassLoader cl = Thread.currentThread().getContextClassLoader();
ManagedContext requestContext = Arc.container().requestContext();
if (serverContainerFactory == null) {
serverContainerFactory = ServerWebSocketContainer::new;
}
Instance<CurrentIdentityAssociation> currentIdentityAssociation = Arc.container().select(CurrentIdentityAssociation.class);
ServerWebSocketContainer container = serverContainerFactory.create(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(), new Supplier<EventLoopGroup>() {
@Override
public EventLoopGroup get() {
return ((VertxInternal) VertxCoreRecorder.getVertx().get()).getEventLoopGroup();
}
}, Collections.singletonList(new ContextSetupHandler() {
@Override
public <T, C> Action<T, C> create(Action<T, C> action) {
return new Action<T, C>() {
CurrentIdentityAssociation getCurrentIdentityAssociation() {
if (currentIdentityAssociation.isResolvable()) {
return currentIdentityAssociation.get();
}
return null;
}
@Override
public T call(C context, UndertowSession session) throws Exception {
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(cl);
boolean required = !requestContext.isActive();
if (required) {
requestContext.activate();
Principal p = session.getUserPrincipal();
if (p instanceof WebSocketPrincipal) {
var current = getCurrentIdentityAssociation();
if (current != null) {
current.setIdentity(((WebSocketPrincipal) p).getSecurityIdentity());
}
}
}
try {
return action.call(context, session);
} finally {
try {
if (required) {
requestContext.terminate();
}
} finally {
Thread.currentThread().setContextClassLoader(old);
}
}
}
};
}
}), info.isDispatchToWorkerThread(), null, null, info.getExecutor(), Collections.emptyList(), info.getMaxFrameSize(), new Supplier<Principal>() {
@Override
public Principal get() {
if (currentIdentityAssociation.isResolvable()) {
return new WebSocketPrincipal(currentIdentityAssociation.get().getIdentity());
}
return null;
}
});
for (Class<?> i : info.getAnnotatedEndpoints()) {
container.addEndpoint(i);
}
for (ServerEndpointConfig i : info.getProgramaticEndpoints()) {
container.addEndpoint(i);
}
UndertowContainerProvider.setDefaultContainer(container);
return new RuntimeValue<>(container);
}
Aggregations