Author: mcombellack
Date: Wed May 28 02:23:31 2008
New Revision: 660874

URL: http://svn.apache.org/viewvc?rev=660874&view=rev
Log:
TUSCANY-2213 - Added support for Serializing and Deserializing 
CallbackReferenceImpl

Modified:
    
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/CallableReferenceImpl.java
    
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ComponentContextHelper.java
    
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackReferenceImpl.java

Modified: 
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/CallableReferenceImpl.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/CallableReferenceImpl.java?rev=660874&r1=660873&r2=660874&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/CallableReferenceImpl.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/CallableReferenceImpl.java
 Wed May 28 02:23:31 2008
@@ -249,7 +249,12 @@
      * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
      */
     public void readExternal(ObjectInput in) throws IOException, 
ClassNotFoundException {
-        this.scdl = in.readUTF();
+        final boolean hasSCDL = in.readBoolean();
+        if (hasSCDL) {
+            this.scdl = in.readUTF();
+        } else {
+            this.scdl = null;
+        }
     }
 
     /**
@@ -259,8 +264,7 @@
         if ((scdl != null || xmlReader != null) && component == null && 
reference == null) {
             ComponentContextHelper componentContextHelper = 
ComponentContextHelper.getCurrentComponentContextHelper();
             if (componentContextHelper != null) {
-                CompositeActivator currentActivator = 
ComponentContextHelper.getCurrentCompositeActivator();
-                this.compositeActivator = currentActivator;
+                this.compositeActivator = 
ComponentContextHelper.getCurrentCompositeActivator();
                 this.conversationManager = 
this.compositeActivator.getConversationManager();
                 Component c;
                 if (xmlReader != null) {
@@ -271,7 +275,7 @@
                     scdl = null;  // OK to GC this now
                 }
                 this.component = (RuntimeComponent)c;
-                currentActivator.configureComponentContext(this.component);
+                compositeActivator.configureComponentContext(this.component);
                 this.reference = 
(RuntimeComponentReference)c.getReferences().get(0);
                 this.reference.setComponent(this.component);
                 clonedRef = reference;
@@ -300,30 +304,17 @@
 
                 for (Binding binding : reference.getBindings()) {
                     if (binding instanceof OptimizableBinding) {
-                        String targetURI = binding.getURI();
-                        if (targetURI.startsWith("/")) {
-                            targetURI = targetURI.substring(1);
-                        }
-                        int index = targetURI.lastIndexOf('/');
-                        String serviceName = "";
-                        if (index > -1) {
-                            serviceName = targetURI.substring(index + 1);
-                            targetURI = targetURI.substring(0, index);
-                        }
-                        Component targetComponent = 
compositeActivator.resolve(targetURI);
-                        ComponentService targetService = null;
-                        if (targetComponent != null) {
-                            if ("".equals(serviceName)) {
-                                targetService = 
ComponentContextHelper.getSingleService(targetComponent);
-                            } else {
-                                for (ComponentService service : 
targetComponent.getServices()) {
-                                    if (service.getName().equals(serviceName)) 
{
-                                        targetService = service;
-                                        break;
-                                    }
-                                }
-                            }
-                        }
+                        // Split up the URI
+                        final String[] splitURI = 
splitComponentURI(binding.getURI());
+                        final String componentURI = splitURI[0];
+                        final String serviceName = splitURI[1];
+                        
+                        // Resolve the Component
+                        final Component targetComponent = 
resolveComponentURI(componentURI);
+                        
+                        // Find the Service
+                        final ComponentService targetService = 
resolveService(serviceName, targetComponent);
+
                         OptimizableBinding optimizableBinding = 
(OptimizableBinding)binding;
                         optimizableBinding.setTargetComponent(targetComponent);
                         
optimizableBinding.setTargetComponentService(targetService);
@@ -357,7 +348,7 @@
                             }
                         });
                         
javaInterface.setJavaClass(classLoader.loadClass(javaInterface.getName()));
-                        
currentActivator.getJavaInterfaceFactory().createJavaInterface(javaInterface,
+                        
compositeActivator.getJavaInterfaceFactory().createJavaInterface(javaInterface,
                                                                                
        javaInterface.getJavaClass());
                         //FIXME: If the interface needs XSDs to be loaded 
(e.g., for static SDO),
                         // this needs to be done here.  We usually search for 
XSDs in the current
@@ -366,7 +357,12 @@
                         }
                     this.businessInterface = 
(Class<B>)javaInterface.getJavaClass();
                 }
-                this.proxyFactory = currentActivator.getProxyFactory();
+                this.proxyFactory = compositeActivator.getProxyFactory();
+            }
+        } else {
+            this.compositeActivator = 
ComponentContextHelper.getCurrentCompositeActivator();
+            if (this.compositeActivator != null) {
+                this.proxyFactory = this.compositeActivator.getProxyFactory();
             }
         }
     }
@@ -376,7 +372,13 @@
      */
     public void writeExternal(ObjectOutput out) throws IOException {
         try {
-            out.writeUTF(toXMLString());
+            final String xml = toXMLString();
+            if (xml == null) {
+                out.writeBoolean(false);
+            } else {
+                out.writeBoolean(true);
+                out.writeUTF(toXMLString());
+            }
         } catch (Exception e) {
             // e.printStackTrace();
             throw new IOException(e.getMessage());
@@ -474,4 +476,66 @@
         return xmlReader;
     }
 
+    /**
+     * Resolves the specified URI to a Component using the compositeActivator.
+     * 
+     * @param componentURI The URI of the Component to resolve
+     * @return The Component for the specified URI or null if not founds
+     */
+    protected Component resolveComponentURI(String componentURI) {
+        final String[] splitUri = splitComponentURI(componentURI);
+        return compositeActivator.resolve(splitUri[0]);
+    }
+
+    /**
+     * This method will split the specified URI into the Component URI
+     * and Service Name.
+     * 
+     * @param componentURI The URI to split
+     * @return [0] = Component URI [1] = ServiceName
+     */
+    protected String[] splitComponentURI(String componentURI) {
+        final String[] result = new String[2];
+
+        if (componentURI.startsWith("/")) {
+            componentURI = componentURI.substring(1);
+        }
+        final int index = componentURI.lastIndexOf('/');
+        String serviceName = "";
+        if (index > -1) {
+            serviceName = componentURI.substring(index + 1);
+            componentURI = componentURI.substring(0, index);
+        }
+
+        // Return the results
+        result[0] = componentURI;
+        result[1] = serviceName;
+        return result;
+    }
+
+    /**
+     * Examines the Services on the specified Component and returns the 
Service that matches the
+     * specified name.
+     * 
+     * @param serviceName The name of the Service to resolve on the Component
+     * @param targetComponent The Component containing the Services
+     * @return The Service with the specified serviceName or null if no such 
Service found.
+     */
+    protected ComponentService resolveService(String serviceName, Component 
targetComponent) {
+        ComponentService targetService = null;
+        if (targetComponent != null) {
+            if ("".equals(serviceName)) {
+                targetService = 
ComponentContextHelper.getSingleService(targetComponent);
+            } else {
+                for (ComponentService service : targetComponent.getServices()) 
{
+                    if (service.getName().equals(serviceName)) {
+                        targetService = service;
+                        break;
+                    }
+                }
+            }
+        }
+
+        return targetService;
+    }
 }

