Index: dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/exporter/TopologyManagerExport.java
===================================================================
--- dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/exporter/TopologyManagerExport.java	(revision 1457734)
+++ dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/exporter/TopologyManagerExport.java	(working copy)
@@ -19,6 +19,7 @@
 package org.apache.cxf.dosgi.topologymanager.exporter;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedHashMap;
@@ -36,11 +37,9 @@
 import org.osgi.framework.ServiceEvent;
 import org.osgi.framework.ServiceListener;
 import org.osgi.framework.ServiceReference;
-import org.osgi.service.remoteserviceadmin.ExportReference;
 import org.osgi.service.remoteserviceadmin.ExportRegistration;
 import org.osgi.service.remoteserviceadmin.RemoteConstants;
 import org.osgi.service.remoteserviceadmin.RemoteServiceAdmin;
-import org.osgi.service.remoteserviceadmin.RemoteServiceAdminEvent;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -61,22 +60,22 @@
     /**
      * Holds all services that are exported by this TopologyManager for each ServiceReference that should be
      * exported a map is maintained which contains information on the endpoints for each RemoteAdminService
-     * 
+     *
      * <pre>
      * Bsp.:
-     * ServiceToExort_1
+     * ServiceToExport_1
      * ---&gt; RemoteAdminService_1 (CXF HTTP)
      * --------&gt; List&lt;EndpointDescription&gt; -&gt; {http://localhost:1234/greeter, http://localhost:8080/hello}
      * ---&gt; RemoteAdminService_2 (ActiveMQ JMS/OpenWire)
      * --------&gt; List&lt;EndpointDescription&gt; -&gt; {OpenWire://127.0.0.1:123/testQueue}
-     * ServiceToExort_2
+     * ServiceToExport_2
      * ---&gt; RemoteAdminService_1 (CXF HTTP)
      * --------&gt; List&lt;EndpointDescription&gt; -&gt; {empty} // not exported yet or not suitable
-     * 
+     *
      * </pre>
      */
-    private final Map<ServiceReference, 
-                      Map<RemoteServiceAdmin, Collection<ExportRegistration>>> exportedServices = 
+    private final Map<ServiceReference,
+                      Map<RemoteServiceAdmin, Collection<ExportRegistration>>> exportedServices =
         new LinkedHashMap<ServiceReference, Map<RemoteServiceAdmin, Collection<ExportRegistration>>>();
 
     public TopologyManagerExport(BundleContext ctx, RemoteServiceAdminTracker rsaTracker) {
@@ -86,10 +85,12 @@
         this.remoteServiceAdminTracker.addListener(new RemoteServiceAdminLifeCycleListener() {
 
             public void added(RemoteServiceAdmin rsa) {
+                LOG.debug("RemoteServiceAdminTracker added {}, size is {}", rsa, remoteServiceAdminTracker.size());
                 triggerExportForRemoteServiceAdmin(rsa);
             }
 
             public void removed(RemoteServiceAdmin rsa) {
+                LOG.debug("RemoteServiceAdminTracker removed {}, size is {}", rsa, remoteServiceAdminTracker.size());
                 removeRemoteServiceAdmin(rsa);
             }
         });
@@ -99,7 +100,7 @@
                 if (event.getType() == ServiceEvent.REGISTERED) {
                     LOG.debug("Received REGISTERED ServiceEvent: {}", event);
                     if (shouldExportService(sref)) {
-                        exportService(sref);
+                        exportService(sref, null);
                     }
                 } else if (event.getType() == ServiceEvent.UNREGISTERING) {
                     LOG.debug("Received UNREGISTERING ServiceEvent: {}", event);
@@ -107,18 +108,15 @@
                 }
             }
         };
-        
+
         epListenerNotifier = new EndpointListenerNotifier(ctx, this);
     }
