tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anat...@apache.org
Subject [3/5] incubator-tamaya git commit: TAMAYA-120: Add Observer for triggering events on certain configuration key ranges
Date Tue, 13 Oct 2015 01:11:45 GMT
TAMAYA-120: Add Observer for triggering events on certain configuration key ranges


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/de48cd2f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/de48cd2f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/de48cd2f

Branch: refs/heads/master
Commit: de48cd2f4663fba8f2a94eec9fccb211ed0ad7ce
Parents: 88c99a1
Author: anatole <anatole@apache.org>
Authored: Tue Oct 13 03:09:08 2015 +0200
Committer: anatole <anatole@apache.org>
Committed: Tue Oct 13 03:09:08 2015 +0200

----------------------------------------------------------------------
 .../apache/tamaya/events/ConfigListener.java    | 11 ++--
 .../tamaya/events/ConfigurationObserver.java    | 54 +++++++++++++-------
 .../internal/DefaultConfigObserverSpi.java      | 47 ++++++-----------
 .../events/internal/LoggingConfigListener.java  | 10 +---
 .../tamaya/events/spi/ConfigObserverSpi.java    | 23 ++++-----
 5 files changed, 65 insertions(+), 80 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/de48cd2f/modules/events/src/main/java/org/apache/tamaya/events/ConfigListener.java
----------------------------------------------------------------------
diff --git a/modules/events/src/main/java/org/apache/tamaya/events/ConfigListener.java b/modules/events/src/main/java/org/apache/tamaya/events/ConfigListener.java
index 1381c38..3608921 100644
--- a/modules/events/src/main/java/org/apache/tamaya/events/ConfigListener.java
+++ b/modules/events/src/main/java/org/apache/tamaya/events/ConfigListener.java
@@ -18,19 +18,14 @@
  */
 package org.apache.tamaya.events;
 
-import org.apache.tamaya.Configuration;
+import org.apache.tamaya.events.delta.ConfigurationChange;
 
 /**
  * Simple observer interface that can be registered using the current {@code ServiceContext}.
  * This class will be called on each configuration change detected in the current environment.
  */
+// @FunctionalInterface
 public interface ConfigListener
