From ba79133f08b3d06dd1475ed0a88aabc45689255a Mon Sep 17 00:00:00 2001 From: antelder Date: Wed, 17 Nov 2010 13:26:10 +0000 Subject: Get the Shell invoke function working and add a services command to show the available services git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1036030 13f79535-47bb-0310-9956-ffa450edef68 --- .../tuscany/sca/runtime/RuntimeComponent.java | 12 ++++ .../core/assembly/impl/RuntimeComponentImpl.java | 47 +++++++++++++ .../org/apache/tuscany/sca/node/impl/NodeImpl.java | 43 +----------- .../java/org/apache/tuscany/sca/shell/Shell.java | 82 +++++++++++++++++++--- 4 files changed, 132 insertions(+), 52 deletions(-) (limited to 'sca-java-2.x/trunk/modules') diff --git a/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/RuntimeComponent.java b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/RuntimeComponent.java index efc4b278b3..01b0111e59 100644 --- a/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/RuntimeComponent.java +++ b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/RuntimeComponent.java @@ -24,6 +24,7 @@ import java.util.List; import org.apache.tuscany.sca.assembly.Component; import org.apache.tuscany.sca.provider.ImplementationProvider; import org.apache.tuscany.sca.provider.PolicyProvider; +import org.oasisopen.sca.ServiceReference; /** * The runtime component interface. Provides the bridge between the @@ -81,4 +82,15 @@ public interface RuntimeComponent extends Component { * @return */ List getPolicyProviders(); + + + /** + * Returns a ServiceReference for a service provided by the component + * + * @param businessInterface the interface that will be used to invoke the service + * @param serviceName the name of the service + * @param the Java type of the business interface for the service + * @return an object that implements the business interface + */ + ServiceReference getServiceReference(Class businessInterface, String name); } diff --git a/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeComponentImpl.java b/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeComponentImpl.java index 6032005b9a..a71c823c11 100644 --- a/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeComponentImpl.java +++ b/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeComponentImpl.java @@ -22,6 +22,9 @@ package org.apache.tuscany.sca.core.assembly.impl; import java.util.ArrayList; import java.util.List; +import org.apache.tuscany.sca.assembly.ComponentService; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.CompositeService; import org.apache.tuscany.sca.assembly.impl.ComponentImpl; import org.apache.tuscany.sca.contribution.resolver.ModelResolver; import org.apache.tuscany.sca.contribution.resolver.ResolverExtension; @@ -31,6 +34,8 @@ import org.apache.tuscany.sca.provider.ImplementationProvider; import org.apache.tuscany.sca.provider.PolicyProvider; import org.apache.tuscany.sca.runtime.RuntimeComponent; import org.apache.tuscany.sca.runtime.RuntimeComponentContext; +import org.oasisopen.sca.ServiceReference; +import org.oasisopen.sca.ServiceRuntimeException; /** * @version $Rev$ $Date$ @@ -108,4 +113,46 @@ public class RuntimeComponentImpl extends ComponentImpl implements RuntimeCompon public String toString() { return getName(); } + + @Override + public ServiceReference getServiceReference(Class businessInterface, String serviceName) { + RuntimeComponentContext componentContext = null; + + // If the component is a composite, then we need to find the + // non-composite component that provides the requested service + if (getImplementation() instanceof Composite) { + for (ComponentService componentService : getServices()) { + String bindingName = null; + if (serviceName != null) { + int index = serviceName.indexOf('/'); + if (index != -1) { + bindingName = serviceName.substring(index + 1); + serviceName = serviceName.substring(0, index); + } + } + if (serviceName == null || serviceName.equals(componentService.getName())) { + CompositeService compositeService = (CompositeService)componentService.getService(); + if (compositeService != null) { + componentContext = + ((RuntimeComponent)compositeService.getPromotedComponent()).getComponentContext(); + serviceName = compositeService.getPromotedService().getName(); + if (bindingName != null) { + serviceName = serviceName + "/" + bindingName; + } + return componentContext.createSelfReference(businessInterface, serviceName); + } + break; + } + } + // No matching service found + throw new ServiceRuntimeException("Composite service not found: " + serviceName); + } else { + componentContext = getComponentContext(); + if (serviceName != null) { + return componentContext.createSelfReference(businessInterface, serviceName); + } else { + return componentContext.createSelfReference(businessInterface); + } + } + } } diff --git a/sca-java-2.x/trunk/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java b/sca-java-2.x/trunk/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java index 8eecca1564..71a4bb704a 100644 --- a/sca-java-2.x/trunk/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java +++ b/sca-java-2.x/trunk/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java @@ -28,9 +28,7 @@ import java.util.logging.Logger; import javax.xml.stream.XMLOutputFactory; import org.apache.tuscany.sca.assembly.Component; -import org.apache.tuscany.sca.assembly.ComponentService; import org.apache.tuscany.sca.assembly.Composite; -import org.apache.tuscany.sca.assembly.CompositeService; import org.apache.tuscany.sca.assembly.Endpoint; import org.apache.tuscany.sca.assembly.Service; import org.apache.tuscany.sca.context.CompositeContext; @@ -52,7 +50,6 @@ import org.apache.tuscany.sca.runtime.DomainRegistryFactory; import org.apache.tuscany.sca.runtime.EndpointRegistry; import org.apache.tuscany.sca.runtime.ExtensibleDomainRegistryFactory; import org.apache.tuscany.sca.runtime.RuntimeComponent; -import org.apache.tuscany.sca.runtime.RuntimeComponentContext; import org.apache.tuscany.sca.runtime.RuntimeComponentService; import org.oasisopen.sca.ServiceReference; import org.oasisopen.sca.ServiceRuntimeException; @@ -261,44 +258,8 @@ public class NodeImpl implements Node { if (component == null) { throw new ServiceUnavailableException("The service " + name + " has not been contributed to the domain"); } - RuntimeComponentContext componentContext = null; - - // If the component is a composite, then we need to find the - // non-composite component that provides the requested service - if (component.getImplementation() instanceof Composite) { - for (ComponentService componentService : component.getServices()) { - String bindingName = null; - if (serviceName != null) { - int index = serviceName.indexOf('/'); - if (index != -1) { - bindingName = serviceName.substring(index + 1); - serviceName = serviceName.substring(0, index); - } - } - if (serviceName == null || serviceName.equals(componentService.getName())) { - CompositeService compositeService = (CompositeService)componentService.getService(); - if (compositeService != null) { - componentContext = - ((RuntimeComponent)compositeService.getPromotedComponent()).getComponentContext(); - serviceName = compositeService.getPromotedService().getName(); - if (bindingName != null) { - serviceName = serviceName + "/" + bindingName; - } - return componentContext.createSelfReference(businessInterface, serviceName); - } - break; - } - } - // No matching service found - throw new ServiceRuntimeException("Composite service not found: " + name); - } else { - componentContext = ((RuntimeComponent)component).getComponentContext(); - if (serviceName != null) { - return componentContext.createSelfReference(businessInterface, serviceName); - } else { - return componentContext.createSelfReference(businessInterface); - } - } + + return ((RuntimeComponent)component).getServiceReference(businessInterface, serviceName); } public NodeConfiguration getConfiguration() { 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 6a47a61284..16fc6122b8 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 @@ -37,6 +37,7 @@ import java.util.Map; import java.util.concurrent.Callable; import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.Endpoint; import org.apache.tuscany.sca.common.java.io.IOHelper; import org.apache.tuscany.sca.contribution.Artifact; import org.apache.tuscany.sca.contribution.Contribution; @@ -44,7 +45,10 @@ import org.apache.tuscany.sca.contribution.processor.ContributionReadException; import org.apache.tuscany.sca.monitor.ValidationException; import org.apache.tuscany.sca.node2.Node; import org.apache.tuscany.sca.node2.NodeFactory; +import org.apache.tuscany.sca.node2.impl.NodeImpl; import org.apache.tuscany.sca.runtime.ActivationException; +import org.apache.tuscany.sca.runtime.EndpointRegistry; +import org.apache.tuscany.sca.runtime.RuntimeComponent; import org.apache.tuscany.sca.runtime.Version; import org.apache.tuscany.sca.shell.jline.JLine; @@ -60,9 +64,8 @@ public class Shell { private Map standaloneNodes = new HashMap(); private Map nodes = new HashMap(); - public static final String[] COMMANDS = new String[] {"bye", "domain", "domains", "help", "install", "installed", - "load", "remove", "run", "save", "start", "status", - "stop"}; + public static final String[] COMMANDS = new String[] {"bye", "domain", "domains", "help", "install", "installed", "invoke", + "load", "remove", "run", "save", "services", "start", "status", "stop"}; public static void main(final String[] args) throws Exception { boolean useJline = true; @@ -181,17 +184,27 @@ public class Shell { } boolean invoke(final List toks) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { - Node node = getNode(); - if (getNode() == null) { + String endpointName = toks.get(1); + String serviceName = null; + if (endpointName.contains("/")) { + int i = endpointName.indexOf("/"); + if (i < endpointName.length()-1) { + serviceName = endpointName.substring(i+1); + } + } + String operationName = toks.get(2); + String params[] = new String[toks.size()- 3]; + System.arraycopy(toks.toArray(), 3, params, 0, params.length); + + EndpointRegistry reg = ((NodeImpl)getNode()).getEndpointRegistry(); + List endpoints = reg.findEndpoint(endpointName); + if (endpoints.size() < 1) { + out.println(" no service found: " + endpointName); 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 proxy = ((RuntimeComponent)endpoints.get(0).getComponent()).getServiceReference(null, serviceName).getService(); Object result = invoke(proxy, operationName, params); + out.println(result); return true; } @@ -287,6 +300,17 @@ public class Shell { return true; } + boolean services() throws IOException { + if (getNode() == null) { + return true; + } + EndpointRegistry reg = ((NodeImpl)getNode()).getEndpointRegistry(); + for (Endpoint endpoint : reg.getEndpoints()) { + out.println(endpoint.getComponent().getURI() + "/" + endpoint.getService().getName()); + } + return true; + } + public boolean stop(List toks) throws ActivationException { String curi = toks.get(1); if (toks.size() > 2) { @@ -496,6 +520,12 @@ public class Shell { return stop(toks); } }; + if (op.equalsIgnoreCase("services")) + return new Callable() { + public Boolean call() throws Exception { + return services(); + } + }; if (op.equalsIgnoreCase("bye")) return new Callable() { public Boolean call() throws Exception { @@ -585,6 +615,8 @@ public class Shell { helpInstall(); } else if ("installed".equalsIgnoreCase(command)) { helpInstalled(); + } else if ("invoke".equalsIgnoreCase(command)) { + helpInvoke(); } else if ("load".equalsIgnoreCase(command)) { helpLoad(); } else if ("remove".equalsIgnoreCase(command)) { @@ -601,9 +633,12 @@ public class Shell { helpStop(); } else if ("startup".equalsIgnoreCase(command)) { helpStartUp(); + } else if ("services".equalsIgnoreCase(command)) { + helpServices(); } else if ("bye".equalsIgnoreCase(command)) { helpBye(); } + out.println(); return true; } @@ -621,10 +656,12 @@ public class Shell { out.println(" domains"); out.println(" install [] [-start] [-metadata ] [-duris ]"); out.println(" installed []"); + out.println(" invoke [/] [ ...]"); out.println(" load "); out.println(" remove "); out.println(" run "); out.println(" save "); + out.println(" services"); out.println(" start |"); out.println(" start [] [-duris ]"); out.println(" status [ []]"); @@ -698,6 +735,20 @@ public class Shell { out.println(" contributionURI - (optional) the URI of an installed contribution"); } + void helpInvoke() { + out.println(" invoke [/] [ ...]"); + out.println(); + out.println(" Invokes an operation of a component service."); + out.println(" (presently parameters and return values are limited to simple types)"); + out.println(); + out.println(" Arguments:"); + out.println(" component - (required) the name of the component"); + out.println(" service - (optional) the name of the component service, which may be omitted"); + out.println(" when the component has a single service."); + out.println(" operation - (required) the name of the operation"); + out.println(" args - (optional) the operation arguments"); + } + void helpLoad() { out.println(" load "); out.println(); @@ -740,6 +791,15 @@ public class Shell { out.println(" directoryPath - (required) the URL of a directory to be used to store the state."); } + void helpServices() { + out.println(" services"); + out.println(); + out.println(" Lists the components and services available in the Domain."); + out.println(); + out.println(" Arguments:"); + out.println(" none"); + } + void helpStart() { out.println(" start |"); out.println(" start [] [-duris ]"); -- cgit v1.2.3