From a755a56cf9a1257d90cdca157f30b005047ac970 Mon Sep 17 00:00:00 2001 From: Stefan Bischof Date: Mon, 2 Sep 2024 13:06:02 +0200 Subject: [PATCH] add command helper for JakartaRS Signed-off-by: Stefan Bischof --- biz.aQute.gogo.commands.provider/bnd.bnd | 3 + .../gogo/commands/provider/Activator.java | 1 + .../gogo/commands/provider/JakartaRS.java | 146 ++++++++++++++++++ .../aQute/gogo/commands/provider/JaxRS.java | 2 +- cnf/central.mvn | 3 +- 5 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 biz.aQute.gogo.commands.provider/src/main/java/biz/aQute/gogo/commands/provider/JakartaRS.java diff --git a/biz.aQute.gogo.commands.provider/bnd.bnd b/biz.aQute.gogo.commands.provider/bnd.bnd index 975c4bdd..99a07b75 100644 --- a/biz.aQute.gogo.commands.provider/bnd.bnd +++ b/biz.aQute.gogo.commands.provider/bnd.bnd @@ -14,6 +14,7 @@ DynamicImport-Package * aQute.libg,\ osgi.core,\ org.osgi.service.component,\ + org.osgi.service.jakartars,\ org.osgi.service.jaxrs,\ org.osgi.service.http.whiteboard,\ biz.aQute.api.dtos @@ -56,4 +57,6 @@ Import-Package: \ org.osgi.service.http.runtime.dto;'resolution:'=optional,\ org.osgi.service.jaxrs.runtime;'resolution:'=optional,\ org.osgi.service.jaxrs.runtime.dto;'resolution:'=optional,\ + org.osgi.service.jakartars.runtime;'resolution:'=optional,\ + org.osgi.service.jakartars.runtime.dto;'resolution:'=optional,\ * \ No newline at end of file diff --git a/biz.aQute.gogo.commands.provider/src/main/java/biz/aQute/gogo/commands/provider/Activator.java b/biz.aQute.gogo.commands.provider/src/main/java/biz/aQute/gogo/commands/provider/Activator.java index 09272cc1..7e1b110e 100644 --- a/biz.aQute.gogo.commands.provider/src/main/java/biz/aQute/gogo/commands/provider/Activator.java +++ b/biz.aQute.gogo.commands.provider/src/main/java/biz/aQute/gogo/commands/provider/Activator.java @@ -48,6 +48,7 @@ public void start(BundleContext context) throws Exception { register(LoggerAdminCommands.class); register(HTTP.class); register(JaxRS.class); + register(JakartaRS.class); } private void registerConverter(BundleContext context) { diff --git a/biz.aQute.gogo.commands.provider/src/main/java/biz/aQute/gogo/commands/provider/JakartaRS.java b/biz.aQute.gogo.commands.provider/src/main/java/biz/aQute/gogo/commands/provider/JakartaRS.java new file mode 100644 index 00000000..c57581bb --- /dev/null +++ b/biz.aQute.gogo.commands.provider/src/main/java/biz/aQute/gogo/commands/provider/JakartaRS.java @@ -0,0 +1,146 @@ +package biz.aQute.gogo.commands.provider; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +import org.apache.felix.service.command.Descriptor; +import org.apache.felix.service.command.Parameter; +import org.osgi.framework.BundleContext; +import org.osgi.service.jakartars.runtime.JakartarsServiceRuntime; +import org.osgi.service.jakartars.runtime.dto.ApplicationDTO; +import org.osgi.service.jakartars.runtime.dto.ExtensionDTO; +import org.osgi.service.jakartars.runtime.dto.FailedApplicationDTO; +import org.osgi.service.jakartars.runtime.dto.FailedExtensionDTO; +import org.osgi.service.jakartars.runtime.dto.FailedResourceDTO; +import org.osgi.service.jakartars.runtime.dto.ResourceDTO; +import org.osgi.service.jakartars.runtime.dto.ResourceMethodInfoDTO; +import org.osgi.service.jakartars.runtime.dto.RuntimeDTO; +import org.osgi.util.tracker.ServiceTracker; + +import biz.aQute.gogo.commands.dtoformatter.DTOFormatter; + +public class JakartaRS { + + final ServiceTracker tracker; + final BundleContext context; + + public JakartaRS(BundleContext context, DTOFormatter formatter) { + this.context = context; + dtos(formatter); + // dtos(formatter); + tracker = new ServiceTracker<>(context, JakartarsServiceRuntime.class, null); + tracker.open(); + } + + void dtos(DTOFormatter formatter) { + formatter.build(RuntimeDTO.class) + .inspect() + .fields("*") + .line() + .fields("*") + .part() + .as(dto -> String.format("[%s] ", dto.serviceDTO.id)); + + formatter.build(FailedApplicationDTO.class) + .inspect() + .fields("*") + .line() + .fields("*") + .part() + .as(dto -> String.format("[%s] %s", dto.serviceId, dto.name)); + + formatter.build(ApplicationDTO.class) + .inspect() + .fields("*") + .line() + .fields("*") + .part() + .as(dto -> String.format("[%s] %s", dto.serviceId, dto.name)); + + formatter.build(FailedExtensionDTO.class) + .inspect() + .fields("*") + .line() + .fields("*") + .part() + .as(dto -> String.format("[%s] %s", dto.serviceId, dto.name)); + + formatter.build(ExtensionDTO.class) + .inspect() + .fields("*") + .line() + .fields("*") + .part() + .as(dto -> String.format("[%s] %s", dto.serviceId, dto.name)); + + formatter.build(FailedResourceDTO.class) + .inspect() + .fields("*") + .line() + .fields("*") + .part() + .as(dto -> String.format("[%s] %s", dto.serviceId, dto.name)); + + formatter.build(ResourceDTO.class) + .inspect() + .fields("*") + .line() + .fields("*") + .part() + .as(dto -> String.format("[%s] %s", dto.serviceId, dto.name)); + + formatter.build(ResourceMethodInfoDTO.class) + .inspect() + .fields("*") + .line() + .fields("*") + .part() + .as(dto -> String.format("[%s] %s", dto.method, dto.path)); + + } + + private List serviceRuntimes() { + + return Arrays.asList((JakartarsServiceRuntime[]) tracker.getServices()); + } + + private JakartarsServiceRuntime serviceRuntime() { + + return tracker.getService(); + } + + @Descriptor("Show the RuntimeDTO of the JakartarsServiceRuntime") + public List jakartarsruntimes() throws InterruptedException { + + return serviceRuntimes().stream() + .map(JakartarsServiceRuntime::getRuntimeDTO) + .collect(Collectors.toList()); + } + + @Descriptor("Show the RuntimeDTO of the JakartarsServiceRuntime") + public RuntimeDTO jakartarsruntime(@Descriptor("service.id") + @Parameter(absentValue = "-1", names = "-s") + long service_id) throws InterruptedException { + + return runtime(service_id).map(JakartarsServiceRuntime::getRuntimeDTO) + .orElse(null); + } + + private Optional runtime(long service_id) { + + if (service_id < 0) { + return Optional.ofNullable(serviceRuntime()); + } + return serviceRuntimes().stream() + .filter(runtime -> runtimeHasServiceId(runtime, service_id)) + .findAny(); + } + + private static boolean runtimeHasServiceId(JakartarsServiceRuntime runtime, long service_id) { + + return service_id == runtime.getRuntimeDTO().serviceDTO.id; + } + +} diff --git a/biz.aQute.gogo.commands.provider/src/main/java/biz/aQute/gogo/commands/provider/JaxRS.java b/biz.aQute.gogo.commands.provider/src/main/java/biz/aQute/gogo/commands/provider/JaxRS.java index 5daffd43..5169a0d0 100644 --- a/biz.aQute.gogo.commands.provider/src/main/java/biz/aQute/gogo/commands/provider/JaxRS.java +++ b/biz.aQute.gogo.commands.provider/src/main/java/biz/aQute/gogo/commands/provider/JaxRS.java @@ -111,7 +111,7 @@ private JaxrsServiceRuntime serviceRuntime() { return tracker.getService(); } - @Descriptor("Show the RuntimeDTO of the HttpServiceRuntime") + @Descriptor("Show the RuntimeDTO of the JaxrsServiceRuntime") public List jaxrsruntimes() throws InterruptedException { return serviceRuntimes().stream() diff --git a/cnf/central.mvn b/cnf/central.mvn index daf374bd..a10b1cfc 100644 --- a/cnf/central.mvn +++ b/cnf/central.mvn @@ -114,7 +114,8 @@ org.osgi:org.osgi.service.coordinator:1.0.2 org.osgi:org.osgi.service.event:1.4.0 org.osgi:org.osgi.service.http.whiteboard:1.1.0 org.osgi:org.osgi.service.http:1.2.1 -org.osgi:org.osgi.service.jaxrs:jar:1.0.0 +org.osgi:org.osgi.service.jakartars:2.0.0 +org.osgi:org.osgi.service.jaxrs:1.0.0 org.osgi:org.osgi.service.jdbc:1.0.0 org.osgi:org.osgi.service.log:1.4.0 org.osgi:org.osgi.service.log:1.3.0