diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2008-12-05 22:32:00 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2008-12-05 22:32:00 +0000 |
commit | 960ef736d702d87ede50b097e191f6336c9be7a4 (patch) | |
tree | c2a207865d919ce42b16ee2179c68be645f15b3b /java/sca | |
parent | 592d4446a5c63ecd06eea498ef63bd6b8ff10766 (diff) |
Further cleanup for ComponentContext, CompositeContext
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@723904 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca')
14 files changed, 553 insertions, 397 deletions
diff --git a/java/sca/modules/core-spi/src/main/java/org/apache/tuscany/sca/context/ComponentContextFactory.java b/java/sca/modules/core-spi/src/main/java/org/apache/tuscany/sca/context/ComponentContextFactory.java index ed4b57639c..b71fe4dd59 100644 --- a/java/sca/modules/core-spi/src/main/java/org/apache/tuscany/sca/context/ComponentContextFactory.java +++ b/java/sca/modules/core-spi/src/main/java/org/apache/tuscany/sca/context/ComponentContextFactory.java @@ -31,8 +31,7 @@ public interface ComponentContextFactory { * Create an instance of ComponentContext * * @param component The runtime component - * @param requestContextFactory The factory to create RequestContext * @return An instance of ComponentContext for the component */ - ComponentContext createComponentContext(RuntimeComponent component, RequestContextFactory requestContextFactory); + ComponentContext createComponentContext(RuntimeComponent component); } diff --git a/java/sca/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/RuntimeComponentContext.java b/java/sca/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/RuntimeComponentContext.java index 10f3c19550..8d13bbe569 100644 --- a/java/sca/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/RuntimeComponentContext.java +++ b/java/sca/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/RuntimeComponentContext.java @@ -25,7 +25,6 @@ import java.io.Writer; import org.apache.tuscany.sca.assembly.Binding; import org.apache.tuscany.sca.assembly.ComponentService; -import org.apache.tuscany.sca.context.PropertyValueFactory; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.osoa.sca.CallableReference; import org.osoa.sca.ComponentContext; @@ -105,5 +104,4 @@ public interface RuntimeComponentContext extends ComponentContext { <B> ServiceReference<B> createSelfReference(Class<B> businessInterface, ComponentService service); ExtensionPointRegistry getExtensionPointRegistry(); - void setPropertyValueFactory(PropertyValueFactory propertyValueFactory); } diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/CompositeActivator.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/CompositeActivator.java index 24be299581..56b7442f51 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/CompositeActivator.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/CompositeActivator.java @@ -113,20 +113,6 @@ public interface CompositeActivator { CompositeContext getCompositeContext(); /** - * Get the proxy factory - * @return - */ - ProxyFactory getProxyFactory(); - - /** - * Get the java interface factory - * @return - */ - JavaInterfaceFactory getJavaInterfaceFactory(); - - ConversationManager getConversationManager(); - - /** * Configure the runtime component with component context * @param component */ diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/CompositeActivatorImpl.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/CompositeActivatorImpl.java index 5d8fdecc2f..67edf3ab9d 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/CompositeActivatorImpl.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/CompositeActivatorImpl.java @@ -37,21 +37,20 @@ import org.apache.tuscany.sca.assembly.Implementation; import org.apache.tuscany.sca.assembly.OptimizableBinding; import org.apache.tuscany.sca.assembly.Reference; import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.context.ComponentContextFactory; import org.apache.tuscany.sca.context.ContextFactoryExtensionPoint; +import org.apache.tuscany.sca.context.PropertyValueFactory; import org.apache.tuscany.sca.context.RequestContextFactory; -import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.core.UtilityExtensionPoint; import org.apache.tuscany.sca.core.assembly.ActivationException; import org.apache.tuscany.sca.core.assembly.CompositeActivator; import org.apache.tuscany.sca.core.context.CompositeContext; -import org.apache.tuscany.sca.core.context.impl.ComponentContextImpl; +import org.apache.tuscany.sca.core.context.impl.CompositeContextImpl; import org.apache.tuscany.sca.core.conversation.ConversationManager; -import org.apache.tuscany.sca.core.invocation.ExtensibleProxyFactory; import org.apache.tuscany.sca.core.invocation.ExtensibleWireProcessor; import org.apache.tuscany.sca.core.invocation.ProxyFactory; -import org.apache.tuscany.sca.core.invocation.ProxyFactoryExtensionPoint; import org.apache.tuscany.sca.core.scope.Scope; import org.apache.tuscany.sca.core.scope.ScopeContainer; import org.apache.tuscany.sca.core.scope.ScopeRegistry; @@ -97,16 +96,20 @@ public class CompositeActivatorImpl implements CompositeActivator { private final ProviderFactoryExtensionPoint providerFactories; private final EndpointResolverFactoryExtensionPoint endpointResolverFactories; + private final ComponentContextFactory componentContextFactory; private final RequestContextFactory requestContextFactory; private final ProxyFactory proxyFactory; private final JavaInterfaceFactory javaInterfaceFactory; + private final PropertyValueFactory propertyValueFactory; + private final ConversationManager conversationManager; - private final CompositeContext componentContextHelper; + private final CompositeContext compositeContext; private Composite domainComposite; public CompositeActivatorImpl(ExtensionPointRegistry extensionPoints) { + this.compositeContext = new CompositeContextImpl(extensionPoints); FactoryExtensionPoint factories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); this.assemblyFactory = factories.getFactory(AssemblyFactory.class); this.messageFactory = factories.getFactory(MessageFactory.class); @@ -117,14 +120,13 @@ public class CompositeActivatorImpl implements CompositeActivator { this.wireProcessor = new ExtensibleWireProcessor(extensionPoints.getExtensionPoint(RuntimeWireProcessorExtensionPoint.class)); this.providerFactories = extensionPoints.getExtensionPoint(ProviderFactoryExtensionPoint.class); this.endpointResolverFactories = extensionPoints.getExtensionPoint(EndpointResolverFactoryExtensionPoint.class); - this.javaInterfaceFactory = factories.getFactory(JavaInterfaceFactory.class); + this.javaInterfaceFactory = compositeContext.getJavaInterfaceFactory(); + this.propertyValueFactory = factories.getFactory(PropertyValueFactory.class); ContextFactoryExtensionPoint contextFactories = extensionPoints.getExtensionPoint(ContextFactoryExtensionPoint.class); + this.componentContextFactory = contextFactories.getFactory(ComponentContextFactory.class); this.requestContextFactory = contextFactories.getFactory(RequestContextFactory.class); - ProxyFactoryExtensionPoint proxyFactories = extensionPoints.getExtensionPoint(ProxyFactoryExtensionPoint.class); - proxyFactory = new ExtensibleProxyFactory(proxyFactories); - this.conversationManager = utilities.getUtility(ConversationManager.class); - StAXArtifactProcessorExtensionPoint processors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); - this.componentContextHelper = new CompositeContext(extensionPoints); + proxyFactory = compositeContext.getProxyFactory(); + this.conversationManager = compositeContext.getConversationManager(); } /** @@ -651,9 +653,7 @@ public class CompositeActivatorImpl implements CompositeActivator { * @param runtimeComponent */ public void configureComponentContext(RuntimeComponent runtimeComponent) { - RuntimeComponentContext componentContext = - new ComponentContextImpl(this, assemblyFactory, proxyFactory, interfaceContractMapper, - requestContextFactory, javaInterfaceFactory, runtimeComponent); + RuntimeComponentContext componentContext = (RuntimeComponentContext) componentContextFactory.createComponentContext(runtimeComponent); runtimeComponent.setComponentContext(componentContext); } @@ -1025,14 +1025,7 @@ public class CompositeActivatorImpl implements CompositeActivator { * @return the referenceHelper */ public CompositeContext getCompositeContext() { - return componentContextHelper; - } - - /** - * @return the proxyFactory - */ - public ProxyFactory getProxyFactory() { - return proxyFactory; + return compositeContext; } /** @@ -1075,19 +1068,5 @@ public class CompositeActivatorImpl implements CompositeActivator { } return null; } - - /** - * @return the javaInterfaceFactory - */ - public JavaInterfaceFactory getJavaInterfaceFactory() { - return javaInterfaceFactory; - } - - /** - * @return the conversationManager - */ - public ConversationManager getConversationManager() { - return conversationManager; - } } diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ComponentContextExt.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ComponentContextExt.java index cb61c57f27..6db3af213d 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ComponentContextExt.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ComponentContextExt.java @@ -27,4 +27,5 @@ import org.apache.tuscany.sca.runtime.RuntimeComponentContext; */ public interface ComponentContextExt extends RuntimeComponentContext { CompositeActivator getCompositeActivator(); + CompositeContext getCompositeContext(); } diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/CompositeContext.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/CompositeContext.java index 76de8a2aaa..c68b05e8fb 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/CompositeContext.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/CompositeContext.java @@ -21,39 +21,21 @@ package org.apache.tuscany.sca.core.context; import java.io.IOException; import java.io.Reader; -import java.io.StringReader; -import java.io.StringWriter; import java.io.Writer; import java.util.ArrayList; import java.util.List; -import javax.xml.namespace.QName; -import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamReader; -import javax.xml.stream.XMLStreamWriter; -import org.apache.tuscany.sca.assembly.AssemblyFactory; -import org.apache.tuscany.sca.assembly.Binding; import org.apache.tuscany.sca.assembly.Component; import org.apache.tuscany.sca.assembly.ComponentReference; import org.apache.tuscany.sca.assembly.ComponentService; -import org.apache.tuscany.sca.assembly.Composite; -import org.apache.tuscany.sca.assembly.Multiplicity; -import org.apache.tuscany.sca.assembly.OptimizableBinding; -import org.apache.tuscany.sca.assembly.Reference; -import org.apache.tuscany.sca.assembly.Service; -import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; -import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; import org.apache.tuscany.sca.core.ExtensionPointRegistry; -import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.core.assembly.CompositeActivator; -import org.apache.tuscany.sca.core.context.impl.ComponentContextImpl; +import org.apache.tuscany.sca.core.conversation.ConversationManager; +import org.apache.tuscany.sca.core.invocation.ProxyFactory; import org.apache.tuscany.sca.core.invocation.ThreadMessageContext; -import org.apache.tuscany.sca.interfacedef.Interface; -import org.apache.tuscany.sca.interfacedef.InterfaceContract; import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException; -import org.apache.tuscany.sca.interfacedef.java.JavaInterface; import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; import org.apache.tuscany.sca.invocation.Message; import org.apache.tuscany.sca.runtime.EndpointReference; @@ -65,20 +47,7 @@ import org.osoa.sca.ServiceRuntimeException; /** * @version $Rev$ $Date$ */ -public class CompositeContext { - private final ExtensionPointRegistry extensionPointRegistry; - private final AssemblyFactory assemblyFactory; - private final JavaInterfaceFactory javaInterfaceFactory; - private final StAXArtifactProcessorExtensionPoint staxProcessors; - - public CompositeContext(ExtensionPointRegistry registry) { - this.extensionPointRegistry = registry; - FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); - this.assemblyFactory = factories.getFactory(AssemblyFactory.class); - this.javaInterfaceFactory = factories.getFactory(JavaInterfaceFactory.class); - this.staxProcessors = registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); - } - +public abstract class CompositeContext { /** * Create a self-reference for a component service * @param component @@ -86,66 +55,10 @@ public class CompositeContext { * @throws CloneNotSupportedException * @throws InvalidInterfaceException */ - public ComponentReference createSelfReference(Component component, - ComponentService service, - Class<?> businessInterface) throws CloneNotSupportedException, - InvalidInterfaceException { - ComponentReference componentReference = assemblyFactory.createComponentReference(); - componentReference.setName("$self$." + service.getName()); - for (Binding binding : service.getBindings()) { - if (binding instanceof OptimizableBinding) { - OptimizableBinding optimizableBinding = (OptimizableBinding)((OptimizableBinding)binding).clone(); - optimizableBinding.setTargetBinding(binding); - optimizableBinding.setTargetComponent(component); - optimizableBinding.setTargetComponentService(service); - componentReference.getBindings().add(optimizableBinding); - } else { - componentReference.getBindings().add(binding); - } - } - - componentReference.setCallback(service.getCallback()); - componentReference.getTargets().add(service); - componentReference.getPolicySets().addAll(service.getPolicySets()); - componentReference.getRequiredIntents().addAll(service.getRequiredIntents()); - - InterfaceContract interfaceContract = service.getInterfaceContract(); - Service componentTypeService = service.getService(); - if (componentTypeService != null && componentTypeService.getInterfaceContract() != null) { - interfaceContract = componentTypeService.getInterfaceContract(); - } - interfaceContract = getInterfaceContract(interfaceContract, businessInterface); - componentReference.setInterfaceContract(interfaceContract); - componentReference.setMultiplicity(Multiplicity.ONE_ONE); - // component.getReferences().add(componentReference); - return componentReference; - } - - /** - * @param interfaceContract - * @param businessInterface - * @return - * @throws CloneNotSupportedException - * @throws InvalidInterfaceException - */ - private InterfaceContract getInterfaceContract(InterfaceContract interfaceContract, Class<?> businessInterface) - throws CloneNotSupportedException, InvalidInterfaceException { - Interface interfaze = interfaceContract.getInterface(); - boolean compatible = false; - if (interfaze instanceof JavaInterface) { - Class<?> cls = ((JavaInterface)interfaze).getJavaClass(); - if (businessInterface.isAssignableFrom(cls)) { - compatible = true; - } - } - if (!compatible) { - // The interface is not assignable from the interface contract - interfaceContract = (InterfaceContract)interfaceContract.clone(); - interfaceContract.setInterface(javaInterfaceFactory.createJavaInterface(businessInterface)); - } - - return interfaceContract; - } + public abstract ComponentReference createSelfReference(Component component, + ComponentService service, + Class<?> businessInterface) + throws CloneNotSupportedException, InvalidInterfaceException; /** * Bind a component reference to a component service @@ -157,108 +70,79 @@ public class CompositeContext { * @throws CloneNotSupportedException * @throws InvalidInterfaceException */ - public RuntimeComponentReference bindComponentReference(Class<?> businessInterface, - RuntimeComponentReference reference, - RuntimeComponent component, - RuntimeComponentService service) - throws CloneNotSupportedException, InvalidInterfaceException { - RuntimeComponentReference ref = (RuntimeComponentReference)reference.clone(); - InterfaceContract interfaceContract = reference.getInterfaceContract(); - Reference componentTypeReference = reference.getReference(); - if (componentTypeReference != null && componentTypeReference.getInterfaceContract() != null) { - interfaceContract = componentTypeReference.getInterfaceContract(); - } - InterfaceContract refInterfaceContract = getInterfaceContract(interfaceContract, businessInterface); - if (refInterfaceContract != interfaceContract) { - ref = (RuntimeComponentReference)reference.clone(); - ref.setInterfaceContract(interfaceContract); - } - ref.setComponent(component); - ref.getTargets().add(service); - ref.getBindings().clear(); - for (Binding binding : service.getBindings()) { - if (binding instanceof OptimizableBinding) { - OptimizableBinding optimizableBinding = (OptimizableBinding)((OptimizableBinding)binding).clone(); - optimizableBinding.setTargetBinding(binding); - optimizableBinding.setTargetComponent(component); - optimizableBinding.setTargetComponentService(service); - ref.getBindings().add(optimizableBinding); - } else { - ref.getBindings().add(binding); - } - } - return ref; - } - - public void write(Component component, ComponentReference reference, Writer writer) throws IOException { - write(component, reference, null, writer); - } + public abstract RuntimeComponentReference bindComponentReference(Class<?> businessInterface, + RuntimeComponentReference reference, + RuntimeComponent component, + RuntimeComponentService service) + throws CloneNotSupportedException, InvalidInterfaceException; - public void write(Component component, ComponentReference reference, ComponentService service, Writer writer) throws IOException { - try { - StAXArtifactProcessor<Composite> processor = staxProcessors.getProcessor(Composite.class); - Composite composite = assemblyFactory.createComposite(); - composite.setName(new QName("http://tuscany.apache.org/xmlns/sca/1.0", "default")); - Component comp = assemblyFactory.createComponent(); - comp.setName("default"); - comp.setURI(component.getURI()); - composite.getComponents().add(comp); - if (reference != null) { - comp.getReferences().add(reference); - } - if (service != null) { - comp.getServices().add(service); - } + /** + * @param component + * @param reference + * @param writer + * @throws IOException + */ + public abstract void write(Component component, ComponentReference reference, Writer writer) throws IOException; - XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); - XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer); - processor.write(composite, streamWriter); - } catch (Exception e) { - throw new IOException(e.getMessage()); - } - } + /** + * @param component + * @param reference + * @param service + * @param writer + * @throws IOException + */ + public abstract void write(Component component, + ComponentReference reference, + ComponentService service, + Writer writer) throws IOException; - public String toXML(Component component, ComponentReference reference) throws IOException { - StringWriter writer = new StringWriter(); - write(component, reference, writer); - return writer.toString(); - } + /** + * @param component + * @param reference + * @return + * @throws IOException + */ + public abstract String toXML(Component component, ComponentReference reference) throws IOException; - public String toXML(Component component, ComponentService service) throws IOException { - StringWriter writer = new StringWriter(); - write(component, null, service, writer); - return writer.toString(); - } + /** + * @param component + * @param service + * @return + * @throws IOException + */ + public abstract String toXML(Component component, ComponentService service) throws IOException; - public RuntimeComponent read(Reader reader) throws IOException { - try { - XMLInputFactory inputFactory = XMLInputFactory.newInstance(); - XMLStreamReader streamReader = inputFactory.createXMLStreamReader(reader); - return read(streamReader); - } catch (Exception e) { - throw new IOException(e.getMessage()); - } - } + /** + * @param reader + * @return + * @throws IOException + */ + public abstract RuntimeComponent read(Reader reader) throws IOException; - public RuntimeComponent read(XMLStreamReader streamReader) throws IOException { - try { - StAXArtifactProcessor<Composite> processor = staxProcessors.getProcessor(Composite.class); - Composite composite = processor.read(streamReader); - RuntimeComponent component = (RuntimeComponent)composite.getComponents().get(0); - return component; - } catch (Exception e) { - throw new IOException(e.getMessage()); - } - } + /** + * @param streamReader + * @return + * @throws IOException + */ + public abstract RuntimeComponent read(XMLStreamReader streamReader) throws IOException; - public Component fromXML(String xml) throws IOException { - return read(new StringReader(xml)); - } + /** + * @param xml + * @return + * @throws IOException + */ + public abstract Component fromXML(String xml) throws IOException; - public Component fromXML(XMLStreamReader streamReader) throws IOException { - return read(streamReader); - } + /** + * @param streamReader + * @return + * @throws IOException + */ + public abstract Component fromXML(XMLStreamReader streamReader) throws IOException; + /** + * @return + */ public static RuntimeComponent getCurrentComponent() { Message message = ThreadMessageContext.getMessageContext(); if (message != null) { @@ -272,15 +156,21 @@ public class CompositeContext { return null; } + /** + * @return + */ public static CompositeActivator getCurrentCompositeActivator() { RuntimeComponent component = getCurrentComponent(); if (component != null) { - ComponentContextImpl context = (ComponentContextImpl)component.getComponentContext(); + ComponentContextExt context = (ComponentContextExt)component.getComponentContext(); return context.getCompositeActivator(); } return null; } + /** + * @return + */ public static CompositeContext getCurrentCompositeContext() { CompositeActivator activator = getCurrentCompositeActivator(); if (activator != null) { @@ -316,8 +206,20 @@ public class CompositeContext { return targetService; } - public ExtensionPointRegistry getExtensionPointRegistry() { - return extensionPointRegistry; - } + public abstract ExtensionPointRegistry getExtensionPointRegistry(); + + public abstract ConversationManager getConversationManager(); + + /** + * Get the java interface factory + * @return + */ + public abstract JavaInterfaceFactory getJavaInterfaceFactory(); + + /** + * Get the proxy factory + * @return + */ + public abstract ProxyFactory getProxyFactory(); } diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/DefaultComponentContextFactory.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/DefaultComponentContextFactory.java index 51da89499d..f1f808d285 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/DefaultComponentContextFactory.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/DefaultComponentContextFactory.java @@ -19,20 +19,9 @@ package org.apache.tuscany.sca.core.context; -import org.apache.tuscany.sca.assembly.AssemblyFactory; import org.apache.tuscany.sca.context.ComponentContextFactory; -import org.apache.tuscany.sca.context.ContextFactoryExtensionPoint; -import org.apache.tuscany.sca.context.RequestContextFactory; import org.apache.tuscany.sca.core.ExtensionPointRegistry; -import org.apache.tuscany.sca.core.FactoryExtensionPoint; -import org.apache.tuscany.sca.core.UtilityExtensionPoint; -import org.apache.tuscany.sca.core.assembly.CompositeActivator; import org.apache.tuscany.sca.core.context.impl.ComponentContextImpl; -import org.apache.tuscany.sca.core.invocation.ExtensibleProxyFactory; -import org.apache.tuscany.sca.core.invocation.ProxyFactory; -import org.apache.tuscany.sca.core.invocation.ProxyFactoryExtensionPoint; -import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; -import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; import org.apache.tuscany.sca.runtime.RuntimeComponent; import org.osoa.sca.ComponentContext; @@ -40,50 +29,14 @@ import org.osoa.sca.ComponentContext; * @version $Rev$ $Date$ */ public class DefaultComponentContextFactory implements ComponentContextFactory { - private final CompositeActivator compositeActivator; - private final RequestContextFactory requestContextFactory; - private final ProxyFactory proxyFactory; - private final AssemblyFactory assemblyFactory; - private final JavaInterfaceFactory javaInterfaceFactory; - private final InterfaceContractMapper interfaceContractMapper; + private final ExtensionPointRegistry registry; public DefaultComponentContextFactory(ExtensionPointRegistry registry) { - FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); - this.assemblyFactory = factories.getFactory(AssemblyFactory.class); - this.javaInterfaceFactory = factories.getFactory(JavaInterfaceFactory.class); - UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); - this.compositeActivator = utilities.getUtility(CompositeActivator.class); - this.interfaceContractMapper = utilities.getUtility(InterfaceContractMapper.class); - this.requestContextFactory = - registry.getExtensionPoint(ContextFactoryExtensionPoint.class).getFactory(RequestContextFactory.class); - this.proxyFactory = new ExtensibleProxyFactory(registry.getExtensionPoint(ProxyFactoryExtensionPoint.class)); + this.registry = registry; } - public DefaultComponentContextFactory(CompositeActivator compositeActivator, - AssemblyFactory assemblyFactory, - ProxyFactory proxyFactory, - InterfaceContractMapper interfaceContractMapper, - RequestContextFactory requestContextFactory, - JavaInterfaceFactory javaInterfaceFactory) { - super(); - this.compositeActivator = compositeActivator; - this.assemblyFactory = assemblyFactory; - this.proxyFactory = proxyFactory; - this.requestContextFactory = requestContextFactory; - this.javaInterfaceFactory = javaInterfaceFactory; - this.interfaceContractMapper = interfaceContractMapper; - } - - /** - * @see org.apache.tuscany.sca.context.ComponentContextFactory#createComponentContext(org.apache.tuscany.sca.runtime.RuntimeComponent, org.apache.tuscany.sca.context.RequestContextFactory) - */ - public ComponentContext createComponentContext(RuntimeComponent component, - RequestContextFactory requestContextFactory) { - if (requestContextFactory == null) { - requestContextFactory = this.requestContextFactory; - } - return new ComponentContextImpl(compositeActivator, assemblyFactory, proxyFactory, interfaceContractMapper, - requestContextFactory, javaInterfaceFactory, component); + public ComponentContext createComponentContext(RuntimeComponent component) { + return new ComponentContextImpl(registry, component); } } diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CallableReferenceImpl.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CallableReferenceImpl.java index 901a642f1c..71410b7e35 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CallableReferenceImpl.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CallableReferenceImpl.java @@ -88,7 +88,7 @@ public class CallableReferenceImpl<B> implements CallableReferenceExt<B> { private transient RuntimeComponentReference clonedRef; private transient ReferenceParameters refParams; private transient XMLStreamReader xmlReader; - + private transient RuntimeWire endpointWire; /* @@ -138,7 +138,7 @@ public class CallableReferenceImpl<B> implements CallableReferenceExt<B> { // FIXME: Should we normalize the componentName/serviceName URI into an absolute SCA URI in the SCA binding? // sca:component1/component11/component112/service1? this.compositeActivator = compositeActivator; - this.conversationManager = this.compositeActivator.getConversationManager(); + this.conversationManager = this.compositeActivator.getCompositeContext().getConversationManager(); initCallbackID(); } @@ -151,7 +151,7 @@ public class CallableReferenceImpl<B> implements CallableReferenceExt<B> { public RuntimeWire getRuntimeWire() { try { resolve(); - if (endpointWire != null){ + if (endpointWire != null) { return endpointWire; } else if (reference != null) { return reference.getRuntimeWire(binding); @@ -165,15 +165,15 @@ public class CallableReferenceImpl<B> implements CallableReferenceExt<B> { protected void bind(RuntimeWire wire) { if (wire != null) { - - if (wire instanceof EndpointWireImpl){ + + if (wire instanceof EndpointWireImpl) { endpointWire = wire; } this.component = wire.getSource().getComponent(); this.reference = (RuntimeComponentReference)wire.getSource().getContract(); this.binding = wire.getSource().getBinding(); this.compositeActivator = ((ComponentContextExt)component.getComponentContext()).getCompositeActivator(); - this.conversationManager = this.compositeActivator.getConversationManager(); + this.conversationManager = this.compositeActivator.getCompositeContext().getConversationManager(); initCallbackID(); } } @@ -189,7 +189,7 @@ public class CallableReferenceImpl<B> implements CallableReferenceExt<B> { public B getProxy() throws ObjectCreationException { try { if (proxy == null) { - proxy = createProxy(); + proxy = createProxy(); } return businessInterface.cast(proxy); } catch (Exception e) { @@ -197,13 +197,13 @@ public class CallableReferenceImpl<B> implements CallableReferenceExt<B> { } } - public void setProxy(Object proxy) { - this.proxy = proxy; - } + public void setProxy(Object proxy) { + this.proxy = proxy; + } protected Object createProxy() throws Exception { return proxyFactory.createProxy(this); - } + } public B getService() { try { @@ -220,12 +220,12 @@ public class CallableReferenceImpl<B> implements CallableReferenceExt<B> { return businessInterface; } catch (Exception e) { throw new ServiceRuntimeException(e); - } + } } public boolean isConversational() { try { - resolve(); + resolve(); return reference == null ? false : reference.getInterfaceContract().getInterface().isConversational(); } catch (Exception e) { throw new ServiceRuntimeException(e); @@ -237,12 +237,12 @@ public class CallableReferenceImpl<B> implements CallableReferenceExt<B> { // resolve from XML just in case this CallableReference is the result of // passing a CallableReference as a parameter resolve(); - + if (conversation == null || conversation.getState() == ConversationState.ENDED) { conversation = null; } return conversation; - + } catch (Exception e) { throw new ServiceRuntimeException(e); } @@ -250,11 +250,11 @@ public class CallableReferenceImpl<B> implements CallableReferenceExt<B> { public Object getCallbackID() { try { - resolve(); + resolve(); return callbackID; } catch (Exception e) { throw new ServiceRuntimeException(e); - } + } } /** @@ -277,14 +277,14 @@ public class CallableReferenceImpl<B> implements CallableReferenceExt<B> { CompositeContext componentContextHelper = CompositeContext.getCurrentCompositeContext(); if (componentContextHelper != null) { this.compositeActivator = CompositeContext.getCurrentCompositeActivator(); - this.conversationManager = this.compositeActivator.getConversationManager(); + this.conversationManager = componentContextHelper.getConversationManager(); Component c; if (xmlReader != null) { c = componentContextHelper.fromXML(xmlReader); - xmlReader = null; // OK to GC this now + xmlReader = null; // OK to GC this now } else { c = componentContextHelper.fromXML(scdl); - scdl = null; // OK to GC this now + scdl = null; // OK to GC this now } this.component = (RuntimeComponent)c; compositeActivator.configureComponentContext(this.component); @@ -309,21 +309,21 @@ public class CallableReferenceImpl<B> implements CallableReferenceExt<B> { // Resolve the Component final String bindingURI = binding.getURI(); final Component targetComponent = resolveComponentURI(bindingURI); - + // Find the Service ComponentService targetService = resolveServiceURI(bindingURI, targetComponent); - + // if the target service is a promoted service then find the // service it promotes if ((targetService != null) && (targetService.getService() instanceof CompositeService)) { - CompositeService compositeService = (CompositeService) targetService.getService(); + CompositeService compositeService = (CompositeService)targetService.getService(); // Find the promoted component service ComponentService promotedComponentService = getPromotedComponentService(compositeService); if (promotedComponentService != null && !promotedComponentService.isUnresolved()) { targetService = promotedComponentService; } } - + OptimizableBinding optimizableBinding = (OptimizableBinding)binding; optimizableBinding.setTargetComponent(targetComponent); optimizableBinding.setTargetComponentService(targetService); @@ -357,28 +357,28 @@ public class CallableReferenceImpl<B> implements CallableReferenceExt<B> { } }); javaInterface.setJavaClass(classLoader.loadClass(javaInterface.getName())); - compositeActivator.getJavaInterfaceFactory().createJavaInterface(javaInterface, - javaInterface.getJavaClass()); + compositeActivator.getCompositeContext().getJavaInterfaceFactory() + .createJavaInterface(javaInterface, javaInterface.getJavaClass()); //FIXME: If the interface needs XSDs to be loaded (e.g., for static SDO), // this needs to be done here. We usually search for XSDs in the current // contribution at resolve time. Is it possible to locate the current // contribution at runtime? - } + } this.businessInterface = (Class<B>)javaInterface.getJavaClass(); } if (binding instanceof BindingBuilderExtension) { ((BindingBuilderExtension)binding).getBuilder().build(component, reference, binding, null); } - this.proxyFactory = compositeActivator.getProxyFactory(); + this.proxyFactory = compositeActivator.getCompositeContext().getProxyFactory(); } } else { this.compositeActivator = CompositeContext.getCurrentCompositeActivator(); if (this.compositeActivator != null) { - this.proxyFactory = this.compositeActivator.getProxyFactory(); + this.proxyFactory = this.compositeActivator.getCompositeContext().getProxyFactory(); } } } - + /** * Follow a service promotion chain down to the inner most (non composite) * component service. @@ -386,26 +386,26 @@ public class CallableReferenceImpl<B> implements CallableReferenceExt<B> { * @param topCompositeService * @return */ - private ComponentService getPromotedComponentService(CompositeService compositeService) { + private ComponentService getPromotedComponentService(CompositeService compositeService) { ComponentService componentService = compositeService.getPromotedService(); if (componentService != null) { Service service = componentService.getService(); if (componentService.getName() != null && service instanceof CompositeService) { - + // Continue to follow the service promotion chain return getPromotedComponentService((CompositeService)service); - + } else { - + // Found a non-composite service return componentService; } } else { - + // No promoted service return null; } - } + } /** * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput) @@ -436,29 +436,28 @@ public class CallableReferenceImpl<B> implements CallableReferenceExt<B> { } if (refParams == null) { refParams = new ReferenceParametersImpl(); - + // remove any existing reference parameters from the clone Object toRemove = null; - for (Object extension : clonedRef.getExtensions()){ - if (extension instanceof ReferenceParameters){ + for (Object extension : clonedRef.getExtensions()) { + if (extension instanceof ReferenceParameters) { toRemove = extension; } } - - if (toRemove != null){ + + if (toRemove != null) { clonedRef.getExtensions().remove(toRemove); } - + // add the new reference parameter object clonedRef.getExtensions().add(refParams); } refParams.setCallbackID(callbackID); - if (conversation != null){ + if (conversation != null) { refParams.setConversationID(conversation.getConversationID()); } - return ((CompositeActivatorImpl)compositeActivator).getCompositeContext() - .toXML(component, clonedRef); - } else { + return ((CompositeActivatorImpl)compositeActivator).getCompositeContext().toXML(component, clonedRef); + } else { return scdl; } } @@ -479,7 +478,7 @@ public class CallableReferenceImpl<B> implements CallableReferenceExt<B> { public void attachConversationID(Object conversationID) { this.conversationID = conversationID; } - + public void attachConversation(ConversationExt conversation) { this.conversation = conversation; } @@ -487,7 +486,7 @@ public class CallableReferenceImpl<B> implements CallableReferenceExt<B> { public void attachConversation(Object conversationID) { if (conversationID != null) { ConversationExt conversation = conversationManager.getConversation(conversationID); - if (conversation == null){ + if (conversation == null) { conversation = conversationManager.startConversation(conversationID); } this.conversation = conversation; @@ -507,8 +506,8 @@ public class CallableReferenceImpl<B> implements CallableReferenceExt<B> { public EndpointReference getEndpointReference() { try { - resolve(); - + resolve(); + // Use the interface contract of the reference on the component type Reference componentTypeRef = reference.getReference(); InterfaceContract sourceContract = @@ -520,7 +519,7 @@ public class CallableReferenceImpl<B> implements CallableReferenceExt<B> { return epr; } catch (Exception e) { throw new ServiceRuntimeException(e); - } + } } public XMLStreamReader getXMLReader() { @@ -571,14 +570,14 @@ public class CallableReferenceImpl<B> implements CallableReferenceExt<B> { * @return The Service with the specified serviceName or null if no such Service found. */ protected ComponentService resolveServiceURI(String bindingURI, Component targetComponent) { - + ComponentService targetService = null; - + if (targetComponent != null) { if (bindingURI.startsWith("/")) { bindingURI = bindingURI.substring(1); } - + final String componentURI = targetComponent.getURI(); final String serviceName; if (componentURI.equals(bindingURI)) { diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ComponentContextImpl.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ComponentContextImpl.java index 9538eb755f..d35d02aa1b 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ComponentContextImpl.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ComponentContextImpl.java @@ -34,16 +34,20 @@ import org.apache.tuscany.sca.assembly.Multiplicity; import org.apache.tuscany.sca.assembly.OptimizableBinding; import org.apache.tuscany.sca.assembly.Reference; import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.context.ContextFactoryExtensionPoint; import org.apache.tuscany.sca.context.PropertyValueFactory; import org.apache.tuscany.sca.context.RequestContextFactory; import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; import org.apache.tuscany.sca.core.assembly.CompositeActivator; import org.apache.tuscany.sca.core.context.ComponentContextExt; import org.apache.tuscany.sca.core.context.CompositeContext; +import org.apache.tuscany.sca.core.invocation.ExtensibleProxyFactory; import org.apache.tuscany.sca.core.invocation.ProxyFactory; +import org.apache.tuscany.sca.core.invocation.ProxyFactoryExtensionPoint; import org.apache.tuscany.sca.interfacedef.Interface; import org.apache.tuscany.sca.interfacedef.InterfaceContract; -import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException; import org.apache.tuscany.sca.interfacedef.java.JavaInterface; import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; @@ -68,30 +72,19 @@ public class ComponentContextImpl implements ComponentContextExt { private final ProxyFactory proxyFactory; private final AssemblyFactory assemblyFactory; private final JavaInterfaceFactory javaInterfaceFactory; - - /** - * This is a reference to the PropertyValueFactory that is provided by the Implementation - * that can be used to get the value from a Property Object. - * - * @see #setPropertyValueFactory(PropertyValueFactory) - * @see #getProperty(Class, String) - */ - private PropertyValueFactory propertyFactory; - - public ComponentContextImpl(CompositeActivator compositeActivator, - AssemblyFactory assemblyFactory, - ProxyFactory proxyFactory, - InterfaceContractMapper interfaceContractMapper, - RequestContextFactory requestContextFactory, - JavaInterfaceFactory javaInterfaceFactory, - RuntimeComponent component) { - super(); - this.compositeActivator = compositeActivator; - this.assemblyFactory = assemblyFactory; - this.proxyFactory = proxyFactory; - this.requestContextFactory = requestContextFactory; - this.javaInterfaceFactory = javaInterfaceFactory; + private final PropertyValueFactory propertyFactory; + + public ComponentContextImpl(ExtensionPointRegistry registry, RuntimeComponent component) { this.component = component; + FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); + this.assemblyFactory = factories.getFactory(AssemblyFactory.class); + this.javaInterfaceFactory = factories.getFactory(JavaInterfaceFactory.class); + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + this.compositeActivator = utilities.getUtility(CompositeActivator.class); + this.requestContextFactory = + registry.getExtensionPoint(ContextFactoryExtensionPoint.class).getFactory(RequestContextFactory.class); + this.proxyFactory = new ExtensibleProxyFactory(registry.getExtensionPoint(ProxyFactoryExtensionPoint.class)); + this.propertyFactory = factories.getFactory(PropertyValueFactory.class); } public String getURI() { @@ -132,19 +125,6 @@ public class ComponentContextImpl implements ComponentContextExt { } /** - * The Implementation is responsible for calling this method to set the - * PropertyValueFactory that is used to get the Property Value from - * a Tuscany Property object. - * - * @param factory The PropertyValueFactory to use - * - * @see #getProperty(Class, String) - */ - public void setPropertyValueFactory(PropertyValueFactory factory) { - propertyFactory = factory; - } - - /** * Gets the value for the specified property with the specified type. * * @param type The type of the property value we are getting @@ -455,8 +435,12 @@ public class ComponentContextImpl implements ComponentContextExt { } /* ******************** Contribution for issue TUSCANY-2281 ******************** */ + public CompositeContext getCompositeContext() { + return compositeActivator.getCompositeContext(); + } + public ExtensionPointRegistry getExtensionPointRegistry() { - return compositeActivator.getCompositeContext().getExtensionPointRegistry(); + return getCompositeContext().getExtensionPointRegistry(); } } diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CompositeContextImpl.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CompositeContextImpl.java new file mode 100644 index 0000000000..71f4e2c9a1 --- /dev/null +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CompositeContextImpl.java @@ -0,0 +1,355 @@ +/* + * 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.core.context.impl; + +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.ComponentReference; +import org.apache.tuscany.sca.assembly.ComponentService; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.Multiplicity; +import org.apache.tuscany.sca.assembly.OptimizableBinding; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.core.assembly.CompositeActivator; +import org.apache.tuscany.sca.core.context.CompositeContext; +import org.apache.tuscany.sca.core.conversation.ConversationManager; +import org.apache.tuscany.sca.core.invocation.ExtensibleProxyFactory; +import org.apache.tuscany.sca.core.invocation.ProxyFactory; +import org.apache.tuscany.sca.core.invocation.ProxyFactoryExtensionPoint; +import org.apache.tuscany.sca.core.invocation.ThreadMessageContext; +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.runtime.EndpointReference; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; +import org.osoa.sca.ServiceRuntimeException; + +/** + * @version $Rev$ $Date$ + */ +public class CompositeContextImpl extends CompositeContext { + private final ExtensionPointRegistry extensionPointRegistry; + private final UtilityExtensionPoint utilityExtensionPoint; + private final AssemblyFactory assemblyFactory; + private final JavaInterfaceFactory javaInterfaceFactory; + private final StAXArtifactProcessorExtensionPoint staxProcessors; + private final XMLInputFactory xmlInputFactory; + private final XMLOutputFactory xmlOutputFactory; + private final ProxyFactory proxyFactory; + + public CompositeContextImpl(ExtensionPointRegistry registry) { + this.extensionPointRegistry = registry; + this.utilityExtensionPoint = extensionPointRegistry.getExtensionPoint(UtilityExtensionPoint.class); + FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); + this.xmlInputFactory = factories.getFactory(XMLInputFactory.class); + this.xmlOutputFactory = factories.getFactory(XMLOutputFactory.class); + this.assemblyFactory = factories.getFactory(AssemblyFactory.class); + this.javaInterfaceFactory = factories.getFactory(JavaInterfaceFactory.class); + this.staxProcessors = registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + ProxyFactoryExtensionPoint proxyFactories = extensionPointRegistry.getExtensionPoint(ProxyFactoryExtensionPoint.class); + this.proxyFactory = new ExtensibleProxyFactory(proxyFactories); + } + + /** + * Create a self-reference for a component service + * @param component + * @param service + * @throws CloneNotSupportedException + * @throws InvalidInterfaceException + */ + public ComponentReference createSelfReference(Component component, + ComponentService service, + Class<?> businessInterface) throws CloneNotSupportedException, + InvalidInterfaceException { + ComponentReference componentReference = assemblyFactory.createComponentReference(); + componentReference.setName("$self$." + service.getName()); + for (Binding binding : service.getBindings()) { + if (binding instanceof OptimizableBinding) { + OptimizableBinding optimizableBinding = (OptimizableBinding)((OptimizableBinding)binding).clone(); + optimizableBinding.setTargetBinding(binding); + optimizableBinding.setTargetComponent(component); + optimizableBinding.setTargetComponentService(service); + componentReference.getBindings().add(optimizableBinding); + } else { + componentReference.getBindings().add(binding); + } + } + + componentReference.setCallback(service.getCallback()); + componentReference.getTargets().add(service); + componentReference.getPolicySets().addAll(service.getPolicySets()); + componentReference.getRequiredIntents().addAll(service.getRequiredIntents()); + + InterfaceContract interfaceContract = service.getInterfaceContract(); + Service componentTypeService = service.getService(); + if (componentTypeService != null && componentTypeService.getInterfaceContract() != null) { + interfaceContract = componentTypeService.getInterfaceContract(); + } + interfaceContract = getInterfaceContract(interfaceContract, businessInterface); + componentReference.setInterfaceContract(interfaceContract); + componentReference.setMultiplicity(Multiplicity.ONE_ONE); + // component.getReferences().add(componentReference); + return componentReference; + } + + /** + * @param interfaceContract + * @param businessInterface + * @return + * @throws CloneNotSupportedException + * @throws InvalidInterfaceException + */ + private InterfaceContract getInterfaceContract(InterfaceContract interfaceContract, Class<?> businessInterface) + throws CloneNotSupportedException, InvalidInterfaceException { + Interface interfaze = interfaceContract.getInterface(); + boolean compatible = false; + if (interfaze instanceof JavaInterface) { + Class<?> cls = ((JavaInterface)interfaze).getJavaClass(); + if (businessInterface.isAssignableFrom(cls)) { + compatible = true; + } + } + if (!compatible) { + // The interface is not assignable from the interface contract + interfaceContract = (InterfaceContract)interfaceContract.clone(); + interfaceContract.setInterface(javaInterfaceFactory.createJavaInterface(businessInterface)); + } + + return interfaceContract; + } + + /** + * Bind a component reference to a component service + * @param <B> + * @param businessInterface + * @param reference + * @param service + * @return + * @throws CloneNotSupportedException + * @throws InvalidInterfaceException + */ + public RuntimeComponentReference bindComponentReference(Class<?> businessInterface, + RuntimeComponentReference reference, + RuntimeComponent component, + RuntimeComponentService service) + throws CloneNotSupportedException, InvalidInterfaceException { + RuntimeComponentReference ref = (RuntimeComponentReference)reference.clone(); + InterfaceContract interfaceContract = reference.getInterfaceContract(); + Reference componentTypeReference = reference.getReference(); + if (componentTypeReference != null && componentTypeReference.getInterfaceContract() != null) { + interfaceContract = componentTypeReference.getInterfaceContract(); + } + InterfaceContract refInterfaceContract = getInterfaceContract(interfaceContract, businessInterface); + if (refInterfaceContract != interfaceContract) { + ref = (RuntimeComponentReference)reference.clone(); + ref.setInterfaceContract(interfaceContract); + } + ref.setComponent(component); + ref.getTargets().add(service); + ref.getBindings().clear(); + for (Binding binding : service.getBindings()) { + if (binding instanceof OptimizableBinding) { + OptimizableBinding optimizableBinding = (OptimizableBinding)((OptimizableBinding)binding).clone(); + optimizableBinding.setTargetBinding(binding); + optimizableBinding.setTargetComponent(component); + optimizableBinding.setTargetComponentService(service); + ref.getBindings().add(optimizableBinding); + } else { + ref.getBindings().add(binding); + } + } + return ref; + } + + public void write(Component component, ComponentReference reference, Writer writer) throws IOException { + write(component, reference, null, writer); + } + + public void write(Component component, ComponentReference reference, ComponentService service, Writer writer) throws IOException { + try { + StAXArtifactProcessor<Composite> processor = staxProcessors.getProcessor(Composite.class); + Composite composite = assemblyFactory.createComposite(); + composite.setName(new QName("http://tuscany.apache.org/xmlns/sca/1.0", "default")); + Component comp = assemblyFactory.createComponent(); + comp.setName("default"); + comp.setURI(component.getURI()); + composite.getComponents().add(comp); + if (reference != null) { + comp.getReferences().add(reference); + } + if (service != null) { + comp.getServices().add(service); + } + + XMLStreamWriter streamWriter = xmlOutputFactory.createXMLStreamWriter(writer); + processor.write(composite, streamWriter); + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + } + + public String toXML(Component component, ComponentReference reference) throws IOException { + StringWriter writer = new StringWriter(); + write(component, reference, writer); + return writer.toString(); + } + + public String toXML(Component component, ComponentService service) throws IOException { + StringWriter writer = new StringWriter(); + write(component, null, service, writer); + return writer.toString(); + } + + public RuntimeComponent read(Reader reader) throws IOException { + try { + XMLStreamReader streamReader = xmlInputFactory.createXMLStreamReader(reader); + return read(streamReader); + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + } + + public RuntimeComponent read(XMLStreamReader streamReader) throws IOException { + try { + StAXArtifactProcessor<Composite> processor = staxProcessors.getProcessor(Composite.class); + Composite composite = processor.read(streamReader); + RuntimeComponent component = (RuntimeComponent)composite.getComponents().get(0); + return component; + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + } + + public Component fromXML(String xml) throws IOException { + return read(new StringReader(xml)); + } + + public Component fromXML(XMLStreamReader streamReader) throws IOException { + return read(streamReader); + } + + public static RuntimeComponent getCurrentComponent() { + Message message = ThreadMessageContext.getMessageContext(); + if (message != null) { + EndpointReference to = message.getTo(); + if (to == null) { + return null; + } + RuntimeComponent component = message.getTo().getComponent(); + return component; + } + return null; + } + + public static CompositeActivator getCurrentCompositeActivator() { + RuntimeComponent component = getCurrentComponent(); + if (component != null) { + ComponentContextImpl context = (ComponentContextImpl)component.getComponentContext(); + return context.getCompositeActivator(); + } + return null; + } + + public static CompositeContext getCurrentCompositeContext() { + CompositeActivator activator = getCurrentCompositeActivator(); + if (activator != null) { + return activator.getCompositeContext(); + } + return null; + } + + /** + * @param component + */ + public static ComponentService getSingleService(Component component) { + ComponentService targetService; + List<ComponentService> services = component.getServices(); + List<ComponentService> regularServices = new ArrayList<ComponentService>(); + for (ComponentService service : services) { + if (service.isCallback()) { + continue; + } + String name = service.getName(); + if (!name.startsWith("$") || name.startsWith("$dynamic$")) { + regularServices.add(service); + } + } + if (regularServices.size() == 0) { + throw new ServiceRuntimeException("No service is declared on component " + component.getURI()); + } + if (regularServices.size() != 1) { + throw new ServiceRuntimeException("More than one service is declared on component " + component.getURI() + + ". Service name is required to get the service."); + } + targetService = regularServices.get(0); + return targetService; + } + + public ExtensionPointRegistry getExtensionPointRegistry() { + return extensionPointRegistry; + } + + public ConversationManager getConversationManager() { + return utilityExtensionPoint.getUtility(ConversationManager.class); + } + + /** + * Get the java interface factory + * @return + */ + public JavaInterfaceFactory getJavaInterfaceFactory() { + return javaInterfaceFactory; + } + + /** + * Get the proxy factory + * @return + */ + public ProxyFactory getProxyFactory() { + return proxyFactory; + } + +} diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/RequestContextImpl.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/RequestContextImpl.java index bd4a49a71a..5f57d26456 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/RequestContextImpl.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/RequestContextImpl.java @@ -24,6 +24,7 @@ import javax.security.auth.Subject; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.context.CallableReferenceExt; +import org.apache.tuscany.sca.core.invocation.ExtensibleProxyFactory; import org.apache.tuscany.sca.core.invocation.ProxyFactory; import org.apache.tuscany.sca.core.invocation.ProxyFactoryExtensionPoint; import org.apache.tuscany.sca.core.invocation.ThreadMessageContext; @@ -101,8 +102,7 @@ public class RequestContextImpl implements RequestContext { JavaInterface javaInterface = (JavaInterface) callbackReference.getInterfaceContract().getInterface(); Class<CB> javaClass = (Class<CB>)javaInterface.getJavaClass(); List<RuntimeWire> wires = callbackReference.getRuntimeWires(); - ProxyFactory proxyFactory = javaClass.isInterface() ? proxyFactoryExtensionPoint.getInterfaceProxyFactory() : - proxyFactoryExtensionPoint.getClassProxyFactory(); + ProxyFactory proxyFactory = new ExtensibleProxyFactory(proxyFactoryExtensionPoint); CallbackReferenceImpl ref = CallbackReferenceImpl.newInstance(javaClass, proxyFactory, wires); if (ref != null) { //ref.resolveTarget(); diff --git a/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/JavaPropertyValueObjectFactory.java b/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/JavaPropertyValueObjectFactory.java index 1eac9ce7a6..3618fa3477 100644 --- a/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/JavaPropertyValueObjectFactory.java +++ b/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/JavaPropertyValueObjectFactory.java @@ -26,6 +26,8 @@ import java.util.StringTokenizer; import org.apache.tuscany.sca.assembly.ComponentProperty;
import org.apache.tuscany.sca.assembly.Property;
import org.apache.tuscany.sca.context.PropertyValueFactory;
+import org.apache.tuscany.sca.core.ExtensionPointRegistry;
+import org.apache.tuscany.sca.core.UtilityExtensionPoint;
import org.apache.tuscany.sca.core.factory.ObjectCreationException;
import org.apache.tuscany.sca.core.factory.ObjectFactory;
import org.apache.tuscany.sca.databinding.Mediator;
@@ -52,6 +54,9 @@ public class JavaPropertyValueObjectFactory implements PropertyValueFactory { private Mediator mediator = null;
private boolean isSimpleType;
+ public JavaPropertyValueObjectFactory(ExtensionPointRegistry registry) {
+ this.mediator = registry.getExtensionPoint(UtilityExtensionPoint.class).getUtility(Mediator.class);
+ }
public JavaPropertyValueObjectFactory(Mediator mediator) {
this.mediator = mediator;
}
diff --git a/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentContextProvider.java b/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentContextProvider.java index ca42c69d4e..809b7c18b8 100644 --- a/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentContextProvider.java +++ b/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentContextProvider.java @@ -60,7 +60,6 @@ import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.interfacedef.java.impl.JavaInterfaceUtil; import org.apache.tuscany.sca.invocation.Invoker; import org.apache.tuscany.sca.runtime.RuntimeComponent; -import org.apache.tuscany.sca.runtime.RuntimeComponentContext; import org.apache.tuscany.sca.runtime.RuntimeComponentReference; import org.apache.tuscany.sca.runtime.RuntimeWire; import org.osoa.sca.CallableReference; @@ -262,12 +261,6 @@ public class JavaComponentContextProvider { } } - // We need to set the PropertyValueFactory on the ComponentContextImpl - // so the ComponentContext can "de-marshal" the property type to a value - // when the getProperty() method is called - RuntimeComponentContext ccImpl = (RuntimeComponentContext)component.getComponentContext(); - ccImpl.setPropertyValueFactory(propertyValueFactory); - //setUpPolicyHandlers(); } diff --git a/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/module/JavaRuntimeModuleActivator.java b/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/module/JavaRuntimeModuleActivator.java index 9dc1d8d21a..1ea3546adb 100644 --- a/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/module/JavaRuntimeModuleActivator.java +++ b/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/module/JavaRuntimeModuleActivator.java @@ -77,7 +77,9 @@ public class JavaRuntimeModuleActivator implements ModuleActivator { DataBindingExtensionPoint dataBindings = registry.getExtensionPoint(DataBindingExtensionPoint.class); TransformerExtensionPoint transformers = registry.getExtensionPoint(TransformerExtensionPoint.class); MediatorImpl mediator = new MediatorImpl(dataBindings, transformers); + utilities.addUtility(mediator); JavaPropertyValueObjectFactory factory = new JavaPropertyValueObjectFactory(mediator); + factories.addFactory(factory); ContextFactoryExtensionPoint contextFactories = registry.getExtensionPoint(ContextFactoryExtensionPoint.class); ComponentContextFactory componentContextFactory = contextFactories.getFactory(ComponentContextFactory.class); |