Repository: karaf-cellar Updated Branches: refs/heads/master 778fcd4c2 -> c05e1e0f7
[KARAF-3646] Remote service method loockup inheritance support Project: http://git-wip-us.apache.org/repos/asf/karaf-cellar/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf-cellar/commit/2b4997c7 Tree: http://git-wip-us.apache.org/repos/asf/karaf-cellar/tree/2b4997c7 Diff: http://git-wip-us.apache.org/repos/asf/karaf-cellar/diff/2b4997c7 Branch: refs/heads/master Commit: 2b4997c7b217b0d1c6d5297f6f4f0aa397a45bc6 Parents: 100c327 Author: Alberto São Marcos <[email protected]> Authored: Wed Apr 8 16:03:05 2015 +0100 Committer: Alberto São Marcos <[email protected]> Committed: Wed Apr 8 16:08:55 2015 +0100 ---------------------------------------------------------------------- .../cellar/dosgi/RemoteServiceCallHandler.java | 50 +++++++++++++++++--- 1 file changed, 43 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/2b4997c7/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 dc0f37e..e879f7f 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 @@ -85,13 +85,7 @@ public class RemoteServiceCallHandler extends CellarSupport implements EventHand } 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()); RemoteServiceResult result = new RemoteServiceResult(event.getId()); result.setResult(obj); @@ -111,6 +105,48 @@ 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 && remoteMethod.getParameterTypes()[i].isAssignableFrom(eventParamTypes[i]); + } + if (allParamsFound) { + result = remoteMethod; + } + // if already found a matching method, no need to continue looking for one + if (result != null) { + break; + } + } + } + } else { + result = targetService.getClass().getMethod(event.getMethod()); + } + + //if method was not found go out with a bang + if (result == null) { + throw new NoSuchMethodException(); + } + + return result; + } + + /** * Get the event type that this handler can handle. * * @return the remote service call event type.