-        extends ConfigEventListener<ChangeNotification<Configuration>> {
-
-        /**
-         * Get a regular expression to define, which keys this listener is interested in.
-         * @return
-         */
-        String getKeyExpression();
+        extends ConfigEventListener<ConfigurationChange> {
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/de48cd2f/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationObserver.java
----------------------------------------------------------------------
diff --git a/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationObserver.java
b/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationObserver.java
index ffde3a3..1eafc0c 100644
--- a/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationObserver.java
+++ b/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationObserver.java
@@ -24,9 +24,10 @@ import org.apache.tamaya.spi.ServiceContextManager;
 
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Set;
 
 /**
- * Created by Anatole on 04.10.2015.
+ * Singleton accessor for managing {@link ConfigListener} instances and mappings.
  */
 public class ConfigurationObserver {
 
@@ -44,48 +45,63 @@ public class ConfigurationObserver {
 
 
     /**
-     * Add a listener for observing change events on {@link org.apache.tamaya.Configuration}.
References of this
-     * component to the listeners must be managed as weak references.
+     * Add key expressions for generating ConfigurationChange events.
      *
-     * @param l the listener not null.
+     * @param keys             the keys to be observed for changes.
      */
-    public static <T> void addListener(ConfigListener l) {
+    public static <T> void addObservedKeys(Collection<String> keys) {
         if (SPI == null) {
             throw new ConfigException("No SPI registered for " +
                     ConfigurationObserver.class.getName());
         }
-        SPI.addListener(l);
+        SPI.addObservedKeys(keys);
     }
 
     /**
-     * Add a listener for observing change events on {@link org.apache.tamaya.spi.PropertySource}.
References of this
-     * component to the listeners must be managed as weak references.
+     * Add key expressions for generating ConfigurationChange events.
      *
-     * @param l the listener not null.
+     * @param keys             the keys to be observed for changes.
      */
-    public static <T> void removeListener(ConfigListener l) {
+    public static <T> void addObservedKeys(String... keys) {
         if (SPI == null) {
             throw new ConfigException("No SPI registered for " +
                     ConfigurationObserver.class.getName());
         }
-        SPI.removeListener(l);
+        SPI.addObservedKeys(Arrays.asList(keys));
     }
 
     /**
-     * Access all registered ConfigEventListeners listening to the given event key(s).
+     * Removes key expressions for generating ConfigurationChange events.
      *
-     * @return a list with the listeners found, never null.
+     * @param keys the keys to be observed for changes.
      */
-    public static Collection<ConfigListener> getListeners(Collection<String>
keys) {
-        return SPI.getListeners(keys);
+    public static <T> void removeObservedKeys(Collection<String> keys) {
+        if (SPI == null) {
+            throw new ConfigException("No SPI registered for " +
+                    ConfigurationObserver.class.getName());
+        }
+        SPI.removeObservedKeys(keys);
+    }
+
+    /**
+     * Removes key expressions for generating ConfigurationChange events.
+     *
+     * @param keys the keys to be observed for changes.
+     */
+    public static <T> void removeObservedKeys(String... keys) {
+        if (SPI == null) {
+            throw new ConfigException("No SPI registered for " +
+                    ConfigurationObserver.class.getName());
+        }
+        SPI.removeObservedKeys(Arrays.asList(keys));
     }
 
     /**
-     * Access all registered ConfigEventListeners listening to the given event key(s).
+     * Get all registered key expressions for generating ConfigurationChange events.
      *
-     * @return a list with the listeners found, never null.
+     * @return  set with the keys found, never null.
      */
-    public static Collection<ConfigListener> getListeners(String... keys) {
-        return SPI.getListeners(Arrays.asList(keys));
+    public static Set<String> getObservedKeys() {
+        return SPI.getObservedKeys();
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/de48cd2f/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigObserverSpi.java
----------------------------------------------------------------------
diff --git a/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigObserverSpi.java
b/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigObserverSpi.java
index 1340f2c..a753040 100644
--- a/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigObserverSpi.java
+++ b/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigObserverSpi.java
@@ -19,6 +19,7 @@
 package org.apache.tamaya.events.internal;
 
 import org.apache.tamaya.ConfigurationProvider;
+import org.apache.tamaya.events.ConfigEventManager;
 import org.apache.tamaya.events.ConfigListener;
 import org.apache.tamaya.events.FrozenConfiguration;
 import org.apache.tamaya.events.delta.ConfigurationChange;
@@ -29,6 +30,7 @@ import org.apache.tamaya.spi.ServiceContextManager;
 import java.beans.PropertyChangeEvent;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -52,7 +54,7 @@ public class DefaultConfigObserverSpi implements ConfigObserverSpi {
 
     private static final Logger LOG = Logger.getLogger(DefaultConfigObserverSpi.class.getName());
 
-    private Map<String, List<ConfigListener>> listenerMap = new ConcurrentHashMap<>();
+    private Set<String> keys = new HashSet<>();
 
     private Timer timer = new Timer("ConfigurationObserver", true);
 
@@ -69,8 +71,9 @@ public class DefaultConfigObserverSpi implements ConfigObserverSpi {
      */
     public DefaultConfigObserverSpi() {
         try {
+            // Load and register ConfigListener from the current ServiceContext
             for (ConfigListener l : ServiceContextManager.getServiceContext().getServices(ConfigListener.class))
{
-                addListener(l);
+                ConfigEventManager.addListener(l);
             }
         } catch (Exception e) {
             LOG.log(Level.WARNING, "Failed to load configured listeners.", e);
@@ -98,49 +101,29 @@ public class DefaultConfigObserverSpi implements ConfigObserverSpi {
         }
         Set<ConfigListener> affected = new HashSet<>();
         for(PropertyChangeEvent evt: changes.getChanges()) {
-            for (Map.Entry<String, List<ConfigListener>> en : listenerMap.entrySet())
{
-                if (evt.getPropertyName().matches(en.getKey())) {
-                    for(ConfigListener l:en.getValue()){
-                        affected.add(l);
-                    }
+            for (String key : keys) {
+                if (evt.getPropertyName().matches(key)) {
+                    ConfigEventManager.fireEvent(changes);
+                    return;
                 }
             }
         }
-        for(ConfigListener l:affected){
-            publisher.submit(new PublishConfigChangeTask(l, changes));
-        }
     }
 
     @Override
-    public synchronized <T> void addListener(final ConfigListener l) {
-        List<ConfigListener> items = listenerMap.get(l.getKeyExpression());
-        if (items == null) {
-            items = new ArrayList<>();
-            listenerMap.put(l.getKeyExpression(), items);
-        }
-        items.add(l);
+    public synchronized <T> void addObservedKeys(Collection<String> keys) {
+        this.keys.addAll(keys);
     }
 
     @Override
-    public synchronized <T> void removeListener(ConfigListener l) {
-        List<ConfigListener> items = listenerMap.get(l.getKeyExpression());
-        if (items != null) {
-            items.remove(l);
-        }
+    public synchronized <T> void removeObservedKeys(Collection<String> keys)
{
+        this.keys.removeAll(keys);
     }
 
 
     @Override
-    public synchronized Collection<ConfigListener> getListeners(Collection<String>
keys) {
-        List<ConfigListener> listeners = new ArrayList<>();
-        for (String key : keys) {
-            for (Map.Entry<String, List<ConfigListener>> en : listenerMap.entrySet())
{
-                if (key.matches(en.getKey())) {
-                    listeners.addAll(en.getValue());
-                }
-            }
-        }
-        return listeners;
+    public synchronized Set<String> getObservedKeys() {
+        return Collections.unmodifiableSet(this.keys);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/de48cd2f/modules/events/src/main/java/org/apache/tamaya/events/internal/LoggingConfigListener.java
----------------------------------------------------------------------
diff --git a/modules/events/src/main/java/org/apache/tamaya/events/internal/LoggingConfigListener.java
b/modules/events/src/main/java/org/apache/tamaya/events/internal/LoggingConfigListener.java
index 059d1b5..43b2700 100644
--- a/modules/events/src/main/java/org/apache/tamaya/events/internal/LoggingConfigListener.java
+++ b/modules/events/src/main/java/org/apache/tamaya/events/internal/LoggingConfigListener.java
@@ -18,9 +18,8 @@
  */
 package org.apache.tamaya.events.internal;
 
-import org.apache.tamaya.Configuration;
-import org.apache.tamaya.events.ChangeNotification;
 import org.apache.tamaya.events.ConfigListener;
+import org.apache.tamaya.events.delta.ConfigurationChange;
 
 import java.util.logging.Logger;
 
@@ -32,12 +31,7 @@ public class LoggingConfigListener implements ConfigListener{
     private static final Logger LOG = Logger.getLogger(LoggingConfigListener.class.getName());
 
     @Override
-    public String getKeyExpression() {
-        return ".*";
-    }
-
-    @Override
-    public void onConfigEvent(ChangeNotification<Configuration> event) {
+    public void onConfigEvent(ConfigurationChange event) {
         LOG.info("Configuration changed: " + event);
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/de48cd2f/modules/events/src/main/java/org/apache/tamaya/events/spi/ConfigObserverSpi.java
----------------------------------------------------------------------
diff --git a/modules/events/src/main/java/org/apache/tamaya/events/spi/ConfigObserverSpi.java
b/modules/events/src/main/java/org/apache/tamaya/events/spi/ConfigObserverSpi.java
index 17f5ccb..95eb6e7 100644
--- a/modules/events/src/main/java/org/apache/tamaya/events/spi/ConfigObserverSpi.java
+++ b/modules/events/src/main/java/org/apache/tamaya/events/spi/ConfigObserverSpi.java
@@ -18,9 +18,8 @@
  */
 package org.apache.tamaya.events.spi;
 
-import org.apache.tamaya.events.ConfigListener;
-
 import java.util.Collection;
+import java.util.Set;
 
 /**
  * SPI interface to implement the {@link org.apache.tamaya.events.ConfigurationObserver}
singleton.
@@ -30,27 +29,25 @@ import java.util.Collection;
  */
 public interface ConfigObserverSpi {
     /**
-     * Add a listener for observing events. References of this
-     * component to the listeners must be managed as weak references.
+     * Add key expressions for generating ConfigurationChange events.
      *
-     * @param l the listener not null.
+     * @param keys             the keys to be observed for changes.
      */
-    <T> void addListener(ConfigListener l);
-
+    <T> void addObservedKeys(Collection<String> keys);
 
     /**
-     * Removes a listener for observing events.
+     * Remove key expressions for generating ConfigurationChange events.
      *
-     * @param l the listener not null.
+     * @param keys the keys to be observed for changes.
      */
-    <T> void removeListener(ConfigListener l);
+    <T> void removeObservedKeys(Collection<String> keys);
 
     /**
-     * Access all registered ConfigEventListeners listening to the given event type.
+     * Get all registered key expressions for generating ConfigurationChange events.
      *
-     * @return a list with the listeners found, never null.
+     * @return a set with the keys found, never null.
      */
-    Collection<ConfigListener> getListeners(Collection<String> keys);
+    Set<String> getObservedKeys();
 
     /**
      * Get the current check period to check for configuration changes.


Mime
View raw message