diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-18 19:06:40 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-18 19:06:40 +0000 |
commit | bf06eae38c29f648bb1b443ea8ea525c61bb630b (patch) | |
tree | b648cd4ede1614b4ab8645380366ee0049d33dff /java/sca/modules/core/src/main/java/org | |
parent | b22c95d7267170bfe00620be78302aa365cfdc8f (diff) |
Rename the methods on RuntimeEndpoint/RuntimeEndpointReference
Defer the unmarshalling of XML into endpoint/endpointReference
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@881883 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/core/src/main/java/org')
4 files changed, 154 insertions, 149 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 4b1fd203fc..58ffb0f9b9 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 @@ -513,14 +513,14 @@ public class CompositeActivatorImpl implements CompositeActivator { epr.bind(compositeContext); ComponentReference reference = endpointReference.getReference(); - InterfaceContract sourceContract = epr.getReferenceInterfaceContract(); + InterfaceContract sourceContract = epr.getComponentTypeReferenceInterfaceContract(); // 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 = ((RuntimeEndpoint) endpointReference.getTargetEndpoint()).getServiceInterfaceContract(); + sourceContract = ((RuntimeEndpoint) endpointReference.getTargetEndpoint()).getComponentTypeServiceInterfaceContract(); reference.setInterfaceContract(sourceContract); } 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 6eb392f084..abe0fdd78b 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 @@ -19,7 +19,6 @@ package org.apache.tuscany.sca.core.assembly.impl; -import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; @@ -36,6 +35,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; @@ -55,25 +55,18 @@ public class EndpointSerializerImpl implements EndpointSerializer { refProcessor = processors.getProcessor(EndpointReference.class); } - - public Endpoint readEndpoint(String xml) throws IOException { + public Endpoint readEndpoint(String xml) { try { XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(xml)); Endpoint result = processor.read(reader, new ProcessorContext(registry)); reader.close(); return result; } catch (Exception e) { - throw wrap(e); + throw new ServiceRuntimeException(e); } } - private IOException wrap(Exception e) { - IOException ex = new IOException(e.getMessage()); - ex.initCause(e); - return ex; - } - - public String write(Endpoint endpoint) throws IOException { + public String write(Endpoint endpoint) { StringWriter sw = new StringWriter(); try { XMLStreamWriter writer = outputFactory.createXMLStreamWriter(sw); @@ -82,22 +75,22 @@ public class EndpointSerializerImpl implements EndpointSerializer { writer.close(); return sw.toString(); } catch (Exception e) { - throw wrap(e); + throw new ServiceRuntimeException(e); } } - public EndpointReference readEndpointReference(String xml) throws IOException { + public EndpointReference readEndpointReference(String xml) { try { XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(xml)); EndpointReference result = refProcessor.read(reader, new ProcessorContext(registry)); reader.close(); return result; } catch (Exception e) { - throw wrap(e); + throw new ServiceRuntimeException(e); } } - public String write(EndpointReference endpointReference) throws IOException { + public String write(EndpointReference endpointReference) { StringWriter sw = new StringWriter(); try { XMLStreamWriter writer = outputFactory.createXMLStreamWriter(sw); @@ -106,7 +99,7 @@ public class EndpointSerializerImpl implements EndpointSerializer { writer.close(); return sw.toString(); } catch (Exception e) { - throw wrap(e); + throw new ServiceRuntimeException(e); } } } 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 29262949df..e0722d3b76 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 @@ -19,9 +19,10 @@ package org.apache.tuscany.sca.core.assembly.impl; +import java.io.Externalizable; import java.io.IOException; -import java.io.ObjectStreamException; -import java.io.Serializable; +import java.io.ObjectInput; +import java.io.ObjectOutput; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; @@ -34,7 +35,6 @@ 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.Service; import org.apache.tuscany.sca.assembly.impl.EndpointImpl; import org.apache.tuscany.sca.context.CompositeContext; @@ -76,25 +76,25 @@ import org.oasisopen.sca.ServiceRuntimeException; /** * Runtime model for Endpoint that supports java serialization */ -public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint { +public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint, Externalizable { private transient CompositeContext compositeContext; private transient EndpointRegistry endpointRegistry; private transient RuntimeWireProcessor wireProcessor; + private transient ProviderFactoryExtensionPoint providerFactories; 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 EndpointSerializer serializer; private transient List<InvocationChain> chains; - private transient final Map<Operation, InvocationChain> invocationChainMap = + private transient 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; @@ -111,6 +111,37 @@ public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint super(registry); } + protected void copyFrom(RuntimeEndpointImpl copy) { + this.xml = copy.xml; + + this.component = copy.component; + this.service = copy.service; + this.interfaceContract = copy.interfaceContract; + this.serviceInterfaceContract = copy.serviceInterfaceContract; + + this.binding = copy.binding; + this.bindingInterfaceContract = copy.interfaceContract; + this.bindingInvocationChain = copy.bindingInvocationChain; + + this.callbackEndpointReferences = copy.callbackEndpointReferences; + + this.requiredIntents = copy.requiredIntents; + this.policySets = copy.policySets; + + this.uri = copy.uri; + this.remote = copy.remote; + this.unresolved = copy.unresolved; + + this.chains = copy.chains; + this.invocationChainMap = copy.invocationChainMap; + this.bindingProvider = copy.bindingProvider; + this.policyProviders = copy.policyProviders; + + if (this.compositeContext == null && copy.compositeContext != null) { + bind(copy.compositeContext); + } + } + public void bind(CompositeContext compositeContext) { this.compositeContext = compositeContext; bind(compositeContext.getExtensionPointRegistry(), compositeContext.getEndpointRegistry()); @@ -231,7 +262,7 @@ public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint //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(); + InterfaceContract targetContract = getComponentTypeServiceInterfaceContract(); // setInterfaceContract(targetContract); for (Operation operation : sourceContract.getInterface().getOperations()) { Operation targetOperation = interfaceContractMapper.map(targetContract.getInterface(), operation); @@ -422,7 +453,7 @@ public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint } } - public ServiceBindingProvider getBindingProvider() { + public synchronized ServiceBindingProvider getBindingProvider() { if (bindingProvider == null) { BindingProviderFactory factory = (BindingProviderFactory)providerFactories.getProviderFactory(getBinding().getClass()); @@ -460,18 +491,6 @@ public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint return compositeContext; } - private synchronized EndpointSerializer getSerializer() { - if (serializer == null) { - if (registry != null) { - serializer = - registry.getExtensionPoint(UtilityExtensionPoint.class).getUtility(EndpointSerializer.class); - } else { - throw new IllegalStateException("No extension registry is set"); - } - } - return serializer; - } - @Override protected void reset() { super.reset(); @@ -479,7 +498,17 @@ public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint } @Override - public void resolve() { + protected synchronized void resolve() { + if (xml != null && component == null) { + if (compositeContext == null) { + compositeContext = CompositeContext.getCurrentCompositeContext(); + if (compositeContext != null) { + bind(compositeContext); + } + } + RuntimeEndpointImpl ep = (RuntimeEndpointImpl)serializer.readEndpoint(xml); + copyFrom(ep); + } super.resolve(); } @@ -493,12 +522,12 @@ public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint bindingInterfaceContract = getInterfaceContract(); } if (bindingInterfaceContract == null) { - bindingInterfaceContract = getServiceInterfaceContract(); + bindingInterfaceContract = getComponentTypeServiceInterfaceContract(); } return bindingInterfaceContract; } - public InterfaceContract getServiceInterfaceContract() { + public InterfaceContract getComponentTypeServiceInterfaceContract() { resolve(); if (serviceInterfaceContract != null) { return serviceInterfaceContract; @@ -512,48 +541,21 @@ public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint } return serviceInterfaceContract; } - - public Object writeReplace() throws ObjectStreamException { - return new EndpointProxy(getSerializer(), this); - } - - public static class EndpointProxy implements Serializable { - private static final long serialVersionUID = 6708978267158501975L; - private String xml; - /** - * @param serializer - */ - public EndpointProxy() { - super(); - } - - /** - * @param serializer - */ - public EndpointProxy(EndpointSerializer serializer, Endpoint endpoint) { - super(); - try { - this.xml = serializer.write(endpoint); - } catch (Exception e) { - throw new ServiceRuntimeException(e); - } - } + public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + this.uri = in.readUTF(); + this.xml = in.readUTF(); + } - public Object readResolve() throws ObjectStreamException { - CompositeContext context = CompositeContext.getCurrentCompositeContext(); - if (context == null) { - throw new IllegalStateException("No context is available for deserializing the endpoint"); - } - UtilityExtensionPoint utilities = - context.getExtensionPointRegistry().getExtensionPoint(UtilityExtensionPoint.class); - EndpointSerializer serializer = utilities.getUtility(EndpointSerializer.class); - try { - RuntimeEndpoint endpoint = (RuntimeEndpoint) serializer.readEndpoint(xml); - endpoint.bind(context); - return endpoint; - } catch (IOException e) { - throw new ServiceRuntimeException(e); + public void writeExternal(ObjectOutput out) throws IOException { + out.writeUTF(getURI()); + if (serializer == null && xml != null) { + out.writeUTF(xml); + } else { + if (serializer != null) { + out.writeUTF(serializer.write(this)); + } else { + throw new IllegalStateException("No serializer is configured"); } } } 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 5b2de2f01f..d38de90422 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 @@ -19,9 +19,10 @@ package org.apache.tuscany.sca.core.assembly.impl; +import java.io.Externalizable; import java.io.IOException; -import java.io.ObjectStreamException; -import java.io.Serializable; +import java.io.ObjectInput; +import java.io.ObjectOutput; import java.lang.reflect.InvocationTargetException; import java.security.AccessController; import java.security.PrivilegedAction; @@ -35,7 +36,6 @@ 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; @@ -77,7 +77,7 @@ import org.oasisopen.sca.ServiceRuntimeException; /** * Runtime model for Endpoint that supports java serialization */ -public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implements RuntimeEndpointReference { +public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implements RuntimeEndpointReference, Externalizable { private transient CompositeContext compositeContext; private transient RuntimeWireProcessor wireProcessor; private transient InterfaceContractMapper interfaceContractMapper; @@ -88,7 +88,7 @@ public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implemen private transient EndpointRegistry endpointRegistry; private transient List<InvocationChain> chains; - private transient final Map<Operation, InvocationChain> invocationChainMap = + private transient Map<Operation, InvocationChain> invocationChainMap = new ConcurrentHashMap<Operation, InvocationChain>(); private transient InvocationChain bindingInvocationChain; @@ -100,6 +100,9 @@ public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implemen protected InterfaceContract bindingInterfaceContract; protected InterfaceContract referenceInterfaceContract; + + private String xml; + /** * No-arg constructor for Java serilization */ @@ -111,11 +114,43 @@ public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implemen super(registry); } + protected void copyFrom(RuntimeEndpointReferenceImpl copy) { + this.xml = copy.xml; + + this.component = copy.component; + this.reference = copy.reference; + this.interfaceContract = copy.interfaceContract; + this.referenceInterfaceContract = copy.referenceInterfaceContract; + this.callbackEndpoint = copy.callbackEndpoint; + this.targetEndpoint = copy.targetEndpoint; + + this.binding = copy.binding; + this.bindingInterfaceContract = copy.interfaceContract; + this.bindingInvocationChain = copy.bindingInvocationChain; + + this.requiredIntents = copy.requiredIntents; + this.policySets = copy.policySets; + + this.uri = copy.uri; + this.remote = copy.remote; + this.unresolved = copy.unresolved; + this.status = copy.status; + + this.chains = copy.chains; + this.invocationChainMap = copy.invocationChainMap; + this.bindingProvider = copy.bindingProvider; + this.policyProviders = copy.policyProviders; + + if (this.compositeContext == null && copy.compositeContext != null) { + bind(copy.compositeContext); + } + } + 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); @@ -214,7 +249,7 @@ public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implemen */ private void initInvocationChains() { chains = new ArrayList<InvocationChain>(); - InterfaceContract sourceContract = getReferenceInterfaceContract(); + InterfaceContract sourceContract = getComponentTypeReferenceInterfaceContract(); // 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); @@ -252,14 +287,13 @@ public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implemen * is first used */ private void resolveEndpointReference() { + resolve(); + 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(); @@ -280,7 +314,6 @@ public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implemen // endpoint.setInterfaceContract(bindingContract); } - private void initReferenceBindingInvocationChains() { // add the binding interceptors to the reference binding wire @@ -369,10 +402,12 @@ public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implemen } public boolean isOutOfDate() { + resolve(); return eprBinder.isOutOfDate(endpointRegistry, this); } - public ReferenceBindingProvider getBindingProvider() { + public synchronized ReferenceBindingProvider getBindingProvider() { + resolve(); // For the case that binding.sca is implemented by another binding if (binding == null) { return null; @@ -394,6 +429,7 @@ public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implemen } public synchronized List<PolicyProvider> getPolicyProviders() { + resolve(); if (policyProviders == null) { policyProviders = new ArrayList<PolicyProvider>(); for (PolicyProviderFactory factory : providerFactories.getPolicyProviderFactories()) { @@ -413,7 +449,7 @@ public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implemen policyProviders = null; invocationChainMap.clear(); } - + public Contract getContract() { resolve(); return reference; @@ -422,18 +458,6 @@ public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implemen public CompositeContext getCompositeContext() { return compositeContext; } - - private synchronized EndpointSerializer getSerializer() { - if (serializer == null) { - if (registry != null) { - serializer = - registry.getExtensionPoint(UtilityExtensionPoint.class).getUtility(EndpointSerializer.class); - } else { - throw new IllegalStateException("No extension registry is set"); - } - } - return serializer; - } public InterfaceContract getBindingInterfaceContract() { resolve(); @@ -445,12 +469,12 @@ public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implemen bindingInterfaceContract = getInterfaceContract(); } if (bindingInterfaceContract == null) { - bindingInterfaceContract = getReferenceInterfaceContract(); + bindingInterfaceContract = getComponentTypeReferenceInterfaceContract(); } return bindingInterfaceContract; } - public InterfaceContract getReferenceInterfaceContract() { + public InterfaceContract getComponentTypeReferenceInterfaceContract() { resolve(); if (referenceInterfaceContract != null) { return referenceInterfaceContract; @@ -464,52 +488,38 @@ public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implemen } return referenceInterfaceContract; } - public Object writeReplace() throws ObjectStreamException { - return new EndpointReferenceProxy(getSerializer(), this); - } - - public static class EndpointReferenceProxy implements Serializable { - private static final long serialVersionUID = 6708978267158501975L; - private String xml; - - /** - * @param serializer - */ - public EndpointReferenceProxy() { - super(); - } - /** - * @param serializer - */ - public EndpointReferenceProxy(EndpointSerializer serializer, EndpointReference endpointReference) { - super(); - try { - this.xml = serializer.write(endpointReference); - } catch (Exception e) { - throw new ServiceRuntimeException(e); + @Override + protected synchronized void resolve() { + if (xml != null && component == null) { + if (compositeContext == null) { + compositeContext = CompositeContext.getCurrentCompositeContext(); + if (compositeContext != null) { + bind(compositeContext); + } } + RuntimeEndpointReferenceImpl epr = (RuntimeEndpointReferenceImpl)serializer.readEndpointReference(xml); + copyFrom(epr); } + super.resolve(); + } - public Object readResolve() throws ObjectStreamException { - CompositeContext context = CompositeContext.getCurrentCompositeContext(); - if (context == null) { - throw new IllegalStateException("No context is available for deserializing the endpoint"); - } - UtilityExtensionPoint utilities = - context.getExtensionPointRegistry().getExtensionPoint(UtilityExtensionPoint.class); - EndpointSerializer serializer = utilities.getUtility(EndpointSerializer.class); - EndpointReferenceBinder eprBinder = utilities.getUtility(EndpointReferenceBinder.class); - try { - RuntimeEndpointReference epr = (RuntimeEndpointReference) serializer.readEndpointReference(xml); - epr.bind(context); - eprBinder.bind(context.getEndpointRegistry(), epr); - return epr; - } catch (IOException e) { - throw new ServiceRuntimeException(e); + public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + this.uri = in.readUTF(); + this.xml = in.readUTF(); + } + + public void writeExternal(ObjectOutput out) throws IOException { + out.writeUTF(getURI()); + if (serializer == null && xml != null) { + out.writeUTF(xml); + } else { + if (serializer != null) { + out.writeUTF(serializer.write(this)); + } else { + throw new IllegalStateException("No serializer is configured"); } } } - } |