From 9beef8ee90525477b3e505ddb66244351a092089 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Livora?= Date: Thu, 6 Oct 2016 21:58:42 +0200 Subject: [PATCH] Fix class discovery in MicroserviceProxyFactory - MicroserviceMethodHandler implementations from other providers were not discovered --- .../camel/CamelMicroserviceProvider.java | 10 ++++------ .../cdi/internal/MicroserviceProxyFactory.java | 18 +++++++----------- .../microservices/util/DeploymentScanner.java | 2 +- .../vertx/VertxMicroserviceProvider.java | 14 ++++---------- 4 files changed, 16 insertions(+), 28 deletions(-) diff --git a/camel-microservice-provider/src/main/java/io/silverware/microservices/providers/camel/CamelMicroserviceProvider.java b/camel-microservice-provider/src/main/java/io/silverware/microservices/providers/camel/CamelMicroserviceProvider.java index 20bba52..b2e0327 100644 --- a/camel-microservice-provider/src/main/java/io/silverware/microservices/providers/camel/CamelMicroserviceProvider.java +++ b/camel-microservice-provider/src/main/java/io/silverware/microservices/providers/camel/CamelMicroserviceProvider.java @@ -65,13 +65,12 @@ public class CamelMicroserviceProvider implements MicroserviceProvider, CamelSil private final DeployStats stats = new DeployStats(); private void createCamelContext() throws SilverWareException { - @SuppressWarnings("unchecked") - Set> camelContextFactories = DeploymentScanner.getContextInstance(context).lookupSubtypes(CamelContextFactory.class); + Set> camelContextFactories = DeploymentScanner.getContextInstance(context).lookupSubtypes(CamelContextFactory.class); if (camelContextFactories.size() >= 2) { throw new SilverWareException("More than one CamelContextFactories found."); } else if (camelContextFactories.size() == 1) { - Class clazz = camelContextFactories.iterator().next(); + Class clazz = camelContextFactories.iterator().next(); try { CamelContextFactory camelContextFactory = clazz.newInstance(); camelContext = camelContextFactory.createCamelContext(context); @@ -86,14 +85,13 @@ private void createCamelContext() throws SilverWareException { } private void loadRoutesFromClasses() { - @SuppressWarnings("unchecked") - final Set> routeBuilders = (Set>) DeploymentScanner.getContextInstance(context).lookupSubtypes(RouteBuilder.class); + final Set> routeBuilders = DeploymentScanner.getContextInstance(context).lookupSubtypes(RouteBuilder.class); if (log.isDebugEnabled()) { log.debug("Initializing Camel route resources..."); } stats.setFound(routeBuilders.size()); - for (Class clazz : routeBuilders) { + for (Class clazz : routeBuilders) { if (log.isDebugEnabled()) { log.debug("Creating Camel route builder: " + clazz.getName()); } diff --git a/cdi-microservice-provider/src/main/java/io/silverware/microservices/providers/cdi/internal/MicroserviceProxyFactory.java b/cdi-microservice-provider/src/main/java/io/silverware/microservices/providers/cdi/internal/MicroserviceProxyFactory.java index bd0e316..ac39a6f 100644 --- a/cdi-microservice-provider/src/main/java/io/silverware/microservices/providers/cdi/internal/MicroserviceProxyFactory.java +++ b/cdi-microservice-provider/src/main/java/io/silverware/microservices/providers/cdi/internal/MicroserviceProxyFactory.java @@ -19,16 +19,14 @@ */ package io.silverware.microservices.providers.cdi.internal; -import org.reflections.Reflections; -import org.reflections.scanners.ResourcesScanner; -import org.reflections.util.ConfigurationBuilder; - import java.lang.reflect.Constructor; import java.util.Comparator; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import javax.annotation.Priority; +import io.silverware.microservices.util.DeploymentScanner; import javassist.util.proxy.MethodHandler; import javassist.util.proxy.ProxyFactory; @@ -45,14 +43,12 @@ public class MicroserviceProxyFactory { private static final List> HANDLER_CLASSES; static { - ConfigurationBuilder builder = ConfigurationBuilder.build("").addScanners(new ResourcesScanner()); - Reflections reflections = new Reflections(builder); + Set> handlerClasses = DeploymentScanner.getDefaultInstance().lookupSubtypes(MicroserviceMethodHandler.class); - HANDLER_CLASSES = reflections.getSubTypesOf(MicroserviceMethodHandler.class) - .stream() - .filter(handler -> handler != DefaultMethodHandler.class) - .sorted(new MethodHandlerPrioritizer()) - .collect(Collectors.toList()); + HANDLER_CLASSES = handlerClasses.stream() + .filter(handler -> handler != DefaultMethodHandler.class) + .sorted(new MethodHandlerPrioritizer()) + .collect(Collectors.toList()); } private MicroserviceProxyFactory() { diff --git a/microservices/src/main/java/io/silverware/microservices/util/DeploymentScanner.java b/microservices/src/main/java/io/silverware/microservices/util/DeploymentScanner.java index dda3581..cd418b8 100644 --- a/microservices/src/main/java/io/silverware/microservices/util/DeploymentScanner.java +++ b/microservices/src/main/java/io/silverware/microservices/util/DeploymentScanner.java @@ -163,7 +163,7 @@ public Set> lookupMicroserviceProviders() * @return All available classes of the given subtype. */ @SuppressWarnings("unchecked") - public Set lookupSubtypes(final Class clazz) { + public Set> lookupSubtypes(final Class clazz) { return reflections.getSubTypesOf(clazz); /*final Set s1 = reflections.getSubTypesOf(clazz); final Set s2 = Sets.newHashSet(ReflectionUtils.forNames( diff --git a/vertx-microservice-provider/src/main/java/io/silverware/microservices/providers/vertx/VertxMicroserviceProvider.java b/vertx-microservice-provider/src/main/java/io/silverware/microservices/providers/vertx/VertxMicroserviceProvider.java index 0ab3a00..13c5213 100644 --- a/vertx-microservice-provider/src/main/java/io/silverware/microservices/providers/vertx/VertxMicroserviceProvider.java +++ b/vertx-microservice-provider/src/main/java/io/silverware/microservices/providers/vertx/VertxMicroserviceProvider.java @@ -131,12 +131,9 @@ private void loadVerticlesFromCP() { } private void loadJavaVerticles() { + Set> verticleClasses = DeploymentScanner.getContextInstance(context).lookupSubtypes(Verticle.class); - @SuppressWarnings("unchecked") - Set> verticleClasses = (Set>) DeploymentScanner.getContextInstance(context) - .lookupSubtypes(Verticle.class); - - for (Class verticleClass : verticleClasses) { + for (Class verticleClass : verticleClasses) { Deployment deployment = verticleClass.getAnnotation(Deployment.class); @@ -150,12 +147,9 @@ private void loadJavaVerticles() { } private void loadGroovyVerticles() { + Set> groovyVerticleClasses = DeploymentScanner.getContextInstance(context).lookupSubtypes(GroovyVerticle.class); - @SuppressWarnings("unchecked") - Set> groovyVerticleClasses = (Set>) DeploymentScanner - .getContextInstance(context).lookupSubtypes(GroovyVerticle.class); - - for (Class groovyVerticleClass : groovyVerticleClasses) { + for (Class groovyVerticleClass : groovyVerticleClasses) { Deployment deployment = groovyVerticleClass.getAnnotation(Deployment.class);