diff options
Diffstat (limited to 'java/sca/modules/core/src/main/java/org/apache/tuscany')
27 files changed, 1380 insertions, 2091 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 f8e3a70ad2..9811667b17 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 @@ -21,12 +21,10 @@ package org.apache.tuscany.sca.core.assembly.impl; import java.security.AccessController; import java.security.PrivilegedAction; -import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; 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; @@ -34,20 +32,15 @@ import org.apache.tuscany.sca.assembly.Composite; import org.apache.tuscany.sca.assembly.Endpoint; 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.CompositeContext; 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.invocation.ExtensibleWireProcessor; import org.apache.tuscany.sca.core.scope.ScopeContainer; import org.apache.tuscany.sca.core.scope.ScopeRegistry; import org.apache.tuscany.sca.core.scope.ScopedRuntimeComponent; import org.apache.tuscany.sca.interfacedef.InterfaceContract; -import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; -import org.apache.tuscany.sca.invocation.MessageFactory; -import org.apache.tuscany.sca.provider.BindingProviderFactory; import org.apache.tuscany.sca.provider.ImplementationProvider; import org.apache.tuscany.sca.provider.ImplementationProviderFactory; import org.apache.tuscany.sca.provider.PolicyProvider; @@ -61,10 +54,8 @@ 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.apache.tuscany.sca.runtime.RuntimeWire; -import org.apache.tuscany.sca.runtime.RuntimeWireProcessor; -import org.apache.tuscany.sca.runtime.RuntimeWireProcessorExtensionPoint; -import org.apache.tuscany.sca.work.WorkScheduler; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; /** * @version $Rev$ $Date$ @@ -72,25 +63,15 @@ import org.apache.tuscany.sca.work.WorkScheduler; public class CompositeActivatorImpl implements CompositeActivator { final Logger logger = Logger.getLogger(CompositeActivatorImpl.class.getName()); - private final ExtensionPointRegistry extensionPoints; private final AssemblyFactory assemblyFactory; - private final MessageFactory messageFactory; - private final InterfaceContractMapper interfaceContractMapper; private final ScopeRegistry scopeRegistry; - private final WorkScheduler workScheduler; - private final RuntimeWireProcessor wireProcessor; private final ProviderFactoryExtensionPoint providerFactories; public CompositeActivatorImpl(ExtensionPointRegistry extensionPoints) { - this.extensionPoints = extensionPoints; UtilityExtensionPoint utilities = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class); FactoryExtensionPoint factories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); this.assemblyFactory = factories.getFactory(AssemblyFactory.class); - this.messageFactory = factories.getFactory(MessageFactory.class); - this.interfaceContractMapper = utilities.getUtility(InterfaceContractMapper.class); this.scopeRegistry = utilities.getUtility(ScopeRegistry.class); - this.workScheduler = utilities.getUtility(WorkScheduler.class); - this.wireProcessor = new ExtensibleWireProcessor(extensionPoints.getExtensionPoint(RuntimeWireProcessorExtensionPoint.class)); this.providerFactories = extensionPoints.getExtensionPoint(ProviderFactoryExtensionPoint.class); } @@ -251,77 +232,10 @@ public class CompositeActivatorImpl implements CompositeActivator { logger.fine("Activating component service: " + component.getURI() + "#" + service.getName()); } - for (Endpoint endpoint : service.getEndpoints()) { - addServiceBindingProvider(endpoint, component, service, endpoint.getBinding()); - } - addServiceWires(compositeContext, component, service); - } - - public void deactivate(RuntimeComponent component, RuntimeComponentService service) { - if (logger.isLoggable(Level.FINE)) { - logger.fine("Deactivating component service: " + component.getURI() + "#" + service.getName()); - } - removeServiceWires(service); - for (Binding binding : service.getBindings()) { - removeServiceBindingProvider(component, service, binding); - } - } - - private ServiceBindingProvider addServiceBindingProvider( - Endpoint endpoint, - RuntimeComponent component, RuntimeComponentService service, - Binding binding) { - BindingProviderFactory providerFactory = (BindingProviderFactory) providerFactories - .getProviderFactory(binding.getClass()); - if (providerFactory != null) { - @SuppressWarnings("unchecked") - ServiceBindingProvider bindingProvider = providerFactory - .createServiceBindingProvider(endpoint); - if (bindingProvider != null) { - ((RuntimeComponentService) service).setBindingProvider(binding, - bindingProvider); - } - for (PolicyProviderFactory f : providerFactories.getPolicyProviderFactories()) { - PolicyProvider policyProvider = f.createServicePolicyProvider(endpoint); - if (policyProvider != null) { - service.addPolicyProvider(binding, policyProvider); - } - } - return bindingProvider; - } else { - throw new IllegalStateException( - "Provider factory not found for binding: " - + binding.getType()); - } - } - - private void removeServiceBindingProvider(RuntimeComponent component, - RuntimeComponentService service, Binding binding) { - service.setBindingProvider(binding, null); - for (Binding b : service.getBindings()) { - List<PolicyProvider> pps = service.getPolicyProviders(b); - if (pps != null) { - pps.clear(); - } - } - } - - private void addServiceWires(CompositeContext compositeContext, Component serviceComponent, ComponentService service) { - if (!(service instanceof RuntimeComponentService)) { - return; - } - - RuntimeComponentService runtimeService = (RuntimeComponentService)service; - // Add a wire for each service Endpoint - for ( Endpoint endpoint : runtimeService.getEndpoints()){ - - // fluff up a fake endpoint reference as we are on the service side - // so we need to represent the reference that will call us - EndpointReference endpointReference = assemblyFactory.createEndpointReference(); - endpointReference.setBinding(endpoint.getBinding()); - endpointReference.setTargetEndpoint(endpoint); - endpointReference.setStatus(EndpointReference.WIRED_TARGET_FOUND_AND_MATCHED); + for ( Endpoint endpoint : service.getEndpoints()){ + RuntimeEndpoint ep = (RuntimeEndpoint) endpoint; + ep.bind(compositeContext); // create the interface contract for the binding and service ends of the wire // that are created as forward only contracts @@ -331,45 +245,18 @@ public class CompositeActivatorImpl implements CompositeActivator { targetService = service; } endpoint.setInterfaceContract(targetService.getInterfaceContract().makeUnidirectional(false)); - endpointReference.setInterfaceContract(getServiceBindingInterfaceContract(service, endpoint.getBinding())); - - // create the wire - RuntimeWire wire = new RuntimeWireImpl(compositeContext, - false, - endpointReference, - endpoint, - interfaceContractMapper, - workScheduler, - wireProcessor, - messageFactory); - - // TODO - EPR - TUSCANY-3187 - keep and eye on this as to code - // has been reported to be working without this fix in some - // environments - //runtimeService.getRuntimeWires().add(wire); - ((RuntimeComponentService)endpoint.getService()).getRuntimeWires().add(wire); } } - private void removeServiceWires(ComponentService service) { - if (!(service instanceof RuntimeComponentService)) { - return; + public void deactivate(RuntimeComponent component, RuntimeComponentService service) { + if (logger.isLoggable(Level.FINE)) { + logger.fine("Deactivating component service: " + component.getURI() + "#" + service.getName()); } - RuntimeComponentService runtimeService = (RuntimeComponentService)service; - runtimeService.getRuntimeWires().clear(); - } - - private InterfaceContract getServiceBindingInterfaceContract(ComponentService service, Binding binding) { - InterfaceContract interfaceContract = service.getInterfaceContract(); - - ServiceBindingProvider provider = ((RuntimeComponentService)service).getBindingProvider(binding); - if (provider != null) { - InterfaceContract bindingContract = provider.getBindingInterfaceContract(); - if (bindingContract != null) { - interfaceContract = bindingContract; + for(Endpoint ep: service.getEndpoints()) { + if(ep instanceof RuntimeEndpoint) { + ((RuntimeEndpoint) ep).unbind(); } } - return interfaceContract.makeUnidirectional(false); } // Reference activation/deactivation @@ -383,34 +270,19 @@ public class CompositeActivatorImpl implements CompositeActivator { // reference is asked to return it's runtime wires. If there are none the reference // asks the component context to start the reference which creates the wires reference.setComponent(component); + for(EndpointReference epr: reference.getEndpointReferences()) { + addReferenceWire(compositeContext, epr); + } + + // TODO reference wires are added at component start for some reason } public void deactivate(RuntimeComponent component, RuntimeComponentReference reference) { if (logger.isLoggable(Level.FINE)) { logger.fine("Deactivating component reference: " + component.getURI() + "#" + reference.getName()); } - removeReferenceWires(reference); - for (EndpointReference endpointReference : reference.getEndpointReferences()) { - if (endpointReference.getBinding() != null){ - removeReferenceBindingProvider(component, reference, endpointReference.getBinding()); - } - } - } - - private void removeReferenceBindingProvider(RuntimeComponent component, - RuntimeComponentReference reference, Binding binding) { - reference.setBindingProvider(binding, null); - for (Binding b : reference.getBindings()) { - List<PolicyProvider> pps = reference.getPolicyProviders(b); - if (pps != null) { - pps.clear(); - } - } - } - - private void removeReferenceWires(ComponentReference reference) { - if (!(reference instanceof RuntimeComponentReference)) { - return; + for(EndpointReference endpointReference: reference.getEndpointReferences()) { + ((RuntimeEndpointReference) endpointReference).unbind(); } } @@ -476,14 +348,14 @@ public class CompositeActivatorImpl implements CompositeActivator { if (logger.isLoggable(Level.FINE)) { logger.fine("Starting component service: " + component.getURI() + "#" + service.getName()); } - RuntimeComponentService runtimeService = (RuntimeComponentService)service; for (Endpoint endpoint : service.getEndpoints()) { + RuntimeEndpoint ep = (RuntimeEndpoint) endpoint; // FIXME: Should the policy providers be started before the endpoint is started? - for (PolicyProvider policyProvider : runtimeService.getPolicyProviders(endpoint.getBinding())) { + for (PolicyProvider policyProvider : ep.getPolicyProviders()) { policyProvider.start(); } - final ServiceBindingProvider bindingProvider = runtimeService.getBindingProvider(endpoint.getBinding()); + final ServiceBindingProvider bindingProvider = ep.getBindingProvider(); if (bindingProvider != null) { // bindingProvider.start(); // Allow bindings to add shutdown hooks. Requires RuntimePermission shutdownHooks in policy. @@ -513,8 +385,9 @@ public class CompositeActivatorImpl implements CompositeActivator { logger.fine("Stopping component service: " + component.getURI() + "#" + service.getName()); } for (Endpoint endpoint : service.getEndpoints()) { + RuntimeEndpoint ep = (RuntimeEndpoint) endpoint; compositeContext.getEndpointRegistry().removeEndpoint(endpoint); - final ServiceBindingProvider bindingProvider = ((RuntimeComponentService)service).getBindingProvider(endpoint.getBinding()); + final ServiceBindingProvider bindingProvider = ep.getBindingProvider(); if (bindingProvider != null) { // Allow bindings to read properties. Requires PropertyPermission read in security policy. AccessController.doPrivileged(new PrivilegedAction<Object>() { @@ -524,8 +397,7 @@ public class CompositeActivatorImpl implements CompositeActivator { } }); } - for (PolicyProvider policyProvider : ((RuntimeComponentService)service).getPolicyProviders(endpoint - .getBinding())) { + for (PolicyProvider policyProvider : ep.getPolicyProviders()) { policyProvider.stop(); } } @@ -534,11 +406,11 @@ public class CompositeActivatorImpl implements CompositeActivator { if (logger.isLoggable(Level.FINE)) { logger.fine("Stopping component reference: " + component.getURI() + "#" + reference.getName()); } - RuntimeComponentReference runtimeRef = ((RuntimeComponentReference)reference); for (EndpointReference endpointReference : reference.getEndpointReferences()) { + RuntimeEndpointReference epr = (RuntimeEndpointReference) endpointReference; compositeContext.getEndpointRegistry().removeEndpointReference(endpointReference); - final ReferenceBindingProvider bindingProvider = runtimeRef.getBindingProvider(endpointReference.getBinding()); + final ReferenceBindingProvider bindingProvider = epr.getBindingProvider(); if (bindingProvider != null) { // Allow bindings to read properties. Requires PropertyPermission read in security policy. AccessController.doPrivileged(new PrivilegedAction<Object>() { @@ -548,8 +420,7 @@ public class CompositeActivatorImpl implements CompositeActivator { } }); } - for (PolicyProvider policyProvider : ((RuntimeComponentReference)reference) - .getPolicyProviders(endpointReference.getBinding())) { + for (PolicyProvider policyProvider : epr.getPolicyProviders()) { policyProvider.stop(); } @@ -607,8 +478,8 @@ public class CompositeActivatorImpl implements CompositeActivator { // current composite). Endpoint reference resolution takes place when the wire // is first used (when the chains are created) for (EndpointReference endpointReference : componentReference.getEndpointReferences()){ - addReferenceWire(compositeContext, component, componentReference, endpointReference); - component.getComponentContext().getCompositeContext().getEndpointRegistry().addEndpointReference(endpointReference); + // addReferenceWire(compositeContext, endpointReference); + compositeContext.getEndpointRegistry().addEndpointReference(endpointReference); } } @@ -622,67 +493,39 @@ public class CompositeActivatorImpl implements CompositeActivator { RuntimeComponent runtimeComponent = (RuntimeComponent) component; EndpointRegistry endpointRegistry = runtimeComponent.getComponentContext().getCompositeContext().getEndpointRegistry(); for ( EndpointReference endpointReference : runtimeRef.getEndpointReferences()){ + RuntimeEndpointReference epr = (RuntimeEndpointReference) endpointReference; endpointRegistry.removeEndpointReference(endpointReference); - ReferenceBindingProvider bindingProvider = runtimeRef.getBindingProvider(endpointReference.getBinding()); + ReferenceBindingProvider bindingProvider = epr.getBindingProvider(); if (bindingProvider != null) { bindingProvider.stop(); } - for (PolicyProvider policyProvider : ((RuntimeComponentReference)reference) - .getPolicyProviders(endpointReference.getBinding())) { + for (PolicyProvider policyProvider : epr.getPolicyProviders()) { policyProvider.stop(); } } } - private void addReferenceWire(CompositeContext compositeContext, Component component, ComponentReference reference, EndpointReference endpointReference) { - RuntimeComponentReference runtimeRef = (RuntimeComponentReference)reference; - - // Use the interface contract of the reference on the component type and if there - // isn't one then use the one from the reference itself - Reference componentTypeRef = reference.getReference(); + private void addReferenceWire(CompositeContext compositeContext, EndpointReference endpointReference) { + RuntimeEndpointReference epr = (RuntimeEndpointReference) endpointReference; + // create the wire + // null endpoint passed in here as the endpoint reference may + // not be resolved yet + epr.bind(compositeContext); - InterfaceContract sourceContract; - if (componentTypeRef == null || componentTypeRef.getInterfaceContract() == null) { - sourceContract = reference.getInterfaceContract(); - } else { - sourceContract = componentTypeRef.getInterfaceContract(); - } + ComponentReference reference = endpointReference.getReference(); + InterfaceContract sourceContract = epr.getReferenceInterfaceContract(); // TODO - EPR - interface contract seems to be null in the implementation.web // case. Not introspecting the CT properly? if (sourceContract == null){ // TODO - Can't do this with move of matching to wire // take the contract from the service to which the reference is connected - sourceContract = endpointReference.getTargetEndpoint().getInterfaceContract(); + sourceContract = ((RuntimeEndpoint) endpointReference.getTargetEndpoint()).getServiceInterfaceContract(); reference.setInterfaceContract(sourceContract); } endpointReference.setInterfaceContract(sourceContract.makeUnidirectional(false)); - - // create the wire - // null endpoint passed in here as the endpoint reference may - // not be resolved yet - RuntimeWire wire = new RuntimeWireImpl(compositeContext, - true, - endpointReference, - null, - interfaceContractMapper, - workScheduler, - wireProcessor, - messageFactory); - runtimeRef.getRuntimeWires().add(wire); - } - private InterfaceContract getInterfaceContract(ComponentReference reference, Binding binding) { - InterfaceContract interfaceContract = reference.getInterfaceContract(); - ReferenceBindingProvider provider = ((RuntimeComponentReference)reference).getBindingProvider(binding); - if (provider != null) { - InterfaceContract bindingContract = provider.getBindingInterfaceContract(); - if (bindingContract != null) { - interfaceContract = bindingContract; - } - } - return interfaceContract.makeUnidirectional(false); - } + } diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/EndpointSerializerImpl.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/EndpointSerializerImpl.java index 82f8ebabea..fb0f327720 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/EndpointSerializerImpl.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/EndpointSerializerImpl.java @@ -36,6 +36,7 @@ import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtens import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.runtime.EndpointSerializer; +import org.oasisopen.sca.ServiceRuntimeException; public class EndpointSerializerImpl implements EndpointSerializer { private ExtensionPointRegistry registry; @@ -91,8 +92,10 @@ public class EndpointSerializerImpl implements EndpointSerializer { endpointReference.setReference(result.getReference()); endpointReference.setBinding(result.getBinding()); endpointReference.setInterfaceContract(result.getReference().getInterfaceContract()); + endpointReference.setTargetEndpoint(result.getTargetEndpoint()); + endpointReference.setCallbackEndpoint(result.getCallbackEndpoint()); } catch (Exception e) { - throw new IOException(e.getMessage()); + throw new ServiceRuntimeException(e); } } @@ -105,7 +108,7 @@ public class EndpointSerializerImpl implements EndpointSerializer { writer.close(); return sw.toString(); } catch (Exception e) { - throw new IOException(e.getMessage()); + throw new ServiceRuntimeException(e); } } }
\ No newline at end of file diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeComponentReferenceImpl.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeComponentReferenceImpl.java index 3fdc35a720..2eea60ccea 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeComponentReferenceImpl.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeComponentReferenceImpl.java @@ -19,22 +19,9 @@ package org.apache.tuscany.sca.core.assembly.impl; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; - -import org.apache.tuscany.sca.assembly.Binding; -import org.apache.tuscany.sca.assembly.EndpointReference; import org.apache.tuscany.sca.assembly.impl.ComponentReferenceImpl; -import org.apache.tuscany.sca.interfacedef.Operation; -import org.apache.tuscany.sca.invocation.InvocationChain; -import org.apache.tuscany.sca.invocation.Invoker; -import org.apache.tuscany.sca.provider.PolicyProvider; -import org.apache.tuscany.sca.provider.ReferenceBindingProvider; import org.apache.tuscany.sca.runtime.RuntimeComponent; import org.apache.tuscany.sca.runtime.RuntimeComponentReference; -import org.apache.tuscany.sca.runtime.RuntimeWire; /** * Implementation of a Component Reference. @@ -42,10 +29,6 @@ import org.apache.tuscany.sca.runtime.RuntimeWire; * @version $Rev$ $Date$ */ public class RuntimeComponentReferenceImpl extends ComponentReferenceImpl implements RuntimeComponentReference { - private ArrayList<RuntimeWire> wires; - private HashMap<Binding, ReferenceBindingProvider> bindingProviders = - new HashMap<Binding, ReferenceBindingProvider>(); - private HashMap<Binding, List<PolicyProvider>> policyProviders = new HashMap<Binding, List<PolicyProvider>>(); private RuntimeComponent component; @@ -53,52 +36,6 @@ public class RuntimeComponentReferenceImpl extends ComponentReferenceImpl implem super(); } - public synchronized List<RuntimeWire> getRuntimeWires() { - if (wires == null) { - wires = new ArrayList<RuntimeWire>(); - component.getComponentContext().start(this); - } - return wires; - } - - // TODO - EPR - shouldn't rely on this anymore - public RuntimeWire getRuntimeWire(Binding binding) { - for (RuntimeWire wire : getRuntimeWires()) { - if (wire.getEndpointReference().getBinding() == binding) { - return wire; - } - } - - return null; - } - - public RuntimeWire getRuntimeWire(EndpointReference endpointReference) { - for (RuntimeWire wire : getRuntimeWires()) { - if (wire.getEndpointReference() == endpointReference) { - return wire; - } - } - - return null; - } - - public ReferenceBindingProvider getBindingProvider(Binding binding) { - return bindingProviders.get(binding); - } - - public void setBindingProvider(Binding binding, ReferenceBindingProvider bindingProvider) { - bindingProviders.put(binding, bindingProvider); - } - - public Invoker getInvoker(Binding binding, Operation operation) { - RuntimeWire wire = getRuntimeWire(binding); - if (wire == null) { - return null; - } - InvocationChain chain = wire.getInvocationChain(operation); - return chain == null ? null : chain.getHeadInvoker(); - } - /** * @return the component */ @@ -119,30 +56,9 @@ public class RuntimeComponentReferenceImpl extends ComponentReferenceImpl implem @Override public Object clone() throws CloneNotSupportedException { RuntimeComponentReferenceImpl ref = (RuntimeComponentReferenceImpl)super.clone(); - ref.wires = null; - ref.bindingProviders = new HashMap<Binding, ReferenceBindingProvider>(); - ref.policyProviders = new HashMap<Binding, List<PolicyProvider>>(); return ref; } - public void addPolicyProvider(Binding binding, PolicyProvider policyProvider) { - List<PolicyProvider> providers = policyProviders.get(binding); - if (providers == null) { - providers = new ArrayList<PolicyProvider>(); - policyProviders.put(binding, providers); - } - providers.add(policyProvider); - } - - public List<PolicyProvider> getPolicyProviders(Binding binding) { - List<PolicyProvider> providers = policyProviders.get(binding); - if (providers == null) { - return Collections.emptyList(); - } else { - return providers; - } - } - @Override public String toString() { return getName(); diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeComponentServiceImpl.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeComponentServiceImpl.java index ffa488b7bd..bc7e6df878 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeComponentServiceImpl.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeComponentServiceImpl.java @@ -19,22 +19,8 @@ package org.apache.tuscany.sca.core.assembly.impl; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; - -import org.apache.tuscany.sca.assembly.Binding; import org.apache.tuscany.sca.assembly.impl.ComponentServiceImpl; -import org.apache.tuscany.sca.interfacedef.InterfaceContract; -import org.apache.tuscany.sca.interfacedef.Operation; -import org.apache.tuscany.sca.invocation.InvocationChain; -import org.apache.tuscany.sca.invocation.Invoker; -import org.apache.tuscany.sca.provider.PolicyProvider; -import org.apache.tuscany.sca.provider.ServiceBindingProvider; import org.apache.tuscany.sca.runtime.RuntimeComponentService; -import org.apache.tuscany.sca.runtime.RuntimeWire; -import org.oasisopen.sca.ServiceRuntimeException; /** * Implementation of a Component Service. @@ -42,126 +28,11 @@ import org.oasisopen.sca.ServiceRuntimeException; * @version $Rev$ $Date$ */ public class RuntimeComponentServiceImpl extends ComponentServiceImpl implements RuntimeComponentService { - private ArrayList<RuntimeWire> wires = new ArrayList<RuntimeWire>(); - private ArrayList<RuntimeWire> callbackWires = new ArrayList<RuntimeWire>(); - private HashMap<Binding, ServiceBindingProvider> bindingProviders = new HashMap<Binding, ServiceBindingProvider>(); - private HashMap<Binding, List<PolicyProvider>> policyProviders = new HashMap<Binding, List<PolicyProvider>>(); public RuntimeComponentServiceImpl() { super(); } - public List<RuntimeWire> getRuntimeWires() { - return wires; - } - - public RuntimeWire getRuntimeWire(Binding binding) { - for (RuntimeWire wire : wires) { - if (wire.getEndpoint().getBinding() == binding) { - return wire; - } - } - return null; - } - - public RuntimeWire getRuntimeWire(Binding binding, InterfaceContract interfaceContract) { - RuntimeWire wire = getRuntimeWire(binding); - if (wire == null) { - return null; - } - if (interfaceContract != null && interfaceContract != wire.getEndpointReference().getInterfaceContract()) { - try { - // FIXME: [rfeng] We could avoid clone() using a better comparison of the two interface contracts - wire = (RuntimeWire)wire.clone(); - wire.getEndpointReference().setInterfaceContract(interfaceContract); - wire.rebuild(); - } catch (CloneNotSupportedException e) { - throw new ServiceRuntimeException(e); - } - } - - return wire; - } - - public List<RuntimeWire> getCallbackWires() { - return callbackWires; - } - - public ServiceBindingProvider getBindingProvider(Binding binding) { - return bindingProviders.get(binding); - } - - public void setBindingProvider(Binding binding, ServiceBindingProvider bindingProvider) { - bindingProviders.put(binding, bindingProvider); - } - - public Invoker getInvoker(Binding binding, Operation operation) { - return getInvoker(binding, null, operation); - } - - public Invoker getInvoker(Binding binding, InterfaceContract interfaceContract, Operation operation) { - InvocationChain chain = getInvocationChain(binding, interfaceContract, operation); - if (chain != null) { - return chain.getHeadInvoker(); - } else { - return null; - } - } - - public InvocationChain getInvocationChain(Binding binding, InterfaceContract interfaceContract, Operation operation) { - RuntimeWire wire = getRuntimeWire(binding); - if (wire == null) { - return null; - } - if (interfaceContract != null && interfaceContract != wire.getEndpointReference().getInterfaceContract()) { - try { - // FIXME: [rfeng] We could avoid clone() using a better comparison of the two interface contracts - wire = (RuntimeWire)wire.clone(); - wire.getEndpointReference().setInterfaceContract(interfaceContract); - wire.rebuild(); - } catch (CloneNotSupportedException e) { - throw new ServiceRuntimeException(e); - } - } - return wire.getInvocationChain(operation); - } - - public InvocationChain getInvocationChain(Binding binding, Operation operation) { - return getInvocationChain(binding, null, operation); - } - - /** - * @see org.apache.tuscany.sca.assembly.impl.ComponentServiceImpl#clone() - */ - @SuppressWarnings("unchecked") - @Override - public Object clone() throws CloneNotSupportedException { - RuntimeComponentServiceImpl clone = (RuntimeComponentServiceImpl)super.clone(); - clone.bindingProviders = (HashMap<Binding, ServiceBindingProvider>)bindingProviders.clone(); - clone.wires = (ArrayList<RuntimeWire>)wires.clone(); - clone.callbackWires = (ArrayList<RuntimeWire>)callbackWires.clone(); - clone.policyProviders = (HashMap<Binding, List<PolicyProvider>>)policyProviders.clone(); - return clone; - } - - public void addPolicyProvider(Binding binding, PolicyProvider policyProvider) { - List<PolicyProvider> providers = policyProviders.get(binding); - if (providers == null) { - providers = new ArrayList<PolicyProvider>(); - policyProviders.put(binding, providers); - } - providers.add(policyProvider); - } - - public List<PolicyProvider> getPolicyProviders(Binding binding) { - List<PolicyProvider> providers = policyProviders.get(binding); - if (providers == null) { - return Collections.emptyList(); - } else { - return providers; - } - } - @Override public String toString() { return getName(); diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeEndpointImpl.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeEndpointImpl.java index 357d19091b..2f60b2d126 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeEndpointImpl.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeEndpointImpl.java @@ -23,20 +23,82 @@ import java.io.Externalizable; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +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.CompositeReference; +import org.apache.tuscany.sca.assembly.CompositeService; +import org.apache.tuscany.sca.assembly.Contract; +import org.apache.tuscany.sca.assembly.Service; import org.apache.tuscany.sca.assembly.impl.EndpointImpl; +import org.apache.tuscany.sca.context.CompositeContext; 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.invocation.ExtensibleWireProcessor; +import org.apache.tuscany.sca.core.invocation.NonBlockingInterceptor; +import org.apache.tuscany.sca.core.invocation.RuntimeInvoker; +import org.apache.tuscany.sca.core.invocation.impl.InvocationChainImpl; +import org.apache.tuscany.sca.core.invocation.impl.PhaseManager; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Interceptor; +import org.apache.tuscany.sca.invocation.InvocationChain; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.invocation.Phase; +import org.apache.tuscany.sca.provider.BindingPolicyProvider; +import org.apache.tuscany.sca.provider.BindingProviderFactory; +import org.apache.tuscany.sca.provider.EndpointProvider; +import org.apache.tuscany.sca.provider.ImplementationProvider; +import org.apache.tuscany.sca.provider.PolicyProvider; +import org.apache.tuscany.sca.provider.PolicyProviderFactory; +import org.apache.tuscany.sca.provider.ProviderFactoryExtensionPoint; +import org.apache.tuscany.sca.provider.ServiceBindingProvider; +import org.apache.tuscany.sca.runtime.EndpointRegistry; import org.apache.tuscany.sca.runtime.EndpointSerializer; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; +import org.apache.tuscany.sca.runtime.RuntimeWireProcessor; +import org.apache.tuscany.sca.runtime.RuntimeWireProcessorExtensionPoint; +import org.apache.tuscany.sca.work.WorkScheduler; +import org.oasisopen.sca.ServiceRuntimeException; /** * Runtime model for Endpoint that supports java serialization */ -public class RuntimeEndpointImpl extends EndpointImpl implements Externalizable { - private EndpointSerializer serializer; - private String bindingURI; +public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint, Externalizable { + private transient CompositeContext compositeContext; + private transient EndpointRegistry endpointRegistry; + private transient RuntimeWireProcessor wireProcessor; + private transient InterfaceContractMapper interfaceContractMapper; + private transient WorkScheduler workScheduler; + private transient PhaseManager phaseManager; + private transient MessageFactory messageFactory; + private transient RuntimeInvoker invoker; + private transient ProviderFactoryExtensionPoint providerFactories; + + private transient List<InvocationChain> chains; + private transient final Map<Operation, InvocationChain> invocationChainMap = + new ConcurrentHashMap<Operation, InvocationChain>(); + private transient InvocationChain bindingInvocationChain; + + private transient ServiceBindingProvider bindingProvider; + private transient List<PolicyProvider> policyProviders; + private transient EndpointSerializer serializer; private String xml; + protected InterfaceContract bindingInterfaceContract; + protected InterfaceContract serviceInterfaceContract; /** * No-arg constructor for Java serilization */ @@ -48,6 +110,355 @@ public class RuntimeEndpointImpl extends EndpointImpl implements Externalizable super(registry); } + public void bind(CompositeContext compositeContext) { + this.compositeContext = compositeContext; + bind(compositeContext.getExtensionPointRegistry(), compositeContext.getEndpointRegistry()); + } + + public void bind(ExtensionPointRegistry registry, EndpointRegistry endpointRegistry) { + if (compositeContext == null) { + compositeContext = new CompositeContext(registry, endpointRegistry); + } + this.registry = registry; + this.endpointRegistry = endpointRegistry; + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + this.interfaceContractMapper = utilities.getUtility(InterfaceContractMapper.class); + this.workScheduler = utilities.getUtility(WorkScheduler.class); + this.wireProcessor = + new ExtensibleWireProcessor(registry.getExtensionPoint(RuntimeWireProcessorExtensionPoint.class)); + + this.messageFactory = registry.getExtensionPoint(FactoryExtensionPoint.class).getFactory(MessageFactory.class); + this.invoker = new RuntimeInvoker(this.messageFactory, this); + + this.phaseManager = utilities.getUtility(PhaseManager.class); + this.serializer = utilities.getUtility(EndpointSerializer.class); + this.providerFactories = registry.getExtensionPoint(ProviderFactoryExtensionPoint.class); + } + + public void unbind() { + compositeContext = null; + bindingInvocationChain = null; + chains = null; + bindingProvider = null; + policyProviders = null; + invocationChainMap.clear(); + } + + public synchronized List<InvocationChain> getInvocationChains() { + if (chains == null) { + initInvocationChains(); + } + return chains; + } + + public synchronized InvocationChain getBindingInvocationChain() { + if (bindingInvocationChain == null) { + bindingInvocationChain = new InvocationChainImpl(null, null, false, phaseManager); + initServiceBindingInvocationChains(); + } + return bindingInvocationChain; + } + + public InvocationChain getInvocationChain(Operation operation) { + InvocationChain cached = invocationChainMap.get(operation); + if (cached == null) { + for (InvocationChain chain : getInvocationChains()) { + Operation op = chain.getTargetOperation(); + + if (interfaceContractMapper.isCompatible(operation, op, op.getInterface().isRemotable())) { + invocationChainMap.put(operation, chain); + return chain; + } + } + invocationChainMap.put(operation, null); + return null; + } else { + return cached; + } + } + + public Message invoke(Message msg) { + return invoker.invokeBinding(msg); + } + + public Object invoke(Operation operation, Object[] args) throws InvocationTargetException { + return invoker.invoke(operation, args); + } + + public Message invoke(Operation operation, Message msg) { + return invoker.invoke(operation, msg); + } + + /** + * Navigate the component/componentType inheritence chain to find the leaf contract + * @param contract + * @return + */ + private Contract getLeafContract(Contract contract) { + Contract prev = null; + Contract current = contract; + while (current != null) { + prev = current; + if (current instanceof ComponentReference) { + current = ((ComponentReference)current).getReference(); + } else if (current instanceof CompositeReference) { + current = ((CompositeReference)current).getPromotedReferences().get(0); + } else if (current instanceof ComponentService) { + current = ((ComponentService)current).getService(); + } else if (current instanceof CompositeService) { + current = ((CompositeService)current).getPromotedService(); + } else { + break; + } + if (current == null) { + return prev; + } + } + return current; + } + + /** + * Initialize the invocation chains + */ + private void initInvocationChains() { + chains = new ArrayList<InvocationChain>(); + InterfaceContract sourceContract = getBindingInterfaceContract(); + + // It's the service wire + RuntimeComponentService service = (RuntimeComponentService)getService(); + RuntimeComponent serviceComponent = (RuntimeComponent)getComponent(); + + //InterfaceContract targetContract = getInterfaceContract(); + // TODO - EPR - why is this looking at the component types. The endpoint should have the right interface contract by this time + InterfaceContract targetContract = getServiceInterfaceContract(); + setInterfaceContract(targetContract); + for (Operation operation : sourceContract.getInterface().getOperations()) { + Operation targetOperation = interfaceContractMapper.map(targetContract.getInterface(), operation); + if (targetOperation == null) { + throw new ServiceRuntimeException("No matching operation for " + operation.getName() + + " is found in service " + + serviceComponent.getURI() + + "#" + + service.getName()); + } + InvocationChain chain = new InvocationChainImpl(operation, targetOperation, false, phaseManager); + if (operation.isNonBlocking()) { + addNonBlockingInterceptor(chain); + } + addServiceBindingInterceptor(chain, operation); + addImplementationInterceptor(serviceComponent, service, chain, targetOperation); + chains.add(chain); + } + + wireProcessor.process(this); + } + + private void initServiceBindingInvocationChains() { + + // add the binding interceptors to the service binding wire + ServiceBindingProvider provider = getBindingProvider(); + if ((provider != null) && (provider instanceof EndpointProvider)) { + ((EndpointProvider)provider).configure(); + } + + // add the policy interceptors to the service binding wire + List<PolicyProvider> pps = getPolicyProviders(); + if (pps != null) { + for (PolicyProvider p : pps) { + if (p instanceof BindingPolicyProvider) { + Interceptor interceptor = ((BindingPolicyProvider)p).createBindingInterceptor(); + if (interceptor != null) { + bindingInvocationChain.addInterceptor(interceptor); + } + } + } + } + + // TODO - add something on the end of the wire to invoke the + // invocation chain. Need to split out the runtime + // wire invoker into conversation, callback interceptors etc + bindingInvocationChain.addInvoker(invoker); + + } + + /** + * Add the interceptor for a binding + * + * @param reference + * @param binding + * @param chain + * @param operation + */ + private void addServiceBindingInterceptor(InvocationChain chain, Operation operation) { + List<PolicyProvider> pps = getPolicyProviders(); + if (pps != null) { + for (PolicyProvider p : pps) { + Interceptor interceptor = p.createInterceptor(operation); + if (interceptor != null) { + chain.addInterceptor(p.createInterceptor(operation)); + } + } + } + } + + /** + * Add a non-blocking interceptor if the service binding needs it + * + * @param service + * @param binding + * @param chain + */ + private void addNonBlockingInterceptor(InvocationChain chain) { + ServiceBindingProvider provider = getBindingProvider(); + if (provider != null) { + if (!provider.supportsOneWayInvocation()) { + chain.addInterceptor(Phase.SERVICE, new NonBlockingInterceptor(workScheduler)); + } + } + } + + /** + * Add the interceptor for a component implementation + * + * @param component + * @param service + * @param chain + * @param operation + */ + private void addImplementationInterceptor(Component component, + ComponentService service, + InvocationChain chain, + Operation operation) { + + if (service.getService() instanceof CompositeService) { + CompositeService compositeService = (CompositeService)service.getService(); + component = getPromotedComponent(compositeService); + service = getPromotedComponentService(compositeService); + } + + ImplementationProvider provider = ((RuntimeComponent)component).getImplementationProvider(); + + if (provider != null) { + Invoker invoker = null; + invoker = provider.createInvoker((RuntimeComponentService)service, operation); + chain.addInvoker(invoker); + } + // TODO - EPR - don't we need to get the policy from the right level in the + // model rather than the leafmost level + List<PolicyProvider> pps = ((RuntimeComponent)component).getPolicyProviders(); + if (pps != null) { + for (PolicyProvider p : pps) { + Interceptor interceptor = p.createInterceptor(operation); + if (interceptor != null) { + chain.addInterceptor(p.createInterceptor(operation)); + } + } + } + } + + /** + * @see java.lang.Object#clone() + */ + @Override + public Object clone() throws CloneNotSupportedException { + RuntimeEndpointImpl copy = (RuntimeEndpointImpl)super.clone(); + copy.invoker = new RuntimeInvoker(copy.messageFactory, copy); + return copy; + } + + /** + * 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; + } + } + + /** + * Follow a service promotion chain down to the innermost (non-composite) component. + * + * @param compositeService + * @return + */ + private Component getPromotedComponent(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 getPromotedComponent((CompositeService)service); + + } else { + + // Found a non-composite service + return compositeService.getPromotedComponent(); + } + } else { + + // No promoted service + return null; + } + } + + public ServiceBindingProvider getBindingProvider() { + if (bindingProvider == null) { + BindingProviderFactory factory = + (BindingProviderFactory)providerFactories.getProviderFactory(getBinding().getClass()); + if (factory == null) { + throw new ServiceRuntimeException("No provider factory is registered for binding " + getBinding() + .getType()); + } + this.bindingProvider = factory.createServiceBindingProvider(this); + } + return bindingProvider; + } + + public synchronized List<PolicyProvider> getPolicyProviders() { + if (policyProviders == null) { + policyProviders = new ArrayList<PolicyProvider>(); + for (PolicyProviderFactory factory : providerFactories.getPolicyProviderFactories()) { + PolicyProvider provider = factory.createServicePolicyProvider(this); + if (provider != null) { + policyProviders.add(provider); + } + } + } + return policyProviders; + } + + public void setBindingProvider(ServiceBindingProvider provider) { + this.bindingProvider = provider; + } + + public Contract getContract() { + return service; + } + + public CompositeContext getCompositeContext() { + return compositeContext; + } + public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { this.uri = in.readUTF(); this.xml = in.readUTF(); @@ -88,14 +499,35 @@ public class RuntimeEndpointImpl extends EndpointImpl implements Externalizable } super.resolve(); } + + public InterfaceContract getBindingInterfaceContract() { + resolve(); + if (bindingInterfaceContract != null) { + return bindingInterfaceContract; + } + bindingInterfaceContract = getBindingProvider().getBindingInterfaceContract(); + if (bindingInterfaceContract == null && service != null) { + bindingInterfaceContract = service.getInterfaceContract(); + } + if (bindingInterfaceContract == null) { + bindingInterfaceContract = getInterfaceContract(); + } + return bindingInterfaceContract; + } - @Override - public void setExtensionPointRegistry(ExtensionPointRegistry registry) { - if (this.registry != registry) { - super.setExtensionPointRegistry(registry); - serializer = null; + public InterfaceContract getServiceInterfaceContract() { + resolve(); + if (serviceInterfaceContract != null) { + return serviceInterfaceContract; + } + if (service == null) { + return getInterfaceContract(); + } + serviceInterfaceContract = getLeafContract(service).getInterfaceContract(); + if (serviceInterfaceContract == null) { + serviceInterfaceContract = getInterfaceContract(); } - // resolve(); + return serviceInterfaceContract; } } diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeEndpointReferenceImpl.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeEndpointReferenceImpl.java index 2b1c85b95a..2132139530 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeEndpointReferenceImpl.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeEndpointReferenceImpl.java @@ -23,19 +23,85 @@ import java.io.Externalizable; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; +import java.lang.reflect.InvocationTargetException; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.tuscany.sca.assembly.ComponentReference; +import org.apache.tuscany.sca.assembly.ComponentService; +import org.apache.tuscany.sca.assembly.CompositeReference; +import org.apache.tuscany.sca.assembly.CompositeService; +import org.apache.tuscany.sca.assembly.Contract; +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.assembly.EndpointReference; import org.apache.tuscany.sca.assembly.impl.EndpointReferenceImpl; +import org.apache.tuscany.sca.context.CompositeContext; 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.invocation.ExtensibleWireProcessor; +import org.apache.tuscany.sca.core.invocation.NonBlockingInterceptor; +import org.apache.tuscany.sca.core.invocation.RuntimeInvoker; +import org.apache.tuscany.sca.core.invocation.impl.InvocationChainImpl; +import org.apache.tuscany.sca.core.invocation.impl.PhaseManager; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Interceptor; +import org.apache.tuscany.sca.invocation.InvocationChain; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.invocation.Phase; +import org.apache.tuscany.sca.provider.BindingPolicyProvider; +import org.apache.tuscany.sca.provider.BindingProviderFactory; +import org.apache.tuscany.sca.provider.EndpointReferenceProvider; +import org.apache.tuscany.sca.provider.PolicyProvider; +import org.apache.tuscany.sca.provider.PolicyProviderFactory; +import org.apache.tuscany.sca.provider.ProviderFactoryExtensionPoint; +import org.apache.tuscany.sca.provider.ReferenceBindingProvider; +import org.apache.tuscany.sca.runtime.EndpointReferenceBinder; +import org.apache.tuscany.sca.runtime.EndpointRegistry; import org.apache.tuscany.sca.runtime.EndpointSerializer; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; +import org.apache.tuscany.sca.runtime.RuntimeWireProcessor; +import org.apache.tuscany.sca.runtime.RuntimeWireProcessorExtensionPoint; +import org.apache.tuscany.sca.work.WorkScheduler; +import org.oasisopen.sca.SCARuntimeException; +import org.oasisopen.sca.ServiceRuntimeException; /** * Runtime model for Endpoint that supports java serialization */ -public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implements Externalizable { - private EndpointSerializer serializer; +public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implements RuntimeEndpointReference, Externalizable { + private transient CompositeContext compositeContext; + private transient RuntimeWireProcessor wireProcessor; + private transient InterfaceContractMapper interfaceContractMapper; + private transient WorkScheduler workScheduler; + private transient PhaseManager phaseManager; + private transient MessageFactory messageFactory; + private transient RuntimeInvoker invoker; + private transient EndpointRegistry endpointRegistry; + + private transient List<InvocationChain> chains; + private transient final Map<Operation, InvocationChain> invocationChainMap = + new ConcurrentHashMap<Operation, InvocationChain>(); + private transient InvocationChain bindingInvocationChain; + + private transient EndpointReferenceBinder eprBinder; + private transient ReferenceBindingProvider bindingProvider; + private transient ProviderFactoryExtensionPoint providerFactories; + private transient List<PolicyProvider> policyProviders; + private transient EndpointSerializer serializer; private String xml; + protected InterfaceContract bindingInterfaceContract; + protected InterfaceContract referenceInterfaceContract; /** * No-arg constructor for Java serilization */ @@ -47,6 +113,318 @@ public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implemen super(registry); } + public void bind(CompositeContext compositeContext) { + this.compositeContext = compositeContext; + bind(compositeContext.getExtensionPointRegistry(), compositeContext.getEndpointRegistry()); + } + + public void bind(ExtensionPointRegistry registry, EndpointRegistry endpointRegistry) { + if (compositeContext == null) { + compositeContext = new CompositeContext(registry, endpointRegistry); + } + this.registry = registry; + this.endpointRegistry = endpointRegistry; + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + this.eprBinder = utilities.getUtility(EndpointReferenceBinder.class); + this.interfaceContractMapper = utilities.getUtility(InterfaceContractMapper.class); + this.workScheduler = utilities.getUtility(WorkScheduler.class); + this.wireProcessor = + new ExtensibleWireProcessor(registry.getExtensionPoint(RuntimeWireProcessorExtensionPoint.class)); + + this.messageFactory = registry.getExtensionPoint(FactoryExtensionPoint.class).getFactory(MessageFactory.class); + this.invoker = new RuntimeInvoker(this.messageFactory, this); + + this.phaseManager = utilities.getUtility(PhaseManager.class); + this.serializer = utilities.getUtility(EndpointSerializer.class); + this.providerFactories = registry.getExtensionPoint(ProviderFactoryExtensionPoint.class); + } + + public synchronized List<InvocationChain> getInvocationChains() { + if (chains == null) { + initInvocationChains(); + } + return chains; + } + + public synchronized InvocationChain getBindingInvocationChain() { + if (bindingInvocationChain == null) { + bindingInvocationChain = new InvocationChainImpl(null, null, true, phaseManager); + initReferenceBindingInvocationChains(); + } + return bindingInvocationChain; + } + + public InvocationChain getInvocationChain(Operation operation) { + InvocationChain cached = invocationChainMap.get(operation); + if (cached == null) { + for (InvocationChain chain : getInvocationChains()) { + Operation op = chain.getSourceOperation(); + if (interfaceContractMapper.isCompatible(operation, op, op.getInterface().isRemotable())) { + invocationChainMap.put(operation, chain); + return chain; + } + } + invocationChainMap.put(operation, null); + return null; + } else { + return cached; + } + } + + public Message invoke(Message msg) { + return invoker.invoke(msg); + } + + public Object invoke(Operation operation, Object[] args) throws InvocationTargetException { + return invoker.invoke(operation, args); + } + + public Message invoke(Operation operation, Message msg) { + return invoker.invoke(operation, msg); + } + + /** + * Navigate the component/componentType inheritence chain to find the leaf contract + * @param contract + * @return + */ + private Contract getLeafContract(Contract contract) { + Contract prev = null; + Contract current = contract; + while (current != null) { + prev = current; + if (current instanceof ComponentReference) { + current = ((ComponentReference)current).getReference(); + } else if (current instanceof CompositeReference) { + current = ((CompositeReference)current).getPromotedReferences().get(0); + } else if (current instanceof ComponentService) { + current = ((ComponentService)current).getService(); + } else if (current instanceof CompositeService) { + current = ((CompositeService)current).getPromotedService(); + } else { + break; + } + if (current == null) { + return prev; + } + } + return current; + } + + /** + * Initialize the invocation chains + */ + private void initInvocationChains() { + chains = new ArrayList<InvocationChain>(); + InterfaceContract sourceContract = getReferenceInterfaceContract(); + // TODO - EPR why is this looking at the component types. The endpoint reference should have the right interface contract by this time + //InterfaceContract sourceContract = getLeafInterfaceContract(endpointReference); + + // It's the reference wire + resolveEndpointReference(); + + InterfaceContract targetContract = getBindingInterfaceContract(); + // TODO - EPR why is this looking at the component types. The endpoint should have the right interface contract by this time + //InterfaceContract targetContract = getLeafInterfaceContract(endpoint); + + RuntimeComponentReference reference = (RuntimeComponentReference)getReference(); + for (Operation operation : sourceContract.getInterface().getOperations()) { + Operation targetOperation = interfaceContractMapper.map(targetContract.getInterface(), operation); + if (targetOperation == null) { + throw new ServiceRuntimeException("No matching operation for " + operation.getName() + + " is found in reference " + + getComponent().getURI() + + "#" + + reference.getName()); + } + InvocationChain chain = new InvocationChainImpl(operation, targetOperation, true, phaseManager); + if (operation.isNonBlocking()) { + addNonBlockingInterceptor(chain); + } + chains.add(chain); + addReferenceBindingInterceptor(chain, operation); + } + + wireProcessor.process(this); + } + + /** + * This code used to be in the activator but has moved here as + * the endpoint reference may not now be resolved until the wire + * is first used + */ + private void resolveEndpointReference() { + boolean ok = eprBinder.bind(endpointRegistry, this); + if (!ok) { + throw new SCARuntimeException("Unable to bind " + this); + } + + // set the endpoint based on the resolved endpoint + Endpoint endpoint = getTargetEndpoint(); + + // start the binding provider + final ReferenceBindingProvider bindingProvider = getBindingProvider(); + + if (bindingProvider != null) { + // Allow bindings to add shutdown hooks. Requires RuntimePermission shutdownHooks in policy. + AccessController.doPrivileged(new PrivilegedAction<Object>() { + public Object run() { + bindingProvider.start(); + return null; + } + }); + } + for (PolicyProvider policyProvider : getPolicyProviders()) { + policyProvider.start(); + } + + InterfaceContract bindingContract = getBindingInterfaceContract(); + endpoint.setInterfaceContract(bindingContract); + } + + + private void initReferenceBindingInvocationChains() { + + // add the binding interceptors to the reference binding wire + ReferenceBindingProvider provider = getBindingProvider(); + if ((provider != null) && (provider instanceof EndpointReferenceProvider)) { + ((EndpointReferenceProvider)provider).configure(); + } + + // add the policy interceptors to the service binding wire + // find out which policies are active + for (PolicyProvider p : getPolicyProviders()) { + if (p instanceof BindingPolicyProvider) { + Interceptor interceptor = ((BindingPolicyProvider)p).createBindingInterceptor(); + if (interceptor != null) { + bindingInvocationChain.addInterceptor(interceptor); + } + } + } + } + + public void rebuild() { + // TODO - can we use the idea of setTarget to rebuild the wire? + // used at the moment by binding.sca when it resets the + // source interface contract for local wires + this.chains = null; + + setStatus(EndpointReference.NOT_CONFIGURED); + + // TODO - cheating here as I fixed the RuntimeComponentService code + // to call this when it resets the interface contract + //endpointReference.setInterfaceContract(epr.getInterfaceContract()); + } + + /** + * Add the interceptor for a reference binding + * + * @param reference + * @param binding + * @param chain + * @param operation + */ + private void addReferenceBindingInterceptor(InvocationChain chain, Operation operation) { + ReferenceBindingProvider provider = getBindingProvider(); + if (provider != null) { + Invoker invoker = provider.createInvoker(operation); + if (invoker != null) { + chain.addInvoker(invoker); + } + } + List<PolicyProvider> pps = getPolicyProviders(); + if (pps != null) { + for (PolicyProvider p : pps) { + Interceptor interceptor = p.createInterceptor(operation); + if (interceptor != null) { + chain.addInterceptor(p.createInterceptor(operation)); + } + } + } + } + + /** + * Add a non-blocking interceptor if the reference binding needs it + * + * @param reference + * @param binding + * @param chain + */ + private void addNonBlockingInterceptor(InvocationChain chain) { + ReferenceBindingProvider provider = getBindingProvider(); + if (provider != null) { + boolean supportsOneWayInvocation = provider.supportsOneWayInvocation(); + if (!supportsOneWayInvocation) { + chain.addInterceptor(Phase.REFERENCE, new NonBlockingInterceptor(workScheduler)); + } + } + } + + /** + * @see java.lang.Object#clone() + */ + @Override + public Object clone() throws CloneNotSupportedException { + RuntimeEndpointReferenceImpl copy = (RuntimeEndpointReferenceImpl)super.clone(); + copy.invoker = new RuntimeInvoker(copy.messageFactory, copy); + return copy; + } + + public boolean isOutOfDate() { + return eprBinder.isOutOfDate(endpointRegistry, this); + } + + public ReferenceBindingProvider getBindingProvider() { + // For the case that binding.sca is implemented by another binding + if (binding == null) { + return null; + } + if (bindingProvider == null) { + BindingProviderFactory factory = + (BindingProviderFactory)providerFactories.getProviderFactory(getBinding().getClass()); + if (factory == null) { + throw new ServiceRuntimeException("No provider factory is registered for binding " + getBinding() + .getType()); + } + this.bindingProvider = factory.createReferenceBindingProvider(this); + } + return bindingProvider; + } + + public void setBindingProvider(ReferenceBindingProvider bindingProvider) { + this.bindingProvider = bindingProvider; + } + + public synchronized List<PolicyProvider> getPolicyProviders() { + if (policyProviders == null) { + policyProviders = new ArrayList<PolicyProvider>(); + for (PolicyProviderFactory factory : providerFactories.getPolicyProviderFactories()) { + PolicyProvider provider = factory.createReferencePolicyProvider(this); + if (provider != null) { + policyProviders.add(provider); + } + } + } + return policyProviders; + } + + public void unbind() { + bindingInvocationChain = null; + chains = null; + bindingProvider = null; + policyProviders = null; + invocationChainMap.clear(); + } + + public Contract getContract() { + resolve(); + return reference; + } + + public CompositeContext getCompositeContext() { + return compositeContext; + } + public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { this.uri = in.readUTF(); this.xml = in.readUTF(); @@ -80,7 +458,9 @@ public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implemen protected void resolve() { if (component == null && xml != null) { try { + bind(CompositeContext.getCurrentCompositeContext()); getSerializer().read(this, xml); + eprBinder.bind(endpointRegistry, this); } catch (IOException e) { throw new IllegalStateException(e); } @@ -88,13 +468,35 @@ public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implemen super.resolve(); } - @Override - public void setExtensionPointRegistry(ExtensionPointRegistry registry) { - if (this.registry != registry) { - super.setExtensionPointRegistry(registry); - serializer = null; + public InterfaceContract getBindingInterfaceContract() { + resolve(); + if (bindingInterfaceContract != null) { + return bindingInterfaceContract; + } + bindingInterfaceContract = getBindingProvider().getBindingInterfaceContract(); + if (bindingInterfaceContract == null && reference != null) { + bindingInterfaceContract = reference.getInterfaceContract(); + } + if (bindingInterfaceContract == null) { + bindingInterfaceContract = getInterfaceContract(); } - // resolve(); + return bindingInterfaceContract; } + public InterfaceContract getReferenceInterfaceContract() { + resolve(); + if (referenceInterfaceContract != null) { + return referenceInterfaceContract; + } + if (reference == null) { + return getInterfaceContract(); + } + referenceInterfaceContract = getLeafContract(reference).getInterfaceContract(); + if (referenceInterfaceContract == null) { + referenceInterfaceContract = getInterfaceContract(); + } + return referenceInterfaceContract; + } + + } diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeWireImpl.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeWireImpl.java deleted file mode 100644 index f3f559a25e..0000000000 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeWireImpl.java +++ /dev/null @@ -1,800 +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.assembly.impl; - -import java.lang.reflect.InvocationTargetException; -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -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.CompositeReference; -import org.apache.tuscany.sca.assembly.CompositeService; -import org.apache.tuscany.sca.assembly.Contract; -import org.apache.tuscany.sca.assembly.Endpoint; -import org.apache.tuscany.sca.assembly.EndpointReference; -import org.apache.tuscany.sca.assembly.Service; -import org.apache.tuscany.sca.context.CompositeContext; -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.invocation.ExtensibleWireProcessor; -import org.apache.tuscany.sca.core.invocation.NonBlockingInterceptor; -import org.apache.tuscany.sca.core.invocation.RuntimeWireInvoker; -import org.apache.tuscany.sca.core.invocation.impl.InvocationChainImpl; -import org.apache.tuscany.sca.core.invocation.impl.PhaseManager; -import org.apache.tuscany.sca.interfacedef.InterfaceContract; -import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; -import org.apache.tuscany.sca.interfacedef.Operation; -import org.apache.tuscany.sca.invocation.Interceptor; -import org.apache.tuscany.sca.invocation.InvocationChain; -import org.apache.tuscany.sca.invocation.Invoker; -import org.apache.tuscany.sca.invocation.Message; -import org.apache.tuscany.sca.invocation.MessageFactory; -import org.apache.tuscany.sca.invocation.Phase; -import org.apache.tuscany.sca.provider.BindingProviderFactory; -import org.apache.tuscany.sca.provider.ImplementationProvider; -import org.apache.tuscany.sca.provider.PolicyProvider; -import org.apache.tuscany.sca.provider.PolicyProviderFactory; -import org.apache.tuscany.sca.provider.PolicyProviderRRB; -import org.apache.tuscany.sca.provider.ProviderFactoryExtensionPoint; -import org.apache.tuscany.sca.provider.ReferenceBindingProvider; -import org.apache.tuscany.sca.provider.ReferenceBindingProviderRRB; -import org.apache.tuscany.sca.provider.ServiceBindingProvider; -import org.apache.tuscany.sca.provider.ServiceBindingProviderRRB; -import org.apache.tuscany.sca.runtime.EndpointReferenceBinder; -import org.apache.tuscany.sca.runtime.RuntimeComponent; -import org.apache.tuscany.sca.runtime.RuntimeComponentReference; -import org.apache.tuscany.sca.runtime.RuntimeComponentService; -import org.apache.tuscany.sca.runtime.RuntimeWire; -import org.apache.tuscany.sca.runtime.RuntimeWireProcessor; -import org.apache.tuscany.sca.runtime.RuntimeWireProcessorExtensionPoint; -import org.apache.tuscany.sca.work.WorkScheduler; -import org.oasisopen.sca.SCARuntimeException; -import org.oasisopen.sca.ServiceRuntimeException; - -/** - * @version $Rev$ $Date$ - */ -public class RuntimeWireImpl implements RuntimeWire { - - private CompositeContext compositeContext; - private ExtensionPointRegistry extensionPoints; - - private Boolean isReferenceWire = false; - private EndpointReference endpointReference; - private Endpoint endpoint; - - private transient RuntimeWireProcessor wireProcessor; - private transient InterfaceContractMapper interfaceContractMapper; - private transient WorkScheduler workScheduler; - private transient PhaseManager phaseManager; - private transient MessageFactory messageFactory; - private transient RuntimeWireInvoker invoker; - - // the following is a very simple cache that avoids re-cloning a wire - // when consecutive callbacks to the same endpoint are made - private Endpoint lastCallback; - private RuntimeWire cachedWire; - private boolean wireReserved; - private RuntimeWireImpl clonedFrom; - - private List<InvocationChain> chains; - private transient final Map<Operation, InvocationChain> invocationChainMap = - new ConcurrentHashMap<Operation, InvocationChain>(); - private InvocationChain bindingInvocationChain; - - private EndpointReferenceBinder eprBinder; - private final ProviderFactoryExtensionPoint providerFactories; - - /** - * @param source - * @param target - * @param interfaceContractMapper - * @param workScheduler - * @param wireProcessor - * @param messageFactory - * @param conversationManager - */ - public RuntimeWireImpl(CompositeContext compositeContext, - boolean isReferenceWire, - EndpointReference endpointReference, - Endpoint endpoint, - InterfaceContractMapper interfaceContractMapper, - WorkScheduler workScheduler, - RuntimeWireProcessor wireProcessor, - MessageFactory messageFactory) { - super(); - this.compositeContext = compositeContext; - this.extensionPoints = compositeContext.getExtensionPointRegistry(); - this.isReferenceWire = isReferenceWire; - this.endpointReference = endpointReference; - this.endpoint = endpoint; - this.interfaceContractMapper = interfaceContractMapper; - this.workScheduler = workScheduler; - this.wireProcessor = wireProcessor; - this.messageFactory = messageFactory; - this.invoker = new RuntimeWireInvoker(this.messageFactory, this); - - UtilityExtensionPoint utilities = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class); - this.eprBinder = utilities.getUtility(EndpointReferenceBinder.class); - this.phaseManager = utilities.getUtility(PhaseManager.class); - this.providerFactories = extensionPoints.getExtensionPoint(ProviderFactoryExtensionPoint.class); - } - - public RuntimeWireImpl(CompositeContext compositeContext, - boolean isReferenceWire, - EndpointReference endpointReference, - Endpoint endpoint) { - super(); - this.compositeContext = compositeContext; - this.extensionPoints = compositeContext.getExtensionPointRegistry(); - this.isReferenceWire = isReferenceWire; - this.endpointReference = endpointReference; - this.endpoint = endpoint; - - UtilityExtensionPoint utilities = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class); - this.interfaceContractMapper = utilities.getUtility(InterfaceContractMapper.class); - this.workScheduler = utilities.getUtility(WorkScheduler.class); - this.wireProcessor = new ExtensibleWireProcessor(extensionPoints.getExtensionPoint(RuntimeWireProcessorExtensionPoint.class)); - FactoryExtensionPoint factories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); - this.messageFactory = factories.getFactory(MessageFactory.class); - this.invoker = new RuntimeWireInvoker(this.messageFactory, this); - - this.eprBinder = utilities.getUtility(EndpointReferenceBinder.class); - this.phaseManager = utilities.getUtility(PhaseManager.class); - this.providerFactories = extensionPoints.getExtensionPoint(ProviderFactoryExtensionPoint.class); - } - - public synchronized List<InvocationChain> getInvocationChains() { - if (chains == null) { - initInvocationChains(); - } - return chains; - } - - public synchronized InvocationChain getBindingInvocationChain() { - if (bindingInvocationChain == null) { - bindingInvocationChain = new InvocationChainImpl(null, null, isReferenceWire, phaseManager); - if (isReferenceWire) { - initReferenceBindingInvocationChains(); - } else { - initServiceBindingInvocationChains(); - } - } - return bindingInvocationChain; - } - - public InvocationChain getInvocationChain(Operation operation) { - InvocationChain cached = invocationChainMap.get(operation); - if (cached == null) { - for (InvocationChain chain : getInvocationChains()) { - Operation op = null; - if (isReferenceWire) { - // Reference chain - op = chain.getSourceOperation(); - } else { - // Service chain - op = chain.getTargetOperation(); - } - if (interfaceContractMapper.isCompatible(operation, op, op.getInterface().isRemotable())) { - invocationChainMap.put(operation, chain); - return chain; - } - } - invocationChainMap.put(operation, null); - return null; - } else { - return cached; - } - } - - public Object invoke(Message msg) throws InvocationTargetException { - return getBindingInvocationChain().getHeadInvoker().invoke(msg); - } - - public Object invoke(Operation operation, Object[] args) throws InvocationTargetException { - Message msg = messageFactory.createMessage(); - msg.setBody(args); - return invoker.invoke(operation, msg); - } - - public Object invoke(Operation operation, Message msg) throws InvocationTargetException { - return invoker.invoke(operation, msg); - } - - /** - * Navigate the component/componentType inheritence chain to find the leaf contract - * @param contract - * @return - */ - private Contract getLeafContract(Contract contract) { - Contract prev = null; - Contract current = contract; - while (current != null) { - prev = current; - if (current instanceof ComponentReference) { - current = ((ComponentReference)current).getReference(); - } else if (current instanceof CompositeReference) { - current = ((CompositeReference)current).getPromotedReferences().get(0); - } else if (current instanceof ComponentService) { - current = ((ComponentService)current).getService(); - } else if (current instanceof CompositeService) { - current = ((CompositeService)current).getPromotedService(); - } else { - break; - } - if (current == null) { - return prev; - } - } - return current; - } - - private InterfaceContract getLeafInterfaceContract(EndpointReference epr) { - ComponentReference reference = epr.getReference(); - if (reference == null) { - return epr.getInterfaceContract(); - } - InterfaceContract interfaceContract = getLeafContract(reference).getInterfaceContract(); - if (interfaceContract == null) { - interfaceContract = epr.getInterfaceContract(); - } - return interfaceContract; - } - - private InterfaceContract getLeafInterfaceContract(Endpoint ep) { - ComponentService service = ep.getService(); - if (service == null) { - return ep.getInterfaceContract(); - } - InterfaceContract interfaceContract = getLeafContract(service).getInterfaceContract(); - if (interfaceContract == null) { - interfaceContract = ep.getInterfaceContract(); - } - return interfaceContract; - } - - /** - * Initialize the invocation chains - */ - private void initInvocationChains() { - chains = new ArrayList<InvocationChain>(); - InterfaceContract sourceContract = endpointReference.getInterfaceContract(); - // TODO - EPR why is this looking at the component types. The endpoint reference should have the right interface contract by this time - //InterfaceContract sourceContract = getLeafInterfaceContract(endpointReference); - - if (isReferenceWire) { - // It's the reference wire - resolveEndpointReference(); - - InterfaceContract targetContract = endpoint.getInterfaceContract(); - // TODO - EPR why is this looking at the component types. The endpoint should have the right interface contract by this time - //InterfaceContract targetContract = getLeafInterfaceContract(endpoint); - - RuntimeComponentReference reference = (RuntimeComponentReference)endpointReference.getReference(); - Binding refBinding = endpointReference.getBinding(); - for (Operation operation : sourceContract.getInterface().getOperations()) { - Operation targetOperation = interfaceContractMapper.map(targetContract.getInterface(), operation); - if (targetOperation == null) { - throw new ServiceRuntimeException("No matching operation for " + operation.getName() - + " is found in reference " - + endpointReference.getComponent().getURI() - + "#" - + reference.getName()); - } - InvocationChain chain = new InvocationChainImpl(operation, targetOperation, true, phaseManager); - if (operation.isNonBlocking()) { - addNonBlockingInterceptor(reference, refBinding, chain); - } - chains.add(chain); - addReferenceBindingInterceptor(reference, refBinding, chain, operation); - } - - } else { - // It's the service wire - RuntimeComponentService service = (RuntimeComponentService)endpoint.getService(); - RuntimeComponent serviceComponent = (RuntimeComponent)endpoint.getComponent(); - Binding serviceBinding = endpoint.getBinding(); - //InterfaceContract targetContract = endpoint.getInterfaceContract(); - // TODO - EPR - why is this looking at the component types. The endpoint should have the right interface contract by this time - InterfaceContract targetContract = getLeafInterfaceContract(endpoint); - endpoint.setInterfaceContract(targetContract); - for (Operation operation : sourceContract.getInterface().getOperations()) { - Operation targetOperation = interfaceContractMapper.map(targetContract.getInterface(), operation); - if (targetOperation == null) { - throw new ServiceRuntimeException("No matching operation for " + operation.getName() - + " is found in service " - + serviceComponent.getURI() - + "#" - + service.getName()); - } - InvocationChain chain = new InvocationChainImpl(operation, targetOperation, false, phaseManager); - if (operation.isNonBlocking()) { - addNonBlockingInterceptor(service, serviceBinding, chain); - } - addServiceBindingInterceptor(service, serviceBinding, chain, operation); - addImplementationInterceptor(serviceComponent, service, chain, targetOperation); - chains.add(chain); - } - - } - wireProcessor.process(this); - } - - - /** - * This code used to be in the activator but has moved here as - * the endpoint reference may not now be resolved until the wire - * is first used - */ - private void resolveEndpointReference(){ - boolean ok = eprBinder.bind(compositeContext.getEndpointRegistry(), endpointReference); - - if (!ok) { - throw new SCARuntimeException("Unable to bind " + endpointReference); - } - - // set the endpoint based on the resolved endpoint - endpoint = endpointReference.getTargetEndpoint(); - - RuntimeComponentReference runtimeRef = (RuntimeComponentReference)endpointReference.getReference(); - - if (runtimeRef.getBindingProvider(endpointReference.getBinding()) == null) { - addReferenceBindingProvider(endpointReference, - (RuntimeComponent)endpointReference.getComponent(), - runtimeRef, - endpointReference.getBinding()); - } - - // start the binding provider - final ReferenceBindingProvider bindingProvider = runtimeRef.getBindingProvider(endpointReference.getBinding()); - - if (bindingProvider != null) { - // Allow bindings to add shutdown hooks. Requires RuntimePermission shutdownHooks in policy. - AccessController.doPrivileged(new PrivilegedAction<Object>() { - public Object run() { - bindingProvider.start(); - return null; - } - }); - } - for (PolicyProvider policyProvider : runtimeRef.getPolicyProviders(endpointReference.getBinding())) { - policyProvider.start(); - } - - InterfaceContract bindingContract = getInterfaceContract(endpointReference.getReference(), endpointReference.getBinding()); - Endpoint endpoint = endpointReference.getTargetEndpoint(); - endpoint.setInterfaceContract(bindingContract); - } - - private ReferenceBindingProvider addReferenceBindingProvider( - EndpointReference endpointReference, - RuntimeComponent component, RuntimeComponentReference reference, - Binding binding) { - BindingProviderFactory providerFactory = (BindingProviderFactory) providerFactories - .getProviderFactory(binding.getClass()); - if (providerFactory != null) { - @SuppressWarnings("unchecked") - ReferenceBindingProvider bindingProvider = providerFactory - .createReferenceBindingProvider(endpointReference); - if (bindingProvider != null) { - ((RuntimeComponentReference) reference).setBindingProvider( - binding, bindingProvider); - } - for (PolicyProviderFactory f : providerFactories - .getPolicyProviderFactories()) { - PolicyProvider policyProvider = f - .createReferencePolicyProvider(endpointReference); - if (policyProvider != null) { - reference.addPolicyProvider(binding, policyProvider); - } - } - - return bindingProvider; - } else { - throw new IllegalStateException( - "Provider factory not found for binding: " - + binding.getType()); - } - } - - private InterfaceContract getInterfaceContract(ComponentReference reference, Binding binding) { - InterfaceContract interfaceContract = reference.getInterfaceContract(); - if (interfaceContract == null) { - interfaceContract = endpoint.getInterfaceContract(); - } - ReferenceBindingProvider provider = ((RuntimeComponentReference)reference).getBindingProvider(binding); - if (provider != null) { - InterfaceContract bindingContract = provider.getBindingInterfaceContract(); - if (bindingContract != null) { - interfaceContract = bindingContract; - } - } - return interfaceContract.makeUnidirectional(false); - } - - private void initReferenceBindingInvocationChains() { - RuntimeComponentReference reference = (RuntimeComponentReference)endpointReference.getReference(); - Binding referenceBinding = endpointReference.getBinding(); - - // add the binding interceptors to the reference binding wire - ReferenceBindingProvider provider = reference.getBindingProvider(referenceBinding); - if ((provider != null) && (provider instanceof ReferenceBindingProviderRRB)) { - ((ReferenceBindingProviderRRB)provider).configureBindingChain(this); - } - - // add the policy interceptors to the service binding wire - // find out which policies are active - List<PolicyProvider> pps = ((RuntimeComponentReference)reference).getPolicyProviders(referenceBinding); - if (pps != null) { - for (PolicyProvider p : pps) { - if (p instanceof PolicyProviderRRB) { - Interceptor interceptor = ((PolicyProviderRRB)p).createBindingInterceptor(); - if (interceptor != null) { - bindingInvocationChain.addInterceptor(interceptor); - } - } - } - } - } - - private void initServiceBindingInvocationChains() { - RuntimeComponentService service = (RuntimeComponentService)endpoint.getService(); - Binding serviceBinding = endpoint.getBinding(); - - // add the binding interceptors to the service binding wire - ServiceBindingProvider provider = service.getBindingProvider(serviceBinding); - if ((provider != null) && (provider instanceof ServiceBindingProviderRRB)) { - ((ServiceBindingProviderRRB)provider).configureBindingChain(this); - } - - // add the policy interceptors to the service binding wire - List<PolicyProvider> pps = ((RuntimeComponentService)service).getPolicyProviders(serviceBinding); - if (pps != null) { - for (PolicyProvider p : pps) { - if (p instanceof PolicyProviderRRB) { - Interceptor interceptor = ((PolicyProviderRRB)p).createBindingInterceptor(); - if (interceptor != null) { - bindingInvocationChain.addInterceptor(interceptor); - } - } - } - } - - // TODO - add something on the end of the wire to invoke the - // invocation chain. Need to split out the runtime - // wire invoker into conversation, callback interceptors etc - bindingInvocationChain.addInvoker(invoker); - - } - - // =============================================================== - // TODO - EPR remove when we convert fully over to EndpointReference2 - - // TODO - remove. Just here during development -/* - static EndpointReference epr; - - public EndpointReference getSource() { - // TODO - EPR convert this into method that returns EndpointReference2 - - // convert the source info into old endpoint reference format - epr = - new EndpointReferenceImpl((RuntimeComponent)endpointReference.getComponent(), endpointReference - .getReference(), endpointReference.getBinding(), endpointReference.getInterfaceContract()); - - if (endpointReference.getCallbackEndpoint() != null) { - // convert the source callback endpoint into old endpoint reference format - EndpointReference cepr; - cepr = - new EndpointReferenceImpl((RuntimeComponent)endpointReference.getComponent(), endpointReference - .getCallbackEndpoint().getService(), endpointReference.getCallbackEndpoint().getBinding(), - endpointReference.getCallbackEndpoint().getInterfaceContract()); - epr.setCallbackEndpoint(cepr); - } - - // TODO - somtimes used to reset the interface contract so we - // copy it back in in the rebuild method below - return epr; - } - - public EndpointReference getTarget() { - // TODO - EPR convert this into method that returns Endpoint2 - - Endpoint2 endpoint = this.endpoint != null ? this.endpoint : endpointReference.getTargetEndpoint(); - - // convert the target info into old endpoint reference format - EndpointReference epr = - new EndpointReferenceImpl((RuntimeComponent)endpoint.getComponent(), endpoint.getService(), endpoint - .getBinding(), endpoint.getInterfaceContract()); - return epr; - } - - public void setTarget(EndpointReference target) { - // TODO - can we use the idea of setTarget to rebuild the wire? - - } -*/ - - // =================================================================== - - public void rebuild() { - // TODO - can we use the idea of setTarget to rebuild the wire? - // used at the moment by binding.sca when it resets the - // source interface contract for local wires - this.chains = null; - - this.endpointReference.setStatus(EndpointReference.NOT_CONFIGURED); - - // TODO - cheating here as I fixed the RuntimeComponentService code - // to call this when it resets the interface contract - //endpointReference.setInterfaceContract(epr.getInterfaceContract()); - } - - public EndpointReference getEndpointReference() { - return endpointReference; - } - - public Endpoint getEndpoint() { - return endpoint; - } - - /** - * Add the interceptor for a reference binding - * - * @param reference - * @param binding - * @param chain - * @param operation - */ - private void addReferenceBindingInterceptor(ComponentReference reference, - Binding binding, - InvocationChain chain, - Operation operation) { - ReferenceBindingProvider provider = ((RuntimeComponentReference)reference).getBindingProvider(binding); - if (provider != null) { - Invoker invoker = provider.createInvoker(operation); - if (invoker != null) { - chain.addInvoker(invoker); - } - } - List<PolicyProvider> pps = ((RuntimeComponentReference)reference).getPolicyProviders(binding); - if (pps != null) { - for (PolicyProvider p : pps) { - Interceptor interceptor = p.createInterceptor(operation); - if (interceptor != null) { - chain.addInterceptor(p.createInterceptor(operation)); - } - } - } - } - - /** - * Add the interceptor for a binding - * - * @param reference - * @param binding - * @param chain - * @param operation - */ - private void addServiceBindingInterceptor(ComponentService service, - Binding binding, - InvocationChain chain, - Operation operation) { - List<PolicyProvider> pps = ((RuntimeComponentService)service).getPolicyProviders(binding); - if (pps != null) { - for (PolicyProvider p : pps) { - Interceptor interceptor = p.createInterceptor(operation); - if (interceptor != null) { - chain.addInterceptor(p.createInterceptor(operation)); - } - } - } - } - - /** - * Add a non-blocking interceptor if the reference binding needs it - * - * @param reference - * @param binding - * @param chain - */ - private void addNonBlockingInterceptor(ComponentReference reference, Binding binding, InvocationChain chain) { - ReferenceBindingProvider provider = ((RuntimeComponentReference)reference).getBindingProvider(binding); - if (provider != null) { - boolean supportsOneWayInvocation = provider.supportsOneWayInvocation(); - if (!supportsOneWayInvocation) { - chain.addInterceptor(Phase.REFERENCE, new NonBlockingInterceptor(workScheduler)); - } - } - } - - /** - * Add a non-blocking interceptor if the service binding needs it - * - * @param service - * @param binding - * @param chain - */ - private void addNonBlockingInterceptor(ComponentService service, Binding binding, InvocationChain chain) { - ServiceBindingProvider provider = ((RuntimeComponentService)service).getBindingProvider(binding); - if (provider != null) { - if (!provider.supportsOneWayInvocation()) { - chain.addInterceptor(Phase.SERVICE, new NonBlockingInterceptor(workScheduler)); - } - } - } - - /** - * Add the interceptor for a component implementation - * - * @param component - * @param service - * @param chain - * @param operation - */ - private void addImplementationInterceptor(Component component, - ComponentService service, - InvocationChain chain, - Operation operation) { - - - - if (service.getService() instanceof CompositeService){ - CompositeService compositeService = (CompositeService)service.getService(); - component = getPromotedComponent(compositeService); - service = getPromotedComponentService(compositeService); - } - - ImplementationProvider provider = ((RuntimeComponent)component).getImplementationProvider(); - - if (provider != null) { - Invoker invoker = null; - invoker = provider.createInvoker((RuntimeComponentService)service, operation); - chain.addInvoker(invoker); - } - // TODO - EPR - don't we need to get the policy from the right level in the - // model rather than the leafmost level - List<PolicyProvider> pps = ((RuntimeComponent)component).getPolicyProviders(); - if (pps != null) { - for (PolicyProvider p : pps) { - Interceptor interceptor = p.createInterceptor(operation); - if (interceptor != null) { - chain.addInterceptor(p.createInterceptor(operation)); - } - } - } - } - - /** - * @see java.lang.Object#clone() - */ - @Override - public Object clone() throws CloneNotSupportedException { - RuntimeWireImpl copy = (RuntimeWireImpl)super.clone(); - copy.endpointReference = (EndpointReference)endpointReference.clone(); - copy.endpoint = copy.endpointReference.getTargetEndpoint(); - copy.invoker = new RuntimeWireInvoker(copy.messageFactory, copy); - copy.cachedWire = null; // TUSCANY-2630 - return copy; - } - - public synchronized RuntimeWire lookupCache(Endpoint callback) { - if (lastCallback != null && - callback.getURI().equals(lastCallback.getURI()) && - !wireReserved) { - wireReserved = true; - return cachedWire; - } else { - return null; - } - } - - public synchronized void addToCache(Endpoint callback, RuntimeWire clonedWire) { - ((RuntimeWireImpl)clonedWire).setClonedFrom(this); - lastCallback = callback; - cachedWire = clonedWire; - wireReserved = true; - } - - public synchronized void releaseClonedWire(RuntimeWire wire) { - if (cachedWire == wire) { - wireReserved = false; - } - } - - public synchronized void releaseWire() { - clonedFrom.releaseClonedWire(this); - } - - private void setClonedFrom(RuntimeWireImpl wire) { - clonedFrom = wire; - } - - public ExtensionPointRegistry getExtensionPoints() { - return extensionPoints; - } - - /** - * 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; - } - } - - /** - * Follow a service promotion chain down to the innermost (non-composite) component. - * - * @param compositeService - * @return - */ - private Component getPromotedComponent(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 getPromotedComponent((CompositeService)service); - - } else { - - // Found a non-composite service - return compositeService.getPromotedComponent(); - } - } else { - - // No promoted service - return null; - } - } - - public boolean isOutOfDate() { - return eprBinder.isOutOfDate(compositeContext.getEndpointRegistry(), getEndpointReference()); - } -} diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ServiceReferenceExt.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ServiceReferenceExt.java index 9b60415132..5206f0945e 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ServiceReferenceExt.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ServiceReferenceExt.java @@ -21,40 +21,21 @@ package org.apache.tuscany.sca.core.context; import java.io.Externalizable; -import javax.xml.stream.XMLStreamReader; - -import org.apache.tuscany.sca.assembly.EndpointReference; -import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; import org.oasisopen.sca.ServiceReference; /** * Extended ServiceReference */ public interface ServiceReferenceExt<B> extends ServiceReference<B>, Externalizable { - /** - * Return the wire that sits behind this service reference - * @return wire - */ - RuntimeWire getRuntimeWire(); /** * Return the EndpointReference that sits behind this service reference * @return endpoint reference */ - EndpointReference getEndpointReference(); - - // TODO - EPR - the the following still required? - /** - * TBD - * - * @param callbackID - */ - void attachCallbackID(Object callbackID); + RuntimeEndpointReference getEndpointReference(); + + B getProxy(); + void setProxy(B proxy); - /** - * TBD - * - * @return - */ - XMLStreamReader getXMLReader(); } diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CallbackServiceReferenceImpl.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CallbackServiceReferenceImpl.java index 5218a7fd01..542a80f926 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CallbackServiceReferenceImpl.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CallbackServiceReferenceImpl.java @@ -18,22 +18,19 @@ */ package org.apache.tuscany.sca.core.context.impl; - import java.util.List; -import org.apache.tuscany.sca.assembly.Binding; import org.apache.tuscany.sca.assembly.Endpoint; import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.context.CompositeContext; import org.apache.tuscany.sca.context.ThreadMessageContext; -import org.apache.tuscany.sca.core.assembly.impl.RuntimeWireImpl; -import org.apache.tuscany.sca.core.invocation.ProxyFactory; import org.apache.tuscany.sca.invocation.Message; -import org.apache.tuscany.sca.runtime.RuntimeComponentReference; -import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; +import org.oasisopen.sca.ServiceRuntimeException; public class CallbackServiceReferenceImpl<B> extends ServiceReferenceImpl<B> { - private RuntimeWire wire; - private List<RuntimeWire> wires; + private RuntimeEndpointReference callbackEPR; + private List<? extends EndpointReference> callbackEPRs; private Endpoint resolvedEndpoint; /* @@ -42,57 +39,65 @@ public class CallbackServiceReferenceImpl<B> extends ServiceReferenceImpl<B> { public CallbackServiceReferenceImpl() { super(); } - - public CallbackServiceReferenceImpl(Class<B> interfaze, List<RuntimeWire> wires, ProxyFactory proxyFactory) { - super(interfaze, null, proxyFactory); - this.wires = wires; - init(); + + public CallbackServiceReferenceImpl(Class<B> interfaze, + List<? extends EndpointReference> callbackEPRs) { + super(interfaze, null, getCompositeContext(callbackEPRs)); + this.callbackEPRs = callbackEPRs; + init(); + } + + private static CompositeContext getCompositeContext(List<? extends EndpointReference> callbackEPRs) { + if(!callbackEPRs.isEmpty()) { + RuntimeEndpointReference epr = (RuntimeEndpointReference) callbackEPRs.get(0); + return epr.getCompositeContext(); + } + return null; } public void init() { Message msgContext = ThreadMessageContext.getMessageContext(); - wire = selectCallbackWire(msgContext); - if (wire == null) { - //FIXME: need better exception - throw new RuntimeException("No callback binding found for " + msgContext.getTo().toString()); + callbackEPR = selectCallbackEPR(msgContext); + if (callbackEPR == null) { + throw new ServiceRuntimeException("No callback binding found for " + msgContext.getTo().toString()); } resolvedEndpoint = msgContext.getFrom().getCallbackEndpoint(); } @Override - protected Object createProxy() throws Exception { + protected B createProxy() throws Exception { return proxyFactory.createCallbackProxy(this); - } + } - public RuntimeWire getCallbackWire() { + public RuntimeEndpointReference getCallbackEPR() { if (resolvedEndpoint == null) { return null; } else { - return cloneAndBind(wire); - } + return cloneAndBind(callbackEPR); + } } public Endpoint getResolvedEndpoint() { - return resolvedEndpoint; - } + return resolvedEndpoint; + } - private RuntimeWire selectCallbackWire(Message msgContext) { + private RuntimeEndpointReference selectCallbackEPR(Message msgContext) { // look for callback binding with same name as service binding Endpoint to = msgContext.getTo(); if (to == null) { //FIXME: need better exception - throw new RuntimeException("Destination for forward call is not available"); + throw new ServiceRuntimeException("Destination for forward call is not available"); } - for (RuntimeWire wire : wires) { - if (wire.getEndpointReference().getBinding().getName().equals(to.getBinding().getName())) { - return wire; + for (EndpointReference epr : callbackEPRs) { + if (epr.getBinding().getName().equals(to.getBinding().getName())) { + return (RuntimeEndpointReference) epr; } } // if no match, look for callback binding with same type as service binding - for (RuntimeWire wire : wires) { - if (wire.getEndpointReference().getBinding().getClass() == to.getBinding().getClass()) { - return wire; + for (EndpointReference epr : callbackEPRs) { + if (epr.getBinding().getType().equals(to.getBinding().getType())) { + return (RuntimeEndpointReference) epr; } } @@ -100,55 +105,20 @@ public class CallbackServiceReferenceImpl<B> extends ServiceReferenceImpl<B> { return null; } - private RuntimeWire cloneAndBind(RuntimeWire wire) { - RuntimeWire boundWire = null; + private RuntimeEndpointReference cloneAndBind(RuntimeEndpointReference endpointReference) { if (resolvedEndpoint != null) { - boundWire = ((RuntimeWireImpl)wire).lookupCache(resolvedEndpoint); - if (boundWire != null) { - return boundWire; - } + try { - // TODO - EPR - is this correct? - - // Fluff up a new response wire based on the callback endpoint - RuntimeComponentReference ref = - bind((RuntimeComponentReference)wire.getEndpointReference().getReference(), - resolvedEndpoint); - - boundWire = ref.getRuntimeWires().get(0); - - Binding binding = wire.getEndpointReference().getBinding(); - - ((RuntimeWireImpl)wire).addToCache(resolvedEndpoint, boundWire); + RuntimeEndpointReference epr = (RuntimeEndpointReference)endpointReference.clone(); + epr.setTargetEndpoint(resolvedEndpoint); + return epr; } catch (CloneNotSupportedException e) { // will not happen + throw new ServiceRuntimeException(e); } + } else { + return null; } - return boundWire; } - private RuntimeComponentReference bind(RuntimeComponentReference reference, - Endpoint callbackEndpoint) throws CloneNotSupportedException { - - // clone the callback reference ready to configure it for this callback endpoint - RuntimeComponentReference ref = (RuntimeComponentReference)reference.clone(); - ref.getTargets().clear(); - ref.getBindings().clear(); - ref.getEndpointReferences().clear(); - - // no access to the assembly factory so clone an existing epr - EndpointReference callbackEndpointReference = (EndpointReference)reference.getEndpointReferences().get(0).clone(); - - callbackEndpointReference.setReference(ref); - callbackEndpointReference.setTargetEndpoint(callbackEndpoint); - callbackEndpointReference.setUnresolved(true); - - // TODO - should really use incoming callback info but awaiting - // decision from OASIS on what will happen with callbacks - // The callback endpoint will be resolved with the registry - // when the wire chains are created - ref.getEndpointReferences().add(callbackEndpointReference); - - return ref; - } } 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 4e71181747..115a4d31dd 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 @@ -23,7 +23,6 @@ import java.util.Collection; import java.util.List; 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.ComponentProperty; import org.apache.tuscany.sca.assembly.ComponentReference; @@ -31,8 +30,8 @@ import org.apache.tuscany.sca.assembly.ComponentService; import org.apache.tuscany.sca.assembly.Endpoint; import org.apache.tuscany.sca.assembly.EndpointReference; 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.SCABinding; import org.apache.tuscany.sca.assembly.Service; import org.apache.tuscany.sca.context.CompositeContext; import org.apache.tuscany.sca.context.ContextFactoryExtensionPoint; @@ -49,16 +48,14 @@ 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.monitor.Monitor; -import org.apache.tuscany.sca.monitor.MonitorFactory; import org.apache.tuscany.sca.runtime.CompositeActivator; import org.apache.tuscany.sca.runtime.EndpointReferenceBinder; 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.RuntimeEndpoint; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; import org.oasisopen.sca.RequestContext; -import org.oasisopen.sca.SCARuntimeException; import org.oasisopen.sca.ServiceReference; import org.oasisopen.sca.ServiceRuntimeException; @@ -78,28 +75,27 @@ public class ComponentContextImpl implements RuntimeComponentContext { private final JavaInterfaceFactory javaInterfaceFactory; private final PropertyValueFactory propertyFactory; private final EndpointReferenceBinder eprBinder; - private final Monitor monitor; - - public ComponentContextImpl(ExtensionPointRegistry registry, CompositeContext compositeContext, 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); - + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); this.compositeContext = compositeContext; - + 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); - + this.eprBinder = utilities.getUtility(EndpointReferenceBinder.class); - MonitorFactory monitorFactory = utilities.getUtility(MonitorFactory.class); - this.monitor = monitorFactory.createMonitor(); } public String getURI() { @@ -116,31 +112,64 @@ public class ComponentContextImpl implements RuntimeComponentContext { } public <B> ServiceReference<B> getServiceReference(Class<B> businessInterface, String referenceName) { - try { - for (ComponentReference ref : component.getReferences()) { - if (referenceName.equals(ref.getName())) { - /* ******************** Contribution for issue TUSCANY-2281 ******************** */ - Multiplicity multiplicity = ref.getMultiplicity(); - if (multiplicity == Multiplicity.ZERO_N || multiplicity == Multiplicity.ONE_N) { - throw new IllegalArgumentException("Reference " + referenceName - + " has multiplicity " - + multiplicity); - } - /* ******************** Contribution for issue TUSCANY-2281 ******************** */ - return getServiceReference(businessInterface, (RuntimeComponentReference)ref, null); + for (ComponentReference ref : component.getReferences()) { + if (referenceName.equals(ref.getName())) { + Multiplicity multiplicity = ref.getMultiplicity(); + if (multiplicity == Multiplicity.ZERO_N || multiplicity == Multiplicity.ONE_N) { + throw new ServiceRuntimeException("Reference " + referenceName + + " has multiplicity " + + multiplicity); } + return getServiceReference(businessInterface, (RuntimeEndpointReference)getEndpointReference(ref)); } - throw new ServiceRuntimeException("Reference not found: " + referenceName); - } catch (ServiceRuntimeException e) { - throw e; - } catch (Exception e) { - if (e instanceof RuntimeException) { - throw (RuntimeException)e; + } + throw new ServiceRuntimeException("Reference not found: " + referenceName); + + } + + /** + * Select an endpoint reference from the component reference + * @param ref + * @return + */ + private EndpointReference getEndpointReference(ComponentReference ref) { + List<EndpointReference> eprs = ref.getEndpointReferences(); + if (eprs.size() == 1) { + // Return 1st one + return eprs.get(0); + } else { + for (EndpointReference epr : eprs) { + // Try to see if there is an EPR using binding.sca + if (epr.getBinding().getType().equals(SCABinding.TYPE)) { + return epr; + } } - throw new ServiceRuntimeException(e.getMessage(), e); + return eprs.get(0); } } + + /** + * Select an endpoint reference from the component reference + * @param ref + * @return + */ + private Endpoint getEndpoint(ComponentService service) { + List<Endpoint> eps = service.getEndpoints(); + if (eps.size() == 1) { + // Return 1st one + return eps.get(0); + } else { + for (Endpoint ep : eps) { + // Try to see if there is an EPR using binding.sca + if (ep.getBinding().getType().equals(SCABinding.TYPE)) { + return ep; + } + } + return eps.get(0); + } + } + /** * Gets the value for the specified property with the specified type. @@ -190,7 +219,6 @@ public class ComponentContextImpl implements RuntimeComponentContext { return targetService; } - public <B> ServiceReference<B> createSelfReference(Class<B> businessInterface) { ComponentService service = getSingleService(component); try { @@ -223,10 +251,10 @@ public class ComponentContextImpl implements RuntimeComponentContext { */ public <B> ServiceReference<B> createSelfReference(Class<B> businessInterface, ComponentService service) { try { - RuntimeComponentReference ref = - (RuntimeComponentReference)createSelfReference(component, service, businessInterface); + RuntimeEndpointReference ref = + (RuntimeEndpointReference)createSelfReference(component, service, businessInterface); ref.setComponent(component); - return getServiceReference(businessInterface, ref, null); + return getServiceReference(businessInterface, ref); } catch (Exception e) { throw new ServiceRuntimeException(e); } @@ -248,18 +276,17 @@ public class ComponentContextImpl implements RuntimeComponentContext { * @throws InvalidInterfaceException */ public <B> ServiceReference<B> getServiceReference(Class<B> businessInterface, - RuntimeComponentReference reference, - EndpointReference endpointReference) { + RuntimeEndpointReference endpointReference) { try { - RuntimeComponentReference ref = (RuntimeComponentReference)reference; - InterfaceContract interfaceContract = reference.getInterfaceContract(); - Reference componentTypeReference = reference.getReference(); + RuntimeComponentReference ref = (RuntimeComponentReference)endpointReference.getReference(); + InterfaceContract interfaceContract = ref.getInterfaceContract(); + Reference componentTypeReference = ref.getReference(); if (componentTypeReference != null && componentTypeReference.getInterfaceContract() != null) { interfaceContract = componentTypeReference.getInterfaceContract(); } InterfaceContract refInterfaceContract = getInterfaceContract(interfaceContract, businessInterface); if (refInterfaceContract != interfaceContract) { - ref = (RuntimeComponentReference)reference.clone(); + ref = (RuntimeComponentReference)ref.clone(); if (interfaceContract != null) { ref.setInterfaceContract(interfaceContract); } else { @@ -267,71 +294,24 @@ public class ComponentContextImpl implements RuntimeComponentContext { } } ref.setComponent(component); - return new ServiceReferenceImpl<B>(businessInterface, component, ref, endpointReference, proxyFactory, - component.getComponentContext().getCompositeContext()); + return new ServiceReferenceImpl<B>(businessInterface, endpointReference, component + .getComponentContext().getCompositeContext()); } catch (Exception e) { throw new ServiceRuntimeException(e); } } - /** - * Bind a component reference to a component service - * @param <B> - * @param businessInterface - * @param reference - * @param service - * @return - * @throws CloneNotSupportedException - * @throws InvalidInterfaceException - */ public <B> ServiceReference<B> getServiceReference(Class<B> businessInterface, - RuntimeComponentReference reference, - RuntimeComponent component, - RuntimeComponentService service) { - try { - 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.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 new ServiceReferenceImpl<B>(businessInterface, component, ref, null, proxyFactory, compositeContext); - } catch (Exception e) { - throw new ServiceRuntimeException(e); - } - } - - public <B> ServiceReference<B> getCallableReference(Class<B> businessInterface, - RuntimeComponent component, - RuntimeComponentService service) { + RuntimeEndpoint endpoint) { try { if (businessInterface == null) { - InterfaceContract contract = service.getInterfaceContract(); + InterfaceContract contract = endpoint.getInterfaceContract(); businessInterface = (Class<B>)((JavaInterface)contract.getInterface()).getJavaClass(); } - RuntimeComponentReference ref = - (RuntimeComponentReference)createSelfReference(component, service, businessInterface); + RuntimeEndpointReference ref = + (RuntimeEndpointReference)createSelfReference(component, endpoint.getService(), businessInterface); ref.setComponent(component); - return new ServiceReferenceImpl<B>(businessInterface, component, ref, null, proxyFactory, - compositeContext); + return new ServiceReferenceImpl<B>(businessInterface, ref, compositeContext); } catch (Exception e) { throw new ServiceRuntimeException(e); } @@ -344,13 +324,16 @@ public class ComponentContextImpl implements RuntimeComponentContext { * @throws CloneNotSupportedException * @throws InvalidInterfaceException */ - private ComponentReference createSelfReference(Component component, - ComponentService service, - Class<?> businessInterface) throws CloneNotSupportedException, + private EndpointReference createSelfReference(Component component, + ComponentService service, + Class<?> businessInterface) throws CloneNotSupportedException, InvalidInterfaceException { ComponentReference componentReference = assemblyFactory.createComponentReference(); componentReference.setName("$self$." + service.getName()); + + Endpoint endpoint = getEndpoint(service); + /* for (Binding binding : service.getBindings()) { if (binding instanceof OptimizableBinding) { OptimizableBinding optimizableBinding = (OptimizableBinding)((OptimizableBinding)binding).clone(); @@ -362,11 +345,13 @@ public class ComponentContextImpl implements RuntimeComponentContext { componentReference.getBindings().add(binding); } } + */ componentReference.setCallback(service.getCallback()); componentReference.getTargets().add(service); componentReference.getPolicySets().addAll(service.getPolicySets()); componentReference.getRequiredIntents().addAll(service.getRequiredIntents()); + componentReference.getBindings().add(endpoint.getBinding()); InterfaceContract interfaceContract = service.getInterfaceContract(); Service componentTypeService = service.getService(); @@ -377,32 +362,31 @@ public class ComponentContextImpl implements RuntimeComponentContext { componentReference.setInterfaceContract(interfaceContract); componentReference.setMultiplicity(Multiplicity.ONE_ONE); // component.getReferences().add(componentReference); - + // create endpoint reference - EndpointReference endpointReference = assemblyFactory - .createEndpointReference(); + EndpointReference endpointReference = assemblyFactory.createEndpointReference(); endpointReference.setComponent(component); endpointReference.setReference(componentReference); + endpointReference.setBinding(endpoint.getBinding()); endpointReference.setUnresolved(false); + endpointReference.setInterfaceContract(interfaceContract); endpointReference.setStatus(EndpointReference.WIRED_TARGET_FOUND_READY_FOR_MATCHING); - // create endpoint. - Endpoint endpoint = assemblyFactory.createEndpoint(); - endpoint.setComponent(component); - endpoint.setService(service); - endpoint.setUnresolved(true); endpointReference.setTargetEndpoint(endpoint); - + componentReference.getEndpointReferences().add(endpointReference); - + ((RuntimeComponentReference)componentReference).setComponent((RuntimeComponent)component); + ((RuntimeEndpointReference) endpointReference).bind(compositeContext); + + /* // do binding matching boolean ok = eprBinder.bind(compositeContext.getEndpointRegistry(), endpointReference); - + if (!ok) { throw new SCARuntimeException("Unable to bind " + endpointReference); } - - return componentReference; + */ + return endpointReference; } /** @@ -446,7 +430,6 @@ public class ComponentContextImpl implements RuntimeComponentContext { compositeActivator.start(compositeContext, component, reference); } - /* ******************** Contribution for issue TUSCANY-2281 ******************** */ /** @@ -470,8 +453,8 @@ public class ComponentContextImpl implements RuntimeComponentContext { if (referenceName.equals(ref.getName())) { ArrayList<ServiceReference<B>> serviceRefs = new ArrayList<ServiceReference<B>>(); for (EndpointReference endpointReference : ref.getEndpointReferences()) { - serviceRefs - .add(getServiceReference(businessInterface, (RuntimeComponentReference)ref, endpointReference)); + RuntimeEndpointReference epr = (RuntimeEndpointReference)endpointReference; + serviceRefs.add(getServiceReference(businessInterface, epr)); } return serviceRefs; } @@ -483,6 +466,7 @@ public class ComponentContextImpl implements RuntimeComponentContext { throw new ServiceRuntimeException(e.getMessage(), e); } } + /* ******************** Contribution for issue TUSCANY-2281 ******************** */ public CompositeContext getCompositeContext() { 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 dbd243a70b..c6a6ecd9c0 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 @@ -23,6 +23,7 @@ import java.util.List; import javax.security.auth.Subject; import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.assembly.EndpointReference; import org.apache.tuscany.sca.context.ThreadMessageContext; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.invocation.ExtensibleProxyFactory; @@ -33,7 +34,7 @@ import org.apache.tuscany.sca.invocation.Message; import org.apache.tuscany.sca.runtime.RuntimeComponent; import org.apache.tuscany.sca.runtime.RuntimeComponentReference; import org.apache.tuscany.sca.runtime.RuntimeComponentService; -import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; import org.oasisopen.sca.RequestContext; import org.oasisopen.sca.ServiceReference; @@ -68,12 +69,11 @@ public class RequestContextImpl implements RequestContext { public <B> ServiceReference<B> getServiceReference() { Message msgContext = ThreadMessageContext.getMessageContext(); // FIXME: [rfeng] Is this the service reference matching the caller side? - Endpoint to = msgContext.getTo(); - RuntimeComponentService service = (RuntimeComponentService) to.getService(); + RuntimeEndpoint to = (RuntimeEndpoint) msgContext.getTo(); RuntimeComponent component = (RuntimeComponent) to.getComponent(); - ServiceReference<B> callableReference = component.getComponentContext().getCallableReference(null, component, service); - + ServiceReference<B> callableReference = component.getComponentContext().getServiceReference(null, to); + return callableReference; } @@ -96,9 +96,9 @@ public class RequestContextImpl implements RequestContext { } JavaInterface javaInterface = (JavaInterface) callbackReference.getInterfaceContract().getInterface(); Class<CB> javaClass = (Class<CB>)javaInterface.getJavaClass(); - List<RuntimeWire> wires = callbackReference.getRuntimeWires(); + List<EndpointReference> wires = callbackReference.getEndpointReferences(); ProxyFactory proxyFactory = new ExtensibleProxyFactory(proxyFactoryExtensionPoint); - ServiceReferenceImpl ref = new CallbackServiceReferenceImpl(javaClass, wires, proxyFactory); + ServiceReferenceImpl ref = new CallbackServiceReferenceImpl(javaClass, wires); return ref; } 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 23126b5123..5edf357e60 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 @@ -21,17 +21,13 @@ 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; @@ -44,7 +40,6 @@ import org.apache.tuscany.sca.assembly.builder.BindingBuilder; import org.apache.tuscany.sca.assembly.builder.BuilderContext; 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.ProcessorContext; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; @@ -52,19 +47,19 @@ import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtens 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.InvalidInterfaceException; 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.Invocable; import org.apache.tuscany.sca.runtime.RuntimeComponent; import org.apache.tuscany.sca.runtime.RuntimeComponentReference; -import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; import org.oasisopen.sca.ServiceRuntimeException; /** @@ -78,27 +73,25 @@ public class ServiceReferenceImpl<B> implements ServiceReferenceExt<B> { protected transient ProxyFactory proxyFactory; protected transient Class<B> businessInterface; - protected transient Object proxy; + protected transient B proxy; protected Object callbackID; // The callbackID should be serializable - protected transient RuntimeComponent component; - protected transient RuntimeComponentReference reference; - protected transient EndpointReference endpointReference; + protected transient RuntimeEndpointReference endpointReference; - protected String scdl; - - private transient XMLStreamReader xmlReader; +// protected String scdl; +// +// private transient XMLStreamReader xmlReader; protected transient CompositeContext compositeContext; - private ExtensionPointRegistry registry; - private FactoryExtensionPoint modelFactories; + protected ExtensionPointRegistry registry; + protected FactoryExtensionPoint modelFactories; protected RuntimeAssemblyFactory assemblyFactory; - private StAXArtifactProcessorExtensionPoint staxProcessors; - private StAXArtifactProcessor<EndpointReference> staxProcessor; - private XMLInputFactory xmlInputFactory; - private XMLOutputFactory xmlOutputFactory; - private BuilderExtensionPoint builders; + protected StAXArtifactProcessorExtensionPoint staxProcessors; + protected StAXArtifactProcessor<EndpointReference> staxProcessor; + protected XMLInputFactory xmlInputFactory; + protected XMLOutputFactory xmlOutputFactory; + protected BuilderExtensionPoint builders; /* * Public constructor for Externalizable serialization/deserialization @@ -107,56 +100,24 @@ public class ServiceReferenceImpl<B> implements ServiceReferenceExt<B> { super(); } - /* - * Public constructor for use by XMLStreamReader2CallableReference - */ - // TODO - EPR - Is this required - public ServiceReferenceImpl(XMLStreamReader xmlReader) throws Exception { - this.xmlReader = xmlReader; - resolve(); - } - - protected ServiceReferenceImpl(Class<B> businessInterface, - RuntimeComponent component, - RuntimeComponentReference reference, - EndpointReference endpointReference, - ProxyFactory proxyFactory, - CompositeContext compositeContext) { - this.proxyFactory = proxyFactory; + public ServiceReferenceImpl(Class<B> businessInterface, + Invocable endpointReference, + CompositeContext compositeContext) { 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); + this.endpointReference = (RuntimeEndpointReference) endpointReference; + if (compositeContext == null) { + compositeContext = endpointReference.getCompositeContext(); } - - // FIXME: Should we normalize the componentName/serviceName URI into an absolute SCA URI in the SCA binding? - // sca:component1/component11/component112/service1? - initCallbackID(); + bind(compositeContext); } - private void getExtensions() { + public ServiceReferenceImpl(Class<B> businessInterface, + Invocable endpointReference) { + this(businessInterface, endpointReference, null); + } + + protected void bind(CompositeContext context) { + this.compositeContext = context; this.registry = compositeContext.getExtensionPointRegistry(); this.modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); this.assemblyFactory = (RuntimeAssemblyFactory)modelFactories.getFactory(AssemblyFactory.class); @@ -165,68 +126,29 @@ public class ServiceReferenceImpl<B> implements ServiceReferenceExt<B> { this.staxProcessors = registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); this.staxProcessor = staxProcessors.getProcessor(EndpointReference.class); this.builders = registry.getExtensionPoint(BuilderExtensionPoint.class); + this.proxyFactory = new ExtensibleProxyFactory(registry.getExtensionPoint(ProxyFactoryExtensionPoint.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() { + public RuntimeEndpointReference 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); + return proxy; } catch (Exception e) { throw new ObjectCreationException(e); } } - public void setProxy(Object proxy) { + public void setProxy(B proxy) { this.proxy = proxy; } - protected Object createProxy() throws Exception { + protected B createProxy() throws Exception { return proxyFactory.createProxy(this); } @@ -248,24 +170,6 @@ public class ServiceReferenceImpl<B> implements ServiceReferenceExt<B> { } } - 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. @@ -295,21 +199,23 @@ public class ServiceReferenceImpl<B> implements ServiceReferenceExt<B> { } // ============ 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 { + out.writeObject(endpointReference); + /* try { String xml = null; - if (scdl == null){ + if (scdl == null) { xml = toXMLString(); } else { xml = scdl; } - + if (xml == null) { out.writeBoolean(false); } else { @@ -320,73 +226,128 @@ public class ServiceReferenceImpl<B> implements ServiceReferenceExt<B> { // e.printStackTrace(); throw new IOException(e.toString()); } - } - + */ + } + /** * write the endpoint reference into an xml string */ - public String toXMLString() throws IOException, XMLStreamException, ContributionWriteException{ + public String toXMLString() throws IOException, XMLStreamException, ContributionWriteException { StringWriter writer = new StringWriter(); XMLStreamWriter streamWriter = xmlOutputFactory.createXMLStreamWriter(writer); staxProcessor.write(endpointReference, streamWriter, new ProcessorContext(registry)); 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 { + this.endpointReference = (RuntimeEndpointReference) in.readObject(); + // Force resolve + endpointReference.getComponent(); + bind(endpointReference.getCompositeContext()); + + RuntimeComponentReference reference = (RuntimeComponentReference)endpointReference.getReference(); + reference.setComponent((RuntimeComponent)endpointReference.getComponent()); + + 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); + + try { + javaInterfaceFactory.createJavaInterface(javaInterface, javaInterface.getJavaClass()); + } catch (InvalidInterfaceException e) { + throw new ServiceRuntimeException(e); + } + //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.getType()); + if (bindingBuilder != null) { + org.apache.tuscany.sca.assembly.builder.BuilderContext context = new BuilderContext(registry); + bindingBuilder.build(endpointReference.getComponent(), reference, endpointReference.getBinding(), context); + } + } + + this.proxyFactory = getProxyFactory(this.compositeContext); + + /* + endpointReference.bind(CompositeContext.getCurrentCompositeContext()); + endpointReference.rebuild(); + */ + /* 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 ){ + + if (scdl != null) { Reader reader = new StringReader(scdl); - - if (xmlInputFactory == null){ + + 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(); + bind(CompositeContext.getCurrentCompositeContext()); } - + streamReader = xmlInputFactory.createXMLStreamReader(reader); } - - endpointReference = staxProcessor.read(streamReader, new ProcessorContext(registry)); - + + endpointReference = (RuntimeEndpointReference) staxProcessor.read(streamReader, new ProcessorContext(registry)); + // ok to GC xmlReader = null; scdl = null; } - + */ + /** * @throws IOException */ private synchronized void resolve() throws Exception { - if ((scdl != null || xmlReader != null) && component == null && reference == null) { + /* + if ((scdl != null || xmlReader != null) && endpointReference == null) { fromXMLString(); - - this.component = (RuntimeComponent)endpointReference.getComponent(); - compositeContext.bindComponent(this.component); - - this.reference = (RuntimeComponentReference)endpointReference.getReference(); - this.reference.setComponent(this.component); - + + compositeContext.bindComponent((RuntimeComponent) endpointReference.getComponent()); + + RuntimeComponentReference reference = (RuntimeComponentReference)endpointReference.getReference(); + reference.setComponent((RuntimeComponent)endpointReference.getComponent()); + ReferenceParameters parameters = null; for (Object ext : reference.getExtensions()) { if (ext instanceof ReferenceParameters) { @@ -394,7 +355,7 @@ public class ServiceReferenceImpl<B> implements ServiceReferenceExt<B> { break; } } - + if (parameters != null) { this.callbackID = parameters.getCallbackID(); } @@ -410,10 +371,10 @@ public class ServiceReferenceImpl<B> implements ServiceReferenceExt<B> { 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 @@ -422,23 +383,24 @@ public class ServiceReferenceImpl<B> implements ServiceReferenceExt<B> { } this.businessInterface = (Class<B>)javaInterface.getJavaClass(); } - + Binding binding = endpointReference.getBinding(); if (binding != null) { BindingBuilder bindingBuilder = builders.getBindingBuilder(binding.getType()); if (bindingBuilder != null) { BuilderContext context = new BuilderContext(registry); - bindingBuilder.build(component, reference, endpointReference.getBinding(), context); + bindingBuilder.build(endpointReference.getComponent(), reference, endpointReference.getBinding(), context); } } - this.proxyFactory = getProxyFactory(this.compositeContext); + 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) { @@ -447,35 +409,19 @@ public class ServiceReferenceImpl<B> implements ServiceReferenceExt<B> { JavaInterfaceFactory javaInterfaceFactory = factories.getFactory(JavaInterfaceFactory.class); return javaInterfaceFactory; } - + private ProxyFactory getProxyFactory(CompositeContext compositeContext) { ExtensionPointRegistry extensionPointRegistry = compositeContext.getExtensionPointRegistry(); - ProxyFactoryExtensionPoint proxyFactories = extensionPointRegistry.getExtensionPoint(ProxyFactoryExtensionPoint.class); + 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/invocation/CallableReferenceObjectFactory.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallableReferenceObjectFactory.java index d4fe266d44..2323973938 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallableReferenceObjectFactory.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallableReferenceObjectFactory.java @@ -18,11 +18,10 @@ */ package org.apache.tuscany.sca.core.invocation; -import org.apache.tuscany.sca.assembly.EndpointReference; import org.apache.tuscany.sca.core.factory.ObjectCreationException; import org.apache.tuscany.sca.core.factory.ObjectFactory; import org.apache.tuscany.sca.runtime.RuntimeComponent; -import org.apache.tuscany.sca.runtime.RuntimeComponentReference; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; import org.oasisopen.sca.ServiceReference; /** @@ -32,9 +31,7 @@ import org.oasisopen.sca.ServiceReference; */ public class CallableReferenceObjectFactory implements ObjectFactory<ServiceReference<?>> { private Class<?> businessInterface; - private RuntimeComponent component; - private RuntimeComponentReference reference; - private EndpointReference endpointReference; + private RuntimeEndpointReference endpointReference; /** * Constructor. @@ -47,17 +44,14 @@ public class CallableReferenceObjectFactory implements ObjectFactory<ServiceRefe * @param binding the binding for the reference */ public CallableReferenceObjectFactory(Class<?> businessInterface, - RuntimeComponent component, - RuntimeComponentReference reference, - EndpointReference endpointReference) { + RuntimeEndpointReference endpointReference) { this.businessInterface = businessInterface; - this.component = component; - this.reference = reference; this.endpointReference = endpointReference; } public ServiceReference<?> getInstance() throws ObjectCreationException { - return component.getComponentContext().getServiceReference(businessInterface, reference, endpointReference); + RuntimeComponent component = (RuntimeComponent) endpointReference.getComponent(); + return component.getComponentContext().getServiceReference(businessInterface, endpointReference); } } diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackReferenceObjectFactory.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackReferenceObjectFactory.java index 021d588cb4..0c77ee75f6 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackReferenceObjectFactory.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackReferenceObjectFactory.java @@ -20,11 +20,10 @@ package org.apache.tuscany.sca.core.invocation; import java.util.List; +import org.apache.tuscany.sca.assembly.EndpointReference; import org.apache.tuscany.sca.core.context.impl.CallbackServiceReferenceImpl; -import org.apache.tuscany.sca.core.context.impl.ServiceReferenceImpl; import org.apache.tuscany.sca.core.factory.ObjectCreationException; import org.apache.tuscany.sca.core.factory.ObjectFactory; -import org.apache.tuscany.sca.runtime.RuntimeWire; import org.oasisopen.sca.ServiceReference; /** @@ -35,16 +34,16 @@ import org.oasisopen.sca.ServiceReference; public class CallbackReferenceObjectFactory implements ObjectFactory<ServiceReference<?>> { private Class<?> businessInterface; private ProxyFactory proxyFactory; - private List<RuntimeWire> wires; + private List<EndpointReference> wires; - public CallbackReferenceObjectFactory(Class<?> interfaze, ProxyFactory proxyFactory, List<RuntimeWire> wires) { + public CallbackReferenceObjectFactory(Class<?> interfaze, ProxyFactory proxyFactory, List<EndpointReference> wires) { this.businessInterface = interfaze; this.proxyFactory = proxyFactory; this.wires = wires; } public ServiceReference<?> getInstance() throws ObjectCreationException { - return new CallbackServiceReferenceImpl(businessInterface, wires, proxyFactory); + return new CallbackServiceReferenceImpl(businessInterface, wires); } } diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackWireObjectFactory.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackWireObjectFactory.java index 2b37c30451..90d7374145 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackWireObjectFactory.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackWireObjectFactory.java @@ -22,7 +22,7 @@ import java.util.List; import org.apache.tuscany.sca.core.factory.ObjectCreationException; import org.apache.tuscany.sca.core.factory.ObjectFactory; -import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.apache.tuscany.sca.runtime.Invocable; /** * Returns proxy instance for a wire callback @@ -32,9 +32,9 @@ import org.apache.tuscany.sca.runtime.RuntimeWire; public class CallbackWireObjectFactory<B> implements ObjectFactory<B> { private Class<B> businessInterface; private ProxyFactory proxyFactory; - private List<RuntimeWire> wires; + private List<Invocable> wires; - public CallbackWireObjectFactory(Class<B> interfaze, ProxyFactory proxyFactory, List<RuntimeWire> wires) { + public CallbackWireObjectFactory(Class<B> interfaze, ProxyFactory proxyFactory, List<Invocable> wires) { this.businessInterface = interfaze; this.proxyFactory = proxyFactory; this.wires = wires; 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 11edc283a4..f04573dd71 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,12 +28,13 @@ 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.ServiceReferenceExt; 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; import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; import org.apache.tuscany.sca.invocation.MessageFactory; -import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.apache.tuscany.sca.runtime.Invocable; import org.oasisopen.sca.ServiceReference; /** @@ -49,8 +50,8 @@ public class CglibProxyFactory implements ProxyFactory { } - public <T> T createProxy(Class<T> interfaze, RuntimeWire wire) throws ProxyCreationException { - ServiceReference<T> serviceReference = new ServiceReferenceImpl(interfaze, wire, this); + public <T> T createProxy(Class<T> interfaze, Invocable wire) throws ProxyCreationException { + ServiceReference<T> serviceReference = new ServiceReferenceImpl(interfaze, wire, null); return createProxy(serviceReference); } @@ -72,8 +73,8 @@ public class CglibProxyFactory implements ProxyFactory { * create the callback proxy with cglib. use the same * JDKCallbackInvocationHandler as JDKProxyService. */ - public <T> T createCallbackProxy(Class<T> interfaze, final List<RuntimeWire> wires) throws ProxyCreationException { - ServiceReferenceImpl<T> callbackReference = new ServiceReferenceImpl(interfaze, wires.get(0), this); + public <T> T createCallbackProxy(Class<T> interfaze, final List<? extends Invocable> wires) throws ProxyCreationException { + ServiceReferenceImpl<T> callbackReference = new ServiceReferenceImpl(interfaze, wires.get(0), null); return callbackReference != null ? createCallbackProxy(callbackReference) : null; } @@ -81,14 +82,15 @@ public class CglibProxyFactory implements ProxyFactory { * create the callback proxy with cglib. use the same * JDKCallbackInvocationHandler as JDKProxyService. */ - public <T> T createCallbackProxy(ServiceReferenceImpl<T> callbackReference) throws ProxyCreationException { + public <T> T createCallbackProxy(ServiceReference<T> callbackReference) throws ProxyCreationException { Enhancer enhancer = new Enhancer(); Class<T> interfaze = callbackReference.getBusinessInterface(); enhancer.setSuperclass(interfaze); enhancer.setCallback(new CglibMethodInterceptor<T>(callbackReference)); - Object proxy = enhancer.create(); - callbackReference.setProxy(proxy); - return interfaze.cast(proxy); + Object object = enhancer.create(); + T proxy = interfaze.cast(object); + ((ServiceReferenceExt<T>)callbackReference).setProxy(proxy); + return proxy; } @SuppressWarnings("unchecked") @@ -124,18 +126,6 @@ public class CglibProxyFactory implements ProxyFactory { invocationHandler = new JDKCallbackInvocationHandler(messageFactory, callbackReference); } - /* - public CglibMethodInterceptor(Class<T> interfaze, RuntimeWire wire) { - ServiceReference<T> serviceRef = new ServiceReferenceImpl<T>(interfaze, wire, CglibProxyFactory.this); - invocationHandler = new JDKInvocationHandler(messageFactory, serviceRef); - } - - public CglibMethodInterceptor(Class<T> interfaze, List<RuntimeWire> wires) { - CallbackReferenceImpl ref = new CallbackReferenceImpl(interfaze, CglibProxyFactory.this, wires); - invocationHandler = new JDKCallbackInvocationHandler(messageFactory, ref); - } - */ - /** * @see net.sf.cglib.proxy.MethodInterceptor#intercept(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], net.sf.cglib.proxy.MethodProxy) */ diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ExtensibleProxyFactory.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ExtensibleProxyFactory.java index fbdfe8a8e6..7f476b2fa5 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ExtensibleProxyFactory.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ExtensibleProxyFactory.java @@ -21,8 +21,7 @@ package org.apache.tuscany.sca.core.invocation; import java.util.List; -import org.apache.tuscany.sca.core.context.impl.ServiceReferenceImpl; -import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.apache.tuscany.sca.runtime.Invocable; import org.oasisopen.sca.ServiceReference; /** @@ -58,7 +57,7 @@ public class ExtensibleProxyFactory implements ProxyFactory { * @see org.apache.tuscany.sca.core.invocation.ProxyFactory#createCallbackProxy(java.lang.Class, * java.util.List) */ - public <T> T createCallbackProxy(Class<T> interfaze, List<RuntimeWire> wires) throws ProxyCreationException { + public <T> T createCallbackProxy(Class<T> interfaze, List<? extends Invocable> wires) throws ProxyCreationException { ProxyFactory interfaceFactory = proxyFactories.getInterfaceProxyFactory(); ProxyFactory classFactory = proxyFactories.getClassProxyFactory(); if (interfaze.isInterface()) { @@ -78,7 +77,7 @@ public class ExtensibleProxyFactory implements ProxyFactory { } } - public <T> T createCallbackProxy(ServiceReferenceImpl<T> callbackReference) throws ProxyCreationException { + public <T> T createCallbackProxy(ServiceReference<T> callbackReference) throws ProxyCreationException { ProxyFactory interfaceFactory = proxyFactories.getInterfaceProxyFactory(); ProxyFactory classFactory = proxyFactories.getClassProxyFactory(); if (callbackReference.getBusinessInterface().isInterface()) { @@ -90,9 +89,9 @@ public class ExtensibleProxyFactory implements ProxyFactory { /** * @see org.apache.tuscany.sca.core.invocation.ProxyFactory#createProxy(java.lang.Class, - * org.apache.tuscany.sca.runtime.RuntimeWire) + * org.apache.tuscany.sca.runtime.Invocable) */ - public <T> T createProxy(Class<T> interfaze, RuntimeWire wire) throws ProxyCreationException { + public <T> T createProxy(Class<T> interfaze, Invocable wire) throws ProxyCreationException { ProxyFactory interfaceFactory = proxyFactories.getInterfaceProxyFactory(); ProxyFactory classFactory = proxyFactories.getClassProxyFactory(); if (interfaze.isInterface()) { diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ExtensibleWireProcessor.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ExtensibleWireProcessor.java index 934f2f7aa0..08019ec3e2 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ExtensibleWireProcessor.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ExtensibleWireProcessor.java @@ -18,7 +18,8 @@ */ package org.apache.tuscany.sca.core.invocation; -import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; import org.apache.tuscany.sca.runtime.RuntimeWireProcessor; import org.apache.tuscany.sca.runtime.RuntimeWireProcessorExtensionPoint; @@ -28,16 +29,22 @@ import org.apache.tuscany.sca.runtime.RuntimeWireProcessorExtensionPoint; * @version $Rev$ $Date$ */ public class ExtensibleWireProcessor implements RuntimeWireProcessor { - + private RuntimeWireProcessorExtensionPoint processors; public ExtensibleWireProcessor(RuntimeWireProcessorExtensionPoint processors) { this.processors = processors; } - - public void process(RuntimeWire wire) { + + public void process(RuntimeEndpoint endpoint) { + for (RuntimeWireProcessor processor : processors.getWireProcessors()) { + processor.process(endpoint); + } + } + + public void process(RuntimeEndpointReference endpointReference) { for (RuntimeWireProcessor processor : processors.getWireProcessors()) { - processor.process(wire); + processor.process(endpointReference); } } diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/NonBlockingInterceptor.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/NonBlockingInterceptor.java index ec2f0a0664..6435efe4d6 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/NonBlockingInterceptor.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/NonBlockingInterceptor.java @@ -18,9 +18,7 @@ */ package org.apache.tuscany.sca.core.invocation; -import java.util.LinkedList; import java.util.List; -import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -31,7 +29,6 @@ import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.invocation.Interceptor; import org.apache.tuscany.sca.invocation.Invoker; import org.apache.tuscany.sca.invocation.Message; -import org.apache.tuscany.sca.runtime.RuntimeWire; import org.apache.tuscany.sca.work.WorkScheduler; import org.oasisopen.sca.ServiceRuntimeException; @@ -123,7 +120,7 @@ public class NonBlockingInterceptor implements Interceptor { */ private static class ImmutableMessage implements Message { - public Object getBody() { + public <T> T getBody() { return null; } @@ -133,10 +130,6 @@ public class NonBlockingInterceptor implements Interceptor { } } - public void setCallbackWires(LinkedList<RuntimeWire> wires) { - - } - public Object getMessageID() { return null; } @@ -176,17 +169,6 @@ public class NonBlockingInterceptor implements Interceptor { public void setOperation(Operation op) { throw new UnsupportedOperationException(); } - - /** - * @see org.apache.tuscany.sca.invocation.Message#getReplyTo() - */ - public EndpointReference getReplyTo() { - return null; - } - - public Map<String, Object> getQoSContext() { - return null; - } public List<Object> getHeaders() { return null; diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ProxyFactory.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ProxyFactory.java index d75ff309ff..fbf703bad0 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ProxyFactory.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ProxyFactory.java @@ -20,8 +20,7 @@ package org.apache.tuscany.sca.core.invocation; import java.util.List; -import org.apache.tuscany.sca.core.context.impl.ServiceReferenceImpl; -import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.apache.tuscany.sca.runtime.Invocable; import org.oasisopen.sca.ServiceReference; /** @@ -36,11 +35,11 @@ public interface ProxyFactory { * Creates a Java proxy for the given wire * * @param interfaze the interface the proxy implements - * @param wire the wire to proxy + * @param invocable the wire to proxy * @return the proxy * @throws ProxyCreationException */ - <T> T createProxy(Class<T> interfaze, RuntimeWire wire) throws ProxyCreationException; + <T> T createProxy(Class<T> interfaze, Invocable invocable) throws ProxyCreationException; /** * Creates a Java proxy for the given CallableReference @@ -58,7 +57,7 @@ public interface ProxyFactory { * @return the proxy * @throws ProxyCreationException */ - <T> T createCallbackProxy(Class<T> interfaze, List<RuntimeWire> wires) throws ProxyCreationException; + <T> T createCallbackProxy(Class<T> interfaze, List<? extends Invocable> invocables) throws ProxyCreationException; /** * Creates a Java proxy for the given callback reference @@ -67,7 +66,7 @@ public interface ProxyFactory { * @return the proxy * @throws ProxyCreationException */ - <T> T createCallbackProxy(ServiceReferenceImpl<T> callbackReference) throws ProxyCreationException; + <T> T createCallbackProxy(ServiceReference<T> callbackReference) throws ProxyCreationException; /** * Cast a proxy to a CallableReference. diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/RuntimeInvoker.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/RuntimeInvoker.java new file mode 100644 index 0000000000..0751fd631b --- /dev/null +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/RuntimeInvoker.java @@ -0,0 +1,97 @@ +/* + * 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.invocation; + +import java.lang.reflect.InvocationTargetException; + +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.context.ThreadMessageContext; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.InvocationChain; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.runtime.Invocable; + +/** + * Invoker for a endpoint or endpoint reference + * @version $Rev$ $Date$ + */ +public class RuntimeInvoker implements Invoker { + protected MessageFactory messageFactory; + protected Invocable invocable; + + public RuntimeInvoker(MessageFactory messageFactory, Invocable invocable) { + this.messageFactory = messageFactory; + this.invocable = invocable; + } + + public Message invokeBinding(Message msg) { + Message context = ThreadMessageContext.setMessageContext(msg); + try { + return invocable.getBindingInvocationChain().getHeadInvoker().invoke(msg); + } finally { + ThreadMessageContext.setMessageContext(context); + } + } + + public Message invoke(Message msg) { + return invoke(msg.getOperation(), msg); + } + + public Object invoke(Operation operation, Object[] args) throws InvocationTargetException { + Message msg = messageFactory.createMessage(); + msg.setBody(args); + Message resp = invoke(operation, msg); + Object body = resp.getBody(); + if (resp.isFault()) { + throw new InvocationTargetException((Throwable)body); + } + return body; + } + + + public Message invoke(Operation operation, Message msg) { + InvocationChain chain = invocable.getInvocationChain(operation); + return invoke(chain, msg); + } + + public Message invoke(InvocationChain chain, Message msg) { + + if (invocable instanceof Endpoint) { + msg.setTo((Endpoint)invocable); + } else if (invocable instanceof EndpointReference) { + msg.setFrom((EndpointReference)invocable); + } + + Invoker headInvoker = chain.getHeadInvoker(); + Operation operation = chain.getTargetOperation(); + msg.setOperation(operation); + + Message msgContext = ThreadMessageContext.setMessageContext(msg); + try { + return headInvoker.invoke(msg); + } finally { + ThreadMessageContext.setMessageContext(msgContext); + } + } + +} diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/RuntimeWireInvoker.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/RuntimeWireInvoker.java deleted file mode 100644 index 43726b85dd..0000000000 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/RuntimeWireInvoker.java +++ /dev/null @@ -1,268 +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.invocation; - -import java.lang.reflect.InvocationTargetException; - -import org.apache.tuscany.sca.assembly.EndpointReference; -import org.apache.tuscany.sca.context.ThreadMessageContext; -import org.apache.tuscany.sca.core.factory.InstanceWrapper; -import org.apache.tuscany.sca.interfacedef.Operation; -import org.apache.tuscany.sca.invocation.InvocationChain; -import org.apache.tuscany.sca.invocation.Invoker; -import org.apache.tuscany.sca.invocation.Message; -import org.apache.tuscany.sca.invocation.MessageFactory; -import org.apache.tuscany.sca.runtime.RuntimeWire; -import org.oasisopen.sca.ServiceRuntimeException; - -/** - * @version $Rev$ $Date$ - */ -public class RuntimeWireInvoker implements Invoker{ - protected MessageFactory messageFactory; - protected Object conversationID; - protected Object callbackID; - protected Object callbackObject; - protected RuntimeWire wire; - - public RuntimeWireInvoker(MessageFactory messageFactory, RuntimeWire wire) { - this.messageFactory = messageFactory; - this.wire = wire; - //init(wire); - } - - /* TODO - EPR - not required for OASIS - protected void init(RuntimeWire wire) { - if (wire != null) { - - ReferenceParameters parameters = wire.getSource().getReferenceParameters(); - this.callbackID = parameters.getCallbackID(); - this.callbackObject = parameters.getCallbackReference(); - this.conversationID = parameters.getConversationID(); - InterfaceContract contract = wire.getSource().getInterfaceContract(); - this.conversational = contract.getInterface().isConversational(); - } - } - */ - - /* - * TODO - Introduced to allow the RuntimeWireInvoker to sit on the end of the - * service binding chain. Runtime wire invoke needs splitting up into - * separate conversation, callback interceptors etc. - */ - public Message invoke(Message msg) { - - try { - Object response = invoke(msg.getOperation(),msg); - // Hack to put the response back in a message. - // shouldn't take it out of the response message in the first place - msg.setBody(response); - } catch (InvocationTargetException e) { -// throw new ServiceRuntimeException(e); - } - - return msg; - } - - public Object invoke(Operation operation, Message msg) throws InvocationTargetException { - return invoke(wire, operation, msg); - } - - public Object invoke(RuntimeWire wire, Operation operation, Message msg) throws InvocationTargetException { - RuntimeWire runtimeWire = wire == null ? this.wire : wire; - InvocationChain chain = runtimeWire.getInvocationChain(operation); - return invoke(chain, msg, runtimeWire); - } - - protected Object invoke(InvocationChain chain, Message msg, RuntimeWire wire) throws InvocationTargetException { - EndpointReference from = msg.getFrom(); - EndpointReference epFrom = wire.getEndpointReference(); - if (from != null) { - from.setComponent(epFrom.getComponent()); - from.setReference(epFrom.getReference()); - from.setBinding(epFrom.getBinding()); - from.setInterfaceContract(epFrom.getInterfaceContract()); - from.setTargetEndpoint(epFrom.getTargetEndpoint()); - //from.setCallbackEndpoint(epFrom.getCallbackEndpoint()); - - // TODO - EPR - what's going on here? - //from.mergeEndpoint(epFrom); - } else { - msg.setFrom(epFrom); - } - msg.setTo(wire.getEndpoint()); - - Invoker headInvoker = chain.getHeadInvoker(); - Operation operation = chain.getTargetOperation(); - msg.setOperation(operation); - - Message msgContext = ThreadMessageContext.getMessageContext(); - // TODO - EPR - no required for OASIS - //Object currentConversationID = msgContext.getFrom().getReferenceParameters().getConversationID(); - - ThreadMessageContext.setMessageContext(msg); - try { - // TODO - EPR - no required for OASIS - //conversationPreinvoke(msg); - // handleCallback(msg, currentConversationID); - // dispatch the wire down the chain and get the response - Message resp = headInvoker.invoke(msg); - Object body = resp.getBody(); - if (resp.isFault()) { - throw new InvocationTargetException((Throwable)body); - } - return body; - } catch (InvocationTargetException e) { - throw e; - } catch (Throwable e) { - throw new ServiceRuntimeException(e); - } finally { - try { - // TODO - EPR - no required for OASIS - //conversationPostInvoke(msg); - //} catch (TargetDestructionException e) { - // throw new ServiceRuntimeException(e); - } finally { - ThreadMessageContext.setMessageContext(msgContext); - } - } - } - - /** - * @param msgContext - */ - /* TODO - EPR - no required for OASIS - protected EndpointReference getCallbackEndpoint(Message msgContext) { - EndpointReference from = msgContext.getFrom(); - return from == null ? null : from.getReferenceParameters().getCallbackReference(); - } - */ - - /** - * Pre-invoke for the conversation handling - * @param msg - * @throws TargetResolutionException - */ - /* TODO - EPR - no required for OASIS - private void conversationPreinvoke(Message msg) { - if (conversational) { - ReferenceParameters parameters = msg.getFrom().getReferenceParameters(); - // in some cases the ConversationID that should be used comes in with the - // message, e.g. when ws binding is in use. - Object convID = parameters.getConversationID(); - if (convID != null) { - conversationID = convID; - } - conversation = conversationManager.getConversation(conversationID); - - if (conversation == null || conversation.getState() == ConversationState.ENDED) { - conversation = conversationManager.startConversation(conversationID); - conversation.initializeConversationAttributes(wire.getTarget().getComponent()); - } else if (conversation.conversationalAttributesInitialized() == false) { - conversation.initializeConversationAttributes(wire.getTarget().getComponent()); - } else if (conversation.isExpired()){ - throw new ConversationEndedException("Conversation has expired."); - } - - conversation.updateLastReferencedTime(); - - parameters.setConversationID(conversation.getConversationID()); - } - } - */ - - /** - * Post-invoke for the conversation handling - * @param wire - * @param operation - * @throws TargetDestructionException - */ - /* TODO - EPR - no required for OASIS - @SuppressWarnings("unchecked") - private void conversationPostInvoke(Message msg) throws TargetDestructionException { - if (conversational) { - Operation operation = msg.getOperation(); - ConversationSequence sequence = operation.getConversationSequence(); - if (sequence == ConversationSequence.CONVERSATION_END) { - // in some cases the ConversationID that should be used comes in with the - // message, e.g. when ws binding is in use. - Object convID = msg.getFrom().getReferenceParameters().getConversationID(); - if (convID != null) { - conversationID = convID; - } - conversation = conversationManager.getConversation(conversationID); - - // remove conversation id from scope container - ScopeContainer scopeContainer = getConversationalScopeContainer(msg); - - if (scopeContainer != null) { - scopeContainer.remove(conversation.getConversationID()); - } - - conversation.end(); - } - } - } - - @SuppressWarnings("unchecked") - private ScopeContainer getConversationalScopeContainer(Message msg) { - ScopeContainer scopeContainer = null; - - RuntimeComponent component = (RuntimeComponent) msg.getTo().getComponent(); - - if (component instanceof ScopedRuntimeComponent) { - ScopedRuntimeComponent scopedRuntimeComponent = (ScopedRuntimeComponent)component; - ScopeContainer container = scopedRuntimeComponent.getScopeContainer(); - - if ((container != null) && (container.getScope() == Scope.CONVERSATION)) { - scopeContainer = container; - } - } - - return scopeContainer; - } - */ - - - /** - * Minimal wrapper for a callback object contained in a ServiceReference - */ - private static class CallbackObjectWrapper<T> implements InstanceWrapper<T> { - - private T instance; - - private CallbackObjectWrapper(T instance) { - this.instance = instance; - } - - public T getInstance() { - return instance; - } - - public void start() { - // do nothing - } - - public void stop() { - // do nothing - } - } - -} diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/WireObjectFactory.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/WireObjectFactory.java index 2fc3d6c7eb..26b3590d14 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/WireObjectFactory.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/WireObjectFactory.java @@ -22,7 +22,7 @@ import org.apache.tuscany.sca.core.context.impl.ServiceReferenceImpl; import org.apache.tuscany.sca.core.factory.ObjectCreationException; import org.apache.tuscany.sca.core.factory.ObjectFactory; import org.apache.tuscany.sca.core.invocation.impl.NoMethodForOperationException; -import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.apache.tuscany.sca.runtime.Invocable; /** * Uses a wire to return an object instance @@ -31,7 +31,7 @@ import org.apache.tuscany.sca.runtime.RuntimeWire; */ public class WireObjectFactory<T> implements ObjectFactory<T> { private Class<T> interfaze; - private RuntimeWire wire; + private Invocable wire; private ProxyFactory proxyService; /** @@ -42,14 +42,14 @@ public class WireObjectFactory<T> implements ObjectFactory<T> { * @param proxyService the wire service to create the proxy * @throws NoMethodForOperationException */ - public WireObjectFactory(Class<T> interfaze, RuntimeWire wire, ProxyFactory proxyService) { + public WireObjectFactory(Class<T> interfaze, Invocable wire, ProxyFactory proxyService) { this.interfaze = interfaze; this.wire = wire; this.proxyService = proxyService; } public T getInstance() throws ObjectCreationException { - return new ServiceReferenceImpl<T>(interfaze, wire, proxyService).getProxy(); + return new ServiceReferenceImpl<T>(interfaze, wire, null).getProxy(); } } diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKCallbackInvocationHandler.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKCallbackInvocationHandler.java index e64b9f3068..168af952db 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKCallbackInvocationHandler.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKCallbackInvocationHandler.java @@ -21,13 +21,11 @@ package org.apache.tuscany.sca.core.invocation.impl; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; - -import org.apache.tuscany.sca.core.assembly.impl.RuntimeWireImpl; import org.apache.tuscany.sca.core.context.impl.CallbackServiceReferenceImpl; -import org.apache.tuscany.sca.core.context.impl.ServiceReferenceImpl; import org.apache.tuscany.sca.invocation.InvocationChain; import org.apache.tuscany.sca.invocation.MessageFactory; -import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; +import org.oasisopen.sca.ServiceReference; import org.oasisopen.sca.ServiceRuntimeException; /** @@ -39,7 +37,7 @@ import org.oasisopen.sca.ServiceRuntimeException; public class JDKCallbackInvocationHandler extends JDKInvocationHandler { private static final long serialVersionUID = -3350283555825935609L; - public JDKCallbackInvocationHandler(MessageFactory messageFactory, ServiceReferenceImpl ref) { + public JDKCallbackInvocationHandler(MessageFactory messageFactory, ServiceReference<?> ref) { super(messageFactory, ref); this.fixedWire = false; } @@ -53,7 +51,7 @@ public class JDKCallbackInvocationHandler extends JDKInvocationHandler { } // obtain a dedicated wire to be used for this callback invocation - RuntimeWire wire = ((CallbackServiceReferenceImpl)callableReference).getCallbackWire(); + RuntimeEndpointReference wire = ((CallbackServiceReferenceImpl)callableReference).getCallbackEPR(); if (wire == null) { //FIXME: need better exception throw new ServiceRuntimeException("No callback wire found"); @@ -67,13 +65,12 @@ public class JDKCallbackInvocationHandler extends JDKInvocationHandler { } try { - return invoke(chain, args, wire, wire.getEndpointReference()); + return invoke(chain, args, wire); } catch (InvocationTargetException e) { Throwable t = e.getCause(); - throw e; + throw t; } finally { // allow the cloned wire to be reused by subsequent callbacks - ((RuntimeWireImpl)wire).releaseWire(); } } diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKInvocationHandler.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKInvocationHandler.java index 08521d1b88..0049214e4a 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKInvocationHandler.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKInvocationHandler.java @@ -28,11 +28,8 @@ import java.util.List; import java.util.Map; import org.apache.tuscany.sca.assembly.Endpoint; -import org.apache.tuscany.sca.assembly.EndpointReference; import org.apache.tuscany.sca.context.ThreadMessageContext; import org.apache.tuscany.sca.core.context.ServiceReferenceExt; -import org.apache.tuscany.sca.core.factory.InstanceWrapper; -import org.apache.tuscany.sca.core.scope.TargetResolutionException; import org.apache.tuscany.sca.interfacedef.DataType; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.interfacedef.java.JavaOperation; @@ -40,7 +37,8 @@ import org.apache.tuscany.sca.invocation.InvocationChain; import org.apache.tuscany.sca.invocation.Invoker; import org.apache.tuscany.sca.invocation.Message; import org.apache.tuscany.sca.invocation.MessageFactory; -import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.apache.tuscany.sca.runtime.Invocable; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; import org.oasisopen.sca.ServiceReference; import org.oasisopen.sca.ServiceRuntimeException; @@ -52,9 +50,8 @@ public class JDKInvocationHandler implements InvocationHandler, Serializable { protected boolean conversational; protected MessageFactory messageFactory; - protected EndpointReference source; protected Endpoint target; - protected RuntimeWire wire; + protected RuntimeEndpointReference source; protected ServiceReferenceExt<?> callableReference; protected Class<?> businessInterface; @@ -62,11 +59,10 @@ public class JDKInvocationHandler implements InvocationHandler, Serializable { protected transient Map<Method, InvocationChain> chains = new IdentityHashMap<Method, InvocationChain>(); - public JDKInvocationHandler(MessageFactory messageFactory, Class<?> businessInterface, RuntimeWire wire) { + public JDKInvocationHandler(MessageFactory messageFactory, Class<?> businessInterface, RuntimeEndpointReference source) { this.messageFactory = messageFactory; - this.wire = wire; + this.source = source; this.businessInterface = businessInterface; - init(this.wire); } public JDKInvocationHandler(MessageFactory messageFactory, ServiceReference<?> callableReference) { @@ -74,49 +70,40 @@ public class JDKInvocationHandler implements InvocationHandler, Serializable { this.callableReference = (ServiceReferenceExt<?>)callableReference; if (callableReference != null) { this.businessInterface = callableReference.getBusinessInterface(); - this.wire = ((ServiceReferenceExt<?>)callableReference).getRuntimeWire(); - if (wire != null) { - init(wire); - } + this.source = (RuntimeEndpointReference) this.callableReference.getEndpointReference(); } } - protected void init(RuntimeWire wire) { - } public Class<?> getBusinessInterface() { return businessInterface; } protected Object getCallbackID() { - return null; + return null; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (Object.class == method.getDeclaringClass()) { return invokeObjectMethod(method, args); } - if (wire == null) { - throw new ServiceRuntimeException("No runtime wire is available"); + if (source == null) { + throw new ServiceRuntimeException("No runtime source is available"); } - if (wire.isOutOfDate()) { - wire.rebuild(); + if (source.isOutOfDate()) { + source.rebuild(); chains.clear(); } - InvocationChain chain = getInvocationChain(method, wire); + InvocationChain chain = getInvocationChain(method, source); if (chain == null) { throw new IllegalArgumentException("No matching operation is found: " + method); } - - // The EndpointReference is not now resolved until the invocation chain - // is first created so reset the source here - source = wire.getEndpointReference(); - // send the invocation down the wire - Object result = invoke(chain, args, wire, source); + // send the invocation down the source + Object result = invoke(chain, args, source); return result; } @@ -200,12 +187,12 @@ public class JDKInvocationHandler implements InvocationHandler, Serializable { } - protected synchronized InvocationChain getInvocationChain(Method method, RuntimeWire wire) { + protected synchronized InvocationChain getInvocationChain(Method method, Invocable source) { if (fixedWire && chains.containsKey(method)) { return chains.get(method); } InvocationChain found = null; - for (InvocationChain chain : wire.getInvocationChains()) { + for (InvocationChain chain : source.getInvocationChains()) { Operation operation = chain.getSourceOperation(); if (operation.isDynamic()) { operation.setName(method.getName()); @@ -226,14 +213,14 @@ public class JDKInvocationHandler implements InvocationHandler, Serializable { this.target = endpoint; } - protected Object invoke(InvocationChain chain, Object[] args, RuntimeWire wire, EndpointReference source) + protected Object invoke(InvocationChain chain, Object[] args, RuntimeEndpointReference source) throws Throwable { Message msg = messageFactory.createMessage(); msg.setFrom(source); if (target != null) { msg.setTo(target); } else { - msg.setTo(wire.getEndpoint()); + msg.setTo(source.getTargetEndpoint()); } Invoker headInvoker = chain.getHeadInvoker(); Operation operation = chain.getTargetOperation(); @@ -241,12 +228,11 @@ public class JDKInvocationHandler implements InvocationHandler, Serializable { msg.setBody(args); Message msgContext = ThreadMessageContext.getMessageContext(); - - handleCallback(msg, wire); + ThreadMessageContext.setMessageContext(msg); - boolean abnormalEndConversation = false; + try { - // dispatch the wire down the chain and get the response + // dispatch the source down the chain and get the response Message resp = headInvoker.invoke(msg); Object body = resp.getBody(); if (resp.isFault()) { @@ -254,29 +240,12 @@ public class JDKInvocationHandler implements InvocationHandler, Serializable { } return body; } finally { - //conversationPostInvoke(msg, wire, abnormalEndConversation); + //conversationPostInvoke(msg, source, abnormalEndConversation); ThreadMessageContext.setMessageContext(msgContext); } } /** - * @param msg - * @param wire - * @param interfaze - * @throws TargetResolutionException - */ - private void handleCallback(Message msg, RuntimeWire wire) - throws TargetResolutionException { - - //ReferenceParameters parameters = msg.getFrom().getReferenceParameters(); - //parameters.setCallbackID(getCallbackID()); - - if (msg.getFrom() == null || msg.getFrom().getCallbackEndpoint() == null) { - return; - } - } - - /** * @return the callableReference */ public ServiceReference<?> getCallableReference() { @@ -290,29 +259,4 @@ public class JDKInvocationHandler implements InvocationHandler, Serializable { this.callableReference = (ServiceReferenceExt<?>)callableReference; } - /** - * Minimal wrapper for a callback object contained in a ServiceReference - */ - private static class CallbackObjectWrapper<T> implements InstanceWrapper<T> { - - private T instance; - - private CallbackObjectWrapper(T instance) { - this.instance = instance; - } - - public T getInstance() { - return instance; - } - - public void start() { - // do nothing - } - - public void stop() { - // do nothing - } - - } - } diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKProxyFactory.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKProxyFactory.java index 853e28fafa..13d4040b8e 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKProxyFactory.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKProxyFactory.java @@ -32,13 +32,14 @@ import javax.xml.ws.Response; import org.apache.tuscany.sca.common.java.collection.LRUCache; import org.apache.tuscany.sca.core.LifeCycleListener; +import org.apache.tuscany.sca.core.context.ServiceReferenceExt; import org.apache.tuscany.sca.core.context.impl.CallbackServiceReferenceImpl; import org.apache.tuscany.sca.core.context.impl.ServiceReferenceImpl; import org.apache.tuscany.sca.core.invocation.ProxyCreationException; import org.apache.tuscany.sca.core.invocation.ProxyFactory; import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; import org.apache.tuscany.sca.invocation.MessageFactory; -import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.apache.tuscany.sca.runtime.Invocable; import org.oasisopen.sca.ServiceReference; @@ -60,8 +61,8 @@ public class JDKProxyFactory implements ProxyFactory, LifeCycleListener { * The original createProxy method assumes that the proxy doesn't want to * share conversation state so sets the conversation object to null */ - public <T> T createProxy(Class<T> interfaze, RuntimeWire wire) throws ProxyCreationException { - ServiceReference<T> serviceReference = new ServiceReferenceImpl(interfaze, wire, this); + public <T> T createProxy(Class<T> interfaze, Invocable wire) throws ProxyCreationException { + ServiceReference<T> serviceReference = new ServiceReferenceImpl<T>(interfaze, wire, null); return createProxy(serviceReference); } @@ -80,9 +81,9 @@ public class JDKProxyFactory implements ProxyFactory, LifeCycleListener { return interfaze.getClassLoader(); } }); - Object proxy = newProxyInstance(cl, new Class[] {interfaze}, handler); - ((ServiceReferenceImpl)callableReference).setProxy(proxy); - return interfaze.cast(proxy); + T proxy = interfaze.cast(newProxyInstance(cl, new Class[] {interfaze}, handler)); + ((ServiceReferenceExt<T>)callableReference).setProxy(proxy); + return proxy; } private boolean isAsync(Class<?> interfaze) { @@ -103,19 +104,19 @@ public class JDKProxyFactory implements ProxyFactory, LifeCycleListener { return false; } - public <T> T createCallbackProxy(Class<T> interfaze, List<RuntimeWire> wires) throws ProxyCreationException { - ServiceReferenceImpl<T> callbackReference = new CallbackServiceReferenceImpl(interfaze, wires, this); + public <T> T createCallbackProxy(Class<T> interfaze, List<? extends Invocable> wires) throws ProxyCreationException { + ServiceReferenceImpl<T> callbackReference = new CallbackServiceReferenceImpl(interfaze, wires); return callbackReference != null ? createCallbackProxy(callbackReference) : null; } - public <T> T createCallbackProxy(ServiceReferenceImpl<T> callbackReference) throws ProxyCreationException { + public <T> T createCallbackProxy(ServiceReference<T> callbackReference) throws ProxyCreationException { assert callbackReference != null; Class<T> interfaze = callbackReference.getBusinessInterface(); InvocationHandler handler = new JDKCallbackInvocationHandler(messageFactory, callbackReference); ClassLoader cl = interfaze.getClassLoader(); - Object proxy = newProxyInstance(cl, new Class[] {interfaze}, handler); - callbackReference.setProxy(proxy); - return interfaze.cast(proxy); + T proxy = interfaze.cast(newProxyInstance(cl, new Class[] {interfaze}, handler)); + ((ServiceReferenceExt<T>) callbackReference).setProxy(proxy); + return proxy; } public <B, R extends ServiceReference<B>> R cast(B target) throws IllegalArgumentException { diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/impl/EndpointReferenceBinderImpl.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/impl/EndpointReferenceBinderImpl.java index bab83bad2d..da5abeb728 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/impl/EndpointReferenceBinderImpl.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/impl/EndpointReferenceBinderImpl.java @@ -68,7 +68,7 @@ public class EndpointReferenceBinderImpl implements EndpointReferenceBinder { /** * Build a single endpoint reference * - * @param endpoint + * @param invocable * @param monitor */ public boolean bind(EndpointRegistry endpointRegistry, EndpointReference endpointReference) { |