diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-10-02 00:38:31 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-10-02 00:38:31 +0000 |
commit | efa0cb0a3d5564bf3880ad2aa20f1c418d63aa7b (patch) | |
tree | 514b04eb9d94a53d58902a832053215d52a0c917 /java/sca/modules/core | |
parent | 9d8f01864f34586fdaf5bf96bad30e2fe06226c1 (diff) |
Make CompositeActivator a stateless utility by using CompositeContext to hold the context from the Node including the endpointRegistry
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@820854 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/core')
7 files changed, 446 insertions, 934 deletions
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 fd0302829d..838ab8a2a0 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 @@ -36,13 +36,10 @@ import org.apache.tuscany.sca.assembly.EndpointReference; import org.apache.tuscany.sca.assembly.Implementation; 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.CompositeContext; -import org.apache.tuscany.sca.context.ContextFactoryExtensionPoint; 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.context.impl.CompositeContextImpl; import org.apache.tuscany.sca.core.invocation.ExtensibleWireProcessor; import org.apache.tuscany.sca.core.scope.ScopeContainer; import org.apache.tuscany.sca.core.scope.ScopeRegistry; @@ -62,7 +59,6 @@ import org.apache.tuscany.sca.runtime.ActivationException; import org.apache.tuscany.sca.runtime.CompositeActivator; import org.apache.tuscany.sca.runtime.EndpointRegistry; 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.RuntimeComponentService; import org.apache.tuscany.sca.runtime.RuntimeWire; @@ -85,18 +81,9 @@ public class CompositeActivatorImpl implements CompositeActivator { private final RuntimeWireProcessor wireProcessor; private final ProviderFactoryExtensionPoint providerFactories; - private final ComponentContextFactory componentContextFactory; - private final EndpointRegistry endpointRegistry; - - private final CompositeContext compositeContext; - - private Composite domainComposite; - public CompositeActivatorImpl(ExtensionPointRegistry extensionPoints) { this.extensionPoints = extensionPoints; UtilityExtensionPoint utilities = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class); - this.endpointRegistry = utilities.getUtility(EndpointRegistry.class); - this.compositeContext = new CompositeContextImpl(extensionPoints, endpointRegistry); FactoryExtensionPoint factories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); this.assemblyFactory = factories.getFactory(AssemblyFactory.class); this.messageFactory = factories.getFactory(MessageFactory.class); @@ -105,8 +92,6 @@ public class CompositeActivatorImpl implements CompositeActivator { this.workScheduler = utilities.getUtility(WorkScheduler.class); this.wireProcessor = new ExtensibleWireProcessor(extensionPoints.getExtensionPoint(RuntimeWireProcessorExtensionPoint.class)); this.providerFactories = extensionPoints.getExtensionPoint(ProviderFactoryExtensionPoint.class); - ContextFactoryExtensionPoint contextFactories = extensionPoints.getExtensionPoint(ContextFactoryExtensionPoint.class); - this.componentContextFactory = contextFactories.getFactory(ComponentContextFactory.class); } //========================================================================= @@ -482,27 +467,27 @@ public class CompositeActivatorImpl implements CompositeActivator { // Composite start/stop - public void start(Composite composite) { + public void start(CompositeContext compositeContext, Composite composite) { if (logger.isLoggable(Level.FINE)) { logger.fine("Starting composite: " + composite.getName()); } for (Component component : composite.getComponents()) { - start(component); + start(compositeContext, component); } } - public void stop(Composite composite) { + public void stop(CompositeContext compositeContext, Composite composite) { if (logger.isLoggable(Level.FINE)) { logger.fine("Stopping composite: " + composite.getName()); } for (final Component component : composite.getComponents()) { - stop(component); + stop(compositeContext, component); } } // Component start/stop - public void start(Component component) { + public void start(CompositeContext compositeContext, Component component) { if (logger.isLoggable(Level.FINE)) { logger.fine("Starting component: " + component.getURI()); } @@ -511,7 +496,7 @@ public class CompositeActivatorImpl implements CompositeActivator { return; } - configureComponentContext(runtimeComponent); + compositeContext.configureComponentContext(runtimeComponent); // Reference bindings aren't started until the wire is first used @@ -536,14 +521,14 @@ public class CompositeActivatorImpl implements CompositeActivator { return null; } }); - endpointRegistry.addEndpoint(endpoint); + compositeContext.getEndpointRegistry().addEndpoint(endpoint); } } } Implementation implementation = component.getImplementation(); if (implementation instanceof Composite) { - start((Composite)implementation); + start(compositeContext, (Composite)implementation); } else { for (PolicyProvider policyProvider : runtimeComponent.getPolicyProviders()) { policyProvider.start(); @@ -564,7 +549,7 @@ public class CompositeActivatorImpl implements CompositeActivator { runtimeComponent.setStarted(true); } - public void stop(Component component) { + public void stop(CompositeContext compositeContext, Component component) { if (!((RuntimeComponent)component).isStarted()) { return; } @@ -576,7 +561,7 @@ public class CompositeActivatorImpl implements CompositeActivator { logger.fine("Stopping component service: " + component.getURI() + "#" + service.getName()); } for (Endpoint endpoint : service.getEndpoints()) { - endpointRegistry.removeEndpoint(endpoint); + compositeContext.getEndpointRegistry().removeEndpoint(endpoint); final ServiceBindingProvider bindingProvider = ((RuntimeComponentService)service).getBindingProvider(endpoint.getBinding()); if (bindingProvider != null) { // Allow bindings to read properties. Requires PropertyPermission read in security policy. @@ -600,7 +585,7 @@ public class CompositeActivatorImpl implements CompositeActivator { RuntimeComponentReference runtimeRef = ((RuntimeComponentReference)reference); for (EndpointReference endpointReference : reference.getEndpointReferences()) { - endpointRegistry.removeEndpointReference(endpointReference); + compositeContext.getEndpointRegistry().removeEndpointReference(endpointReference); final ReferenceBindingProvider bindingProvider = runtimeRef.getBindingProvider(endpointReference.getBinding()); if (bindingProvider != null) { // Allow bindings to read properties. Requires PropertyPermission read in security policy. @@ -620,7 +605,7 @@ public class CompositeActivatorImpl implements CompositeActivator { } Implementation implementation = component.getImplementation(); if (implementation instanceof Composite) { - stop((Composite)implementation); + stop(compositeContext, (Composite)implementation); } else { final ImplementationProvider implementationProvider = ((RuntimeComponent)component).getImplementationProvider(); if (implementationProvider != null) { @@ -648,11 +633,6 @@ public class CompositeActivatorImpl implements CompositeActivator { ((RuntimeComponent)component).setStarted(false); } - public void configureComponentContext(RuntimeComponent runtimeComponent) { - RuntimeComponentContext componentContext = (RuntimeComponentContext) componentContextFactory.createComponentContext(runtimeComponent); - runtimeComponent.setComponentContext(componentContext); - } - // Service start/stop // TODO - EPR done as part of the component start above @@ -717,7 +697,7 @@ public class CompositeActivatorImpl implements CompositeActivator { // is first used (when the chains are created) for (EndpointReference endpointReference : componentReference.getEndpointReferences()){ addReferenceWire(component, componentReference, endpointReference); - endpointRegistry.addEndpointReference(endpointReference); + component.getComponentContext().getCompositeContext().getEndpointRegistry().addEndpointReference(endpointReference); } } @@ -728,6 +708,8 @@ public class CompositeActivatorImpl implements CompositeActivator { logger.fine("Stopping component reference: " + component.getURI() + "#" + reference.getName()); } RuntimeComponentReference runtimeRef = ((RuntimeComponentReference)reference); + RuntimeComponent runtimeComponent = (RuntimeComponent) component; + EndpointRegistry endpointRegistry = runtimeComponent.getComponentContext().getCompositeContext().getEndpointRegistry(); for ( EndpointReference endpointReference : runtimeRef.getEndpointReferences()){ endpointRegistry.removeEndpointReference(endpointReference); ReferenceBindingProvider bindingProvider = runtimeRef.getBindingProvider(endpointReference.getBinding()); @@ -832,22 +814,6 @@ public class CompositeActivatorImpl implements CompositeActivator { } - - // Utility functions - // TODO - EPR - can we get rid of these? - - public CompositeContext getCompositeContext() { - return compositeContext; - } - - public Composite getDomainComposite() { - return domainComposite; - } - - public void setDomainComposite(Composite domainComposite) { - this.domainComposite = domainComposite; - } - /* TODO - EPR - Resolved via registry now public Component resolve(String componentURI) { for (Composite composite : domainComposite.getIncludes()) { 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 4e43518738..2de52bebb2 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 @@ -20,6 +20,7 @@ package org.apache.tuscany.sca.core.context; import org.apache.tuscany.sca.context.ComponentContextFactory; +import org.apache.tuscany.sca.context.CompositeContext; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.context.impl.ComponentContextImpl; import org.apache.tuscany.sca.runtime.RuntimeComponent; @@ -35,8 +36,8 @@ public class DefaultComponentContextFactory implements ComponentContextFactory { this.registry = registry; } - public ComponentContext createComponentContext(RuntimeComponent component) { - return new ComponentContextImpl(registry, component); + public ComponentContext createComponentContext(CompositeContext compositeContext, RuntimeComponent component) { + return new ComponentContextImpl(registry, compositeContext, 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 deleted file mode 100644 index d6a029cd55..0000000000 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CallableReferenceImpl.java +++ /dev/null @@ -1,488 +0,0 @@ -/* - * 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.ObjectInput; -import java.io.ObjectOutput; -import java.io.Reader; -import java.io.StringReader; -import java.io.StringWriter; -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.util.UUID; - -import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLOutputFactory; -import javax.xml.stream.XMLStreamException; -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.ComponentService; -import org.apache.tuscany.sca.assembly.CompositeService; -import org.apache.tuscany.sca.assembly.EndpointReference; -import org.apache.tuscany.sca.assembly.Service; -import org.apache.tuscany.sca.assembly.builder.BindingBuilder; -import org.apache.tuscany.sca.assembly.builder.BuilderExtensionPoint; -import org.apache.tuscany.sca.context.CompositeContext; -import org.apache.tuscany.sca.contribution.processor.ContributionReadException; -import org.apache.tuscany.sca.contribution.processor.ContributionWriteException; -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.RuntimeAssemblyFactory; -import org.apache.tuscany.sca.core.assembly.impl.ReferenceParametersImpl; -import org.apache.tuscany.sca.core.context.ServiceReferenceExt; -import org.apache.tuscany.sca.core.factory.ObjectCreationException; -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.java.JavaInterface; -import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; -import org.apache.tuscany.sca.runtime.CompositeActivator; -import org.apache.tuscany.sca.runtime.ReferenceParameters; -import org.apache.tuscany.sca.runtime.RuntimeComponent; -import org.apache.tuscany.sca.runtime.RuntimeComponentReference; -import org.apache.tuscany.sca.runtime.RuntimeWire; -import org.oasisopen.sca.ServiceRuntimeException; - -/** - * The old base class for implementations of service and callback references. We have maintained - * this in Tuscany 2.x for the time being even though SCA 1.1 only defines service references and - * not callable reference - * - * @version $Rev$ $Date$ - * @param <B> the type of the business interface - */ -public class CallableReferenceImpl<B> implements ServiceReferenceExt<B> { - static final long serialVersionUID = -521548304761848325L; - - protected transient ProxyFactory proxyFactory; - protected transient Class<B> businessInterface; - protected transient Object proxy; - - protected Object callbackID; // The callbackID should be serializable - - protected transient RuntimeComponent component; - protected transient RuntimeComponentReference reference; - protected transient EndpointReference endpointReference; - - protected String scdl; - - private transient RuntimeComponentReference clonedRef; - private transient ReferenceParameters refParams; - private transient XMLStreamReader xmlReader; - - protected transient CompositeActivator compositeActivator; - private ExtensionPointRegistry registry; - private FactoryExtensionPoint modelFactories; - protected RuntimeAssemblyFactory assemblyFactory; - private StAXArtifactProcessorExtensionPoint staxProcessors; - private StAXArtifactProcessor<EndpointReference> staxProcessor; - private XMLInputFactory xmlInputFactory; - private XMLOutputFactory xmlOutputFactory; - private BuilderExtensionPoint builders; - - /* - * Public constructor for Externalizable serialization/deserialization - */ - public CallableReferenceImpl() { - super(); - } - - /* - * Public constructor for use by XMLStreamReader2CallableReference - */ - // TODO - EPR - Is this required - public CallableReferenceImpl(XMLStreamReader xmlReader) throws Exception { - this.xmlReader = xmlReader; - resolve(); - } - - protected CallableReferenceImpl(Class<B> businessInterface, - RuntimeComponent component, - RuntimeComponentReference reference, - EndpointReference endpointReference, - ProxyFactory proxyFactory, - CompositeActivator compositeActivator) { - this.proxyFactory = proxyFactory; - this.businessInterface = businessInterface; - this.component = component; - this.reference = reference; - this.endpointReference = endpointReference; - this.compositeActivator = compositeActivator; - - getExtensions(); - - // FIXME: The SCA Specification is not clear how we should handle multiplicity - // for CallableReference - if (this.endpointReference == null) { - - // TODO - EPR - If no endpoint reference specified assume the first one - // This will happen when a self reference is created in which case the - // the reference should only have one endpointReference so use that - if (this.reference.getEndpointReferences().size() == 0){ - throw new ServiceRuntimeException("The reference " + reference.getName() + " in component " + - component.getName() + " has no endpoint references"); - } - - if (this.reference.getEndpointReferences().size() > 1){ - throw new ServiceRuntimeException("The reference " + reference.getName() + " in component " + - component.getName() + " has more than one endpoint reference"); - } - - this.endpointReference = this.reference.getEndpointReferences().get(0); - } - - // 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; - initCallbackID(); - } - - private void getExtensions() { - this.registry = compositeActivator.getCompositeContext().getExtensionPointRegistry(); - this.modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); - this.assemblyFactory = (RuntimeAssemblyFactory)modelFactories.getFactory(AssemblyFactory.class); - this.xmlInputFactory = modelFactories.getFactory(XMLInputFactory.class); - this.xmlOutputFactory = modelFactories.getFactory(XMLOutputFactory.class); - this.staxProcessors = registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); - this.staxProcessor = staxProcessors.getProcessor(EndpointReference.class); - this.builders = registry.getExtensionPoint(BuilderExtensionPoint.class); - } - - public CallableReferenceImpl(Class<B> businessInterface, RuntimeWire wire, ProxyFactory proxyFactory) { - this.proxyFactory = proxyFactory; - this.businessInterface = businessInterface; - //ExtensionPointRegistry registry = ((RuntimeWireImpl)wire).getExtensionPoints(); - //this.modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); - //this.assemblyFactory = (RuntimeAssemblyFactory)modelFactories.getFactory(AssemblyFactory.class); - bind(wire); - } - - public RuntimeWire getRuntimeWire() { - try { - resolve(); - if (endpointReference != null){ - return reference.getRuntimeWire(endpointReference); - } else { - return null; - } - } catch (Exception e) { - throw new ServiceRuntimeException(e); - } - } - - public EndpointReference getEndpointReference() { - return endpointReference; - } - - protected void bind(RuntimeWire wire) { - if (wire != null) { - this.component = (RuntimeComponent)wire.getEndpointReference().getComponent(); - this.reference = (RuntimeComponentReference)wire.getEndpointReference().getReference(); - this.endpointReference = wire.getEndpointReference(); - this.compositeActivator = component.getComponentContext().getCompositeActivator(); - initCallbackID(); - } - } - - protected void initCallbackID() { - if (reference.getInterfaceContract() != null) { - if (reference.getInterfaceContract().getCallbackInterface() != null) { - this.callbackID = createCallbackID(); - } - } - } - - public B getProxy() throws ObjectCreationException { - try { - if (proxy == null) { - proxy = createProxy(); - } - return businessInterface.cast(proxy); - } catch (Exception e) { - throw new ObjectCreationException(e); - } - } - - public void setProxy(Object proxy) { - this.proxy = proxy; - } - - protected Object createProxy() throws Exception { - return proxyFactory.createProxy(this); - } - - public B getService() { - try { - resolve(); - return getProxy(); - } catch (Exception e) { - throw new ServiceRuntimeException(e); - } - } - - public Class<B> getBusinessInterface() { - try { - resolve(); - return businessInterface; - } catch (Exception e) { - throw new ServiceRuntimeException(e); - } - } - - public boolean isConversational() { - try { - resolve(); - return reference == null ? false : reference.getInterfaceContract().getInterface().isConversational(); - } catch (Exception e) { - throw new ServiceRuntimeException(e); - } - } - - public Object getCallbackID() { - try { - resolve(); - return callbackID; - } catch (Exception e) { - throw new ServiceRuntimeException(e); - } - } - - /** - * Follow a service promotion chain down to the inner most (non composite) - * component service. - * - * @param topCompositeService - * @return - */ - 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; - } - } - - // ============ WRITE AND READ THE REFERENCE TO EXTERNAL XML ======================== - - /** - * write the reference to a stream - * - * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput) - */ - public void writeExternal(ObjectOutput out) throws IOException { - try { - String xml = null; - if (scdl == null){ - xml = toXMLString(); - } else { - xml = scdl; - } - - if (xml == null) { - out.writeBoolean(false); - } else { - out.writeBoolean(true); - out.writeUTF(xml); - } - } catch (Exception e) { - // e.printStackTrace(); - throw new IOException(e.toString()); - } - } - - /** - * write the endpoint reference into an xml string - */ - public String toXMLString() throws IOException, XMLStreamException, ContributionWriteException{ - StringWriter writer = new StringWriter(); - XMLStreamWriter streamWriter = xmlOutputFactory.createXMLStreamWriter(writer); - staxProcessor.write(endpointReference, streamWriter); - return writer.toString(); - } - - /** - * Read the reference from a stream - * - * @see java.io.Externalizable#readExternal(java.io.ObjectInput) - */ - public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - final boolean hasSCDL = in.readBoolean(); - if (hasSCDL) { - this.scdl = in.readUTF(); - } else { - this.scdl = null; - } - } - - /** - * Read xml string into the endpoint reference - */ - public void fromXMLString() throws IOException, XMLStreamException, ContributionReadException { - - XMLStreamReader streamReader = xmlReader; - - if (scdl != null ){ - Reader reader = new StringReader(scdl); - - if (xmlInputFactory == null){ - // this is a reference being read from a external stream - // so set up enough of the reference in order to resolved the - // xml being read - CompositeContext componentContextHelper = CompositeContext.getCurrentCompositeContext(); - this.compositeActivator = CompositeContext.getCurrentCompositeActivator(); - getExtensions(); - } - - streamReader = xmlInputFactory.createXMLStreamReader(reader); - } - - endpointReference = staxProcessor.read(streamReader); - - // ok to GC - xmlReader = null; - scdl = null; - } - - /** - * @throws IOException - */ - private synchronized void resolve() throws Exception { - if ((scdl != null || xmlReader != null) && component == null && reference == null) { - fromXMLString(); - - this.component = (RuntimeComponent)endpointReference.getComponent(); - compositeActivator.configureComponentContext(this.component); - - this.reference = (RuntimeComponentReference)endpointReference.getReference(); - this.reference.setComponent(this.component); - clonedRef = reference; - - ReferenceParameters parameters = null; - for (Object ext : reference.getExtensions()) { - if (ext instanceof ReferenceParameters) { - parameters = (ReferenceParameters)ext; - break; - } - } - - if (parameters != null) { - refParams = parameters; - this.callbackID = parameters.getCallbackID(); - } - - Interface i = reference.getInterfaceContract().getInterface(); - if (i instanceof JavaInterface) { - JavaInterface javaInterface = (JavaInterface)i; - if (javaInterface.isUnresolved()) { - // Allow privileged access to get ClassLoader. Requires RuntimePermission in - // security policy. - ClassLoader classLoader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { - public ClassLoader run() { - return Thread.currentThread().getContextClassLoader(); - } - }); - - javaInterface.setJavaClass(classLoader.loadClass(javaInterface.getName())); - JavaInterfaceFactory javaInterfaceFactory = getJavaInterfaceFactory(compositeActivator); - - javaInterfaceFactory.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(); - } - - Binding binding = endpointReference.getBinding(); - if (binding != null) { - BindingBuilder bindingBuilder = builders.getBindingBuilder(binding.getClass()); - if (bindingBuilder != null) { - bindingBuilder.build(component, reference, endpointReference.getBinding(), null); - } - } - - this.proxyFactory = getProxyFactory(this.compositeActivator); - } else if (compositeActivator == null) { - this.compositeActivator = CompositeContext.getCurrentCompositeActivator(); - if (this.compositeActivator != null) { - this.proxyFactory = getProxyFactory(this.compositeActivator); - } - } - } - - private JavaInterfaceFactory getJavaInterfaceFactory(CompositeActivator activator) { - ExtensionPointRegistry extensionPointRegistry = activator.getCompositeContext().getExtensionPointRegistry(); - FactoryExtensionPoint factories = extensionPointRegistry.getExtensionPoint(FactoryExtensionPoint.class); - JavaInterfaceFactory javaInterfaceFactory = factories.getFactory(JavaInterfaceFactory.class); - return javaInterfaceFactory; - } - - private ProxyFactory getProxyFactory(CompositeActivator activator) { - ExtensionPointRegistry extensionPointRegistry = activator.getCompositeContext().getExtensionPointRegistry(); - ProxyFactoryExtensionPoint proxyFactories = extensionPointRegistry.getExtensionPoint(ProxyFactoryExtensionPoint.class); - return new ExtensibleProxyFactory(proxyFactories); - } - - // ================================================================================== - - /** - * Create a callback id - * - * @return the callback id - */ - private String createCallbackID() { - return UUID.randomUUID().toString(); - } - - public void attachCallbackID(Object callbackID) { - this.callbackID = callbackID; - } - - protected ReferenceParameters getReferenceParameters() { - ReferenceParameters parameters = new ReferenceParametersImpl(); - parameters.setCallbackID(callbackID); - return parameters; - } - - public XMLStreamReader getXMLReader() { - return xmlReader; - } - -} 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 fa8c1a4dfa..13b0299554 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 @@ -18,9 +18,6 @@ */ package org.apache.tuscany.sca.core.context.impl; -import java.io.IOException; -import java.io.Reader; -import java.io.Writer; import java.util.ArrayList; import java.util.Collection; @@ -73,6 +70,7 @@ import org.oasisopen.sca.ServiceRuntimeException; public class ComponentContextImpl implements RuntimeComponentContext { private final RuntimeComponent component; + private final CompositeContext compositeContext; private final CompositeActivator compositeActivator; private final RequestContextFactory requestContextFactory; private final ProxyFactory proxyFactory; @@ -82,14 +80,15 @@ public class ComponentContextImpl implements RuntimeComponentContext { private final EndpointReferenceBuilder endpointReferenceBuilder; private final Monitor monitor; - public ComponentContextImpl(ExtensionPointRegistry registry, RuntimeComponent component) { + public ComponentContextImpl(ExtensionPointRegistry registry, CompositeContext compositeContext, RuntimeComponent component) { this.component = component; FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); this.assemblyFactory = factories.getFactory(AssemblyFactory.class); this.javaInterfaceFactory = factories.getFactory(JavaInterfaceFactory.class); - // FIXME: We need to have node-specific compositeActivator UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + this.compositeContext = compositeContext; + this.compositeActivator = utilities.getUtility(CompositeActivator.class); this.requestContextFactory = @@ -238,7 +237,7 @@ public class ComponentContextImpl implements RuntimeComponentContext { } ref.setComponent(component); return new ServiceReferenceImpl<B>(businessInterface, component, ref, endpointReference, proxyFactory, - compositeActivator); + component.getComponentContext().getCompositeContext()); } catch (Exception e) { throw new ServiceRuntimeException(e); } @@ -283,7 +282,7 @@ public class ComponentContextImpl implements RuntimeComponentContext { ref.getBindings().add(binding); } } - return new ServiceReferenceImpl<B>(businessInterface, component, ref, proxyFactory, compositeActivator); + return new ServiceReferenceImpl<B>(businessInterface, component, ref, null, proxyFactory, compositeContext); } catch (Exception e) { throw new ServiceRuntimeException(e); } @@ -301,7 +300,7 @@ public class ComponentContextImpl implements RuntimeComponentContext { (RuntimeComponentReference)createSelfReference(component, service, businessInterface); ref.setComponent(component); return new ServiceReferenceImpl<B>(businessInterface, component, ref, null, proxyFactory, - compositeActivator); + compositeContext); } catch (Exception e) { throw new ServiceRuntimeException(e); } @@ -410,34 +409,12 @@ public class ComponentContextImpl implements RuntimeComponentContext { } /** - * @return the compositeActivator - */ - public CompositeActivator getCompositeActivator() { - return compositeActivator; - } - - /** * @see org.apache.tuscany.sca.runtime.RuntimeComponentContext#start(org.apache.tuscany.sca.runtime.RuntimeComponentReference) */ public void start(RuntimeComponentReference reference) { compositeActivator.start(component, reference); } - /** - * @see org.apache.tuscany.sca.runtime.RuntimeComponentContext#read(java.io.Reader) - */ - public RuntimeComponent read(Reader reader) throws IOException { - RuntimeComponent component = compositeActivator.getCompositeContext().read(reader); - compositeActivator.configureComponentContext(component); - return component; - } - - /** - * @see org.apache.tuscany.sca.runtime.RuntimeComponentContext#write(org.apache.tuscany.sca.runtime.RuntimeComponentReference, java.io.Writer) - */ - public void write(RuntimeComponentReference reference, Writer writer) throws IOException { - compositeActivator.getCompositeContext().write(component, reference, writer); - } /* ******************** Contribution for issue TUSCANY-2281 ******************** */ @@ -478,7 +455,7 @@ public class ComponentContextImpl implements RuntimeComponentContext { /* ******************** Contribution for issue TUSCANY-2281 ******************** */ public CompositeContext getCompositeContext() { - return compositeActivator.getCompositeContext(); + return compositeContext; } public ExtensionPointRegistry 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 deleted file mode 100644 index b26861aa79..0000000000 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CompositeContextImpl.java +++ /dev/null @@ -1,340 +0,0 @@ -/* - * 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.Endpoint; -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.CompositeContext; -import org.apache.tuscany.sca.context.ThreadMessageContext; -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.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.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.CompositeActivator; -import org.apache.tuscany.sca.runtime.EndpointRegistry; -import org.apache.tuscany.sca.runtime.RuntimeComponent; -import org.apache.tuscany.sca.runtime.RuntimeComponentReference; -import org.apache.tuscany.sca.runtime.RuntimeComponentService; -import org.oasisopen.sca.ServiceRuntimeException; - -/** - * @version $Rev$ $Date$ - */ -public class CompositeContextImpl extends CompositeContext { - private final ExtensionPointRegistry extensionPointRegistry; - private final EndpointRegistry endpointRegistry; - private final AssemblyFactory assemblyFactory; - private final JavaInterfaceFactory javaInterfaceFactory; - private final StAXArtifactProcessorExtensionPoint staxProcessors; - private final XMLInputFactory xmlInputFactory; - private final XMLOutputFactory xmlOutputFactory; - - public CompositeContextImpl(ExtensionPointRegistry registry, EndpointRegistry endpointRegistry) { - this.extensionPointRegistry = registry; - this.endpointRegistry = endpointRegistry; - 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); - } - - private ProxyFactory getProxyFactory() { - ProxyFactoryExtensionPoint proxyFactories = extensionPointRegistry.getExtensionPoint(ProxyFactoryExtensionPoint.class); - return 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.1", "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) { - Endpoint to = message.getTo(); - if (to == null) { - return null; - } - RuntimeComponent component = (RuntimeComponent) 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.isForCallback()) { - 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 EndpointRegistry getEndpointRegistry() { - return endpointRegistry; - } - -} diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ServiceReferenceImpl.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ServiceReferenceImpl.java index 20b607ce74..a1fc91e824 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ServiceReferenceImpl.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ServiceReferenceImpl.java @@ -18,14 +18,52 @@ */ package org.apache.tuscany.sca.core.context.impl; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.io.Reader; +import java.io.StringReader; +import java.io.StringWriter; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.UUID; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamException; 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.ComponentService; +import org.apache.tuscany.sca.assembly.CompositeService; import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.assembly.builder.BindingBuilder; +import org.apache.tuscany.sca.assembly.builder.BuilderExtensionPoint; +import org.apache.tuscany.sca.context.CompositeContext; +import org.apache.tuscany.sca.contribution.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.processor.ContributionWriteException; +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.RuntimeAssemblyFactory; +import org.apache.tuscany.sca.core.assembly.impl.ReferenceParametersImpl; +import org.apache.tuscany.sca.core.context.ServiceReferenceExt; +import org.apache.tuscany.sca.core.factory.ObjectCreationException; +import org.apache.tuscany.sca.core.invocation.ExtensibleProxyFactory; import org.apache.tuscany.sca.core.invocation.ProxyFactory; -import org.apache.tuscany.sca.runtime.CompositeActivator; +import org.apache.tuscany.sca.core.invocation.ProxyFactoryExtensionPoint; +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; +import org.apache.tuscany.sca.runtime.ReferenceParameters; import org.apache.tuscany.sca.runtime.RuntimeComponent; import org.apache.tuscany.sca.runtime.RuntimeComponentReference; import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.oasisopen.sca.ServiceRuntimeException; /** * Default implementation of a ServiceReference. @@ -33,10 +71,32 @@ import org.apache.tuscany.sca.runtime.RuntimeWire; * @version $Rev$ $Date$ * @param <B> the type of the business interface */ -public class ServiceReferenceImpl<B> extends CallableReferenceImpl<B> { +public class ServiceReferenceImpl<B> implements ServiceReferenceExt<B> { private static final long serialVersionUID = 6763709434194361540L; - protected transient Object callback; + protected transient ProxyFactory proxyFactory; + protected transient Class<B> businessInterface; + protected transient Object proxy; + + protected Object callbackID; // The callbackID should be serializable + + protected transient RuntimeComponent component; + protected transient RuntimeComponentReference reference; + protected transient EndpointReference endpointReference; + + protected String scdl; + + private transient XMLStreamReader xmlReader; + + protected transient CompositeContext compositeContext; + private ExtensionPointRegistry registry; + private FactoryExtensionPoint modelFactories; + protected RuntimeAssemblyFactory assemblyFactory; + private StAXArtifactProcessorExtensionPoint staxProcessors; + private StAXArtifactProcessor<EndpointReference> staxProcessor; + private XMLInputFactory xmlInputFactory; + private XMLOutputFactory xmlOutputFactory; + private BuilderExtensionPoint builders; /* * Public constructor for Externalizable serialization/deserialization @@ -48,34 +108,371 @@ public class ServiceReferenceImpl<B> extends CallableReferenceImpl<B> { /* * Public constructor for use by XMLStreamReader2CallableReference */ - // TODO - EPR - is this required + // TODO - EPR - Is this required public ServiceReferenceImpl(XMLStreamReader xmlReader) throws Exception { - super(xmlReader); + this.xmlReader = xmlReader; + resolve(); + } + + protected ServiceReferenceImpl(Class<B> businessInterface, + RuntimeComponent component, + RuntimeComponentReference reference, + EndpointReference endpointReference, + ProxyFactory proxyFactory, + CompositeContext compositeContext) { + this.proxyFactory = proxyFactory; + this.businessInterface = businessInterface; + this.component = component; + this.reference = reference; + this.endpointReference = endpointReference; + this.compositeContext = compositeContext; + + getExtensions(); + + // FIXME: The SCA Specification is not clear how we should handle multiplicity + // for CallableReference + if (this.endpointReference == null) { + + // TODO - EPR - If no endpoint reference specified assume the first one + // This will happen when a self reference is created in which case the + // the reference should only have one endpointReference so use that + if (this.reference.getEndpointReferences().size() == 0){ + throw new ServiceRuntimeException("The reference " + reference.getName() + " in component " + + component.getName() + " has no endpoint references"); + } + + if (this.reference.getEndpointReferences().size() > 1){ + throw new ServiceRuntimeException("The reference " + reference.getName() + " in component " + + component.getName() + " has more than one endpoint reference"); + } + + this.endpointReference = this.reference.getEndpointReferences().get(0); + } + + // FIXME: Should we normalize the componentName/serviceName URI into an absolute SCA URI in the SCA binding? + // sca:component1/component11/component112/service1? + initCallbackID(); + } + + private void getExtensions() { + this.registry = compositeContext.getExtensionPointRegistry(); + this.modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); + this.assemblyFactory = (RuntimeAssemblyFactory)modelFactories.getFactory(AssemblyFactory.class); + this.xmlInputFactory = modelFactories.getFactory(XMLInputFactory.class); + this.xmlOutputFactory = modelFactories.getFactory(XMLOutputFactory.class); + this.staxProcessors = registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + this.staxProcessor = staxProcessors.getProcessor(EndpointReference.class); + this.builders = registry.getExtensionPoint(BuilderExtensionPoint.class); + } + + public ServiceReferenceImpl(Class<B> businessInterface, RuntimeWire wire, ProxyFactory proxyFactory) { + this.proxyFactory = proxyFactory; + this.businessInterface = businessInterface; + //ExtensionPointRegistry registry = ((RuntimeWireImpl)wire).getExtensionPoints(); + //this.modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); + //this.assemblyFactory = (RuntimeAssemblyFactory)modelFactories.getFactory(AssemblyFactory.class); + bind(wire); + } + + public RuntimeWire getRuntimeWire() { + try { + resolve(); + if (endpointReference != null){ + return reference.getRuntimeWire(endpointReference); + } else { + return null; + } + } catch (Exception e) { + throw new ServiceRuntimeException(e); + } + } + + public EndpointReference getEndpointReference() { + return endpointReference; + } + + protected void bind(RuntimeWire wire) { + if (wire != null) { + this.component = (RuntimeComponent)wire.getEndpointReference().getComponent(); + this.reference = (RuntimeComponentReference)wire.getEndpointReference().getReference(); + this.endpointReference = wire.getEndpointReference(); + this.compositeContext = component.getComponentContext().getCompositeContext(); + initCallbackID(); + } + } + + protected void initCallbackID() { + if (reference.getInterfaceContract() != null) { + if (reference.getInterfaceContract().getCallbackInterface() != null) { + this.callbackID = createCallbackID(); + } + } + } + + public B getProxy() throws ObjectCreationException { + try { + if (proxy == null) { + proxy = createProxy(); + } + return businessInterface.cast(proxy); + } catch (Exception e) { + throw new ObjectCreationException(e); + } + } + + public void setProxy(Object proxy) { + this.proxy = proxy; + } + + protected Object createProxy() throws Exception { + return proxyFactory.createProxy(this); + } + + public B getService() { + try { + resolve(); + return getProxy(); + } catch (Exception e) { + throw new ServiceRuntimeException(e); + } + } + + public Class<B> getBusinessInterface() { + try { + resolve(); + return businessInterface; + } catch (Exception e) { + throw new ServiceRuntimeException(e); + } + } + + public boolean isConversational() { + try { + resolve(); + return reference == null ? false : reference.getInterfaceContract().getInterface().isConversational(); + } catch (Exception e) { + throw new ServiceRuntimeException(e); + } + } + + public Object getCallbackID() { + try { + resolve(); + return callbackID; + } catch (Exception e) { + throw new ServiceRuntimeException(e); + } } /** - * @param businessInterface - * @param wire - * @param proxyFactory + * Follow a service promotion chain down to the inner most (non composite) + * component service. + * + * @param topCompositeService + * @return */ - public ServiceReferenceImpl(Class<B> businessInterface, RuntimeWire wire, ProxyFactory proxyFactory) { - super(businessInterface, wire, proxyFactory); + 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; + } + } + + // ============ WRITE AND READ THE REFERENCE TO EXTERNAL XML ======================== + + /** + * write the reference to a stream + * + * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput) + */ + public void writeExternal(ObjectOutput out) throws IOException { + try { + String xml = null; + if (scdl == null){ + xml = toXMLString(); + } else { + xml = scdl; + } + + if (xml == null) { + out.writeBoolean(false); + } else { + out.writeBoolean(true); + out.writeUTF(xml); + } + } catch (Exception e) { + // e.printStackTrace(); + throw new IOException(e.toString()); + } + } + + /** + * write the endpoint reference into an xml string + */ + public String toXMLString() throws IOException, XMLStreamException, ContributionWriteException{ + StringWriter writer = new StringWriter(); + XMLStreamWriter streamWriter = xmlOutputFactory.createXMLStreamWriter(writer); + staxProcessor.write(endpointReference, streamWriter); + return writer.toString(); + } + + /** + * Read the reference from a stream + * + * @see java.io.Externalizable#readExternal(java.io.ObjectInput) + */ + public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + final boolean hasSCDL = in.readBoolean(); + if (hasSCDL) { + this.scdl = in.readUTF(); + } else { + this.scdl = null; + } + } + + /** + * Read xml string into the endpoint reference + */ + public void fromXMLString() throws IOException, XMLStreamException, ContributionReadException { + + XMLStreamReader streamReader = xmlReader; + + if (scdl != null ){ + Reader reader = new StringReader(scdl); + + if (xmlInputFactory == null){ + // this is a reference being read from a external stream + // so set up enough of the reference in order to resolved the + // xml being read + this.compositeContext = CompositeContext.getCurrentCompositeContext(); + getExtensions(); + } + + streamReader = xmlInputFactory.createXMLStreamReader(reader); + } + + endpointReference = staxProcessor.read(streamReader); + + // ok to GC + xmlReader = null; + scdl = null; + } + + /** + * @throws IOException + */ + private synchronized void resolve() throws Exception { + if ((scdl != null || xmlReader != null) && component == null && reference == null) { + fromXMLString(); + + this.component = (RuntimeComponent)endpointReference.getComponent(); + compositeContext.configureComponentContext(this.component); + + this.reference = (RuntimeComponentReference)endpointReference.getReference(); + this.reference.setComponent(this.component); + + ReferenceParameters parameters = null; + for (Object ext : reference.getExtensions()) { + if (ext instanceof ReferenceParameters) { + parameters = (ReferenceParameters)ext; + break; + } + } + + if (parameters != null) { + this.callbackID = parameters.getCallbackID(); + } + + Interface i = reference.getInterfaceContract().getInterface(); + if (i instanceof JavaInterface) { + JavaInterface javaInterface = (JavaInterface)i; + if (javaInterface.isUnresolved()) { + // Allow privileged access to get ClassLoader. Requires RuntimePermission in + // security policy. + ClassLoader classLoader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { + public ClassLoader run() { + return Thread.currentThread().getContextClassLoader(); + } + }); + + javaInterface.setJavaClass(classLoader.loadClass(javaInterface.getName())); + JavaInterfaceFactory javaInterfaceFactory = getJavaInterfaceFactory(compositeContext); + + javaInterfaceFactory.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(); + } + + Binding binding = endpointReference.getBinding(); + if (binding != null) { + BindingBuilder bindingBuilder = builders.getBindingBuilder(binding.getClass()); + if (bindingBuilder != null) { + bindingBuilder.build(component, reference, endpointReference.getBinding(), null); + } + } + + this.proxyFactory = getProxyFactory(this.compositeContext); + } else if (compositeContext == null) { + this.compositeContext = CompositeContext.getCurrentCompositeContext(); + if (this.compositeContext != null) { + this.proxyFactory = getProxyFactory(this.compositeContext); + } + } + } + + private JavaInterfaceFactory getJavaInterfaceFactory(CompositeContext compositeContext) { + ExtensionPointRegistry extensionPointRegistry = compositeContext.getExtensionPointRegistry(); + FactoryExtensionPoint factories = extensionPointRegistry.getExtensionPoint(FactoryExtensionPoint.class); + JavaInterfaceFactory javaInterfaceFactory = factories.getFactory(JavaInterfaceFactory.class); + return javaInterfaceFactory; + } + + private ProxyFactory getProxyFactory(CompositeContext compositeContext) { + ExtensionPointRegistry extensionPointRegistry = compositeContext.getExtensionPointRegistry(); + ProxyFactoryExtensionPoint proxyFactories = extensionPointRegistry.getExtensionPoint(ProxyFactoryExtensionPoint.class); + return new ExtensibleProxyFactory(proxyFactories); + } + + // ================================================================================== + + /** + * Create a callback id + * + * @return the callback id + */ + private String createCallbackID() { + return UUID.randomUUID().toString(); + } + + public void attachCallbackID(Object callbackID) { + this.callbackID = callbackID; } - public ServiceReferenceImpl(Class<B> businessInterface, - RuntimeComponent component, - RuntimeComponentReference reference, - ProxyFactory proxyFactory, - CompositeActivator compositeActivator) { - super(businessInterface, component, reference, null, proxyFactory, compositeActivator); + protected ReferenceParameters getReferenceParameters() { + ReferenceParameters parameters = new ReferenceParametersImpl(); + parameters.setCallbackID(callbackID); + return parameters; } - public ServiceReferenceImpl(Class<B> businessInterface, - RuntimeComponent component, - RuntimeComponentReference reference, - EndpointReference endpointReference, - ProxyFactory proxyFactory, - CompositeActivator compositeActivator) { - super(businessInterface, component, reference, endpointReference, proxyFactory, compositeActivator); + public XMLStreamReader getXMLReader() { + return xmlReader; } } diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CglibProxyFactory.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CglibProxyFactory.java index 0bf7b3554e..11edc283a4 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CglibProxyFactory.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CglibProxyFactory.java @@ -28,7 +28,6 @@ import net.sf.cglib.proxy.Factory; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; -import org.apache.tuscany.sca.core.context.impl.CallableReferenceImpl; import org.apache.tuscany.sca.core.context.impl.ServiceReferenceImpl; import org.apache.tuscany.sca.core.invocation.impl.JDKCallbackInvocationHandler; import org.apache.tuscany.sca.core.invocation.impl.JDKInvocationHandler; |