Author: rfeng
Date: Mon Aug 20 21:00:57 2007
New Revision: 567938

URL: http://svn.apache.org/viewvc?rev=567938&view=rev
Log:
Refactor the CompositeActivatorImpl to use activate()/deactivate() for 
references/services

Added:
    
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/component/ReferenceHelper.java
   (with props)
Modified:
    
incubator/tuscany/java/sca/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/RuntimeWire.java
    
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/component/CallableReferenceImpl.java
    
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/CompositeActivatorImpl.java

Modified: 
incubator/tuscany/java/sca/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/RuntimeWire.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/RuntimeWire.java?rev=567938&r1=567937&r2=567938&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/RuntimeWire.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/RuntimeWire.java
 Mon Aug 20 21:00:57 2007
@@ -44,7 +44,13 @@
      * @return The end point reference of the target
      */
     EndpointReference getTarget();
+
+    /**
+     * Rebind the runtime wire with the given target
+     * @param target The target endpoint reference
+     */
     void setTarget(EndpointReference target);
+    
     /**
      * Returns the invocation chains for service operations associated with the
      * wire

Modified: 
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/component/CallableReferenceImpl.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/component/CallableReferenceImpl.java?rev=567938&r1=567937&r2=567938&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/component/CallableReferenceImpl.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/component/CallableReferenceImpl.java
 Mon Aug 20 21:00:57 2007
@@ -22,7 +22,6 @@
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
-import java.io.StringReader;
 import java.util.List;
 import java.util.StringTokenizer;
 
@@ -135,7 +134,7 @@
      */
     public void readExternal(ObjectInput in) throws IOException, 
