From 9db210b9c8754219714968e6273778cbab9767e4 Mon Sep 17 00:00:00 2001 From: rfeng Date: Tue, 17 Nov 2009 06:07:34 +0000 Subject: 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 --- .../core/assembly/impl/EndpointSerializerImpl.java | 48 +++++++++--- .../core/assembly/impl/RuntimeEndpointImpl.java | 69 +++++++++++++----- .../impl/RuntimeEndpointReferenceImpl.java | 85 +++++++++++++--------- 3 files changed, 137 insertions(+), 65 deletions(-) (limited to 'java/sca/modules/core') 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 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 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); + } + } + } } -- cgit v1.2.3