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
This commit is contained in:
rfeng 2009-11-22 22:17:59 +00:00
parent a4bf0d96a5
commit 3c6b7d709c
2 changed files with 139 additions and 1 deletions

View file

@ -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.

View file

@ -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 {
}
}