ClassNotFoundException {
         String scdl = in.readUTF();
-        Component c = ((CompositeActivatorImpl)compositeActivator).read(new 
StringReader(scdl));
+        Component c = 
((CompositeActivatorImpl)compositeActivator).getReferenceHelper().fromXML(scdl);
         this.component = (RuntimeComponent)c;
         this.reference = (RuntimeComponentReference)c.getReferences().get(0);
         Interface i = reference.getInterfaceContract().getInterface();
@@ -169,7 +168,7 @@
      */
     public void writeExternal(ObjectOutput out) throws IOException {
         try {
-            String scdl = 
((CompositeActivatorImpl)compositeActivator).write(component, reference);
+            String scdl = 
((CompositeActivatorImpl)compositeActivator).getReferenceHelper().toXML(component,
 reference);
             out.writeUTF(scdl);
             StringBuffer uri = new StringBuffer(baseURI);
             boolean first = true;

Added: 
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/component/ReferenceHelper.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/component/ReferenceHelper.java?rev=567938&view=auto
==============================================================================
--- 
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/component/ReferenceHelper.java
 (added)
+++ 
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/component/ReferenceHelper.java
 Mon Aug 20 21:00:57 2007
@@ -0,0 +1,225 @@
+/*
+ * 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.component;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.Writer;
+
+import javax.xml.namespace.QName;
+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.AssemblyFactory;
+import org.apache.tuscany.sca.assembly.Binding;
+import org.apache.tuscany.sca.assembly.Component;
+import org.apache.tuscany.sca.assembly.ComponentReference;
+import org.apache.tuscany.sca.assembly.ComponentService;
+import org.apache.tuscany.sca.assembly.Composite;
+import org.apache.tuscany.sca.assembly.Multiplicity;
+import org.apache.tuscany.sca.assembly.Reference;
+import org.apache.tuscany.sca.assembly.Service;
+import org.apache.tuscany.sca.assembly.WireableBinding;
+import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
+import 
org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint;
+import org.apache.tuscany.sca.interfacedef.Interface;
+import org.apache.tuscany.sca.interfacedef.InterfaceContract;
+import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException;
+import org.apache.tuscany.sca.interfacedef.java.JavaInterface;
+import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory;
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
+import org.apache.tuscany.sca.runtime.RuntimeComponentReference;
+import org.apache.tuscany.sca.runtime.RuntimeComponentService;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ReferenceHelper {
+
+    private final AssemblyFactory assemblyFactory;
+    private final JavaInterfaceFactory javaInterfaceFactory;
+    private final StAXArtifactProcessorExtensionPoint staxProcessors;
+
+    /**
+     * @param assemblyFactory The factory to create assembly models
+     * @param processors The extension point for stax artifact processors
+     */
+    public ReferenceHelper(AssemblyFactory assemblyFactory,
+                           JavaInterfaceFactory javaInterfaceFactory,
+                           StAXArtifactProcessorExtensionPoint processors) {
+        this.assemblyFactory = assemblyFactory;
+        this.javaInterfaceFactory = javaInterfaceFactory;
+        this.staxProcessors = processors;
+    }
+
+    /**
+     * Create a self-reference for a component service
+     * @param component
+     * @param service
+     * @throws CloneNotSupportedException 
+     * @throws InvalidInterfaceException 
+     */
+    public ComponentReference createSelfReference(Component component,
+                                                  ComponentService service,
+                                                  Class<?> businessInterface) 
throws CloneNotSupportedException,
+        InvalidInterfaceException {
+        ComponentReference componentReference = 
assemblyFactory.createComponentReference();
+        componentReference.setName("$self$." + service.getName());
+        for (Binding binding : service.getBindings()) {
+            if (binding instanceof WireableBinding) {
+                WireableBinding wireableBinding = 
(WireableBinding)((WireableBinding)binding).clone();
+                wireableBinding.setTargetBinding(binding);
+                wireableBinding.setTargetComponent(component);
+                wireableBinding.setTargetComponentService(service);
+                wireableBinding.setRemote(false);
+                componentReference.getBindings().add(wireableBinding);
+            } else {
+                componentReference.getBindings().add(binding);
+            }
+        }
+
+        componentReference.setCallback(service.getCallback());
+        componentReference.getTargets().add(service);
+        componentReference.getPolicySets().addAll(service.getPolicySets());
+        
componentReference.getRequiredIntents().addAll(service.getRequiredIntents());
+
+        InterfaceContract interfaceContract = service.getInterfaceContract();
+        Service componentTypeService = service.getService();
+        if (componentTypeService != null && 
componentTypeService.getInterfaceContract() != null) {
+            interfaceContract = componentTypeService.getInterfaceContract();
+        }
+        interfaceContract = getInterfaceContract(interfaceContract, 
businessInterface);
+        componentReference.setInterfaceContract(interfaceContract);
+        componentReference.setMultiplicity(Multiplicity.ONE_ONE);
+        // component.getReferences().add(componentReference);
+        return componentReference;
+    }
+
+    /**
+     * @param interfaceContract
+     * @param businessInterface
+     * @return
+     * @throws CloneNotSupportedException
+     * @throws InvalidInterfaceException
+     */
+    private InterfaceContract getInterfaceContract(InterfaceContract 
interfaceContract, Class<?> businessInterface)
+        throws CloneNotSupportedException, InvalidInterfaceException {
+        Interface interfaze = interfaceContract.getInterface();
+        if (interfaze instanceof JavaInterface) {
+            Class<?> cls = ((JavaInterface)interfaze).getJavaClass();
+            if (!businessInterface.isAssignableFrom(cls)) {
+                // The interface is not assignable from the interface contract
+                interfaceContract = 
(InterfaceContract)interfaceContract.clone();
+                
interfaceContract.setInterface(javaInterfaceFactory.createJavaInterface(businessInterface));
+            }
+        }
+        return interfaceContract;
+    }
+
+    /**
+     * Bind a component reference to a component service
+     * @param <B>
+     * @param businessInterface
+     * @param reference
+     * @param service
+     * @return
+     * @throws CloneNotSupportedException
+     * @throws InvalidInterfaceException
+     */
+    public RuntimeComponentReference bindComponentReference(Class<?> 
businessInterface,
+                                                          
RuntimeComponentReference reference,
+                                                          RuntimeComponent 
component,
+                                                          
RuntimeComponentService service)
+        throws CloneNotSupportedException, InvalidInterfaceException {
+        RuntimeComponentReference ref = 
(RuntimeComponentReference)reference.clone();
+        InterfaceContract interfaceContract = reference.getInterfaceContract();
+        Reference componentTypeReference = reference.getReference();
+        if (componentTypeReference != null && 
componentTypeReference.getInterfaceContract() != null) {
+            interfaceContract = componentTypeReference.getInterfaceContract();
+        }
+        InterfaceContract refInterfaceContract = 
getInterfaceContract(interfaceContract, businessInterface);
+        if (refInterfaceContract != interfaceContract) {
+            ref = (RuntimeComponentReference)reference.clone();
+            ref.setInterfaceContract(interfaceContract);
+        }
+        ref.setComponent(component);
+        ref.getTargets().add(service);
+        ref.getBindings().clear();
+        for (Binding binding : service.getBindings()) {
+            if (binding instanceof WireableBinding) {
+                WireableBinding wireableBinding = 
(WireableBinding)((WireableBinding)binding).clone();
+                wireableBinding.setTargetBinding(binding);
+                wireableBinding.setTargetComponent(component);
+                wireableBinding.setTargetComponentService(service);
+                wireableBinding.setRemote(false);
+                ref.getBindings().add(wireableBinding);
+            } else {
+                ref.getBindings().add(binding);
+            }
+        }
+        return ref;
+    }
+
+    public void marshal(Component component, ComponentReference reference, 
Writer writer) throws IOException {
+        try {
+            StAXArtifactProcessor<Composite> processor = 
staxProcessors.getProcessor(Composite.class);
+            Composite composite = assemblyFactory.createComposite();
+            composite.setName(new 
QName("http://tuscany.apache.org/xmlns/sca/1.0";, "default"));
+            Component comp = assemblyFactory.createComponent();
+            comp.setName("default");
+            comp.setURI(component.getURI());
+            composite.getComponents().add(comp);
+            comp.getReferences().add(reference);
+
+            XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
+            XMLStreamWriter streamWriter = 
outputFactory.createXMLStreamWriter(writer);
+            processor.write(composite, streamWriter);
+        } catch (Exception e) {
+            throw new IOException(e.getMessage());
+        }
+    }
+
+    public String toXML(Component component, ComponentReference reference) 
throws IOException {
+        StringWriter writer = new StringWriter();
+        marshal(component, reference, writer);
+        return writer.toString();
+    }
+
+    public Component unmarshal(Reader reader) throws IOException {
+        try {
+            StAXArtifactProcessor<Composite> processor = 
staxProcessors.getProcessor(Composite.class);
+            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
+            XMLStreamReader streamReader = 
inputFactory.createXMLStreamReader(reader);
+            Composite composite = processor.read(streamReader);
+            return composite.getComponents().get(0);
+        } catch (Exception e) {
+            throw new IOException(e.getMessage());
+        }
+    }
+
+    public Component fromXML(String xml) throws IOException {
+        return unmarshal(new StringReader(xml));
+    }
+
+}

Propchange: 
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/component/ReferenceHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/component/ReferenceHelper.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: 
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/CompositeActivatorImpl.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/CompositeActivatorImpl.java?rev=567938&r1=567937&r2=567938&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/CompositeActivatorImpl.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/CompositeActivatorImpl.java
 Mon Aug 20 21:00:57 2007
@@ -19,18 +19,6 @@
 
 package org.apache.tuscany.sca.core.runtime;
 
-import java.io.IOException;
-import java.io.Reader;
-import java.io.StringWriter;
-import java.io.Writer;
-import java.util.List;
-
-import javax.xml.namespace.QName;
-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.AssemblyFactory;
 import org.apache.tuscany.sca.assembly.Binding;
 import org.apache.tuscany.sca.assembly.Component;
@@ -44,11 +32,10 @@
 import org.apache.tuscany.sca.assembly.Service;
 import org.apache.tuscany.sca.assembly.WireableBinding;
 import org.apache.tuscany.sca.context.RequestContextFactory;
-import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
 import 
org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint;
 import org.apache.tuscany.sca.core.component.ComponentContextImpl;
+import org.apache.tuscany.sca.core.component.ReferenceHelper;
 import org.apache.tuscany.sca.core.invocation.ProxyFactory;
-import 
org.apache.tuscany.sca.interfacedef.IncompatibleInterfaceContractException;
 import org.apache.tuscany.sca.interfacedef.InterfaceContract;
 import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper;
 import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory;
@@ -82,12 +69,13 @@
     private final WorkScheduler workScheduler;
     private final RuntimeWireProcessor wireProcessor;
     private final ProviderFactoryExtensionPoint providerFactories;
-    private final StAXArtifactProcessorExtensionPoint staxProcessors;
 
     private final RequestContextFactory requestContextFactory;
     private final ProxyFactory proxyFactory;
     private final JavaInterfaceFactory javaInterfaceFactory;
 
+    private final ReferenceHelper referenceHelper;
+
     /**
      * @param assemblyFactory
      * @param interfaceContractMapper
@@ -115,79 +103,16 @@
         this.javaInterfaceFactory = javaInterfaceFactory;
         this.requestContextFactory = requestContextFactory;
         this.proxyFactory = proxyFactory;
-        this.staxProcessors = processors;
-    }
-
-    /**
-     * Configure a composite
-     * 
-     * @param composite
-     * @throws IncompatibleInterfaceContractException
-     */
-    private void addRuntimeProviders(Composite composite) throws 
IncompatibleInterfaceContractException {
-        for (Component component : composite.getComponents()) {
-
-            for (ComponentService service : component.getServices()) {
-                addServiceBindingProviders((RuntimeComponent)component, 
(RuntimeComponentService)service, service
-                    .getBindings());
-            }
-
-            // [rfeng] Defer this
-            /*
-            for (ComponentReference reference : component.getReferences()) {
-                addReferenceBindingProviders((RuntimeComponent)component,
-                                                
(RuntimeComponentReference)reference,
-                                                reference.getBindings());
-            }
-            */
-
-            Implementation implementation = component.getImplementation();
-            if (implementation instanceof Composite) {
-                addRuntimeProviders((Composite)implementation);
-            } else if (implementation != null) {
-                addImplementationProvider((RuntimeComponent)component, 
implementation);
-                addScopeContainer(component);
-            }
-        }
+        this.referenceHelper = new ReferenceHelper(assemblyFactory, 
javaInterfaceFactory, processors);
     }
 
     /**
-     * Configure a composite
-     * 
-     * @param composite
-     * @throws IncompatibleInterfaceContractException
-     */
-    private void removeRuntimeProviders(Composite composite) throws 
IncompatibleInterfaceContractException {
-        for (Component component : composite.getComponents()) {
-
-            for (ComponentService service : component.getServices()) {
-                removeServiceBindingProviders((RuntimeComponent)component, 
(RuntimeComponentService)service, service
-                    .getBindings());
-            }
-
-            for (ComponentReference reference : component.getReferences()) {
-                removeReferenceBindingProviders((RuntimeComponent)component,
-                                                
(RuntimeComponentReference)reference,
-                                                reference.getBindings());
-            }
-
-            Implementation implementation = component.getImplementation();
-            if (implementation instanceof Composite) {
-                removeRuntimeProviders((Composite)implementation);
-            } else if (implementation != null) {
-                removeImplementationProvider((RuntimeComponent)component);
-                removeScopeContainer(component);
-            }
-        }
-    }
-
-
-    /**
      * @see 
org.apache.tuscany.sca.core.runtime.CompositeActivator#activate(org.apache.tuscany.sca.runtime.RuntimeComponent,
 org.apache.tuscany.sca.runtime.RuntimeComponentReference)
      */
     public void activate(RuntimeComponent component, RuntimeComponentReference 
ref) {
-        addReferenceBindingProviders(component, ref, ref.getBindings());
+        resolveTargets(ref);
         for (Binding binding : ref.getBindings()) {
+            addReferenceBindingProvider(component, ref, binding);
             addReferenceWire(component, ref, binding);
             ReferenceBindingProvider provider = 
ref.getBindingProvider(binding);
             if (provider != null) {
@@ -196,27 +121,42 @@
         }
     }
 
-    private void addReferenceBindingProviders(RuntimeComponent component,
-                                              RuntimeComponentReference 
reference,
-                                              List<Binding> bindings) {
-
-        for (Binding binding : bindings) {
-            BindingProviderFactory providerFactory =
-                
(BindingProviderFactory)providerFactories.getProviderFactory(binding.getClass());
-            if (providerFactory != null) {
-                @SuppressWarnings("unchecked")
-                ReferenceBindingProvider bindingProvider =
-                    
providerFactory.createReferenceBindingProvider((RuntimeComponent)component,
-                                                                   
(RuntimeComponentReference)reference,
-                                                                   binding);
-                if (bindingProvider != null) {
-                    
((RuntimeComponentReference)reference).setBindingProvider(binding, 
bindingProvider);
-                }
-            } else {
-                throw new IllegalStateException("Provider factory not found 
for class: " + binding.getClass().getName());
+    public void deactivate(RuntimeComponent component, 
RuntimeComponentReference ref) {
+        removeReferenceWires(ref);
+        for (Binding binding : ref.getBindings()) {
+            removeReferenceBindingProvider(component, ref, binding);
+        }
+
+    }
+
+    /**
+     * @param component
+     * @param reference
+     * @param binding
+     */
+    private void addReferenceBindingProvider(RuntimeComponent component,
+                                             RuntimeComponentReference 
reference,
+                                             Binding binding) {
+        BindingProviderFactory providerFactory =
+            
(BindingProviderFactory)providerFactories.getProviderFactory(binding.getClass());
+        if (providerFactory != null) {
+            @SuppressWarnings("unchecked")
+            ReferenceBindingProvider bindingProvider =
+                
providerFactory.createReferenceBindingProvider((RuntimeComponent)component,
+                                                               
(RuntimeComponentReference)reference,
+                                                               binding);
+            if (bindingProvider != null) {
+                
((RuntimeComponentReference)reference).setBindingProvider(binding, 
bindingProvider);
             }
+        } else {
+            throw new IllegalStateException("Provider factory not found for 
class: " + binding.getClass().getName());
         }
+    }
 
+    /**
+     * @param reference
+     */
+    private void resolveTargets(RuntimeComponentReference reference) {
         // Support for distributed domain follows
 
         // go over any targets that have not been resolved yet (as they are 
running on other nodes)
@@ -229,16 +169,8 @@
                     // TODO - we should look at all the bindings now 
associated with the 
                     //        unresolved target but we assume the SCA binding 
here as
                     //        its currently the only wireable one
-                    if (binding instanceof SCABinding) {
-                        SCABinding scaBinding = (SCABinding)binding;
-
-                        BindingProviderFactory providerFactory =
-                            
(BindingProviderFactory)providerFactories.getProviderFactory(SCABinding.class);
-
-                        if (providerFactory == null) {
-                            throw new IllegalStateException("Provider factory 
not found for class: " + scaBinding
-                                .getClass().getName());
-                        }
+                    if (binding instanceof WireableBinding) {
+                        WireableBinding scaBinding = (WireableBinding)binding;
 
                         // clone the SCA binding and fill in service details 
                         SCABinding clonedSCABinding = null;
@@ -246,30 +178,19 @@
                             clonedSCABinding = 
(SCABinding)((WireableBinding)scaBinding).clone();
                             clonedSCABinding.setURI(service.getName());
                             
((WireableBinding)clonedSCABinding).setRemote(true);
-                        } catch (Exception e) {
-                            // warning("The binding doesn't support clone: " + 
binding.getClass().getSimpleName(), binding);
-                        }
-
-                        @SuppressWarnings("unchecked")
-                        ReferenceBindingProvider bindingProvider =
-                            
providerFactory.createReferenceBindingProvider((RuntimeComponent)component,
-                                                                           
(RuntimeComponentReference)reference,
-                                                                           
clonedSCABinding);
-                        if (bindingProvider != null) {
-                            ((RuntimeComponentReference)reference)
-                                .setBindingProvider(clonedSCABinding, 
bindingProvider);
-
                             // add the cloned SCA binding to the reference as 
it will be used to look up the 
                             // provider later
                             reference.getBindings().remove(binding);
                             reference.getBindings().add(clonedSCABinding);
-                        } else {
-                            throw new IllegalStateException(
-                                                            "Unable to create 
a distributed SCA binding provider for reference: " + 
-                                                             
reference.getName()
-                                                                + " and 
target: "
-                                                                + 
service.getName());
+                        } catch (Exception e) {
+                            // warning("The binding doesn't support clone: " + 
binding.getClass().getSimpleName(), binding);
                         }
+                    } else {
+                        throw new IllegalStateException(
+                                                        "Unable to create a 
distributed SCA binding provider for reference: " + reference
+                                                            .getName()
+                                                            + " and target: "
+                                                            + 
service.getName());
                     }
                 }
             }
@@ -419,87 +340,43 @@
         component.setImplementationProvider(null);
     }
 
-    private void addServiceBindingProviders(RuntimeComponent component,
-                                            RuntimeComponentService service,
-                                            List<Binding> bindings) {
-        for (Binding binding : bindings) {
-            BindingProviderFactory providerFactory =
-                
(BindingProviderFactory)providerFactories.getProviderFactory(binding.getClass());
-            if (providerFactory != null) {
-                @SuppressWarnings("unchecked")
-                ServiceBindingProvider bindingProvider =
-                    
providerFactory.createServiceBindingProvider((RuntimeComponent)component,
-                                                                 
(RuntimeComponentService)service,
-                                                                 binding);
-                if (bindingProvider != null) {
-                    
((RuntimeComponentService)service).setBindingProvider(binding, bindingProvider);
-                }
-            } else {
-                throw new IllegalStateException("Provider factory not found 
for class: " + binding.getClass().getName());
-            }
+    /**
+     * @param component
+     * @param service
+     * @param binding
+     */
+    private void addServiceBindingProvider(RuntimeComponent component, 
RuntimeComponentService service, Binding binding) {
+        // REVIEW: Is this the right way to mark the binding as remote?
+        if ((binding instanceof WireableBinding) && 
(service.getInterfaceContract().getInterface().isRemotable())) {
+            WireableBinding wireableBinding = (WireableBinding)binding;
+            wireableBinding.setRemote(true);
         }
-
-        // support for distributed domain follows
-        // TODO - roll into above code but keeping separate so that it is 
obvious 
-
-        // If there is an SCA binding for the service add a second one marked 
as
-        // remote in the case where the service interface is remotable. This
-        // service binding provides a separate wire that will be active if 
-        // this service is referenced remotely.  
-        SCABinding clonedSCABinding = null;
-
-        for (Binding binding : bindings) {
-            if ((binding instanceof SCABinding) && 
(service.getInterfaceContract().getInterface().isRemotable())) {
-                SCABinding scaBinding = (SCABinding)binding;
-
-                BindingProviderFactory providerFactory =
-                    
(BindingProviderFactory)providerFactories.getProviderFactory(binding.getClass());
-                if (providerFactory != null) {
-
-                    // clone the SCA binding and fill in service details 
-                    try {
-                        clonedSCABinding = 
(SCABinding)((WireableBinding)scaBinding).clone();
-                        ((WireableBinding)clonedSCABinding).setRemote(true);
-                    } catch (Exception e) {
-                        // warning("The binding doesn't support clone: " + 
binding.getClass().getSimpleName(), binding);
-                    }
-
-                    @SuppressWarnings("unchecked")
-                    ServiceBindingProvider bindingProvider =
-                        
providerFactory.createServiceBindingProvider((RuntimeComponent)component,
-                                                                     
(RuntimeComponentService)service,
-                                                                     
clonedSCABinding);
-                    if (bindingProvider != null) {
-                        
((RuntimeComponentService)service).setBindingProvider(clonedSCABinding, 
bindingProvider);
-                    }
-                } else {
-                    throw new IllegalStateException("Provider factory not 
found for class: " + binding.getClass()
-                        .getName());
-                }
-            }
-            // add the cloned SCA binding to the service as it will be used to 
look up the provider later
-            if (clonedSCABinding != null) {
-                service.getBindings().remove(binding);
-                service.getBindings().add(clonedSCABinding);
+        BindingProviderFactory providerFactory =
+            
(BindingProviderFactory)providerFactories.getProviderFactory(binding.getClass());
+        if (providerFactory != null) {
+            @SuppressWarnings("unchecked")
+            ServiceBindingProvider bindingProvider =
+                
providerFactory.createServiceBindingProvider((RuntimeComponent)component,
+                                                             
(RuntimeComponentService)service,
+                                                             binding);
+            if (bindingProvider != null) {
+                ((RuntimeComponentService)service).setBindingProvider(binding, 
bindingProvider);
             }
+        } else {
+            throw new IllegalStateException("Provider factory not found for 
class: " + binding.getClass().getName());
         }
-
     }
 
-    private void removeServiceBindingProviders(RuntimeComponent component,
-                                               RuntimeComponentService service,
-                                               List<Binding> bindings) {
-        for (Binding binding : bindings) {
-            ((RuntimeComponentService)service).setBindingProvider(binding, 
null);
-        }
+    private void removeServiceBindingProvider(RuntimeComponent component,
+                                              RuntimeComponentService service,
+                                              Binding binding) {
+        service.setBindingProvider(binding, null);
     }
 
-    private void removeReferenceBindingProviders(RuntimeComponent component,
+    private void removeReferenceBindingProvider(RuntimeComponent component,
                                                  RuntimeComponentReference 
reference,
-                                                 List<Binding> bindings) {
-        for (Binding binding : bindings) {
-            ((RuntimeComponentReference)reference).setBindingProvider(binding, 
null);
-        }
+                                                 Binding binding) {
+        reference.setBindingProvider(binding, null);
     }
 
     public void start(Composite composite) {
@@ -601,62 +478,6 @@
     }
 
     /**
-     * Create runtime wires for the composite
-     * 
-     * @param composite
-     * @throws IncompatibleInterfaceContractException
-     */
-    private void addRuntimeWires(Composite composite) throws 
IncompatibleInterfaceContractException {
-        for (Component component : composite.getComponents()) {
-            Implementation implementation = component.getImplementation();
-            if (implementation instanceof Composite) {
-                // Recursively create runtime wires
-                addRuntimeWires((Composite)implementation);
-            } else {
-                /* [rfeng] Defer this
-                // Create outbound wires for the component references
-                for (ComponentReference reference : component.getReferences()) 
{
-                    for (Binding binding : reference.getBindings()) {
-                        addReferenceWire(component, reference, binding);
-                    }
-                }
-                */
-                // Create inbound wires for the component services
-                for (ComponentService service : component.getServices()) {
-                    for (Binding binding : service.getBindings()) {
-                        addServiceWire(component, service, binding);
-                    }
-                }
-            }
-        }
-    }
-
-    /**
-     * Remove runtime wires for the composite
-     * 
-     * @param composite
-     * @throws IncompatibleInterfaceContractException
-     */
-    private void removeRuntimeWires(Composite composite) throws 
IncompatibleInterfaceContractException {
-        for (Component component : composite.getComponents()) {
-            Implementation implementation = component.getImplementation();
-            if (implementation instanceof Composite) {
-                // Recursively remove runtime wires
-                removeRuntimeWires((Composite)implementation);
-            } else {
-                // Remove outbound wires for the component references
-                for (ComponentReference reference : component.getReferences()) 
{
-                    removeReferenceWires(reference);
-                }
-                // Remove inbound wires for the component services
-                for (ComponentService service : component.getServices()) {
-                    removeServiceWires(service);
-                }
-            }
-        }
-    }
-
-    /**
      * Get the effective interface contract for a reference binding
      * 
      * @param reference
@@ -719,7 +540,6 @@
             return;
         }
         RuntimeComponentService runtimeService = 
(RuntimeComponentService)service;
-
         runtimeService.getRuntimeWires().clear();
     }
 
@@ -759,6 +579,20 @@
         return wire;
     }
 
+    public void activate(RuntimeComponent component, RuntimeComponentService 
service) {
+        for (Binding binding : service.getBindings()) {
+            addServiceBindingProvider(component, service, binding);
+            addServiceWire(component, service, binding);
+        }
+    }
+
+    public void deactivate(RuntimeComponent component, RuntimeComponentService 
service) {
+        removeServiceWires(service);
+        for (Binding binding : service.getBindings()) {
+            removeServiceBindingProvider(component, service, binding);
+        }
+    }
+
     private void addScopeContainer(Component component) {
         if (!(component instanceof ScopedRuntimeComponent)) {
             return;
@@ -777,8 +611,20 @@
 
     public void activate(Composite composite) throws ActivationException {
         try {
-            addRuntimeProviders(composite);
-            addRuntimeWires(composite);
+            for (Component component : composite.getComponents()) {
+
+                Implementation implementation = component.getImplementation();
+                if (implementation instanceof Composite) {
+                    activate((Composite)implementation);
+                } else if (implementation != null) {
+                    addImplementationProvider((RuntimeComponent)component, 
implementation);
+                    addScopeContainer(component);
+                }
+
+                for (ComponentService service : component.getServices()) {
+                    activate((RuntimeComponent)component, 
(RuntimeComponentService)service);
+                }
+            }
         } catch (Exception e) {
             throw new ActivationException(e);
         }
@@ -786,48 +632,33 @@
 
     public void deactivate(Composite composite) throws ActivationException {
         try {
-            removeRuntimeProviders(composite);
-            removeRuntimeWires(composite);
-        } catch (Exception e) {
-            throw new ActivationException(e);
-        }
-    }
+            for (Component component : composite.getComponents()) {
+                for (ComponentService service : component.getServices()) {
+                    deactivate((RuntimeComponent)component, 
(RuntimeComponentService)service);
+                }
 
-    public void write(Component component, ComponentReference reference, 
Writer writer) throws IOException {
-        try {
-            StAXArtifactProcessor<Composite> processor = 
staxProcessors.getProcessor(Composite.class);
-            Composite composite = assemblyFactory.createComposite();
-            composite.setName(new 
QName("http://tuscany.apache.org/xmlns/sca/1.0";, "default"));
-            Component comp = assemblyFactory.createComponent();
-            comp.setName("default");
-            comp.setURI(component.getURI());
-            composite.getComponents().add(comp);
-            comp.getReferences().add(reference);
-
-            XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
-            XMLStreamWriter streamWriter = 
outputFactory.createXMLStreamWriter(writer);
-            processor.write(composite, streamWriter);
+                for (ComponentReference reference : component.getReferences()) 
{
+                    deactivate((RuntimeComponent)component, 
(RuntimeComponentReference)reference);
+                }
+
+                Implementation implementation = component.getImplementation();
+                if (implementation instanceof Composite) {
+                    deactivate((Composite)implementation);
+                } else if (implementation != null) {
+                    removeImplementationProvider((RuntimeComponent)component);
+                    removeScopeContainer(component);
+                }
+            }
         } catch (Exception e) {
-            throw new IOException(e.getMessage());
+            throw new ActivationException(e);
         }
     }
 
-    public String write(Component component, ComponentReference reference) 
throws IOException {
-        StringWriter writer = new StringWriter();
-        write(component, reference, writer);
-        return writer.toString();
-    }
-
-    public Component read(Reader reader) throws IOException {
-        try {
-            StAXArtifactProcessor<Composite> processor = 
staxProcessors.getProcessor(Composite.class);
-            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
-            XMLStreamReader streamReader = 
inputFactory.createXMLStreamReader(reader);
-            Composite composite = processor.read(streamReader);
-            return composite.getComponents().get(0);
-        } catch (Exception e) {
-            throw new IOException(e.getMessage());
-        }
+    /**
+     * @return the referenceHelper
+     */
+    public ReferenceHelper getReferenceHelper() {
+        return referenceHelper;
     }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to