From 3c6b7d709c7197078f8261f8e3464b0f217c988e Mon Sep 17 00:00:00 2001 From: rfeng Date: Sun, 22 Nov 2009 22:17:59 +0000 Subject: Add an option to invoke a service after the node is started git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@883155 13f79535-47bb-0310-9956-ffa450edef68 --- .../tuscany/sca/node/launcher/NodeLauncher.java | 63 +++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) (limited to 'sca-java-2.x/trunk/modules/node-launcher/src/main/java/org/apache/tuscany/sca/node') diff --git a/sca-java-2.x/trunk/modules/node-launcher/src/main/java/org/apache/tuscany/sca/node/launcher/NodeLauncher.java b/sca-java-2.x/trunk/modules/node-launcher/src/main/java/org/apache/tuscany/sca/node/launcher/NodeLauncher.java index d8df30377b..36fa9e6a21 100644 --- a/sca-java-2.x/trunk/modules/node-launcher/src/main/java/org/apache/tuscany/sca/node/launcher/NodeLauncher.java +++ b/sca-java-2.x/trunk/modules/node-launcher/src/main/java/org/apache/tuscany/sca/node/launcher/NodeLauncher.java @@ -23,6 +23,8 @@ import static org.apache.tuscany.sca.node.launcher.NodeLauncherUtil.node; import java.io.File; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.util.List; @@ -134,8 +136,9 @@ public class NodeLauncher { options.addOption(opt2); Option opt3 = new Option("t", "ttl", true, "Time to live"); opt3.setArgName("timeToLiveInMilliseconds"); - // opt4.setType(long.class); options.addOption(opt3); + Option opt4 = new Option("s", "service", true, "Service to invoke (componentName/serviceName#operation(arg0,...,argN)"); + options.addOption(opt4); return options; } @@ -181,6 +184,7 @@ public class NodeLauncher { formatter.setSyntaxPrefix("Usage: "); formatter.printHelp("java " + NodeLauncher.class.getName() + " [-c ]" + + " [-s ]" + " [-t ]" + " contribution1 ... contributionN", options); return; } @@ -202,6 +206,26 @@ public class NodeLauncher { } logger.info("SCA Node is now started."); + String service = cli.getOptionValue("service"); + String regex = "(#|\\(|,|\\))"; + if (service != null) { + // componentName/serviceName/bindingName#methodName(arg0, ..., agrN) + String tokens[] = service.split(regex); + String serviceName = tokens[0]; + String operationName = tokens[1]; + String params[] = new String[tokens.length - 2]; + System.arraycopy(tokens, 2, params, 0, params.length); + logger.info("Invoking service: " + service); + Method method = node.getClass().getMethod("getService", Class.class, String.class); + Object proxy = method.invoke(node, null, serviceName); + + Object result = invoke(proxy, operationName, params); + if (result != null) { + logger.info("Result is: " + result); + } + break; + } + // Install a shutdown hook shutdown = new ShutdownThread(node); Runtime.getRuntime().addShutdownHook(shutdown); @@ -266,6 +290,43 @@ public class NodeLauncher { } } } + + static Object invoke(Object proxy, String operationName, String... params) throws IllegalAccessException, + InvocationTargetException { + for (Method m : proxy.getClass().getMethods()) { + if (m.getName().equals(operationName) && m.getParameterTypes().length == params.length) { + Object parameters[] = new Object[params.length]; + int i = 0; + for (Class type : m.getParameterTypes()) { + if (type == byte.class || type == Byte.class) { + parameters[i] = Byte.valueOf(params[i]); + } else if (type == char.class || type == Character.class) { + parameters[i] = params[i].charAt(0); + } else if (type == boolean.class || type == Boolean.class) { + parameters[i] = Boolean.valueOf(params[i]); + } else if (type == short.class || type == Short.class) { + parameters[i] = Short.valueOf(params[i]); + } else if (type == int.class || type == Integer.class) { + parameters[i] = Integer.valueOf(params[i]); + } else if (type == long.class || type == Long.class) { + parameters[i] = Long.valueOf(params[i]); + } else if (type == float.class || type == Float.class) { + parameters[i] = Float.valueOf(params[i]); + } else if (type == double.class || type == Double.class) { + parameters[i] = Double.valueOf(params[i]); + } else if (type == String.class) { + parameters[i] = params[i]; + } else { + throw new IllegalArgumentException("Parameter type is not supported: " + type); + } + i++; + } + Object result = m.invoke(proxy, parameters); + return result; + } + } + throw new IllegalArgumentException("Invalid service operation: " + operationName); + } /** * Stop the given node. -- cgit v1.2.3