This is an automated email from the ASF dual-hosted git repository. amichair pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/aries-rsa.git
commit 0f338d44e04d7b1e36451022ccae2657bd112516 Author: Amichai Rothman <[email protected]> AuthorDate: Tue Mar 31 01:04:01 2026 +0300 Little refactorings in LocalDiscovery --- .../aries/rsa/discovery/local/LocalDiscovery.java | 93 ++++++++++------------ 1 file changed, 44 insertions(+), 49 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 1cdc0289..e4526ef0 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 @@ -20,7 +20,6 @@ package org.apache.aries.rsa.discovery.local; import java.util.ArrayList; import java.util.Collection; -import java.util.Dictionary; import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; @@ -50,16 +49,14 @@ import org.osgi.service.remoteserviceadmin.EndpointEventListener; @Component(immediate = true) public class LocalDiscovery implements BundleListener { - // this is effectively a set which allows for multiple service descriptions with the - // same interface name but different properties and takes care of itself with respect to concurrency - final Map<EndpointDescription, Bundle> endpointDescriptions = new ConcurrentHashMap<>(); + final Map<EndpointDescription, Bundle> endpoints = new ConcurrentHashMap<>(); final Map<EndpointEventListener, Collection<String>> listenerToFilters = new HashMap<>(); final Map<String, Collection<EndpointEventListener>> filterToListeners = new HashMap<>(); - EndpointDescriptionBundleParser bundleParser; + EndpointDescriptionBundleParser parser; public LocalDiscovery() { - this.bundleParser = new EndpointDescriptionBundleParser(); + this.parser = new EndpointDescriptionBundleParser(); } @Activate @@ -74,39 +71,40 @@ public class LocalDiscovery implements BundleListener { } protected void processExistingBundles(Bundle[] bundles) { - for (Bundle b : bundles) { - if (b.getState() == Bundle.ACTIVE) { - addDeclaredRemoteServices(b); + for (Bundle bundle : bundles) { + if (bundle.getState() == Bundle.ACTIVE) { + addEndpoints(bundle); } } } @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC) - void bindListener(ServiceReference<EndpointEventListener> endpointListenerRef, EndpointEventListener endpointListener) { - List<String> filters = StringPlus.normalize(endpointListenerRef.getProperty(EndpointEventListener.ENDPOINT_LISTENER_SCOPE)); + void bindListener(ServiceReference<EndpointEventListener> sref, EndpointEventListener listener) { + List<String> filters = StringPlus.normalize(sref.getProperty(EndpointEventListener.ENDPOINT_LISTENER_SCOPE)); if (filters.isEmpty()) { return; } synchronized (listenerToFilters) { - listenerToFilters.put(endpointListener, filters); + listenerToFilters.put(listener, filters); for (String filter : filters) { - filterToListeners.computeIfAbsent(filter, k -> new ArrayList<>()).add(endpointListener); + filterToListeners.computeIfAbsent(filter, k -> new ArrayList<>()).add(listener); } } - triggerCallbacks(filters, endpointListener); + publishAllToListener(filters, listener); } /** * If the tracker was removed or the scope was changed this doesn't require * additional callbacks on the tracker. It's the responsibility of the tracker * itself to clean up any orphans. See Remote Service Admin spec 122.6.3 - * @param endpointListener + * + * @param listener the listener being removed */ - void unbindListener(EndpointEventListener endpointListener) { + void unbindListener(EndpointEventListener listener) { synchronized (listenerToFilters) { - Collection<String> filters = listenerToFilters.remove(endpointListener); + Collection<String> filters = listenerToFilters.remove(listener); if (filters == null) { return; } @@ -114,7 +112,7 @@ public class LocalDiscovery implements BundleListener { for (String filter : filters) { Collection<EndpointEventListener> listeners = filterToListeners.get(filter); if (listeners != null) { - listeners.remove(endpointListener); + listeners.remove(listener); if (listeners.isEmpty()) { filterToListeners.remove(filter); } @@ -123,7 +121,7 @@ public class LocalDiscovery implements BundleListener { } } - void updatedListener(ServiceReference<EndpointEventListener> endpointListenerRef, EndpointEventListener endpointListener) { + void updatedListener(ServiceReference<EndpointEventListener> sref, EndpointEventListener listener) { // if service properties have been updated, the filter (scope) // might have changed so we remove and re-add the listener // TODO fix this so that we don't: @@ -131,8 +129,8 @@ public class LocalDiscovery implements BundleListener { // 2. remove and add instead of modifying // 3. remove instead of modified end match synchronized (listenerToFilters) { - unbindListener(endpointListener); - bindListener(endpointListenerRef, endpointListener); + unbindListener(listener); + bindListener(sref, listener); } } @@ -152,60 +150,58 @@ public class LocalDiscovery implements BundleListener { // BundleListener method @Override - public void bundleChanged(BundleEvent be) { - switch (be.getType()) { - case BundleEvent.STARTED: - addDeclaredRemoteServices(be.getBundle()); - break; - case BundleEvent.STOPPED: - removeDeclaredRemoteServices(be.getBundle()); - break; - default: + public void bundleChanged(BundleEvent event) { + switch (event.getType()) { + case BundleEvent.STARTED: + addEndpoints(event.getBundle()); + break; + case BundleEvent.STOPPED: + removeEndpoints(event.getBundle()); + break; + default: } } - private void addDeclaredRemoteServices(Bundle bundle) { - List<EndpointDescription> endpoints = bundleParser.getAllEndpointDescriptions(bundle); + private void addEndpoints(Bundle bundle) { + List<EndpointDescription> endpoints = parser.getAllEndpointDescriptions(bundle); for (EndpointDescription endpoint : endpoints) { - endpointDescriptions.put(endpoint, bundle); - EndpointEvent event = new EndpointEvent(EndpointEvent.ADDED, endpoint); - triggerCallbacks(event); + this.endpoints.put(endpoint, bundle); + publishToAllListeners(new EndpointEvent(EndpointEvent.ADDED, endpoint)); } } - private void removeDeclaredRemoteServices(Bundle bundle) { - for (Iterator<Entry<EndpointDescription, Bundle>> i = endpointDescriptions.entrySet().iterator(); - i.hasNext();) { + private void removeEndpoints(Bundle bundle) { + for (Iterator<Entry<EndpointDescription, Bundle>> i = endpoints.entrySet().iterator(); + i.hasNext();) { Entry<EndpointDescription, Bundle> entry = i.next(); if (bundle.equals(entry.getValue())) { - EndpointEvent event = new EndpointEvent(EndpointEvent.REMOVED, entry.getKey()); - triggerCallbacks(event); + publishToAllListeners(new EndpointEvent(EndpointEvent.REMOVED, entry.getKey())); i.remove(); } } } - private void triggerCallbacks(EndpointEvent event) { + private void publishToAllListeners(EndpointEvent event) { Map<String, Collection<EndpointEventListener>> matched = getMatchingListeners(event.getEndpoint()); for (Map.Entry<String, Collection<EndpointEventListener>> entry : matched.entrySet()) { for (EndpointEventListener listener : entry.getValue()) { - triggerCallbacks(listener, entry.getKey(), event); + publishIfMatched(listener, entry.getKey(), event); } } } - private void triggerCallbacks(Collection<String> filters, EndpointEventListener endpointListener) { + private void publishAllToListener(Collection<String> filters, EndpointEventListener listener) { for (String filter : filters) { - for (EndpointDescription endpoint : endpointDescriptions.keySet()) { + for (EndpointDescription endpoint : endpoints.keySet()) { EndpointEvent event = new EndpointEvent(EndpointEvent.ADDED, endpoint); - triggerCallbacks(endpointListener, filter, event); + publishIfMatched(listener, filter, event); } } } - private void triggerCallbacks(EndpointEventListener endpointListener, String filter, EndpointEvent event) { + private void publishIfMatched(EndpointEventListener listener, String filter, EndpointEvent event) { if (LocalDiscovery.matchFilter(filter, event.getEndpoint())) { - endpointListener.endpointChanged(event, filter); + listener.endpointChanged(event, filter); } } @@ -216,8 +212,7 @@ public class LocalDiscovery implements BundleListener { try { Filter f = FrameworkUtil.createFilter(filter); - Dictionary<String, Object> dict = new Hashtable<>(endpoint.getProperties()); - return f.match(dict); + return f.match(new Hashtable<>(endpoint.getProperties())); } catch (Exception e) { return false; }
