Skip to content

Commit

Permalink
Improve CDI handling of JsonRpcRouter
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Nov 27, 2024
1 parent b8ea581 commit 29ce6b5
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Scanner;
import java.util.stream.Collectors;

import jakarta.enterprise.context.ApplicationScoped;

import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.IndexView;
Expand All @@ -32,6 +34,7 @@

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.BeanContainerBuildItem;
import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.arc.processor.BuiltinScope;
import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.annotations.BuildProducer;
Expand Down Expand Up @@ -126,6 +129,19 @@ public class DevUIProcessor {

private static final Logger log = Logger.getLogger(DevUIProcessor.class);

@BuildStep(onlyIf = IsDevelopment.class)
@Record(ExecutionTime.RUNTIME_INIT)
void registerDevUIWebSocketHandler(DevUIRecorder recorder,
BeanContainerBuildItem beanContainerBuildItem,
NonApplicationRootPathBuildItem nonApplicationRootPathBuildItem,
BuildProducer<RouteBuildItem> routeProducer) {
routeProducer.produce(
nonApplicationRootPathBuildItem
.routeBuilder().route(DEVUI + SLASH + JSONRPC)
.handler(recorder.communicationHandler(beanContainerBuildItem.getValue()))
.build());
}

@BuildStep(onlyIf = IsDevelopment.class)
@Record(ExecutionTime.STATIC_INIT)
void registerDevUiHandlers(
Expand Down Expand Up @@ -156,13 +172,6 @@ void registerDevUiHandlers(
.build());
}

// Websocket for JsonRPC comms
routeProducer.produce(
nonApplicationRootPathBuildItem
.routeBuilder().route(DEVUI + SLASH + JSONRPC)
.handler(recorder.communicationHandler())
.build());

// Static handler for components
for (DevUIRoutesBuildItem devUIRoutesBuildItem : devUIRoutesBuildItems) {
String route = devUIRoutesBuildItem.getPath();
Expand Down Expand Up @@ -271,7 +280,6 @@ void additionalBean(BuildProducer<AdditionalBeanBuildItem> additionalBeanProduce
List<JsonRPCProvidersBuildItem> jsonRPCProvidersBuildItems) {

additionalBeanProducer.produce(AdditionalBeanBuildItem.builder()
.addBeanClass(JsonRpcRouter.class)
.addBeanClass(VertxRouteInfoService.class)
.setUnremovable().build());

Expand All @@ -289,12 +297,6 @@ void additionalBean(BuildProducer<AdditionalBeanBuildItem> additionalBeanProduce
.setDefaultScope(defaultBeanScope)
.setUnremovable().build());
}

additionalBeanProducer.produce(AdditionalBeanBuildItem.builder()
.addBeanClass(JsonRpcRouter.class)
.setDefaultScope(BuiltinScope.APPLICATION.getName())
.setUnremovable().build());

}

/**
Expand Down Expand Up @@ -406,18 +408,25 @@ void findAllJsonRPCMethods(BuildProducer<JsonRPCRuntimeMethodsBuildItem> jsonRPC
@BuildStep(onlyIf = IsDevelopment.class)
@Record(ExecutionTime.RUNTIME_INIT)
void createJsonRpcRouter(DevUIRecorder recorder,
BeanContainerBuildItem beanContainer,
JsonRPCRuntimeMethodsBuildItem jsonRPCMethodsBuildItem,
DeploymentMethodBuildItem deploymentMethodBuildItem) {
DeploymentMethodBuildItem deploymentMethodBuildItem,
BuildProducer<SyntheticBeanBuildItem> beanProducer) {

if (jsonRPCMethodsBuildItem != null) {
Map<String, Map<JsonRpcMethodName, JsonRpcMethod>> extensionMethodsMap = jsonRPCMethodsBuildItem
.getExtensionMethodsMap();

DevConsoleManager.setGlobal(DevUIRecorder.DEV_MANAGER_GLOBALS_JSON_MAPPER_FACTORY,
JsonMapper.Factory.deploymentLinker().createLinkData(new DevUIDatabindCodec.Factory()));
recorder.createJsonRpcRouter(beanContainer.getValue(), extensionMethodsMap, deploymentMethodBuildItem.getMethods(),
deploymentMethodBuildItem.getSubscriptions(), deploymentMethodBuildItem.getRecordedValues());

beanProducer.produce(SyntheticBeanBuildItem
.configure(JsonRpcRouter.class)
.setRuntimeInit()
.unremovable()
.supplier(recorder.createJsonRpcRouter(extensionMethodsMap, deploymentMethodBuildItem.getMethods(),
deploymentMethodBuildItem.getSubscriptions(), deploymentMethodBuildItem.getRecordedValues()))
.scope(ApplicationScoped.class)
.done());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

import org.jboss.logging.Logger;

import io.netty.handler.codec.http.HttpResponseStatus;
import io.quarkus.arc.runtime.BeanContainer;
import io.quarkus.dev.console.DevConsoleManager;
import io.quarkus.devui.runtime.comms.JsonRpcRouter;
import io.quarkus.devui.runtime.jsonrpc.JsonRpcCodec;
import io.quarkus.devui.runtime.jsonrpc.JsonRpcMethod;
import io.quarkus.devui.runtime.jsonrpc.JsonRpcMethodName;
import io.quarkus.devui.runtime.jsonrpc.json.JsonMapper;
Expand All @@ -45,18 +47,18 @@ public void shutdownTask(ShutdownContext shutdownContext, String devUIBasePath)
shutdownContext.addShutdownTask(new DeleteDirectoryRunnable(devUIBasePath));
}

public void createJsonRpcRouter(BeanContainer beanContainer,
public Supplier<JsonRpcRouter> createJsonRpcRouter(
Map<String, Map<JsonRpcMethodName, JsonRpcMethod>> extensionMethodsMap,
List<String> deploymentMethods,
List<String> deploymentSubscriptions,
Map<String, RuntimeValue> recordedValues) {
JsonRpcRouter jsonRpcRouter = beanContainer.beanInstance(JsonRpcRouter.class);
jsonRpcRouter.populateJsonRPCRuntimeMethods(extensionMethodsMap);
jsonRpcRouter.setJsonRPCDeploymentActions(deploymentMethods, deploymentSubscriptions);
if (recordedValues != null && !recordedValues.isEmpty()) {
jsonRpcRouter.setRecordedValues(recordedValues);
}
jsonRpcRouter.initializeCodec(createJsonMapper());
return new Supplier<>() {
@Override
public JsonRpcRouter get() {
return new JsonRpcRouter(new JsonRpcCodec(createJsonMapper()), extensionMethodsMap, deploymentMethods,
deploymentSubscriptions, recordedValues);
}
};
}

private JsonMapper createJsonMapper() {
Expand All @@ -77,8 +79,8 @@ private JsonMapper createJsonMapper() {
}));
}

public Handler<RoutingContext> communicationHandler() {
return new DevUIWebSocket();
public Handler<RoutingContext> communicationHandler(BeanContainer beanContainer) {
return new DevUIWebSocket(beanContainer.beanInstance(JsonRpcRouter.class));
}

public Handler<RoutingContext> uiHandler(String finalDestination,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.quarkus.devui.runtime;

import jakarta.enterprise.inject.spi.CDI;

import org.jboss.logging.Logger;

import io.quarkus.devui.runtime.comms.JsonRpcRouter;
Expand All @@ -16,6 +14,12 @@
public class DevUIWebSocket implements Handler<RoutingContext> {
private static final Logger LOG = Logger.getLogger(DevUIWebSocket.class.getName());

private final JsonRpcRouter jsonRpcRouter;

public DevUIWebSocket(JsonRpcRouter jsonRpcRouter) {
this.jsonRpcRouter = jsonRpcRouter;
}

@Override
public void handle(RoutingContext event) {
if (WEBSOCKET.equalsIgnoreCase(event.request().getHeader(UPGRADE)) && !event.request().isEnded()) {
Expand All @@ -37,7 +41,6 @@ public void handle(AsyncResult<ServerWebSocket> event) {

private void addSocket(ServerWebSocket session) {
try {
JsonRpcRouter jsonRpcRouter = CDI.current().select(JsonRpcRouter.class).get();
jsonRpcRouter.addSocket(session);
} catch (IllegalStateException ise) {
LOG.debug("Failed to connect to dev ui communication server, " + ise.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import io.quarkus.devui.runtime.jsonrpc.JsonRpcMethod;
import io.quarkus.devui.runtime.jsonrpc.JsonRpcMethodName;
import io.quarkus.devui.runtime.jsonrpc.JsonRpcRequest;
import io.quarkus.devui.runtime.jsonrpc.json.JsonMapper;
import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.StartupEvent;
import io.smallrye.mutiny.Multi;
Expand Down Expand Up @@ -51,14 +50,25 @@ public class JsonRpcRouter {
private final Map<String, RuntimeValue> recordedValues = new HashMap<>();

private static final List<ServerWebSocket> SESSIONS = Collections.synchronizedList(new ArrayList<>());
private JsonRpcCodec codec;
private final JsonRpcCodec codec;

public JsonRpcRouter(JsonRpcCodec codec, Map<String, Map<JsonRpcMethodName, JsonRpcMethod>> extensionMethodsMap,
List<String> deploymentMethods, List<String> deploymentSubscriptions,
Map<String, RuntimeValue> recordedValues) {
this.codec = codec;
populateJsonRPCRuntimeMethods(extensionMethodsMap);
setJsonRPCDeploymentActions(deploymentMethods, deploymentSubscriptions);
if (recordedValues != null && !recordedValues.isEmpty()) {
setRecordedValues(recordedValues);
}
}

/**
* This gets called on build to build into of the classes we are going to call in runtime
*
* @param extensionMethodsMap
*/
public void populateJsonRPCRuntimeMethods(Map<String, Map<JsonRpcMethodName, JsonRpcMethod>> extensionMethodsMap) {
private void populateJsonRPCRuntimeMethods(Map<String, Map<JsonRpcMethodName, JsonRpcMethod>> extensionMethodsMap) {
for (Map.Entry<String, Map<JsonRpcMethodName, JsonRpcMethod>> extension : extensionMethodsMap.entrySet()) {
String extensionName = extension.getKey();
Map<JsonRpcMethodName, JsonRpcMethod> jsonRpcMethods = extension.getValue();
Expand Down Expand Up @@ -90,22 +100,18 @@ public void populateJsonRPCRuntimeMethods(Map<String, Map<JsonRpcMethodName, Jso
}
}

public void setJsonRPCDeploymentActions(List<String> methods, List<String> subscriptions) {
private void setJsonRPCDeploymentActions(List<String> methods, List<String> subscriptions) {
this.jsonRpcMethodToDeploymentClassPathJava.clear();
this.jsonRpcMethodToDeploymentClassPathJava.addAll(methods);
this.jsonRpcSubscriptionToDeploymentClassPathJava.clear();
this.jsonRpcSubscriptionToDeploymentClassPathJava.addAll(subscriptions);
}

public void setRecordedValues(Map<String, RuntimeValue> recordedValues) {
private void setRecordedValues(Map<String, RuntimeValue> recordedValues) {
this.recordedValues.clear();
this.recordedValues.putAll(recordedValues);
}

public void initializeCodec(JsonMapper jsonMapper) {
this.codec = new JsonRpcCodec(jsonMapper);
}

public void addSocket(ServerWebSocket socket) {
SESSIONS.add(socket);
socket.textMessageHandler((e) -> {
Expand Down

0 comments on commit 29ce6b5

Please sign in to comment.