diff options
Diffstat (limited to 'sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo')
7 files changed, 504 insertions, 0 deletions
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooBinding.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooBinding.java new file mode 100644 index 0000000000..75aabff105 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooBinding.java @@ -0,0 +1,59 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed 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.container.java.mock.binding.foo; + +import org.apache.tuscany.model.assembly.AssemblyModelContext; +import org.apache.tuscany.model.assembly.AssemblyModelVisitor; +import org.apache.tuscany.model.assembly.Binding; +import org.apache.tuscany.model.assembly.ModelInitException; + +/** + * Represents a mock binding that echoes back a single parameter + * + * @version $Rev$ $Date$ + */ +public class FooBinding implements Binding { + + public FooBinding() { + } + + public String getURI() { + return null; + } + + public void setURI(String value) { + } + + public void initialize(AssemblyModelContext modelContext) throws ModelInitException { + } + + public void freeze() { + } + + public boolean accept(AssemblyModelVisitor visitor) { + return false; + } + + private Object config; + + public void setRuntimeConfiguration(Object configuration) { + config = configuration; + } + + public Object getRuntimeConfiguration() { + System.out.println("retting"); + return config; + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooBindingBuilder.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooBindingBuilder.java new file mode 100644 index 0000000000..248f7e54bc --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooBindingBuilder.java @@ -0,0 +1,192 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed 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.container.java.mock.binding.foo; + +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.apache.tuscany.core.builder.BuilderException; +import org.apache.tuscany.core.builder.ObjectFactory; +import org.apache.tuscany.core.builder.RuntimeConfigurationBuilder; +import org.apache.tuscany.core.builder.impl.EntryPointRuntimeConfiguration; +import org.apache.tuscany.core.builder.impl.HierarchicalBuilder; +import org.apache.tuscany.core.config.JavaIntrospectionHelper; +import org.apache.tuscany.core.context.Context; +import org.apache.tuscany.core.context.QualifiedName; +import org.apache.tuscany.core.injection.ObjectCreationException; +import org.apache.tuscany.core.invocation.InvocationConfiguration; +import org.apache.tuscany.core.invocation.ProxyConfiguration; +import org.apache.tuscany.core.invocation.impl.InvokerInterceptor; +import org.apache.tuscany.core.invocation.spi.ProxyFactory; +import org.apache.tuscany.core.invocation.spi.ProxyFactoryFactory; +import org.apache.tuscany.core.message.MessageFactory; +import org.apache.tuscany.core.runtime.RuntimeContext; +import org.apache.tuscany.core.system.annotation.Autowire; +import org.apache.tuscany.model.assembly.AssemblyModelObject; +import org.apache.tuscany.model.assembly.ConfiguredService; +import org.apache.tuscany.model.assembly.EntryPoint; +import org.apache.tuscany.model.assembly.ExternalService; +import org.apache.tuscany.model.assembly.Service; +import org.apache.tuscany.model.assembly.ServiceContract; +import org.osoa.sca.annotations.Init; +import org.osoa.sca.annotations.Scope; + +/** + * Creates a <code>RuntimeConfigurationBuilder</code> for an entry point or external service configured with the + * {@link FooBinding} + * + * @version $Rev$ $Date$ + */ +@Scope("MODULE") +public class FooBindingBuilder implements RuntimeConfigurationBuilder { + + @Autowire + private RuntimeContext runtimeContext; + + private ProxyFactoryFactory proxyFactoryFactory; + + private MessageFactory messageFactory; + + /* the top-level builder responsible for evaluating policies */ + private HierarchicalBuilder policyBuilder = new HierarchicalBuilder(); + + public FooBindingBuilder() { + } + + @Init(eager = true) + public void init() { + runtimeContext.addBuilder(this); + } + + /** + * @param runtimeContext The runtimeContext to set. + */ + @Autowire + public void setRuntimeContext(RuntimeContext runtimeContext) { + this.runtimeContext = runtimeContext; + } + + /** + * Sets the factory used to construct proxies implmementing the business interface required by a reference + */ + @Autowire + public void setProxyFactoryFactory(ProxyFactoryFactory factory) { + this.proxyFactoryFactory = factory; + } + + /** + * Sets the factory used to construct invocation messages + * + * @param msgFactory + */ + @Autowire + public void setMessageFactory(MessageFactory msgFactory) { + this.messageFactory = msgFactory; + } + + /** + * Adds a builder responsible for creating source-side and target-side invocation chains for a reference. The + * reference builder may be hierarchical, containing other child reference builders that operate on specific + * metadata used to construct and invocation chain. + */ + public void addPolicyBuilder(RuntimeConfigurationBuilder builder) { + policyBuilder.addBuilder(builder); + } + + public void build(AssemblyModelObject object, Context context) throws BuilderException { + if (object instanceof EntryPoint) { + EntryPoint ep = (EntryPoint) object; + if (ep.getBindings().size() < 1 || !(ep.getBindings().get(0) instanceof FooBinding)) { + return; + } + EntryPointRuntimeConfiguration config = new FooEntryPointRuntimeConfiguration(ep.getName(), ep.getConfiguredService() + .getService().getName(), messageFactory); + + ConfiguredService configuredService = ep.getConfiguredService(); + Service service = configuredService.getService(); + ServiceContract serviceContract = service.getServiceContract(); + Map<Method, InvocationConfiguration> iConfigMap = new HashMap(); + ProxyFactory proxyFactory = proxyFactoryFactory.createProxyFactory(); + Set<Method> javaMethods = JavaIntrospectionHelper.getAllUniqueMethods(serviceContract.getInterface()); + for (Method method : javaMethods) { + InvocationConfiguration iConfig = new InvocationConfiguration(method); + iConfigMap.put(method, iConfig); + } + QualifiedName qName = new QualifiedName(ep.getConfiguredReference().getTargetConfiguredServices().get(0).getAggregatePart().getName() + "/" + service.getName()); + ProxyConfiguration pConfiguration = new ProxyConfiguration(qName, iConfigMap, serviceContract.getInterface().getClassLoader(), messageFactory); + proxyFactory.setBusinessInterface(serviceContract.getInterface()); + proxyFactory.setProxyConfiguration(pConfiguration); + config.addSourceProxyFactory(service.getName(), proxyFactory); + configuredService.setProxyFactory(proxyFactory); + if (policyBuilder != null) { + // invoke the reference builder to handle additional policy metadata + policyBuilder.build(configuredService, context); + } + // add tail interceptor + for (InvocationConfiguration iConfig : (Collection<InvocationConfiguration>) iConfigMap.values()) { + iConfig.addTargetInterceptor(new InvokerInterceptor()); + } + ep.getConfiguredReference().setRuntimeConfiguration(config); + + } else if (object instanceof ExternalService) { + ExternalService es = (ExternalService) object; + if (es.getBindings().size() < 1 || !(es.getBindings().get(0) instanceof FooBinding)) { + return; + } + + FooExternalServiceRuntimeConfiguration config = new FooExternalServiceRuntimeConfiguration(es.getName(), + new FooClientFactory()); + + ConfiguredService configuredService = es.getConfiguredService(); + Service service = configuredService.getService(); + ServiceContract serviceContract = service.getServiceContract(); + Map<Method, InvocationConfiguration> iConfigMap = new HashMap(); + ProxyFactory proxyFactory = proxyFactoryFactory.createProxyFactory(); + Set<Method> javaMethods = JavaIntrospectionHelper.getAllUniqueMethods(serviceContract.getInterface()); + for (Method method : javaMethods) { + InvocationConfiguration iConfig = new InvocationConfiguration(method); + iConfigMap.put(method, iConfig); + } + QualifiedName qName = new QualifiedName(es.getName() + "/" + service.getName()); + ProxyConfiguration pConfiguration = new ProxyConfiguration(qName, iConfigMap, serviceContract.getInterface().getClassLoader(), messageFactory); + proxyFactory.setBusinessInterface(serviceContract.getInterface()); + proxyFactory.setProxyConfiguration(pConfiguration); + config.addTargetProxyFactory(service.getName(), proxyFactory); + configuredService.setProxyFactory(proxyFactory); + if (policyBuilder != null) { + // invoke the reference builder to handle additional policy metadata + policyBuilder.build(configuredService, context); + } + // add tail interceptor + for (InvocationConfiguration iConfig : (Collection<InvocationConfiguration>) iConfigMap.values()) { + iConfig.addTargetInterceptor(new InvokerInterceptor()); + } + + es.getConfiguredService().setRuntimeConfiguration(config); + } else { + return; + + } + } + + private class FooClientFactory implements ObjectFactory { + + public Object getInstance() throws ObjectCreationException { + return new FooClient(); + } + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooBindingWireBuilder.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooBindingWireBuilder.java new file mode 100644 index 0000000000..bba70e4c84 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooBindingWireBuilder.java @@ -0,0 +1,56 @@ +package org.apache.tuscany.container.java.mock.binding.foo; + +import org.apache.tuscany.core.builder.BuilderConfigException; +import org.apache.tuscany.core.builder.WireBuilder; +import org.apache.tuscany.core.context.ScopeContext; +import org.apache.tuscany.core.invocation.InvocationConfiguration; +import org.apache.tuscany.core.invocation.spi.ProxyFactory; +import org.apache.tuscany.core.runtime.RuntimeContext; +import org.apache.tuscany.core.system.annotation.Autowire; +import org.osoa.sca.annotations.Init; + +public class FooBindingWireBuilder implements WireBuilder { + + public FooBindingWireBuilder() { + super(); + } + + private RuntimeContext runtimeContext; + + @Autowire + public void setRuntimeContext(RuntimeContext context) { + runtimeContext = context; + } + + @Init(eager=true) + public void init() { + runtimeContext.addBuilder(this); + } + + + public void connect(ProxyFactory sourceFactory, ProxyFactory targetFactory, Class targetType, boolean downScope, + ScopeContext targetScopeContext) throws BuilderConfigException { + if (!FooExternalServiceRuntimeConfiguration.class.isAssignableFrom(targetType)) { + return; + } + for (InvocationConfiguration sourceInvocationConfig : sourceFactory.getProxyConfiguration().getInvocationConfigurations() + .values()) { + FooESTargetInvoker invoker = new FooESTargetInvoker(sourceFactory.getProxyConfiguration().getTargetName() + .getPartName(), targetScopeContext); + sourceInvocationConfig.setTargetInvoker(invoker); + // if (downScope) { + // // the source scope is shorter than the target, so the invoker can cache the target instance + // invoker.setCacheable(true); + // } else { + // invoker.setCacheable(false); + // } + } + + } + + public void completeTargetChain(ProxyFactory targetFactory, Class targetType, ScopeContext targetScopeContext) + throws BuilderConfigException { + // TODO implement + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooClient.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooClient.java new file mode 100644 index 0000000000..9f67eb7786 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooClient.java @@ -0,0 +1,37 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed 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.container.java.mock.binding.foo; + +import java.lang.reflect.Array; + +import org.apache.tuscany.core.context.TargetException; + +/** + * A mock client for a transport binding + * + * @version $Rev$ $Date$ + */ +public class FooClient { + + public FooClient() { + } + + public Object invoke(Object msg) { + if (msg!=null && msg.getClass().isArray() && Array.getLength(msg) == 1){ + return Array.get(msg,0); + }else{ + throw new TargetException("This binding only understands operations with a single parameter"); + } + } +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooESTargetInvoker.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooESTargetInvoker.java new file mode 100644 index 0000000000..6dd42ef4d6 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooESTargetInvoker.java @@ -0,0 +1,97 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed 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.container.java.mock.binding.foo; + +import java.lang.reflect.InvocationTargetException; + +import org.apache.tuscany.core.context.ExternalServiceContext; +import org.apache.tuscany.core.context.InstanceContext; +import org.apache.tuscany.core.context.ScopeContext; +import org.apache.tuscany.core.context.TargetException; +import org.apache.tuscany.core.invocation.Interceptor; +import org.apache.tuscany.core.invocation.TargetInvoker; +import org.apache.tuscany.core.message.Message; + +/** + * Responsible for invoking a mock transport binding client configured for an external service + * + * @version $Rev$ $Date$ + */ +public class FooESTargetInvoker implements TargetInvoker { + + private String name; + + private ScopeContext container; + + private ExternalServiceContext context; + + public FooESTargetInvoker(String esName, ScopeContext container) { + assert (esName != null) : "No external service name specified"; + assert (container != null) : "No scope container specified"; + name = esName; + this.container = container; + } + + public Object invokeTarget(Object payload) throws InvocationTargetException { + if (context == null) { + InstanceContext iContext = container.getContext(name); + if (!(iContext instanceof ExternalServiceContext)) { + TargetException te = new TargetException("Unexpected target context type"); + te.setIdentifier(iContext.getClass().getName()); + te.addContextName(iContext.getName()); + throw te; + } + context = (ExternalServiceContext) iContext; + } + FooClient client = (FooClient) context.getImplementationInstance(true); + if (payload != null) { + return client.invoke(payload); + } else { + return client.invoke(null); + } + } + + public boolean isCacheable() { + return false; + } + + public Message invoke(Message msg) { + try { + Object resp = invokeTarget(msg.getBody()); + msg.setBody(resp); + } catch (InvocationTargetException e) { + msg.setBody(e.getCause()); + } catch (Throwable e) { + msg.setBody(e); + } + return msg; + } + + public void setNext(Interceptor next) { + throw new UnsupportedOperationException(); + } + + public Object clone() { + try { + FooESTargetInvoker invoker = (FooESTargetInvoker) super.clone(); + invoker.container = container; + invoker.context = this.context; + invoker.name = this.name; + return invoker; + } catch (CloneNotSupportedException e) { + return null; // will not happen + } + } + +} diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooEntryPointRuntimeConfiguration.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooEntryPointRuntimeConfiguration.java new file mode 100644 index 0000000000..bee388127e --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooEntryPointRuntimeConfiguration.java @@ -0,0 +1,31 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed 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.container.java.mock.binding.foo; + +import org.apache.tuscany.core.builder.impl.EntryPointRuntimeConfiguration; +import org.apache.tuscany.core.message.MessageFactory; + +/** + * + * + * @version $Rev$ $Date$ + */ +public class FooEntryPointRuntimeConfiguration extends EntryPointRuntimeConfiguration { + + public FooEntryPointRuntimeConfiguration(String name, String serviceName, MessageFactory msgFactory) { + super(name, serviceName, msgFactory); + } + +} + diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooExternalServiceRuntimeConfiguration.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooExternalServiceRuntimeConfiguration.java new file mode 100644 index 0000000000..e06a78d529 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/test/java/org/apache/tuscany/container/java/mock/binding/foo/FooExternalServiceRuntimeConfiguration.java @@ -0,0 +1,32 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed 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.container.java.mock.binding.foo; + +import org.apache.tuscany.core.builder.ObjectFactory; +import org.apache.tuscany.core.builder.impl.BaseExternalServiceRuntimeConfiguration; + +/** + * Creates instances of {@link org.apache.tuscany.core.context.ExternalServiceContext} configured with the appropriate + * invocation chains and bindings. This implementation serves as a marker for + * {@link org.apache.tuscany.container.java.mock.binding.foo.FooBindingWireBuilder} + * + * @version $Rev$ $Date$ + */ +public class FooExternalServiceRuntimeConfiguration extends BaseExternalServiceRuntimeConfiguration { + + public FooExternalServiceRuntimeConfiguration(String name, ObjectFactory objectFactory) { + super(name, objectFactory); + } + +} |