From 1265ef2c9979752024588a8aacbde31fed1e6df1 Mon Sep 17 00:00:00 2001 From: rfeng Date: Wed, 10 Jun 2009 05:57:51 +0000 Subject: Create the runtime version of Endpoint2 for serialization Serialize the EP using XML git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@783213 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/core/assembly/EndpointSerializer.java | 40 ++++++++ .../sca/core/assembly/RuntimeAssemblyFactory.java | 15 +++ .../core/assembly/impl/EndpointSerializerImpl.java | 109 +++++++++++++++++++++ .../core/assembly/impl/RuntimeEndpointImpl.java | 61 ++++++++++++ 4 files changed, 225 insertions(+) create mode 100644 java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/EndpointSerializer.java create mode 100644 java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/EndpointSerializerImpl.java create mode 100644 java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeEndpointImpl.java (limited to 'java/sca/modules/core') diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/EndpointSerializer.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/EndpointSerializer.java new file mode 100644 index 0000000000..1cdc1c6209 --- /dev/null +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/EndpointSerializer.java @@ -0,0 +1,40 @@ +/* + * 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; + +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; + +import org.apache.tuscany.sca.assembly.Endpoint2; +import org.apache.tuscany.sca.assembly.EndpointReference2; + +/** + * A utility to seralize/deserialize Endpoint/EndpointReference objects + */ +public interface EndpointSerializer { + void readExternal(Endpoint2 endpoint, ObjectInput input) throws IOException; + + void writeExternal(Endpoint2 endpoint, ObjectOutput output) throws IOException; + + void readExternal(EndpointReference2 endpointReference, ObjectInput input) throws IOException; + + void writeExternal(EndpointReference2 endpointReference, ObjectOutput output) throws IOException; +} diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeAssemblyFactory.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeAssemblyFactory.java index 547c8f6750..27845b445c 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeAssemblyFactory.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeAssemblyFactory.java @@ -26,7 +26,10 @@ import org.apache.tuscany.sca.assembly.ComponentReference; import org.apache.tuscany.sca.assembly.ComponentService; import org.apache.tuscany.sca.assembly.Contract; import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.assembly.Endpoint2; +import org.apache.tuscany.sca.assembly.EndpointReference2; import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.assembly.impl.RuntimeEndpointImpl; import org.apache.tuscany.sca.core.assembly.impl.EndpointReferenceImpl; import org.apache.tuscany.sca.core.assembly.impl.ReferenceParametersImpl; import org.apache.tuscany.sca.core.assembly.impl.RuntimeComponentImpl; @@ -78,4 +81,16 @@ public class RuntimeAssemblyFactory extends DefaultAssemblyFactory implements As return new ReferenceParametersImpl(); } + @Override + public Endpoint2 createEndpoint() { + // Create an instance of EndpointImpl that can be serialized/deserialized using the Tuscany + // runtime extension points and extensions + return new RuntimeEndpointImpl(registry); + } + + @Override + public EndpointReference2 createEndpointReference() { + return super.createEndpointReference(); + } + } 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 new file mode 100644 index 0000000000..bf3339d8ad --- /dev/null +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/EndpointSerializerImpl.java @@ -0,0 +1,109 @@ +/* + * 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.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.io.StringReader; +import java.io.StringWriter; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.assembly.Endpoint2; +import org.apache.tuscany.sca.assembly.EndpointReference2; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.core.assembly.EndpointSerializer; + +public class EndpointSerializerImpl implements EndpointSerializer { + private XMLInputFactory inputFactory; + private XMLOutputFactory outputFactory; + private StAXArtifactProcessor processor; + private StAXArtifactProcessor refProcessor; + + public EndpointSerializerImpl(ExtensionPointRegistry registry) { + FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); + inputFactory = factories.getFactory(XMLInputFactory.class); + outputFactory = factories.getFactory(XMLOutputFactory.class); + StAXArtifactProcessorExtensionPoint processors = + registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + processor = processors.getProcessor(Endpoint2.class); + refProcessor = processors.getProcessor(EndpointReference2.class); + } + + public void readExternal(Endpoint2 endpoint, ObjectInput input) throws IOException { + try { + String xml = input.readUTF(); + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(xml)); + Endpoint2 result = processor.read(reader); + endpoint.setComponent(result.getComponent()); + endpoint.setService(result.getService()); + endpoint.setBinding(result.getBinding()); + } catch (Exception e) { + throw new IOException(e); + } + + } + + public void writeExternal(Endpoint2 endpoint, ObjectOutput output) throws IOException { + StringWriter sw = new StringWriter(); + try { + XMLStreamWriter writer = outputFactory.createXMLStreamWriter(sw); + processor.write(endpoint, writer); + writer.flush(); + output.writeUTF(sw.toString()); + writer.close(); + } catch (Exception e) { + throw new IOException(e); + } + } + + public void readExternal(EndpointReference2 endpointReference, ObjectInput input) throws IOException { + try { + String xml = input.readUTF(); + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(xml)); + EndpointReference2 result = refProcessor.read(reader); + reader.close(); + endpointReference.setComponent(result.getComponent()); + endpointReference.setReference(result.getReference()); + endpointReference.setBinding(result.getBinding()); + } catch (Exception e) { + throw new IOException(e); + } + } + + public void writeExternal(EndpointReference2 endpointReference, ObjectOutput output) throws IOException { + StringWriter sw = new StringWriter(); + try { + XMLStreamWriter writer = outputFactory.createXMLStreamWriter(sw); + refProcessor.write(endpointReference, writer); + writer.flush(); + output.writeUTF(sw.toString()); + } catch (Exception e) { + throw new IOException(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 new file mode 100644 index 0000000000..41222a1148 --- /dev/null +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeEndpointImpl.java @@ -0,0 +1,61 @@ +/* + * 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.io.Externalizable; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; + +import org.apache.tuscany.sca.assembly.impl.Endpoint2Impl; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.assembly.EndpointSerializer; + +/** + * Runtime model for Endpoint that supports java serialization + */ +public class RuntimeEndpointImpl extends Endpoint2Impl implements Externalizable { + private static EndpointSerializer serializer; + + /** + * No-arg constructor for Java serilization + */ + public RuntimeEndpointImpl() { + super(null); + } + + public RuntimeEndpointImpl(ExtensionPointRegistry registry) { + super(registry); + if (registry != null) { + serializer = new EndpointSerializerImpl(registry); + } + } + + public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + // When this method is invoked, the instance is created using the no-arg constructor + // We need to keep the serializer as a static + serializer.readExternal(this, in); + } + + public void writeExternal(ObjectOutput out) throws IOException { + serializer.writeExternal(this, out); + } + +} -- cgit v1.2.3