From 667940fec12c91bb231d7f8ff477f9e78112b80b Mon Sep 17 00:00:00 2001 From: rfeng Date: Fri, 12 Jun 2009 20:57:24 +0000 Subject: Add methods to Endpoint/EndpointReference to set the ExtensionPointRegistry so that deserialized object can be resolved Make the EndpointSerializer a declared utility git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@784270 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/core/assembly/RuntimeAssemblyFactory.java | 25 +----- .../core/assembly/impl/RuntimeEndpointImpl.java | 35 +++++--- .../impl/RuntimeEndpointReferenceImpl.java | 100 +++++++++++++++++++++ ...he.tuscany.sca.core.assembly.EndpointSerializer | 17 ++++ 4 files changed, 145 insertions(+), 32 deletions(-) create mode 100644 java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeEndpointReferenceImpl.java create mode 100644 java/sca/modules/core/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.assembly.EndpointSerializer (limited to 'java/sca/modules/core/src/main') 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 73416f3a9f..8c84a9e399 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 @@ -20,7 +20,6 @@ package org.apache.tuscany.sca.core.assembly; import org.apache.tuscany.sca.assembly.AssemblyFactory; - import org.apache.tuscany.sca.assembly.Component; import org.apache.tuscany.sca.assembly.ComponentReference; import org.apache.tuscany.sca.assembly.ComponentService; @@ -28,10 +27,11 @@ import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; import org.apache.tuscany.sca.assembly.Endpoint; import org.apache.tuscany.sca.assembly.EndpointReference; import org.apache.tuscany.sca.core.ExtensionPointRegistry; -import org.apache.tuscany.sca.core.assembly.impl.RuntimeEndpointImpl; import org.apache.tuscany.sca.core.assembly.impl.RuntimeComponentImpl; import org.apache.tuscany.sca.core.assembly.impl.RuntimeComponentReferenceImpl; import org.apache.tuscany.sca.core.assembly.impl.RuntimeComponentServiceImpl; +import org.apache.tuscany.sca.core.assembly.impl.RuntimeEndpointImpl; +import org.apache.tuscany.sca.core.assembly.impl.RuntimeEndpointReferenceImpl; /** @@ -59,25 +59,6 @@ public class RuntimeAssemblyFactory extends DefaultAssemblyFactory implements As return new RuntimeComponentServiceImpl(); } - /* TODO - EPR - remove now - // FIXME: [rfeng] We need to find a more consistent story to deal with EPR, EP and CallableReference - public EndpointReference createEndpointReference(String uri) { - return new EndpointReferenceImpl(uri); - } - - public EndpointReference createEndpointReference(RuntimeComponent component, - Contract contract, - Binding binding, - InterfaceContract interfaceContract) { - return new EndpointReferenceImpl(component, contract, binding, interfaceContract); - } - - - public ReferenceParameters createReferenceParameters() { - return new ReferenceParametersImpl(); - } - */ - @Override public Endpoint createEndpoint() { // Create an instance of EndpointImpl that can be serialized/deserialized using the Tuscany @@ -87,7 +68,7 @@ public class RuntimeAssemblyFactory extends DefaultAssemblyFactory implements As @Override public EndpointReference createEndpointReference() { - return super.createEndpointReference(); + return new RuntimeEndpointReferenceImpl(registry); } } 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 770d94f211..f2ec7f8019 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 @@ -26,17 +26,14 @@ import java.io.ObjectOutput; import org.apache.tuscany.sca.assembly.impl.EndpointImpl; import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; import org.apache.tuscany.sca.core.assembly.EndpointSerializer; /** * Runtime model for Endpoint that supports java serialization */ public class RuntimeEndpointImpl extends EndpointImpl implements Externalizable { - /** - * FIXME: What's the best way to get the extension point registry upon deserialization? - * We can expose a method to receive the extension point registry - */ - private static EndpointSerializer serializer; + private EndpointSerializer serializer; private String xml; /** @@ -48,9 +45,6 @@ public class RuntimeEndpointImpl extends EndpointImpl implements Externalizable public RuntimeEndpointImpl(ExtensionPointRegistry registry) { super(registry); - if (registry != null) { - serializer = new EndpointSerializerImpl(registry); - } } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { @@ -61,7 +55,19 @@ public class RuntimeEndpointImpl extends EndpointImpl implements Externalizable public void writeExternal(ObjectOutput out) throws IOException { out.writeUTF(getURI()); - out.writeUTF(serializer.write(this)); + out.writeUTF(getSerializer().write(this)); + } + + 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 @@ -74,7 +80,7 @@ public class RuntimeEndpointImpl extends EndpointImpl implements Externalizable protected void resolve() { if (component == null && xml != null) { try { - serializer.read(this, xml); + getSerializer().read(this, xml); } catch (IOException e) { throw new IllegalStateException(e); } @@ -82,4 +88,13 @@ public class RuntimeEndpointImpl extends EndpointImpl implements Externalizable super.resolve(); } + @Override + public void setExtensionPointRegistry(ExtensionPointRegistry registry) { + if (this.registry != registry) { + super.setExtensionPointRegistry(registry); + serializer = null; + } + // resolve(); + } + } 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 new file mode 100644 index 0000000000..36f49eac5e --- /dev/null +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/RuntimeEndpointReferenceImpl.java @@ -0,0 +1,100 @@ +/* + * 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.EndpointReferenceImpl; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.core.assembly.EndpointSerializer; + +/** + * Runtime model for Endpoint that supports java serialization + */ +public class RuntimeEndpointReferenceImpl extends EndpointReferenceImpl implements Externalizable { + private EndpointSerializer serializer; + private String xml; + + /** + * No-arg constructor for Java serilization + */ + public RuntimeEndpointReferenceImpl() { + super(null); + } + + public RuntimeEndpointReferenceImpl(ExtensionPointRegistry registry) { + super(registry); + } + + 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) { + 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(); + this.xml = null; + } + + @Override + protected void resolve() { + if (component == null && xml != null) { + try { + getSerializer().read(this, xml); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + super.resolve(); + } + + @Override + public void setExtensionPointRegistry(ExtensionPointRegistry registry) { + if (this.registry != registry) { + super.setExtensionPointRegistry(registry); + serializer = null; + } + // resolve(); + } + +} diff --git a/java/sca/modules/core/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.assembly.EndpointSerializer b/java/sca/modules/core/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.assembly.EndpointSerializer new file mode 100644 index 0000000000..cc3306b325 --- /dev/null +++ b/java/sca/modules/core/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.assembly.EndpointSerializer @@ -0,0 +1,17 @@ +# 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. +org.apache.tuscany.sca.core.assembly.impl.EndpointSerializerImpl \ No newline at end of file -- cgit v1.2.3