Repository: karaf-cellar Updated Branches: refs/heads/cellar-3.0.x f2ffeb5ff -> e512e50b4
[KARAF-3646] moved away from java.lang.isAssignableFrom in favor of org.apache.commons.lang3.ClassUtils.isAssignable; Project: http://git-wip-us.apache.org/repos/asf/karaf-cellar/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf-cellar/commit/e512e50b Tree: http://git-wip-us.apache.org/repos/asf/karaf-cellar/tree/e512e50b Diff: http://git-wip-us.apache.org/repos/asf/karaf-cellar/diff/e512e50b Branch: refs/heads/cellar-3.0.x Commit: e512e50b4a889c1cd2298241820778d000f350c7 Parents: f2ffeb5 Author: Jean-Baptiste Onofré <[email protected]> Authored: Sun Sep 20 21:14:54 2015 +0200 Committer: Jean-Baptiste Onofré <[email protected]> Committed: Sun Sep 20 21:14:54 2015 +0200 ---------------------------------------------------------------------- dosgi/pom.xml | 8 +++ .../cellar/dosgi/RemoteServiceCallHandler.java | 51 +++++++++++++++++--- 2 files changed, 52 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/e512e50b/dosgi/pom.xml ---------------------------------------------------------------------- diff --git a/dosgi/pom.xml b/dosgi/pom.xml index a5832af..d121931 100644 --- a/dosgi/pom.xml +++ b/dosgi/pom.xml @@ -53,6 +53,11 @@ <artifactId>org.osgi.compendium</artifactId> <scope>provided</scope> </dependency> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-lang3</artifactId> + <version>3.3.2</version> + </dependency> <!-- Logging Dependencies --> <dependency> @@ -90,6 +95,9 @@ org.osgi*, org.slf4j;version="[1.6,2)";resolution:=optional </Import-Package> + <Private-Package> + org.apache.commons.lang3*;-split-package:=merge-first + </Private-Package> <DynamicImport-Package>javax.*,org.w3c.*,org.xml.*,*</DynamicImport-Package> </instructions> </configuration> http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/e512e50b/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/RemoteServiceCallHandler.java ---------------------------------------------------------------------- diff --git a/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/RemoteServiceCallHandler.java b/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/RemoteServiceCallHandler.java index c6f5732..810d4f4 100644 --- a/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/RemoteServiceCallHandler.java +++ b/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/RemoteServiceCallHandler.java @@ -13,6 +13,7 @@ */ package org.apache.karaf.cellar.dosgi; +import org.apache.commons.lang3.ClassUtils; import org.apache.karaf.cellar.core.CellarSupport; import org.apache.karaf.cellar.core.Configurations; import org.apache.karaf.cellar.core.control.BasicSwitch; @@ -31,6 +32,7 @@ import org.slf4j.LoggerFactory; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.Arrays; /** * Handler for cluster remote service call event. @@ -88,13 +90,7 @@ public class RemoteServiceCallHandler extends CellarSupport implements EventHand RemoteServiceResult result = new RemoteServiceResult(event.getId()); EventProducer producer = eventTransportFactory.getEventProducer(Constants.RESULT_PREFIX + Constants.SEPARATOR + event.getSourceNode().getId() + event.getEndpointId(), false); try { - Method method; - if (classes.length > 0) { - method = targetService.getClass().getMethod(event.getMethod(), classes); - } else { - method = targetService.getClass().getMethod(event.getMethod()); - } - + Method method = getMethod(classes, targetService, event); Object obj = method.invoke(targetService, event.getArguments().toArray()); result.setResult(obj); producer.produce(result); @@ -117,6 +113,47 @@ public class RemoteServiceCallHandler extends CellarSupport implements EventHand } /** + * <p>Gets a matching method in the <code>Object targetService<code/>.<br/> + * Inheritance is supported.</p> + * + * @param eventParamTypes + * @param targetService + * @param event + * @return a method instance from the <code>Object targetService<code/> + * @throws NoSuchMethodException + */ + private Method getMethod(Class[] eventParamTypes, Object targetService, RemoteServiceCall event) throws NoSuchMethodException { + + Method result = null; + if (eventParamTypes.length > 0) { + for (Method remoteMethod : targetService.getClass().getMethods()) { + //need to find a method with a matching name and with the same number of parameters + if (remoteMethod.getName().equals(event.getMethod()) && remoteMethod.getParameterTypes().length == eventParamTypes.length) { + boolean allParamsFound = true; + for (int i = 0; i < remoteMethod.getParameterTypes().length; i++) { + allParamsFound = allParamsFound && ClassUtils.isAssignable(eventParamTypes[i], remoteMethod.getParameterTypes()[i]); + } + + // if already found a matching method, no need to continue looking for one + if (allParamsFound) { + result = remoteMethod; + break; + } + } + } + } else { + result = targetService.getClass().getMethod(event.getMethod()); + } + + //if method was not found go out with a bang + if (result == null) { + throw new NoSuchMethodException(String.format("No match for method [%s] %s", event.getMethod(), Arrays.toString(eventParamTypes))); + } + + return result; + } + + /** * Get the event type that this handler can handle. * * @return the remote service call event type.
