summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/trunk/modules/node-launcher/src
diff options
context:
space:
mode:
authorrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2009-11-22 22:17:59 +0000
committerrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2009-11-22 22:17:59 +0000
commit3c6b7d709c7197078f8261f8e3464b0f217c988e (patch)
tree6c5461240129beb5ec2d08b1a66092c981e05534 /sca-java-2.x/trunk/modules/node-launcher/src
parenta4bf0d96a5e5df85e755e03317a2d5548d24abdd (diff)
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
Diffstat (limited to 'sca-java-2.x/trunk/modules/node-launcher/src')
-rw-r--r--sca-java-2.x/trunk/modules/node-launcher/src/main/java/org/apache/tuscany/sca/node/launcher/NodeLauncher.java63
-rw-r--r--sca-java-2.x/trunk/modules/node-launcher/src/test/java/org/apache/tuscany/sca/node/launcher/ServiceInvocationTestCase.java77
2 files changed, 139 insertions, 1 deletions
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 <compositeURI>]"
+ + " [-s <service>]"
+ " [-t <ttl>]"
+ " 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.
diff --git a/sca-java-2.x/trunk/modules/node-launcher/src/test/java/org/apache/tuscany/sca/node/launcher/ServiceInvocationTestCase.java b/sca-java-2.x/trunk/modules/node-launcher/src/test/java/org/apache/tuscany/sca/node/launcher/ServiceInvocationTestCase.java
new file mode 100644
index 0000000000..b1fe8770b0
--- /dev/null
+++ b/sca-java-2.x/trunk/modules/node-launcher/src/test/java/org/apache/tuscany/sca/node/launcher/ServiceInvocationTestCase.java
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.sca.node.launcher;
+
+import java.lang.reflect.InvocationTargetException;
+
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class ServiceInvocationTestCase {
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ }
+
+ public float add(float x, float y) {
+ return x + y;
+ }
+
+ @Test
+ public void testInvoke() throws Exception {
+ String service = "component1/service1#add(1.0, 2.0)";
+ Object proxy = this;
+ invoke(service, proxy);
+ }
+
+ private Object invoke(String service, Object proxy) throws IllegalAccessException, InvocationTargetException {
+ String regex = "(#|\\(|,|\\))";
+ if (service != null) {
+ // componentName/serviceName/bindingName#methodName(arg0, ..., agrN)
+ String tokens[] = service.split(regex);
+ String serviceName = tokens[0];
+ Assert.assertEquals("component1/service1", serviceName);
+ String operationName = tokens[1];
+ Assert.assertEquals("add", operationName);
+ String params[] = new String[tokens.length - 2];
+ System.arraycopy(tokens, 2, params, 0, params.length);
+ Object result = NodeLauncher.invoke(proxy, operationName, params);
+ Assert.assertEquals(new Float(3.0f), result);
+ return result;
+ }
+ return null;
+ }
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ }
+
+}