You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by am...@apache.org on 2021/03/10 10:27:28 UTC

[aries-rsa] 03/04: Clean up LocalDiscovery

This is an automated email from the ASF dual-hosted git repository.

amichai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/aries-rsa.git

commit 99090043acdc472474bfc95859b318b5e4a73823
Author: Amichai Rothman <am...@apache.org>
AuthorDate: Tue Mar 9 16:59:41 2021 +0200

    Clean up LocalDiscovery
---
 .../aries/rsa/discovery/local/LocalDiscovery.java  | 39 ++++++++++------------
 1 file changed, 18 insertions(+), 21 deletions(-)

diff --git a/discovery/local/src/main/java/org/apache/aries/rsa/discovery/local/LocalDiscovery.java b/discovery/local/src/main/java/org/apache/aries/rsa/discovery/local/LocalDiscovery.java
index e0c4884..0181bb2 100644
--- a/discovery/local/src/main/java/org/apache/aries/rsa/discovery/local/LocalDiscovery.java
+++ b/discovery/local/src/main/java/org/apache/aries/rsa/discovery/local/LocalDiscovery.java
@@ -62,9 +62,8 @@ public class LocalDiscovery implements BundleListener {
 
     @Activate
     public void activate(BundleContext context) {
-        Bundle[] bundles = context.getBundles();
-        processExistingBundles(bundles);
         context.addBundleListener(this);
+        processExistingBundles(context.getBundles());
     }
 
     @Deactivate
@@ -72,14 +71,14 @@ public class LocalDiscovery implements BundleListener {
         context.removeBundleListener(this);
     }
 
-    public void processExistingBundles(Bundle[] bundles) {
+    protected void processExistingBundles(Bundle[] bundles) {
         if (bundles == null) {
             return;
         }
 
         for (Bundle b : bundles) {
             if (b.getState() == Bundle.ACTIVE) {
-                findDeclaredRemoteServices(b);
+                addDeclaredRemoteServices(b);
             }
         }
     }
@@ -94,8 +93,7 @@ public class LocalDiscovery implements BundleListener {
         synchronized (listenerToFilters) {
             listenerToFilters.put(endpointListener, filters);
             for (String filter : filters) {
-                Collection<EndpointEventListener> listeners = filterToListeners.computeIfAbsent(filter, k -> new ArrayList<>());
-                listeners.add(endpointListener);
+                filterToListeners.computeIfAbsent(filter, k -> new ArrayList<>()).add(endpointListener);
             }
         }
 
@@ -142,19 +140,20 @@ public class LocalDiscovery implements BundleListener {
     }
 
     // BundleListener method
+    @Override
     public void bundleChanged(BundleEvent be) {
         switch (be.getType()) {
         case BundleEvent.STARTED:
-            findDeclaredRemoteServices(be.getBundle());
+            addDeclaredRemoteServices(be.getBundle());
             break;
         case BundleEvent.STOPPED:
-            removeServicesDeclaredInBundle(be.getBundle());
+            removeDeclaredRemoteServices(be.getBundle());
             break;
         default:
         }
     }
 
-    private void findDeclaredRemoteServices(Bundle bundle) {
+    private void addDeclaredRemoteServices(Bundle bundle) {
         List<EndpointDescription> endpoints = bundleParser.getAllEndpointDescriptions(bundle);
         for (EndpointDescription endpoint : endpoints) {
             endpointDescriptions.put(endpoint, bundle);
@@ -163,7 +162,7 @@ public class LocalDiscovery implements BundleListener {
         }
     }
 
-    private void removeServicesDeclaredInBundle(Bundle bundle) {
+    private void removeDeclaredRemoteServices(Bundle bundle) {
         for (Iterator<Entry<EndpointDescription, Bundle>> i = endpointDescriptions.entrySet().iterator();
             i.hasNext();) {
             Entry<EndpointDescription, Bundle> entry = i.next();
@@ -176,22 +175,14 @@ public class LocalDiscovery implements BundleListener {
     }
 
     private void triggerCallbacks(EndpointEvent event) {
-        EndpointDescription endpoint = event.getEndpoint();
-        for (Map.Entry<String, Collection<EndpointEventListener>> entry : getMatchingListeners(endpoint).entrySet()) {
-            String filter = entry.getKey();
+        Map<String, Collection<EndpointEventListener>> matched = getMatchingListeners(event.getEndpoint());
+        for (Map.Entry<String, Collection<EndpointEventListener>> entry : matched.entrySet()) {
             for (EndpointEventListener listener : entry.getValue()) {
-                triggerCallbacks(listener, filter, event);
+                triggerCallbacks(listener, entry.getKey(), event);
             }
         }
     }
 
-    private void triggerCallbacks(EndpointEventListener endpointListener, String filter, EndpointEvent event) {
-        if (!LocalDiscovery.matchFilter(filter, event.getEndpoint())) {
-            return;
-        }
-        endpointListener.endpointChanged(event, filter);
-    }
-
     private void triggerCallbacks(Collection<String> filters, EndpointEventListener endpointListener) {
         for (String filter : filters) {
             for (EndpointDescription endpoint : endpointDescriptions.keySet()) {
@@ -201,6 +192,12 @@ public class LocalDiscovery implements BundleListener {
         }
     }
 
+    private void triggerCallbacks(EndpointEventListener endpointListener, String filter, EndpointEvent event) {
+        if (LocalDiscovery.matchFilter(filter, event.getEndpoint())) {
+            endpointListener.endpointChanged(event, filter);
+        }
+    }
+
     private static boolean matchFilter(String filter, EndpointDescription endpoint) {
         if (filter == null) {
             return false;