diff options
author | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2010-11-16 11:27:51 +0000 |
---|---|---|
committer | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2010-11-16 11:27:51 +0000 |
commit | 7fa97f7e83c044796e7068ede121ce29bc523837 (patch) | |
tree | 86d0772e120029945f9c233c4cc5fd61aeb4ecc6 /sca-java-2.x/trunk/modules/shell/src/main/java/org/apache/tuscany | |
parent | 6e96ac103fa3e53ddb1cf1d7a4a9516e7c58c23b (diff) |
Start adding invoke function to the Shell
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1035589 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules/shell/src/main/java/org/apache/tuscany')
-rw-r--r-- | sca-java-2.x/trunk/modules/shell/src/main/java/org/apache/tuscany/sca/shell/Shell.java | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/sca-java-2.x/trunk/modules/shell/src/main/java/org/apache/tuscany/sca/shell/Shell.java b/sca-java-2.x/trunk/modules/shell/src/main/java/org/apache/tuscany/sca/shell/Shell.java index d0cfc25e88..6a47a61284 100644 --- a/sca-java-2.x/trunk/modules/shell/src/main/java/org/apache/tuscany/sca/shell/Shell.java +++ b/sca-java-2.x/trunk/modules/shell/src/main/java/org/apache/tuscany/sca/shell/Shell.java @@ -26,6 +26,8 @@ import java.io.BufferedReader; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
@@ -178,6 +180,58 @@ public class Shell { return true;
}
+ boolean invoke(final List<String> toks) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
+ Node node = getNode();
+ if (getNode() == null) {
+ return true;
+ }
+ String serviceName = toks.get(0);
+ String operationName = toks.get(1);
+ String params[] = new String[toks.size()- 2];
+ System.arraycopy(toks.toArray(), 2, params, 0, params.length);
+ Method method = node.getClass().getMethod("getService", Class.class, String.class);
+ Object proxy = method.invoke(node, null, serviceName);
+ Object result = invoke(proxy, operationName, params);
+ out.println(result);
+ return true;
+ }
+
+ 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);
+ }
+
boolean listComposites(final String curi) {
if (getNode() == null) {
return true;
@@ -400,6 +454,12 @@ public class Shell { return installed(toks);
}
};
+ if (op.equalsIgnoreCase("invoke"))
+ return new Callable<Boolean>() {
+ public Boolean call() throws Exception {
+ return invoke(toks);
+ }
+ };
if (op.equalsIgnoreCase("load"))
return new Callable<Boolean>() {
public Boolean call() throws Exception {
|