Author: gertv
Date: Fri Feb 20 14:44:33 2009
New Revision: 746257

URL: http://svn.apache.org/viewvc?rev=746257&view=rev
Log:
SMX4NMR-20: Removing wire from registry when SA is stopped

Modified:
    
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceAssemblyImpl.java
    
servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceAssemblyImplTest.java

Modified: 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceAssemblyImpl.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceAssemblyImpl.java?rev=746257&r1=746256&r2=746257&view=diff
==============================================================================
--- 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceAssemblyImpl.java
 (original)
+++ 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceAssemblyImpl.java
 Fri Feb 20 14:44:33 2009
@@ -18,24 +18,22 @@
 
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 import javax.jbi.JBIException;
-import javax.xml.namespace.QName;
 
 import org.apache.servicemix.jbi.deployer.ServiceAssembly;
 import org.apache.servicemix.jbi.deployer.ServiceUnit;
 import org.apache.servicemix.jbi.deployer.descriptor.Connection;
 import org.apache.servicemix.jbi.deployer.descriptor.DescriptorFactory;
-import org.apache.servicemix.jbi.deployer.descriptor.Provider;
 import org.apache.servicemix.jbi.deployer.descriptor.ServiceAssemblyDesc;
 import org.apache.servicemix.jbi.deployer.impl.AssemblyReferencesListener;
-import org.apache.servicemix.nmr.api.Endpoint;
 import org.apache.servicemix.nmr.api.Wire;
-import org.apache.servicemix.nmr.api.service.ServiceHelper;
 import org.apache.servicemix.nmr.core.util.MapToDictionary;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.prefs.Preferences;
 
 /**
@@ -73,6 +71,9 @@
 
     private final AssemblyReferencesListener listener;
 
+    // map of wires and the matching OSGi ServiceRegistration
+    private Map<Wire, ServiceRegistration> wires = new HashMap<Wire, 
ServiceRegistration>();
+
     public ServiceAssemblyImpl(Bundle bundle,
                                ServiceAssemblyDesc serviceAssemblyDesc,
                                List<ServiceUnitImpl> serviceUnits,
@@ -256,26 +257,19 @@
         if (serviceAssemblyDesc.getConnections() != null && 
serviceAssemblyDesc.getConnections().getConnections() != null) {
             for (Connection connection : 
serviceAssemblyDesc.getConnections().getConnections()) {
                 Wire wire = connection.getWire();
-                registerWire(wire, wire.getFrom());
+                wires.put(wire, registerWire(wire));
             }
         }
     }
     
     private void stopConnections() {
-        if (serviceAssemblyDesc.getConnections() != null && 
serviceAssemblyDesc.getConnections().getConnections() != null) {
-            for (Connection connection : 
serviceAssemblyDesc.getConnections().getConnections()) {
-                Wire wire = connection.getWire();
-                unregisterWire(wire, wire.getFrom());
-            }
+        for (Wire wire : wires.keySet()) {
+            wires.get(wire).unregister();
         }
     }
     
-    protected void registerWire(Wire wire, Map<String, ?> from) {
-        bundle.getBundleContext().registerService(Wire.class.getName(), 
-                                                  wire, new 
MapToDictionary(from));
-    }
-    
-    protected void unregisterWire(Wire wire, Map<String, ?> from) {
-        // TODO
+    protected ServiceRegistration registerWire(Wire wire) {
+        return bundle.getBundleContext().registerService(Wire.class.getName(), 
+                                                         wire, new 
MapToDictionary(wire.getFrom()));
     }
 }

Modified: 
servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceAssemblyImplTest.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceAssemblyImplTest.java?rev=746257&r1=746256&r2=746257&view=diff
==============================================================================
--- 
servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceAssemblyImplTest.java
 (original)
+++ 
servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceAssemblyImplTest.java
 Fri Feb 20 14:44:33 2009
@@ -33,6 +33,7 @@
 import org.jmock.Mockery;
 import org.junit.Before;
 import org.junit.Test;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.prefs.Preferences;
 
 /**
@@ -59,21 +60,25 @@
             one(prefs).put("state", State.Stopped.name());
             one(prefs).flush();
         }});
-        final List<Wire> wires = new LinkedList<Wire>();
+        final List<ServiceRegistration> wires = new 
LinkedList<ServiceRegistration>();
         ServiceAssembly sa = new ServiceAssemblyImpl(null, descriptor, new 
ArrayList<ServiceUnitImpl>(), prefs, new AssemblyReferencesListener(), false) {
             @Override
-            protected void registerWire(Wire wire, Map<String, ?> from) {
-                wires.add(wire);
-            }
-            @Override
-            protected void unregisterWire(Wire wire, Map<String, ?> from) {
-                wires.remove(wire);
+            protected ServiceRegistration registerWire(Wire wire) {
+                ServiceRegistration registration = 
mockery.mock(ServiceRegistration.class, wire.getFrom().toString());
+                wires.add(registration);
+                return registration;
             }
         };
         sa.start();
         assertEquals(2, wires.size());
         
+        // ServiceRegistrations should be unregistered when the SA is stopped
+        for (final ServiceRegistration registration : wires) {
+            mockery.checking(new Expectations() {{
+                one(registration).unregister();
+            }});
+        }
         sa.stop();
-        assertEquals(0, wires.size());
+        mockery.assertIsSatisfied();
     }
 }


Reply via email to