-    
+
     /**
      * checks if a Service is intended to be exported
      */
     private boolean shouldExportService(ServiceReference sref) {
-        if (sref.getProperty(RemoteConstants.SERVICE_EXPORTED_INTERFACES) != null) {
-            return true;
-        }
-        return false;
+        return sref.getProperty(RemoteConstants.SERVICE_EXPORTED_INTERFACES) != null;
     }
 
     /**
@@ -127,10 +125,9 @@
      */
     protected void removeRemoteServiceAdmin(RemoteServiceAdmin rsa) {
         synchronized (exportedServices) {
-            for (Map<RemoteServiceAdmin, Collection<ExportRegistration>> exports : exportedServices
-                .values()) {
-                if (exports.containsKey(rsa)) {
-                    Collection<ExportRegistration> endpoints = exports.get(rsa);
+            for (Map<RemoteServiceAdmin, Collection<ExportRegistration>> exports : exportedServices.values()) {
+                Collection<ExportRegistration> endpoints = exports.get(rsa);
+                if (endpoints != null) {
                     this.epListenerNotifier.notifyAllListenersOfRemoval(endpoints);
                     exports.remove(rsa);
                 }
@@ -139,19 +136,19 @@
     }
 
     protected void triggerExportForRemoteServiceAdmin(RemoteServiceAdmin rsa) {
-        LOG.debug("triggerExportImportForRemoteSericeAdmin()");
+        LOG.debug("triggerExportForRemoteServiceAdmin()");
 
         synchronized (exportedServices) {
-            for (ServiceReference serviceRef : exportedServices.keySet()) {
-                Map<RemoteServiceAdmin, Collection<ExportRegistration>> rsaExports = exportedServices.get(serviceRef);
-                String bundleName = serviceRef.getBundle().getSymbolicName();
-                if (rsaExports.containsKey(rsa)) {
+            for (Map.Entry<ServiceReference, Map<RemoteServiceAdmin, Collection<ExportRegistration>>> entry :
+                    exportedServices.entrySet()) {
+                String bundleName = entry.getKey().getBundle().getSymbolicName();
+                if (entry.getValue().containsKey(rsa)) {
                     // already handled....
                     LOG.debug("service from bundle {} is already handled by this RSA", bundleName);
                 } else {
                     // trigger export of this service....
                     LOG.debug("service from bundle {} is to be exported by this RSA", bundleName);
-                    exportService(serviceRef);
+                    exportService(entry.getKey(), rsa);
                 }
             }
         }
@@ -178,15 +175,17 @@
 
     void removeService(ServiceReference sref) {
         synchronized (exportedServices) {
-            if (exportedServices.containsKey(sref)) {
-                Map<RemoteServiceAdmin, Collection<ExportRegistration>> rsas = exportedServices.get(sref);
-                for (Map.Entry<RemoteServiceAdmin, Collection<ExportRegistration>> entry : rsas.entrySet()) {
-                    if (entry.getValue() != null) {
-                        Collection<ExportRegistration> registrations = entry.getValue();
-                        this.epListenerNotifier.notifyListenersOfRemoval(registrations);
-                        for (ExportRegistration exReg : registrations) {
-                            if (exReg != null) {
-                                exReg.close();
+            Map<RemoteServiceAdmin, Collection<ExportRegistration>> rsas = exportedServices.get(sref);
+            if (rsas != null) {
+                synchronized (rsas) {
+                    for (Map.Entry<RemoteServiceAdmin, Collection<ExportRegistration>> entry : rsas.entrySet()) {
+                        if (entry.getValue() != null) {
+                            Collection<ExportRegistration> registrations = entry.getValue();
+                            this.epListenerNotifier.notifyListenersOfRemoval(registrations);
+                            for (ExportRegistration exReg : registrations) {
+                                if (exReg != null) {
+                                    exReg.close();
+                                }
                             }
                         }
                     }
@@ -196,48 +195,48 @@
             }
         }
     }
-    
-    protected void exportService(ServiceReference sref) {
+
+    protected void exportService(ServiceReference sref, RemoteServiceAdmin rsa) {
         // add to local list of services that should/are be exported
         synchronized (exportedServices) {
             LOG.info("TopologyManager: adding service to exportedServices list to export it --- from bundle:  "
                       + sref.getBundle().getSymbolicName());
-            exportedServices.put(sref,
-                                 new LinkedHashMap<RemoteServiceAdmin, Collection<ExportRegistration>>());
+            if (!exportedServices.containsKey(sref))
+                exportedServices.put(sref, Collections.synchronizedMap(
+                    new LinkedHashMap<RemoteServiceAdmin, Collection<ExportRegistration>>()));
         }
-        triggerExport(sref);
+        triggerExport(sref, rsa);
     }
 
-    private void triggerExport(final ServiceReference sref) {
+    private void triggerExport(final ServiceReference sref, final RemoteServiceAdmin rsa) {
         execService.execute(new Runnable() {
             public void run() {
-                doExportService(sref);
+                doExportService(sref, rsa);
             }
         });
     }
 
-    private void doExportService(final ServiceReference sref) {
+    private void doExportService(final ServiceReference sref, RemoteServiceAdmin rsa) {
         LOG.debug("Exporting service");
 
-        Map<RemoteServiceAdmin, Collection<ExportRegistration>> exports = null;
+        List<RemoteServiceAdmin> rsrs = rsa != null ? Arrays.asList(rsa) : remoteServiceAdminTracker.getList();
 
-        synchronized (exportedServices) {
-            exports = Collections.synchronizedMap(exportedServices.get(sref));
-        }
-        // FIXME: Not thread safe...?
-        if (exports == null) {
-            return;
-        }
-        if (remoteServiceAdminTracker == null || remoteServiceAdminTracker.size() == 0) {
+        if (rsrs.isEmpty()) {
             LOG.error(
                     "No RemoteServiceAdmin available! Unable to export service from bundle {}, interfaces: {}",
                     sref.getBundle().getSymbolicName(),
                     sref.getProperty(org.osgi.framework.Constants.OBJECTCLASS));
         }
 
+        Map<RemoteServiceAdmin, Collection<ExportRegistration>> exports;
+        synchronized (exportedServices) {
+            exports = exportedServices.get(sref);
+        }
+        if (exports == null) {
+            return;
+        }
 
-        for (final RemoteServiceAdmin remoteServiceAdmin : remoteServiceAdminTracker
-                .getList()) {
+        for (final RemoteServiceAdmin remoteServiceAdmin : rsrs) {
             LOG.info("TopologyManager: handling remoteServiceAdmin "
                     + remoteServiceAdmin);
 
@@ -247,14 +246,13 @@
             } else {
                 // TODO: additional parameter Map ?
                 LOG.debug("exporting ...");
-                Collection<ExportRegistration> endpoints = remoteServiceAdmin
-                        .exportService(sref, null);
+                Collection<ExportRegistration> endpoints = remoteServiceAdmin.exportService(sref, null);
                 if (endpoints == null) {
                     // TODO export failed -> What should be done here?
                     LOG.error("export failed");
                     exports.put(remoteServiceAdmin, null);
                 } else {
-                    LOG.info("TopologyManager: export sucessful Endpoints: {}", endpoints);
+                    LOG.info("TopologyManager: export successful Endpoints: {}", endpoints);
                     // enqueue in local list of endpoints
                     exports.put(remoteServiceAdmin, endpoints);
 
@@ -263,14 +261,16 @@
             }
         }
     }
-    
+
     public Collection<ExportRegistration> getAllExportRegistrations() {
         List<ExportRegistration> registrations = new ArrayList<ExportRegistration>();
         synchronized (exportedServices) {
             for (Map<RemoteServiceAdmin, Collection<ExportRegistration>> exports : exportedServices.values()) {
-                for (Collection<ExportRegistration> regs : exports.values()) {
-                    if (regs != null) {
-                        registrations.addAll(regs);
+                synchronized (exports) {
+                    for (Collection<ExportRegistration> regs : exports.values()) {
+                        if (regs != null) {
+                            registrations.addAll(regs);
+                        }
                     }
                 }
             }
@@ -280,46 +280,12 @@
 
     private void exportExistingServices() throws InvalidSyntaxException {
         String filter = "(" + RemoteConstants.SERVICE_EXPORTED_INTERFACES + "=*)";
-        ServiceReference[] references = bctx.getServiceReferences(null, filter);
+        ServiceReference[] references = bctx.getServiceReferences((String)null, filter);
 
         if (references != null) {
             for (ServiceReference sref : references) {
-                exportService(sref);
+                exportService(sref, null);
             }
         }
     }
-
-    public void removeExportRegistration(ExportRegistration exportRegistration) {
-        ServiceReference sref = exportRegistration.getExportReference().getExportedService();
-        if (sref != null) {
-            synchronized (exportedServices) {
-
-                Map<RemoteServiceAdmin, Collection<ExportRegistration>> ex = exportedServices.get(sref);
-                if (ex != null) {
-                    for (Map.Entry<RemoteServiceAdmin, Collection<ExportRegistration>> export : ex.entrySet()) {
-                        export.getValue().contains(exportRegistration);
-                    }
-                }
-            }
-        } else {
-            // the manager will be notified by its own service listener about this case
-        }
-    }
-
-    /**
-     * This method is called once a RemoteServiceAdminEvent for an removed export reference is received.
-     * However the current implementation has no special support for multiple topology managers, therefore this method
-     * does nothing for the moment.
-     */
-    public void removeExportReference(ExportReference anyObject) {
-        // TODO Auto-generated method stub
-        // LOG.severe("NOT implemented !!!");
-    }
-
-    public void remoteAdminEvent(RemoteServiceAdminEvent event) {
-        if (event.getType() == RemoteServiceAdminEvent.EXPORT_UNREGISTRATION) {
-            removeExportReference(event.getExportReference());
-        }
-    }
-
 }

Reply via email to