diff options
author | dims <dims@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-17 00:23:01 +0000 |
---|---|---|
committer | dims <dims@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-17 00:23:01 +0000 |
commit | bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a (patch) | |
tree | 38a92061c0793434c4be189f1d70c3458b6bc41d /branches/pre-spec-changes/test/src/main/java/org/apache |
Move Tuscany from Incubator to top level.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'branches/pre-spec-changes/test/src/main/java/org/apache')
16 files changed, 1059 insertions, 0 deletions
diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/ArtifactFactory.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/ArtifactFactory.java new file mode 100644 index 0000000000..a7dbe0f90f --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/ArtifactFactory.java @@ -0,0 +1,154 @@ +/* + * 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.test; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.spi.builder.Connector; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.Service; +import org.apache.tuscany.spi.idl.InvalidServiceContractException; +import org.apache.tuscany.spi.idl.java.JavaInterfaceProcessorRegistry; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.InboundInvocationChain; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.OutboundInvocationChain; +import org.apache.tuscany.spi.wire.OutboundWire; +import org.apache.tuscany.spi.wire.WireService; + +import org.apache.tuscany.core.builder.ConnectorImpl; +import org.apache.tuscany.core.component.WorkContextImpl; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; +import org.apache.tuscany.core.implementation.composite.ServiceImpl; +import org.apache.tuscany.core.wire.InboundInvocationChainImpl; +import org.apache.tuscany.core.wire.InboundWireImpl; +import org.apache.tuscany.core.wire.InvokerInterceptor; +import org.apache.tuscany.core.wire.OutboundInvocationChainImpl; +import org.apache.tuscany.core.wire.OutboundWireImpl; +import org.apache.tuscany.core.wire.jdk.JDKWireService; + +/** + * A factory for creating runtime artifacts to facilitate testing without directly instantiating core implementation + * classes + * + * @version $$Rev$$ $$Date$$ + */ +public final class ArtifactFactory { + + private ArtifactFactory() { + } + + public static Connector createConnector() { + return new ConnectorImpl(createWireService(), null, null, null); + } + + public static WireService createWireService() { + return new JDKWireService(new WorkContextImpl(), null); + } + + public static Service createService(String name, CompositeComponent parent, ServiceContract<?> contract) { + return new ServiceImpl(name, parent, contract); + } + + /** + * Creates an inbound wire. After a wire is returned, client code must call {@link + * #terminateWire(org.apache.tuscany.spi.wire.InboundWire)}. These two methods have been separated to allow wires to + * be decorated with interceptors or handlers prior to their completion + * + * @param serviceName the service name associated with the wire + * @param interfaze the interface associated with the wire + */ + public static <T> InboundWire createLocalInboundWire(String serviceName, Class<T> interfaze) + throws InvalidServiceContractException { + InboundWire wire = new InboundWireImpl(); + JavaInterfaceProcessorRegistry registry = new JavaInterfaceProcessorRegistryImpl(); + ServiceContract<?> contract = registry.introspect(interfaze); + wire.setServiceContract(contract); + wire.setServiceName(serviceName); + wire.addInvocationChains(createInboundChains(interfaze)); + return wire; + } + + /** + * Creates an outbound wire. After a wire is returned, client code must call {@link + * #terminateWire(org.apache.tuscany.spi.wire.OutboundWire)}. These two methods have been separated to allow wires + * to be decorated with interceptors or handlers prior to their completion + * + * @param refName the reference name the wire is associated with on the client + * @param interfaze the interface associated with the wire + */ + public static <T> OutboundWire createLocalOutboundWire(String refName, Class<T> interfaze) + throws InvalidServiceContractException { + OutboundWire wire = new OutboundWireImpl(); + wire.setReferenceName(refName); + wire.addInvocationChains(createOutboundChains(interfaze)); + JavaInterfaceProcessorRegistry registry = new JavaInterfaceProcessorRegistryImpl(); + ServiceContract<?> contract = registry.introspect(interfaze); + wire.setServiceContract(contract); + return wire; + } + + + /** + * Finalizes the target wire + */ + public static void terminateWire(InboundWire wire) { + for (InboundInvocationChain chain : wire.getInvocationChains().values()) { + // add tail interceptor + chain.addInterceptor(new InvokerInterceptor()); + } + } + + public static void terminateWire(OutboundWire wire) { + for (OutboundInvocationChain chain : wire.getInvocationChains().values()) { + // add tail interceptor + chain.addInterceptor(new InvokerInterceptor()); + } + } + + private static Map<Operation<?>, OutboundInvocationChain> createOutboundChains(Class<?> interfaze) + throws InvalidServiceContractException { + Map<Operation<?>, OutboundInvocationChain> invocations = new HashMap<Operation<?>, OutboundInvocationChain>(); + JavaInterfaceProcessorRegistry registry = new JavaInterfaceProcessorRegistryImpl(); + ServiceContract<?> contract = registry.introspect(interfaze); + for (Operation operation : contract.getOperations().values()) { + OutboundInvocationChain chain = new OutboundInvocationChainImpl(operation); + invocations.put(operation, chain); + } + return invocations; + } + + private static Map<Operation<?>, InboundInvocationChain> createInboundChains(Class<?> interfaze) + throws InvalidServiceContractException { + Map<Operation<?>, InboundInvocationChain> invocations = new HashMap<Operation<?>, InboundInvocationChain>(); + JavaInterfaceProcessorRegistry registry = new JavaInterfaceProcessorRegistryImpl(); + ServiceContract<?> contract = registry.introspect(interfaze); + for (Operation<?> operation : contract.getOperations().values()) { + InboundInvocationChain chain = new InboundInvocationChainImpl(operation); + // add tail interceptor + //chain.addInterceptor(new InvokerInterceptor()); + invocations.put(operation, chain); + } + return invocations; + } + + +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/SCATestCase.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/SCATestCase.java new file mode 100644 index 0000000000..829f99b6f9 --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/SCATestCase.java @@ -0,0 +1,183 @@ +/* + * 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.test; + +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.spi.bootstrap.ComponentNames; +import org.apache.tuscany.spi.builder.BuilderException; +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.Component; +import org.apache.tuscany.spi.component.ComponentException; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.SCAObject; +import org.apache.tuscany.spi.deployer.Deployer; +import org.apache.tuscany.spi.deployer.DeploymentMonitor; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.model.ComponentDefinition; +import org.apache.tuscany.spi.wire.WireService; + +import junit.framework.TestCase; +import org.apache.tuscany.api.TuscanyException; +import org.apache.tuscany.core.implementation.system.model.SystemCompositeImplementation; +import org.apache.tuscany.core.launcher.CompositeContextImpl; +import org.apache.tuscany.core.launcher.LauncherImpl; +import org.apache.tuscany.core.monitor.JavaLoggingMonitorFactory; +import org.apache.tuscany.host.MonitorFactory; +import org.apache.tuscany.host.runtime.InitializationException; + +import org.osoa.sca.CurrentCompositeContext; + +/** + * Base class for JUnit tests that want to run in an SCA client environment. + * + * @version $Rev$ $Date$ + */ +public abstract class SCATestCase extends TestCase { + protected CompositeComponent component; + private CompositeContextImpl context; + private Map<String, URL> extensions = new HashMap<String, URL>(); + private URL applicationSCDL; + private LauncherImpl launcher; + private MonitorFactory monitorFactory; + + protected void setUp() throws Exception { + super.setUp(); + if (monitorFactory == null) { + monitorFactory = new JavaLoggingMonitorFactory(); + } + ClassLoader cl = getClass().getClassLoader(); + launcher = new LauncherImpl(); + launcher.setApplicationLoader(cl); + URL scdl = cl.getResource(LauncherImpl.METAINF_SYSTEM_SCDL_PATH); + + try { + CompositeComponent composite = launcher.bootRuntime(scdl, monitorFactory); + for (String extensionName : extensions.keySet()) { + deployExtension(composite, extensionName, extensions.get(extensionName)); + } + + SCAObject wireServiceComponent = composite.getSystemChild(ComponentNames.TUSCANY_WIRE_SERVICE); + if (!(wireServiceComponent instanceof AtomicComponent)) { + throw new InitializationException("WireService must be an atomic component"); + } + + WireService wireService = (WireService) ((AtomicComponent) wireServiceComponent).getTargetInstance(); + + if (applicationSCDL == null) { + throw new RuntimeException("application SCDL not found: " + applicationSCDL); + } + component = launcher.bootApplication("application", applicationSCDL); + component.start(); + context = new CompositeContextImpl(component, wireService); + CurrentCompositeContext.setContext(context); + } catch (TuscanyException e) { + DeploymentMonitor monitor = monitorFactory.getMonitor(DeploymentMonitor.class); + monitor.deploymentError(e); + throw e; + } + + } + + /** + * A TestCase can use this to overide the default SCDL location of "META-INF/sca/default.scdl" + */ + protected void setApplicationSCDL(URL applicationSCDL) { + this.applicationSCDL = applicationSCDL; + } + + /** + * Set the application scdl based on the classpath entry for a class. Normally this will be a class in the + * production code associated with this test case. + * + * @param aClass a Class from which to determine the resource base url + * @param path location of the application SCDL relative to the base class + * @throws MalformedURLException if the path is malformed + */ + protected void setApplicationSCDL(Class<?> aClass, String path) throws MalformedURLException { + URL root = getRoot(aClass); + setApplicationSCDL(new URL(root, path)); + } + + /** + * A TestCase can use this to add the SCDL location of an extention to be deployed to the runtime + */ + protected void addExtension(String extensionName, URL extentionSCDL) { + extensions.put(extensionName, extentionSCDL); + } + + + /** + * Sets the monitor factory to use + * + * @param monitorFactory the monitor factory to use + */ + protected void setMonitorFactory(MonitorFactory monitorFactory) { + this.monitorFactory = monitorFactory; + } + + protected void deployExtension(CompositeComponent composite, String extensionName, URL scdlURL) + throws LoaderException, BuilderException, ComponentException, InitializationException { + SystemCompositeImplementation implementation = new SystemCompositeImplementation(); + implementation.setScdlLocation(scdlURL); + implementation.setClassLoader(new URLClassLoader(new URL[]{scdlURL}, getClass().getClassLoader())); + + ComponentDefinition<SystemCompositeImplementation> definition = + new ComponentDefinition<SystemCompositeImplementation>(extensionName, implementation); + + + SCAObject child = composite.getSystemChild(ComponentNames.TUSCANY_DEPLOYER); + if (!(child instanceof AtomicComponent)) { + throw new InitializationException("Deployer must be an atomic component"); + } + Deployer deployer = (Deployer) ((AtomicComponent) child).getTargetInstance(); + Component component = deployer.deploy(composite, definition); + component.start(); + } + + + protected static URL getRoot(Class<?> aClass) { + String name = aClass.getName(); + String classPath = "/" + name.replace('.', '/') + ".class"; + URL classURL = aClass.getResource(classPath); + assert classURL != null; + StringBuilder prefix = new StringBuilder(); + for (int i = 0; i < name.length(); i++) { + if (name.charAt(i) == '.') { + prefix.append("../"); + } + } + try { + return new URL(classURL, prefix.toString()); + } catch (MalformedURLException e) { + throw new AssertionError(); + } + } + + protected void tearDown() throws Exception { + CurrentCompositeContext.setContext(null); + component.stop(); + launcher.shutdownRuntime(); + super.tearDown(); + } +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/TestLauncher.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/TestLauncher.java new file mode 100644 index 0000000000..b66716bac0 --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/TestLauncher.java @@ -0,0 +1,34 @@ +/* + * 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.test; + +import java.io.File; + +import org.apache.tuscany.core.launcher.LauncherImpl; + +/** + * @version $Rev$ $Date$ + */ +public class TestLauncher extends LauncherImpl { + + public File getInstallDirectory() { + return new File("."); + } + +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingBuilder.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingBuilder.java new file mode 100644 index 0000000000..8354b0710a --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingBuilder.java @@ -0,0 +1,53 @@ +/* + * 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.test.binding; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.ReferenceBinding; +import org.apache.tuscany.spi.component.ServiceBinding; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.BindingBuilderExtension; +import org.apache.tuscany.spi.model.BoundReferenceDefinition; +import org.apache.tuscany.spi.model.BoundServiceDefinition; + +/** + * @version $Rev$ $Date$ + */ +public class TestBindingBuilder extends BindingBuilderExtension<TestBindingDefinition> { + + @SuppressWarnings("unchecked") + public ServiceBinding build(CompositeComponent parent, + BoundServiceDefinition definition, + TestBindingDefinition bindingDefinition, + DeploymentContext ctx) { + return new TestBindingServiceBinding(definition.getName(), parent); + } + + public ReferenceBinding build(CompositeComponent parent, + BoundReferenceDefinition definition, + TestBindingDefinition bindingDefinition, + DeploymentContext ctx) { + String name = definition.getName(); + return new TestReferenceBinding(name, parent); + } + + protected Class<TestBindingDefinition> getBindingType() { + return TestBindingDefinition.class; + } +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingDefinition.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingDefinition.java new file mode 100644 index 0000000000..19228fd86a --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingDefinition.java @@ -0,0 +1,32 @@ +/* + * 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.test.binding; + +import org.apache.tuscany.spi.model.BindingDefinition; + + +/** + * A simple binding for test purposes. + * + * @version $$Rev$$ $$Date$$ + */ +public class TestBindingDefinition extends BindingDefinition { + + +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingLoader.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingLoader.java new file mode 100644 index 0000000000..ef7a0ebb6f --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingLoader.java @@ -0,0 +1,57 @@ +/* + * 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.test.binding; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.osoa.sca.Version; +import org.osoa.sca.annotations.Constructor; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.LoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.apache.tuscany.spi.model.ModelObject; + +/** + * @version $Rev$ $Date$ + */ +public class TestBindingLoader extends LoaderExtension<TestBindingDefinition> { + + public static final QName BINDING_TEST = new QName(Version.XML_NAMESPACE_1_0, "binding.test"); + + @Constructor + public TestBindingLoader(@Autowire LoaderRegistry registry) { + super(registry); + } + + public QName getXMLType() { + return BINDING_TEST; + } + + public TestBindingDefinition load(CompositeComponent parent, + ModelObject object, XMLStreamReader reader, + DeploymentContext context) throws XMLStreamException, LoaderException { + return new TestBindingDefinition(); + } +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingRuntimeException.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingRuntimeException.java new file mode 100644 index 0000000000..88c62d1420 --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingRuntimeException.java @@ -0,0 +1,24 @@ +package org.apache.tuscany.test.binding; + +import org.apache.tuscany.api.TuscanyRuntimeException; + +/** + * @version $Rev$ $Date$ + */ +public class TestBindingRuntimeException extends TuscanyRuntimeException { + + public TestBindingRuntimeException() { + } + + public TestBindingRuntimeException(String message) { + super(message); + } + + public TestBindingRuntimeException(String message, Throwable cause) { + super(message, cause); + } + + public TestBindingRuntimeException(Throwable cause) { + super(cause); + } +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingServiceBinding.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingServiceBinding.java new file mode 100644 index 0000000000..341be523a3 --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingServiceBinding.java @@ -0,0 +1,43 @@ +/* + * 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.test.binding; + +import javax.xml.namespace.QName; + +import org.osoa.sca.Version; + +import org.apache.tuscany.spi.CoreRuntimeException; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.extension.ServiceBindingExtension; + +/** + * @version $Rev$ $Date$ + */ +public class TestBindingServiceBinding extends ServiceBindingExtension { + private static final QName BINDING_TEST = new QName(Version.XML_NAMESPACE_1_0, "binding.socket"); + + public TestBindingServiceBinding(String name, CompositeComponent parent) throws CoreRuntimeException { + super(name, parent); + // do nothing, but this could register with the host environment + } + + public QName getBindingType() { + return BINDING_TEST; + } +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestInvoker.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestInvoker.java new file mode 100644 index 0000000000..c3b02024af --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestInvoker.java @@ -0,0 +1,49 @@ +package org.apache.tuscany.test.binding; + +import java.lang.reflect.InvocationTargetException; + +import org.apache.tuscany.spi.wire.InvocationRuntimeException; +import org.apache.tuscany.spi.wire.Message; +import org.apache.tuscany.spi.wire.TargetInvoker; + +/** + * @version $Rev$ $Date$ + */ +public class TestInvoker implements TargetInvoker { + + private boolean cacheable; + + public boolean isCacheable() { + return cacheable; + } + + public void setCacheable(boolean cacheable) { + this.cacheable = cacheable; + } + + public boolean isOptimizable() { + return isCacheable(); + } + + public Object invokeTarget(final Object payload, final short sequence) throws InvocationTargetException { + // echo back the result, a real binding would invoke some API for flowing the request + return ((Object[]) payload)[0]; + } + + public Message invoke(Message msg) throws InvocationRuntimeException { + try { + Object resp = invokeTarget(msg.getBody(), TargetInvoker.NONE); + msg.setBody(resp); + } catch (InvocationTargetException e) { + msg.setBodyWithFault(e.getCause()); + } catch (Throwable e) { + msg.setBodyWithFault(e); + } + return msg; + } + + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } + +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestReferenceBinding.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestReferenceBinding.java new file mode 100644 index 0000000000..025cfb1e1e --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestReferenceBinding.java @@ -0,0 +1,30 @@ +package org.apache.tuscany.test.binding; + +import javax.xml.namespace.QName; + +import org.osoa.sca.Version; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.extension.ReferenceBindingExtension; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.TargetInvoker; + +/** + * @version $Rev$ $Date$ + */ +public class TestReferenceBinding extends ReferenceBindingExtension { + private static final QName BINDING_TEST = new QName(Version.XML_NAMESPACE_1_0, "binding.socket"); + + public TestReferenceBinding(String name, CompositeComponent parent) { + super(name, parent); + } + + public QName getBindingType() { + return BINDING_TEST; + } + + public TargetInvoker createTargetInvoker(ServiceContract contract, Operation operation) { + return new TestInvoker(); + } +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingBuilder.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingBuilder.java new file mode 100644 index 0000000000..020bdd3ef9 --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingBuilder.java @@ -0,0 +1,37 @@ +package org.apache.tuscany.test.binding; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.ReferenceBinding; +import org.apache.tuscany.spi.component.ServiceBinding; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.BindingBuilderExtension; +import org.apache.tuscany.spi.model.BoundReferenceDefinition; +import org.apache.tuscany.spi.model.BoundServiceDefinition; + +/** + * @version $Rev$ $Date$ + */ +public class TestSocketBindingBuilder extends BindingBuilderExtension<TestSocketBindingDefinition> { + + public ServiceBinding build(CompositeComponent parent, + BoundServiceDefinition definition, + TestSocketBindingDefinition bindingDefinition, + DeploymentContext ctx) { + int port = bindingDefinition.getPort(); + return new TestSocketBindingServiceBinding(definition.getName(), port, parent); + } + + public ReferenceBinding build(CompositeComponent parent, + BoundReferenceDefinition definition, + TestSocketBindingDefinition bindingDefinition, + DeploymentContext ctx) { + String name = definition.getName(); + int port = bindingDefinition.getPort(); + String host = bindingDefinition.getHost(); + return new TestSocketReferenceBinding(name, host, port, parent); + } + + protected Class<TestSocketBindingDefinition> getBindingType() { + return TestSocketBindingDefinition.class; + } +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingDefinition.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingDefinition.java new file mode 100644 index 0000000000..fa5f7b2c57 --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingDefinition.java @@ -0,0 +1,27 @@ +package org.apache.tuscany.test.binding; + +import org.apache.tuscany.spi.model.BindingDefinition; + +/** + * A simple socket-based binding. Service operations may onyl take one parameter that is <code>Serializable</code> + * + * @version $$Rev$$ $$Date$$ + */ +public class TestSocketBindingDefinition extends BindingDefinition { + private String host; + private int port; + + public TestSocketBindingDefinition(String host, int port) { + this.host = host; + this.port = port; + } + + public String getHost() { + return host; + } + + public int getPort() { + return port; + } + +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingLoader.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingLoader.java new file mode 100644 index 0000000000..cdec616c35 --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingLoader.java @@ -0,0 +1,46 @@ +package org.apache.tuscany.test.binding; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.osoa.sca.Version; +import org.osoa.sca.annotations.Constructor; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.LoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.apache.tuscany.spi.model.ModelObject; + +/** + * @version $Rev$ $Date$ + */ +public class TestSocketBindingLoader extends LoaderExtension<TestSocketBindingDefinition> { + + public static final QName BINDING_TEST = new QName(Version.XML_NAMESPACE_1_0, "binding.socket"); + + @Constructor + public TestSocketBindingLoader(@Autowire LoaderRegistry registry) { + super(registry); + } + + public QName getXMLType() { + return TestSocketBindingLoader.BINDING_TEST; + } + + public TestSocketBindingDefinition load(CompositeComponent parent, + ModelObject object, XMLStreamReader reader, + DeploymentContext context) throws XMLStreamException, LoaderException { + String host = reader.getAttributeValue(null, "host"); + int port; + try { + port = Integer.parseInt(reader.getAttributeValue(null, "port")); + } catch (NumberFormatException e) { + throw new LoaderException("Invalid port specified", e); + } + return new TestSocketBindingDefinition(host, port); + } +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingServiceBinding.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingServiceBinding.java new file mode 100644 index 0000000000..a1531b6f5f --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingServiceBinding.java @@ -0,0 +1,129 @@ +package org.apache.tuscany.test.binding; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import javax.xml.namespace.QName; + +import org.osoa.sca.Version; + +import org.apache.tuscany.spi.CoreRuntimeException; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.extension.ServiceBindingExtension; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.wire.InboundInvocationChain; +import org.apache.tuscany.spi.wire.Message; +import org.apache.tuscany.spi.wire.MessageImpl; + +/** + * Implements a very simple remote, socket-based binding for test purposes. This binding exposes serviceBindings using a socket + * on a given port. Service operations must take only one paramter that is <code>Serializable</code>. + * + * @version $Rev$ $Date$ + */ +public class TestSocketBindingServiceBinding extends ServiceBindingExtension { + private static final QName BINDING_TEST = new QName(Version.XML_NAMESPACE_1_0, "binding.socket"); + private int port; + private ServerSocket socket; + private ExecutorService executor; + private TestSocketBindingServiceBinding.ServerRunnable runnable; + + public TestSocketBindingServiceBinding(String name, int port, CompositeComponent parent) throws CoreRuntimeException { + super(name, parent); + this.port = port; + } + + public QName getBindingType() { + return BINDING_TEST; + } + + public void start() { + executor = Executors.newSingleThreadExecutor(); + // create a listener, note that a work scheduler should normally be used to spawn work in different threads + runnable = new ServerRunnable(); + executor.execute(runnable); + } + + public void stop() { + try { + runnable.setEnd(true); + socket.close(); + executor.shutdownNow(); + } catch (IOException e) { + throw new TestBindingRuntimeException(e); + } + } + + /** + * Creates a socket listener in another thread which handles one client at a time. For a real binding, a work + * scheduler should be used + */ + private class ServerRunnable implements Runnable { + + private boolean end; + + public void setEnd(boolean end) { + this.end = end; + } + + public void run() { + Socket clientSocket; + ObjectInputStream is = null; + ObjectOutputStream os = null; + try { + socket = new ServerSocket(port); + } catch (IOException e) { + throw new TestBindingRuntimeException(e); + } + while (!end) { + try { + clientSocket = socket.accept(); + is = new ObjectInputStream(clientSocket.getInputStream()); + String operation = is.readUTF(); + int argn = is.readInt(); + Object[] args = new Object[argn]; + for (int i = 0; i < argn; i++) { + args[i] = is.readObject(); + } + Map<Operation<?>, InboundInvocationChain> chains = getInboundWire().getInvocationChains(); + for (InboundInvocationChain chain : chains.values()) { + if (chain.getOperation().getName().equals(operation)) { + Message message = new MessageImpl(); + message.setTargetInvoker(chain.getTargetInvoker()); + message.setBody(args); + message = chain.getHeadInterceptor().invoke(message); + os = new ObjectOutputStream(clientSocket.getOutputStream()); + os.writeObject(message.getBody()); + os.flush(); + } + } + } catch (IOException e) { + throw new TestBindingRuntimeException(e); + } catch (ClassNotFoundException e) { + throw new TestBindingRuntimeException(e); + } finally { + if (os != null) { + try { + os.close(); + } catch (IOException e) { + // ingore + } + } + if (is != null) { + try { + is.close(); + } catch (IOException e) { + // ignore + } + } + } + } + } + } +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketInvoker.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketInvoker.java new file mode 100644 index 0000000000..15ebd672df --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketInvoker.java @@ -0,0 +1,123 @@ +package org.apache.tuscany.test.binding; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.lang.reflect.InvocationTargetException; +import java.net.Socket; +import java.net.UnknownHostException; + +import org.apache.tuscany.spi.wire.InvocationRuntimeException; +import org.apache.tuscany.spi.wire.Message; +import org.apache.tuscany.spi.wire.TargetInvoker; + +/** + * Responsible for serializing an operation parameter flowing the invocation through the socket + * + * @version $Rev$ $Date$ + */ +public class TestSocketInvoker implements TargetInvoker { + private String host; + private int port; + private String operation; + + private boolean cacheable; + + public TestSocketInvoker(String host, int port, String operation) { + this.host = host; + this.port = port; + this.operation = operation; + } + + public boolean isCacheable() { + return cacheable; + } + + public void setCacheable(boolean cacheable) { + this.cacheable = cacheable; + } + + public boolean isOptimizable() { + return isCacheable(); + } + + public Message invoke(Message msg) throws InvocationRuntimeException { + try { + Object resp = invokeTarget(msg.getBody(), TargetInvoker.NONE); + msg.setBody(resp); + } catch (InvocationTargetException e) { + msg.setBodyWithFault(e.getCause()); + } catch (Throwable e) { + msg.setBodyWithFault(e); + } + return msg; + } + + /** + * Sends the payload over a socket + */ + public Object invokeTarget(final Object object, final short sequence) throws InvocationTargetException { + int argn; + if (object == null) { + argn = 0; + } else if (!object.getClass().isArray()) { + argn = 1; + } else { + argn = ((Object[])object).length; + } + + Socket socket = null; + ObjectOutputStream os = null; + ObjectInputStream is = null; + try { + socket = new Socket(host, port); + os = new ObjectOutputStream(socket.getOutputStream()); + os.writeUTF(operation); + os.writeInt(argn); + for (int i=0; i<argn; i++) { + if (!object.getClass().isArray()) { + os.writeObject(object); + } + else { + os.writeObject(((Object[])object)[i]); + } + } + os.flush(); + is = new ObjectInputStream(socket.getInputStream()); + return is.readObject(); + } catch (ClassNotFoundException e) { + throw new InvocationTargetException(e); + } catch (UnknownHostException e) { + throw new InvocationTargetException(e); + } catch (IOException e) { + throw new InvocationTargetException(e); + } finally { + if (os != null) { + try { + os.close(); + } catch (IOException e) { + // ignore + } + } + if (is != null) { + try { + is.close(); + } catch (IOException e) { + // ignore + } + } + if (socket != null) { + try { + socket.close(); + } catch (IOException e) { + // ignore + } + } + } + } + + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } + +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketReferenceBinding.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketReferenceBinding.java new file mode 100644 index 0000000000..ac9cb9c479 --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketReferenceBinding.java @@ -0,0 +1,38 @@ +package org.apache.tuscany.test.binding; + +import javax.xml.namespace.QName; + +import org.osoa.sca.Version; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.extension.ReferenceBindingExtension; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.TargetInvoker; + +/** + * @version $Rev$ $Date$ + */ +public class TestSocketReferenceBinding extends ReferenceBindingExtension { + private static final QName BINDING_TEST = new QName(Version.XML_NAMESPACE_1_0, "binding.socket"); + + private String host; + private int port; + + public TestSocketReferenceBinding(String name, + String host, + int port, + CompositeComponent parent) { + super(name, parent); + this.port = port; + this.host = host; + } + + public QName getBindingType() { + return BINDING_TEST; + } + + public TargetInvoker createTargetInvoker(ServiceContract contract, Operation operation) { + return new TestSocketInvoker(host, port, operation.getName()); + } +} |