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 69ab66efe88a79f6b5910f1f4ebf63d819b0d3fb Author: Amichai Rothman <[email protected]> AuthorDate: Tue Mar 17 01:21:41 2026 +0200 ConfigDiscovery cleanup --- .../rsa/discovery/config/ConfigDiscovery.java | 84 +++++++++++----------- 1 file changed, 40 insertions(+), 44 deletions(-) diff --git a/discovery/config/src/main/java/org/apache/aries/rsa/discovery/config/ConfigDiscovery.java b/discovery/config/src/main/java/org/apache/aries/rsa/discovery/config/ConfigDiscovery.java index 052dd0f0..75932755 100644 --- a/discovery/config/src/main/java/org/apache/aries/rsa/discovery/config/ConfigDiscovery.java +++ b/discovery/config/src/main/java/org/apache/aries/rsa/discovery/config/ConfigDiscovery.java @@ -23,7 +23,6 @@ import org.osgi.framework.Filter; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceReference; -import org.osgi.service.cm.ConfigurationException; import org.osgi.service.cm.ManagedServiceFactory; import org.osgi.service.remoteserviceadmin.EndpointDescription; import org.osgi.service.remoteserviceadmin.EndpointEvent; @@ -31,6 +30,11 @@ import org.osgi.service.remoteserviceadmin.EndpointEventListener; import java.util.*; +/** + * Monitors adding/updating/removing of configuration instances under the factory pid, + * as well as adding/removing of EndpointEventListeners, and triggers EndpointEvents + * accordingly as endpoints are created/updated/removed (via the configuration changes). + */ class ConfigDiscovery implements ManagedServiceFactory { private final Map<String, EndpointDescription> endpoints = new HashMap<>(); private final Map<EndpointEventListener, Collection<Filter>> listenerToFilters = new HashMap<>(); @@ -41,16 +45,43 @@ class ConfigDiscovery implements ManagedServiceFactory { } @Override - public void updated(String pid, Dictionary<String, ?> properties) throws ConfigurationException { - addDeclaredRemoteService(pid, properties); + public void updated(String pid, Dictionary<String, ?> properties) { + EndpointDescription endpoint = new EndpointDescription(PropertyValidator.validate(properties)); + EndpointDescription old; + Set<Map.Entry<Filter, EndpointEventListener>> oldMatches, newMatches; + synchronized (this) { + old = endpoints.put(pid, endpoint); + oldMatches = old == null ? null : new HashSet<>(findMatches(old)); + newMatches = new HashSet<>(findMatches(endpoint)); + } + List<Map.Entry<Filter, EndpointEventListener>> added = new ArrayList<>(newMatches); + if (old != null) { + added.removeAll(oldMatches); + List<Map.Entry<Filter, EndpointEventListener>> endmatch = new ArrayList<>(oldMatches); + endmatch.removeAll(newMatches); + List<Map.Entry<Filter, EndpointEventListener>> modified = new ArrayList<>(oldMatches); + modified.removeAll(endmatch); + triggerEvents(new EndpointEvent(EndpointEvent.MODIFIED, endpoint), modified); + triggerEvents(new EndpointEvent(EndpointEvent.MODIFIED_ENDMATCH, old), endmatch); + } + triggerEvents(new EndpointEvent(EndpointEvent.ADDED, endpoint), added); } @Override public void deleted(String pid) { - removeServiceDeclaredInConfig(pid); + EndpointDescription endpoint; + List<Map.Entry<Filter, EndpointEventListener>> matched; + synchronized (this) { + endpoint = endpoints.remove(pid); + if (endpoint == null) { + return; + } + matched = findMatches(endpoint); + } + triggerEvents(new EndpointEvent(EndpointEvent.REMOVED, endpoint), matched); } - private static List<Filter> createFilters(ServiceReference<EndpointEventListener> ref) { + private static Collection<Filter> createFilters(ServiceReference<EndpointEventListener> ref) { List<String> values = StringPlus.normalize(ref.getProperty(EndpointEventListener.ENDPOINT_LISTENER_SCOPE)); List<Filter> filters = new ArrayList<>(values.size()); for (String value : values) { @@ -63,14 +94,14 @@ class ConfigDiscovery implements ManagedServiceFactory { } void addListener(ServiceReference<EndpointEventListener> ref, EndpointEventListener listener) { - List<Filter> filters = createFilters(ref); + Collection<Filter> filters = createFilters(ref); if (!filters.isEmpty()) { List<Map.Entry<Filter, EndpointDescription>> matched; synchronized (this) { listenerToFilters.put(listener, filters); matched = findMatches(filters); } - triggerEvent(listener, matched); + triggerEvents(listener, matched); } } @@ -80,41 +111,6 @@ class ConfigDiscovery implements ManagedServiceFactory { } } - private void addDeclaredRemoteService(String pid, Dictionary<String, ?> config) { - EndpointDescription endpoint = new EndpointDescription(PropertyValidator.validate(config)); - EndpointDescription old; - Set<Map.Entry<Filter, EndpointEventListener>> oldMatches, newMatches; - synchronized (this) { - old = endpoints.put(pid, endpoint); - oldMatches = old == null ? null : new HashSet<>(findMatches(old)); - newMatches = new HashSet<>(findMatches(endpoint)); - } - List<Map.Entry<Filter, EndpointEventListener>> added = new ArrayList<>(newMatches); - if (old != null) { - added.removeAll(oldMatches); - List<Map.Entry<Filter, EndpointEventListener>> endmatch = new ArrayList<>(oldMatches); - endmatch.removeAll(newMatches); - List<Map.Entry<Filter, EndpointEventListener>> modified = new ArrayList<>(oldMatches); - modified.removeAll(endmatch); - triggerEvent(new EndpointEvent(EndpointEvent.MODIFIED, endpoint), modified); - triggerEvent(new EndpointEvent(EndpointEvent.MODIFIED_ENDMATCH, old), endmatch); - } - triggerEvent(new EndpointEvent(EndpointEvent.ADDED, endpoint), added); - } - - private void removeServiceDeclaredInConfig(String pid) { - EndpointDescription endpoint; - List<Map.Entry<Filter, EndpointEventListener>> matched; - synchronized (this) { - endpoint = endpoints.remove(pid); - if (endpoint == null) { - return; - } - matched = findMatches(endpoint); - } - triggerEvent(new EndpointEvent(EndpointEvent.REMOVED, endpoint), matched); - } - private List<Map.Entry<Filter, EndpointEventListener>> findMatches(EndpointDescription endpoint) { // called with lock, makes a copy of matched filters/listeners so we can later trigger events without locks List<Map.Entry<Filter, EndpointEventListener>> matched = new ArrayList<>(); @@ -144,14 +140,14 @@ class ConfigDiscovery implements ManagedServiceFactory { return matched; } - private void triggerEvent(EndpointEvent event, List<Map.Entry<Filter, EndpointEventListener>> matched) { + private void triggerEvents(EndpointEvent event, List<Map.Entry<Filter, EndpointEventListener>> matched) { // trigger events without holding a lock for (Map.Entry<Filter, EndpointEventListener> entry : matched) { entry.getValue().endpointChanged(event, entry.getKey().toString()); } } - private void triggerEvent(EndpointEventListener listener, List<Map.Entry<Filter, EndpointDescription>> matched) { + private void triggerEvents(EndpointEventListener listener, List<Map.Entry<Filter, EndpointDescription>> matched) { // trigger events without holding a lock for (Map.Entry<Filter, EndpointDescription> entry : matched) { EndpointEvent event = new EndpointEvent(EndpointEvent.ADDED, entry.getValue());