Modified: 
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ComponentContextHelper.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ComponentContextHelper.java?rev=660874&r1=660873&r2=660874&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ComponentContextHelper.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ComponentContextHelper.java
 Wed May 28 02:23:31 2008
@@ -192,6 +192,10 @@
     }
 
     public void write(Component component, ComponentReference reference, 
Writer writer) throws IOException {
+        write(component, reference, null, writer);
+    }
+
+    public void write(Component component, ComponentReference reference, 
ComponentService service, Writer writer) throws IOException {
         try {
             StAXArtifactProcessor<Composite> processor = 
staxProcessors.getProcessor(Composite.class);
             Composite composite = assemblyFactory.createComposite();
@@ -200,7 +204,12 @@
             comp.setName("default");
             comp.setURI(component.getURI());
             composite.getComponents().add(comp);
-            comp.getReferences().add(reference);
+            if (reference != null) {
+                comp.getReferences().add(reference);
+            }
+            if (service != null) {
+                comp.getServices().add(service);
+            }
 
             XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
             XMLStreamWriter streamWriter = 
outputFactory.createXMLStreamWriter(writer);
@@ -216,6 +225,12 @@
         return writer.toString();
     }
 
+    public String toXML(Component component, ComponentService service) throws 
IOException {
+        StringWriter writer = new StringWriter();
+        write(component, null, service, writer);
+        return writer.toString();
+    }
+
     public RuntimeComponent read(Reader reader) throws IOException {
         try {
             XMLInputFactory inputFactory = XMLInputFactory.newInstance();

Modified: 
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackReferenceImpl.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackReferenceImpl.java?rev=660874&r1=660873&r2=660874&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackReferenceImpl.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackReferenceImpl.java
 Wed May 28 02:23:31 2008
@@ -18,13 +18,23 @@
  */
 package org.apache.tuscany.sca.core.invocation;
 
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
 import java.util.List;
 
 import org.apache.tuscany.sca.assembly.Binding;
+import org.apache.tuscany.sca.assembly.Component;
+import org.apache.tuscany.sca.assembly.ComponentService;
 import org.apache.tuscany.sca.assembly.Contract;
 import org.apache.tuscany.sca.assembly.OptimizableBinding;
+import org.apache.tuscany.sca.core.assembly.EndpointReferenceImpl;
+import org.apache.tuscany.sca.core.assembly.RuntimeComponentReferenceImpl;
 import org.apache.tuscany.sca.core.assembly.RuntimeWireImpl;
 import org.apache.tuscany.sca.core.context.CallableReferenceImpl;
+import org.apache.tuscany.sca.core.context.ComponentContextHelper;
+import org.apache.tuscany.sca.interfacedef.InterfaceContract;
+import org.apache.tuscany.sca.interfacedef.java.JavaInterface;
 import org.apache.tuscany.sca.invocation.Message;
 import org.apache.tuscany.sca.runtime.EndpointReference;
 import org.apache.tuscany.sca.runtime.RuntimeComponent;
@@ -53,6 +63,13 @@
         }
     }
 
+    /**
+     * Public constructor for Externalizable serialization/deserialization.
+     */
+    public CallbackReferenceImpl() {
+        super();
+    }
+
     private CallbackReferenceImpl(Class<B> interfaze, ProxyFactory 
proxyFactory, List<RuntimeWire> wires) {
         super(interfaze, null, proxyFactory);
         this.wires = wires;
@@ -194,4 +211,59 @@
         RuntimeComponentReference ref = 
(RuntimeComponentReference)wire.getSource().getContract();
         
wire.getTarget().setInterfaceContract(ref.getBindingProvider(binding).getBindingInterfaceContract());
     }
+
+    /**
+     * [EMAIL PROTECTED]
+     */
+    @Override
+    public void readExternal(ObjectInput in) throws IOException, 
ClassNotFoundException {
+        super.readExternal(in);
+        this.callbackID = in.readObject();
+
+        this.compositeActivator = 
ComponentContextHelper.getCurrentCompositeActivator();
+
+        // Get the target Component and Service from the URI
+        final String uri = in.readUTF();
+        final String[] splitURI = super.splitComponentURI(uri);
+        final String componentURI = splitURI[0];
+        final String serviceName = splitURI[1];
+        final Component targetComponent = 
super.resolveComponentURI(componentURI);
+        final ComponentService targetService = 
super.resolveService(serviceName, targetComponent);
+        final InterfaceContract targetServiceIfaceContract = 
targetService.getInterfaceContract();
+
+        // Re-create the resolved Endpoint
+        this.resolvedEndpoint = new EndpointReferenceImpl(
+                (RuntimeComponent) targetComponent, targetService, null, 
+                targetServiceIfaceContract);
+
+        // Copy the Java Interface from the Service
+        final JavaInterface ji = (JavaInterface) 
targetServiceIfaceContract.getInterface();
+        this.businessInterface = (Class<B>) ji.getJavaClass();
+        
+        // We need to re-create the callback wire. We need to do this on a 
clone of the Service
+        // wire since we need to change some details on it.
+        // FIXME: Is this the best way to do this?
+        final RuntimeWire cbWire = ((RuntimeComponentService) 
targetService).getRuntimeWires().get(0);
+        try {
+            this.wire = (RuntimeWireImpl) cbWire.clone();
+        } catch (CloneNotSupportedException e) {
+            throw new IOException(e.toString());
+        }
+
+        // Setup the reference on the cloned wire
+        final RuntimeComponentReference ref = new 
RuntimeComponentReferenceImpl();
+        ref.setComponent((RuntimeComponent) targetComponent);
+        ref.setInterfaceContract(targetServiceIfaceContract);
+        ((EndpointReferenceImpl) this.wire.getSource()).setContract(ref);
+    }
+
+    /**
+     * [EMAIL PROTECTED]
+     */
+    @Override
+    public void writeExternal(ObjectOutput out) throws IOException {
+        super.writeExternal(out);
+        out.writeObject(this.callbackID);
+        out.writeUTF(this.resolvedEndpoint.getURI());
+    }
 }


Reply via email to