diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-17 06:07:34 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-17 06:07:34 +0000 |
commit | 9db210b9c8754219714968e6273778cbab9767e4 (patch) | |
tree | 0b53210304a6554888fe8856b060635c7fe49299 /java | |
parent | 854248d8c6516f04766e95cc6a64272b45215be5 (diff) |
Tidy up the endpoint/endpoint reference serialization/deseralization
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@881164 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java')
9 files changed, 163 insertions, 74 deletions
diff --git a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/EndpointReferenceProcessor.java b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/EndpointReferenceProcessor.java index dd1b6f5d62..22ce8a08d4 100644 --- a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/EndpointReferenceProcessor.java +++ b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/EndpointReferenceProcessor.java @@ -45,8 +45,8 @@ import org.apache.tuscany.sca.core.FactoryExtensionPoint; * */ public class EndpointReferenceProcessor extends BaseAssemblyProcessor implements StAXArtifactProcessor<EndpointReference> { - private final static String ENDPOINT = "endpointReference"; - private final static QName ENDPOINT_QNAME = new QName(Constants.SCA11_TUSCANY_NS, ENDPOINT); + private final static String ENDPOINT_REFERENCE = "endpointReference"; + private final static QName ENDPOINT_REFERENCE_QNAME = new QName(Constants.SCA11_TUSCANY_NS, ENDPOINT_REFERENCE); private ExtensionPointRegistry registry; @@ -69,7 +69,7 @@ public class EndpointReferenceProcessor extends BaseAssemblyProcessor implements } public QName getArtifactType() { - return ENDPOINT_QNAME; + return ENDPOINT_REFERENCE_QNAME; } public EndpointReference read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException, XMLStreamException { @@ -115,7 +115,7 @@ public class EndpointReferenceProcessor extends BaseAssemblyProcessor implements private Composite wrap(EndpointReference endpointReference) { try { Composite composite = assemblyFactory.createComposite(); - composite.setName(ENDPOINT_QNAME); + composite.setName(ENDPOINT_REFERENCE_QNAME); composite.setLocal(false); Component component = (Component)endpointReference.getComponent().clone(); composite.getComponents().add(component); diff --git a/java/sca/modules/assembly-xml/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor b/java/sca/modules/assembly-xml/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor index 7927cc0f6d..7826aa0eea 100644 --- a/java/sca/modules/assembly-xml/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor +++ b/java/sca/modules/assembly-xml/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor @@ -19,8 +19,8 @@ org.apache.tuscany.sca.assembly.xml.ComponentTypeProcessor;qname=http://docs.oasis-open.org/ns/opencsa/sca/200903#componentType,model=org.apache.tuscany.sca.assembly.ComponentType
org.apache.tuscany.sca.assembly.xml.CompositeProcessor;qname=http://docs.oasis-open.org/ns/opencsa/sca/200903#composite,model=org.apache.tuscany.sca.assembly.Composite
org.apache.tuscany.sca.assembly.xml.SCABindingProcessor;qname=http://docs.oasis-open.org/ns/opencsa/sca/200903#binding.sca,model=org.apache.tuscany.sca.assembly.SCABinding
-org.apache.tuscany.sca.assembly.xml.EndpointProcessor;qname=http://tuscany.apache.org/xmlns/sca/1.1#binding.rmi#endpoint,model=org.apache.tuscany.sca.assembly.Endpoint
-org.apache.tuscany.sca.assembly.xml.EndpointReferenceProcessor;qname=http://tuscany.apache.org/xmlns/sca/1.1#binding.rmi#endpointReference,model=org.apache.tuscany.sca.assembly.EndpointReference
+org.apache.tuscany.sca.assembly.xml.EndpointProcessor;qname=http://tuscany.apache.org/xmlns/sca/1.1#endpoint,model=org.apache.tuscany.sca.assembly.Endpoint
+org.apache.tuscany.sca.assembly.xml.EndpointReferenceProcessor;qname=http://tuscany.apache.org/xmlns/sca/1.1#endpointReference,model=org.apache.tuscany.sca.assembly.EndpointReference
org.apache.tuscany.sca.definitions.xml.DefinitionsProcessor;qname=http://docs.oasis-open.org/ns/opencsa/sca/200903#definitions,model=org.apache.tuscany.sca.definitions.Definitions
org.apache.tuscany.sca.policy.xml.BindingTypeProcessor;qname=http://docs.oasis-open.org/ns/opencsa/sca/200903#bindingType,model=org.apache.tuscany.sca.policy.BindingType
org.apache.tuscany.sca.policy.xml.ImplementationTypeProcessor;qname=http://docs.oasis-open.org/ns/opencsa/sca/200903#implementationType,model=org.apache.tuscany.sca.policy.ImplementationType
diff --git a/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/BindingBuilder.java b/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/BindingBuilder.java index 5f10af6510..973bc987c7 100644 --- a/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/BindingBuilder.java +++ b/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/BindingBuilder.java @@ -37,13 +37,13 @@ public interface BindingBuilder<B extends Binding> { * * @param component The component for the binding's service or reference * @param contract The binding's service or reference - * @param context TODO + * @param context The context for the builder */ void build(Component component, Contract contract, B binding, BuilderContext context); /** * Get QName of the binding type - * @return + * @return The binding type */ QName getBindingType(); diff --git a/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/BuilderContext.java b/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/BuilderContext.java index 8a16fac9e4..7deca4486a 100644 --- a/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/BuilderContext.java +++ b/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/BuilderContext.java @@ -33,7 +33,7 @@ import org.apache.tuscany.sca.monitor.Monitor; import org.apache.tuscany.sca.monitor.MonitorFactory; /** - * + * Context for builders */ public class BuilderContext { protected Definitions definitions; @@ -54,11 +54,19 @@ public class BuilderContext { this.monitor = monitor; } + /** + * Create a builder context with the monitor + * @param monitor + */ public BuilderContext(Monitor monitor) { super(); this.monitor = monitor; } + /** + * Create a builder context with the extension point registry + * @param registry + */ public BuilderContext(ExtensionPointRegistry registry) { super(); MonitorFactory monitorFactory = @@ -66,6 +74,9 @@ public class BuilderContext { this.monitor = monitorFactory.createMonitor(); } + /** + * Create a builder context with a default monitor + */ public BuilderContext() { super(); this.monitor = new DefaultMonitorFactory().createMonitor(); diff --git a/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/PolicyBuilder.java b/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/PolicyBuilder.java index 2df42c2c23..dcdc9e06c0 100644 --- a/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/PolicyBuilder.java +++ b/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/PolicyBuilder.java @@ -31,6 +31,10 @@ import org.apache.tuscany.sca.assembly.Implementation; * for SCA endpoints, endpoint references and component implementations */ public interface PolicyBuilder<T> { + /** + * Get the policy type + * @return + */ QName getPolicyType(); /** diff --git a/java/sca/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/EndpointSerializer.java b/java/sca/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/EndpointSerializer.java index c56869170a..60c05648a3 100644 --- a/java/sca/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/EndpointSerializer.java +++ b/java/sca/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/EndpointSerializer.java @@ -33,6 +33,8 @@ public interface EndpointSerializer { String write(Endpoint endpoint) throws IOException; void read(EndpointReference endpointReference, String xml) throws IOException; + EndpointReference readEndpointReference(String xml) throws IOException; + Endpoint readEndpoint(String xml) throws IOException; String write(EndpointReference endpointReference) throws IOException; } 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 fb0f327720..2a8ce544d0 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,7 +36,6 @@ 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; @@ -46,7 +45,7 @@ public class EndpointSerializerImpl implements EndpointSerializer { private StAXArtifactProcessor<EndpointReference> refProcessor; public EndpointSerializerImpl(ExtensionPointRegistry registry) { - this.registry =registry; + this.registry = registry; FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); inputFactory = factories.getFactory(XMLInputFactory.class); outputFactory = factories.getFactory(XMLOutputFactory.class); @@ -58,16 +57,32 @@ public class EndpointSerializerImpl implements EndpointSerializer { public void read(Endpoint endpoint, String xml) throws IOException { try { - XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(xml)); - Endpoint result = processor.read(reader, new ProcessorContext(registry)); + Endpoint result = readEndpoint(xml); endpoint.setComponent(result.getComponent()); endpoint.setService(result.getService()); endpoint.setBinding(result.getBinding()); endpoint.setInterfaceContract(result.getService().getInterfaceContract()); } catch (Exception e) { - throw new IOException(e.getMessage()); + throw wrap(e); + } + + } + + public Endpoint readEndpoint(String xml) throws IOException { + 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); } + } + private IOException wrap(Exception e) { + IOException ex = new IOException(e.getMessage()); + ex.initCause(e); + return ex; } public String write(Endpoint endpoint) throws IOException { @@ -79,15 +94,13 @@ public class EndpointSerializerImpl implements EndpointSerializer { writer.close(); return sw.toString(); } catch (Exception e) { - throw new IOException(e.getMessage()); + throw wrap(e); } } public void read(EndpointReference endpointReference, String xml) throws IOException { try { - XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(xml)); - EndpointReference result = refProcessor.read(reader, new ProcessorContext(registry)); - reader.close(); + EndpointReference result = readEndpointReference(xml); endpointReference.setComponent(result.getComponent()); endpointReference.setReference(result.getReference()); endpointReference.setBinding(result.getBinding()); @@ -95,7 +108,18 @@ public class EndpointSerializerImpl implements EndpointSerializer { endpointReference.setTargetEndpoint(result.getTargetEndpoint()); endpointReference.setCallbackEndpoint(result.getCallbackEndpoint()); } catch (Exception e) { - throw new ServiceRuntimeException(e); + throw wrap(e); + } + } + + public EndpointReference readEndpointReference(String xml) throws IOException { + 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); } } @@ -108,7 +132,7 @@ public class EndpointSerializerImpl implements EndpointSerializer { writer.close(); return sw.toString(); } catch (Exception e) { - throw new ServiceRuntimeException(e); + throw wrap(e); } } -}
\ No newline at end of file +} 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 2f60b2d126..a9759d2e91 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,10 +19,9 @@ package org.apache.tuscany.sca.core.assembly.impl; -import java.io.Externalizable; import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; +import java.io.ObjectStreamException; +import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; @@ -35,6 +34,7 @@ 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,7 +76,7 @@ import org.oasisopen.sca.ServiceRuntimeException; /** * Runtime model for Endpoint that supports java serialization */ -public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint, Externalizable { +public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint { private transient CompositeContext compositeContext; private transient EndpointRegistry endpointRegistry; private transient RuntimeWireProcessor wireProcessor; @@ -99,6 +99,7 @@ public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint protected InterfaceContract bindingInterfaceContract; protected InterfaceContract serviceInterfaceContract; + /** * No-arg constructor for Java serilization */ @@ -114,7 +115,7 @@ public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint this.compositeContext = compositeContext; bind(compositeContext.getExtensionPointRegistry(), compositeContext.getEndpointRegistry()); } - + public void bind(ExtensionPointRegistry registry, EndpointRegistry endpointRegistry) { if (compositeContext == null) { compositeContext = new CompositeContext(registry, endpointRegistry); @@ -458,17 +459,6 @@ public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint public CompositeContext getCompositeContext() { return compositeContext; } - - public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - this.uri = in.readUTF(); - this.xml = in.readUTF(); - // Defer the loading to resolve(); - } - - public void writeExternal(ObjectOutput out) throws IOException { - out.writeUTF(getURI()); - out.writeUTF(getSerializer().write(this)); - } private synchronized EndpointSerializer getSerializer() { if (serializer == null) { @@ -499,7 +489,7 @@ public class RuntimeEndpointImpl extends EndpointImpl implements RuntimeEndpoint } super.resolve(); } - + public InterfaceContract getBindingInterfaceContract() { resolve(); if (bindingInterfaceContract != null) { @@ -529,5 +519,50 @@ 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 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); + } + } + } } 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 2132139530..efdbc71bbd 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,10 +19,9 @@ package org.apache.tuscany.sca.core.assembly.impl; -import java.io.Externalizable; import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; +import java.io.ObjectStreamException; +import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.security.AccessController; import java.security.PrivilegedAction; @@ -78,7 +77,7 @@ import org.oasisopen.sca.ServiceRuntimeException; /** * Runtime model for Endpoint that supports java serialization */ -public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implements RuntimeEndpointReference, Externalizable { +public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implements RuntimeEndpointReference { private transient CompositeContext compositeContext; private transient RuntimeWireProcessor wireProcessor; private transient InterfaceContractMapper interfaceContractMapper; @@ -98,7 +97,6 @@ public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implemen private transient ProviderFactoryExtensionPoint providerFactories; private transient List<PolicyProvider> policyProviders; private transient EndpointSerializer serializer; - private String xml; protected InterfaceContract bindingInterfaceContract; protected InterfaceContract referenceInterfaceContract; @@ -425,17 +423,6 @@ public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implemen return compositeContext; } - public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - this.uri = in.readUTF(); - this.xml = in.readUTF(); - // Defer the loading to resolve(); - } - - public void writeExternal(ObjectOutput out) throws IOException { - out.writeUTF(getURI()); - out.writeUTF(getSerializer().write(this)); - } - private synchronized EndpointSerializer getSerializer() { if (serializer == null) { if (registry != null) { @@ -448,26 +435,6 @@ public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implemen return serializer; } - @Override - protected void reset() { - super.reset(); - this.xml = null; - } - - @Override - 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); - } - } - super.resolve(); - } - public InterfaceContract getBindingInterfaceContract() { resolve(); if (bindingInterfaceContract != null) { @@ -497,6 +464,52 @@ 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); + } + } + + 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); + } + } + } } |