TAMAYA-115: Implemented multi classpath environment PropertySourceProvider, with relocation capability.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/commit/fb52d6cb Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/fb52d6cb Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/fb52d6cb Branch: refs/heads/master Commit: fb52d6cb24417d22c96f02f376e915a001f9d2e7 Parents: b9e708a Author: anatole <anat...@apache.org> Authored: Tue Oct 6 09:48:40 2015 +0200 Committer: Oliver B. Fischer <ple...@apache.org> Committed: Tue Sep 27 00:18:31 2016 +0200 ---------------------------------------------------------------------- .../tamaya/events/ChangeNotification.java | 66 +++++++ .../org/apache/tamaya/events/ConfigEvent.java | 93 ---------- .../tamaya/events/ConfigEventListener.java | 2 +- .../tamaya/events/ConfigEventManager.java | 107 +++++++++++ .../apache/tamaya/events/ConfigListener.java | 36 ++++ .../tamaya/events/ConfigurationObserver.java | 91 ++++++++++ .../events/delta/ConfigurationChange.java | 9 +- .../delta/ConfigurationContextChange.java | 10 +- .../events/delta/PropertySourceChange.java | 9 +- .../ObservingPropertySourceProvider.java | 4 +- .../internal/DefaultConfigEventManagerSpi.java | 110 ++++++++++++ .../events/internal/DefaultConfigEventSpi.java | 100 ----------- .../internal/DefaultConfigObserverSpi.java | 179 +++++++++++++++++++ .../events/internal/LoggingConfigListener.java | 43 +++++ .../org/apache/tamaya/events/package-info.java | 2 +- .../events/spi/ConfigEventManagerSpi.java | 63 +++++++ .../tamaya/events/spi/ConfigEventSpi.java | 54 ------ .../tamaya/events/spi/ConfigObserverSpi.java | 74 ++++++++ .../apache/tamaya/events/spi/package-info.java | 2 +- .../org.apache.tamaya.events.ConfigListener | 18 ++ ...ache.tamaya.events.spi.ConfigEventManagerSpi | 19 ++ ...g.apache.tamaya.events.spi.ConfigObserverSpi | 19 ++ .../tamaya/events/ConfigEventManagerTest.java | 63 +++++++ .../apache/tamaya/events/ConfigEventTest.java | 63 ------- .../events/delta/ConfigurationChangeTest.java | 14 +- .../events/delta/PropertySourceChangeTest.java | 6 +- .../DefaultConfigEventManagerSpiTest.java | 65 +++++++ .../internal/DefaultConfigEventSpiTest.java | 65 ------- 28 files changed, 983 insertions(+), 403 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/java/org/apache/tamaya/events/ChangeNotification.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/events/ChangeNotification.java b/src/main/java/org/apache/tamaya/events/ChangeNotification.java new file mode 100644 index 0000000..73a4fa9 --- /dev/null +++ b/src/main/java/org/apache/tamaya/events/ChangeNotification.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.events; + +import java.beans.PropertyChangeEvent; +import java.util.Collection; + + +/** + * Event that contains a set current changes that were applied or could be applied. + * @param <T> the event type + */ +public interface ChangeNotification<T>{ + + /** + * Get the underlying property provider/configuration. + * @return the underlying property provider/configuration, never null. + */ + T getResource(); + + /** + * Get the version relative to the observed resource. The version is required to be unique for + * each change emmitted for a resource. There is no further requirement how this uniqueness is + * modelled, so returning a UUID is cometely valid. + * @return the base version. + */ + String getVersion(); + + /** + * Get the timestamp in millis from the current epoch. it is expected that the timestamp and the version are unique to + * identify a changeset. + * @return the timestamp, when this changeset was created. + */ + long getTimestamp(); + + /** + * Get the changes recorded. + * @return the recorded changes, never null. + */ + Collection<PropertyChangeEvent> getChanges(); + + /** + * Checks if the given key is added, or updated OR removed. + * @param key the target key (can also be a regular expression), that matches the requested keys, + * not null. + * @return true, if the given key was added, or updated BUT NOT removed. + */ + boolean isKeyAffected(String key); + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/java/org/apache/tamaya/events/ConfigEvent.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/events/ConfigEvent.java b/src/main/java/org/apache/tamaya/events/ConfigEvent.java deleted file mode 100644 index 3ed2ba2..0000000 --- a/src/main/java/org/apache/tamaya/events/ConfigEvent.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.events; - -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.events.spi.ConfigEventSpi; -import org.apache.tamaya.spi.ServiceContextManager; - -/** - * Singleton accessor for accessing the event support component that distributes change events of - * {@link org.apache.tamaya.spi.PropertySource} and {@link org.apache.tamaya.Configuration}. - */ -public final class ConfigEvent { - /** - * The backing SPI. - */ - private static final ConfigEventSpi SPI = ServiceContextManager.getServiceContext() - .getService(ConfigEventSpi.class); - - /** - * Private singleton constructor. - */ - private ConfigEvent() { - } - - /** - * 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. - * - * @param l the listener not null. - */ - public static <T> void addListener(ConfigEventListener<T> l) { - if (SPI == null) { - throw new ConfigException("No SPI registered for " + - ConfigEvent.class.getName()); - } - SPI.addListener(l); - } - - /** - * 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. - * - * @param l the listener not null. - */ - public static <T> void removeListener(ConfigEventListener<T> l) { - if (SPI == null) { - throw new ConfigException("No SPI registered for " + - ConfigEvent.class.getName()); - } - SPI.removeListener(l); - } - - /** - * Publishes sn event to all interested listeners. - * - * @param event the event, not null. - */ - public static void fireEvent(Object event) { - fireEvent(event, (Class)event.getClass()); - } - - /** - * Publishes a {@link org.apache.tamaya.events.delta.ConfigurationChange} to all interested listeners. - * - * @param event the event, not null. - * @param eventType the event type, the vent may be a subclass. - */ - public static <T> void fireEvent(T event, Class<T> eventType) { - if (SPI == null) { - throw new ConfigException("No SPI registered for " + - ConfigEvent.class.getName()); - } - SPI.fireEvent(event, eventType); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/java/org/apache/tamaya/events/ConfigEventListener.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/events/ConfigEventListener.java b/src/main/java/org/apache/tamaya/events/ConfigEventListener.java index e2c848c..dde5175 100644 --- a/src/main/java/org/apache/tamaya/events/ConfigEventListener.java +++ b/src/main/java/org/apache/tamaya/events/ConfigEventListener.java @@ -22,7 +22,7 @@ package org.apache.tamaya.events; * Interface to be implemented for listening on changes on {@link org.apache.tamaya.Configuration} instances. * @param <T> the type listened to. */ -@FunctionalInterface +//@FunctionalInterface public interface ConfigEventListener<T> { /** * Called if an event occurred. http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/java/org/apache/tamaya/events/ConfigEventManager.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/events/ConfigEventManager.java b/src/main/java/org/apache/tamaya/events/ConfigEventManager.java new file mode 100644 index 0000000..1095d48 --- /dev/null +++ b/src/main/java/org/apache/tamaya/events/ConfigEventManager.java @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.events; + +import org.apache.tamaya.ConfigException; +import org.apache.tamaya.events.spi.ConfigEventManagerSpi; +import org.apache.tamaya.spi.ServiceContextManager; + +import java.util.Collection; + +/** + * Singleton accessor for accessing the event support component that distributes change events of + * {@link org.apache.tamaya.spi.PropertySource} and {@link org.apache.tamaya.Configuration}. + */ +public final class ConfigEventManager { + /** + * The backing SPI. + */ + private static final ConfigEventManagerSpi SPI = ServiceContextManager.getServiceContext() + .getService(ConfigEventManagerSpi.class); + + /** + * Private singleton constructor. + */ + private ConfigEventManager() { + } + + /** + * 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. + * + * @param l the listener not null. + */ + public static <T> void addListener(ConfigEventListener<T> l) { + if (SPI == null) { + throw new ConfigException("No SPI registered for " + + ConfigEventManager.class.getName()); + } + SPI.addListener(l); + } + + /** + * 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. + * + * @param l the listener not null. + */ + public static <T> void removeListener(ConfigEventListener<T> l) { + if (SPI == null) { + throw new ConfigException("No SPI registered for " + + ConfigEventManager.class.getName()); + } + SPI.removeListener(l); + } + + /** + * Access all registered ConfigEventListeners listening to the given event type. + * @param type the event type + * @param <T> type param + * @return a list with the listeners found, never null. + */ + public static <T> + Collection<? extends ConfigEventListener<T>> getListeneters(Class<T> type) { + return SPI.getListeners(type); + } + + + /** + * Publishes sn event to all interested listeners. + * + * @param event the event, not null. + */ + public static void fireEvent(Object event) { + fireEvent(event, (Class)event.getClass()); + } + + /** + * Publishes a {@link org.apache.tamaya.events.delta.ConfigurationChange} to all interested listeners. + * + * @param event the event, not null. + * @param eventType the event type, the vent may be a subclass. + */ + public static <T> void fireEvent(T event, Class<T> eventType) { + if (SPI == null) { + throw new ConfigException("No SPI registered for " + + ConfigEventManager.class.getName()); + } + SPI.fireEvent(event, eventType); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/java/org/apache/tamaya/events/ConfigListener.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/events/ConfigListener.java b/src/main/java/org/apache/tamaya/events/ConfigListener.java new file mode 100644 index 0000000..1381c38 --- /dev/null +++ b/src/main/java/org/apache/tamaya/events/ConfigListener.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.events; + +import org.apache.tamaya.Configuration; + +/** + * 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. + */ +public interface ConfigListener + extends ConfigEventListener<ChangeNotification<Configuration>> { + + /** + * Get a regular expression to define, which keys this listener is interested in. + * @return + */ + String getKeyExpression(); + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/java/org/apache/tamaya/events/ConfigurationObserver.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/events/ConfigurationObserver.java b/src/main/java/org/apache/tamaya/events/ConfigurationObserver.java new file mode 100644 index 0000000..ffde3a3 --- /dev/null +++ b/src/main/java/org/apache/tamaya/events/ConfigurationObserver.java @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.events; + +import org.apache.tamaya.ConfigException; +import org.apache.tamaya.events.spi.ConfigObserverSpi; +import org.apache.tamaya.spi.ServiceContextManager; + +import java.util.Arrays; +import java.util.Collection; + +/** + * Created by Anatole on 04.10.2015. + */ +public class ConfigurationObserver { + + /** + * Private singleton constructor. + */ + private ConfigurationObserver() { + } + + /** + * The backing SPI. + */ + private static final ConfigObserverSpi SPI = ServiceContextManager.getServiceContext() + .getService(ConfigObserverSpi.class); + + + /** + * 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. + * + * @param l the listener not null. + */ + public static <T> void addListener(ConfigListener l) { + if (SPI == null) { + throw new ConfigException("No SPI registered for " + + ConfigurationObserver.class.getName()); + } + SPI.addListener(l); + } + + /** + * 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. + * + * @param l the listener not null. + */ + public static <T> void removeListener(ConfigListener l) { + if (SPI == null) { + throw new ConfigException("No SPI registered for " + + ConfigurationObserver.class.getName()); + } + SPI.removeListener(l); + } + + /** + * Access all registered ConfigEventListeners listening to the given event key(s). + * + * @return a list with the listeners found, never null. + */ + public static Collection<ConfigListener> getListeners(Collection<String> keys) { + return SPI.getListeners(keys); + } + + /** + * Access all registered ConfigEventListeners listening to the given event key(s). + * + * @return a list with the listeners found, never null. + */ + public static Collection<ConfigListener> getListeners(String... keys) { + return SPI.getListeners(Arrays.asList(keys)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/java/org/apache/tamaya/events/delta/ConfigurationChange.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/events/delta/ConfigurationChange.java b/src/main/java/org/apache/tamaya/events/delta/ConfigurationChange.java index 5f26eaa..41c6ba1 100644 --- a/src/main/java/org/apache/tamaya/events/delta/ConfigurationChange.java +++ b/src/main/java/org/apache/tamaya/events/delta/ConfigurationChange.java @@ -19,6 +19,7 @@ package org.apache.tamaya.events.delta; import org.apache.tamaya.Configuration; +import org.apache.tamaya.events.ChangeNotification; import org.apache.tamaya.events.FrozenConfiguration; import java.beans.PropertyChangeEvent; @@ -36,7 +37,7 @@ import java.util.UUID; * * Created by Anatole on 22.10.2014. */ -public final class ConfigurationChange implements Serializable{ +public final class ConfigurationChange implements ChangeNotification<Configuration>, Serializable{ private static final long serialVersionUID = 1L; /** The base property provider/configuration. */ @@ -78,7 +79,7 @@ public final class ConfigurationChange implements Serializable{ * Get the underlying property provider/configuration. * @return the underlying property provider/configuration, never null. */ - public Configuration getConfiguration(){ + public Configuration getResource(){ return this.configuration; } @@ -103,7 +104,7 @@ public final class ConfigurationChange implements Serializable{ * Get the changes recorded. * @return the recorded changes, never null. */ - public Collection<PropertyChangeEvent> getEvents(){ + public Collection<PropertyChangeEvent> getChanges(){ return Collections.unmodifiableCollection(this.changes.values()); } @@ -190,7 +191,7 @@ public final class ConfigurationChange implements Serializable{ * @param key the target key, not null. * @return true, if the given key was added, or updated BUT NOT removed. */ - public boolean containsKey(String key) { + public boolean isKeyAffected(String key) { PropertyChangeEvent change = this.changes.get(key); return change != null && change.getNewValue() != null; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/java/org/apache/tamaya/events/delta/ConfigurationContextChange.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/events/delta/ConfigurationContextChange.java b/src/main/java/org/apache/tamaya/events/delta/ConfigurationContextChange.java index 2378418..0aef2fd 100644 --- a/src/main/java/org/apache/tamaya/events/delta/ConfigurationContextChange.java +++ b/src/main/java/org/apache/tamaya/events/delta/ConfigurationContextChange.java @@ -115,7 +115,7 @@ public final class ConfigurationContextChange implements Serializable{ List<PropertySource> result = new ArrayList<>(); for (PropertySourceChange pc : this.changedPropertySources) { if (pc.getChangeType() == ChangeType.DELETED) { - result.add(pc.getPropertySource()); + result.add(pc.getResource()); } } return result; @@ -131,7 +131,7 @@ public final class ConfigurationContextChange implements Serializable{ List<PropertySource> result = new ArrayList<>(); for (PropertySourceChange pc : this.changedPropertySources) { if (pc.getChangeType() == ChangeType.NEW) { - result.add(pc.getPropertySource()); + result.add(pc.getResource()); } } return result; @@ -147,7 +147,7 @@ public final class ConfigurationContextChange implements Serializable{ List<PropertySource> result = new ArrayList<>(); for (PropertySourceChange pc : this.changedPropertySources) { if (pc.getChangeType() == ChangeType.UPDATED) { - result.add(pc.getPropertySource()); + result.add(pc.getResource()); } } return result; @@ -162,8 +162,8 @@ public final class ConfigurationContextChange implements Serializable{ */ public boolean isAffected(PropertySource propertySource) { for (PropertySourceChange ps : this.changedPropertySources) { - if (ps.getPropertySource() == propertySource || - ps.getPropertySource().getName().equals(propertySource.getName())) { + if (ps.getResource() == propertySource || + ps.getResource().getName().equals(propertySource.getName())) { return true; } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/java/org/apache/tamaya/events/delta/PropertySourceChange.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/events/delta/PropertySourceChange.java b/src/main/java/org/apache/tamaya/events/delta/PropertySourceChange.java index 4e9acda..330f3b0 100644 --- a/src/main/java/org/apache/tamaya/events/delta/PropertySourceChange.java +++ b/src/main/java/org/apache/tamaya/events/delta/PropertySourceChange.java @@ -18,6 +18,7 @@ */ package org.apache.tamaya.events.delta; +import org.apache.tamaya.events.ChangeNotification; import org.apache.tamaya.events.FrozenPropertySource; import org.apache.tamaya.spi.PropertySource; @@ -36,7 +37,7 @@ import java.util.UUID; * * Created by Anatole on 22.10.2014. */ -public final class PropertySourceChange implements Serializable{ +public final class PropertySourceChange implements ChangeNotification<PropertySource>, Serializable{ private static final long serialVersionUID = 1L; /** The base property provider/configuration. */ @@ -80,7 +81,7 @@ public final class PropertySourceChange implements Serializable{ * Get the underlying property provider/configuration. * @return the underlying property provider/configuration, or null, if the change instance was deserialized. */ - public PropertySource getPropertySource(){ + public PropertySource getResource(){ return this.propertySource; } @@ -105,7 +106,7 @@ public final class PropertySourceChange implements Serializable{ * Get the changes recorded. * @return the recorded changes, never null. */ - public Collection<PropertyChangeEvent> getEvents(){ + public Collection<PropertyChangeEvent> getChanges(){ return Collections.unmodifiableCollection(this.changes.values()); } @@ -191,7 +192,7 @@ public final class PropertySourceChange implements Serializable{ * @param key the target key, not null. * @return true, if the given key was added, or updated BUT NOT removed. */ - public boolean containsKey(String key) { + public boolean isKeyAffected(String key) { PropertyChangeEvent change = this.changes.get(key); return change != null && change.getNewValue() != null; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/java/org/apache/tamaya/events/folderobserver/ObservingPropertySourceProvider.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/events/folderobserver/ObservingPropertySourceProvider.java b/src/main/java/org/apache/tamaya/events/folderobserver/ObservingPropertySourceProvider.java index 86f68b9..f05c98c 100644 --- a/src/main/java/org/apache/tamaya/events/folderobserver/ObservingPropertySourceProvider.java +++ b/src/main/java/org/apache/tamaya/events/folderobserver/ObservingPropertySourceProvider.java @@ -19,7 +19,7 @@ package org.apache.tamaya.events.folderobserver; import org.apache.tamaya.ConfigException; -import org.apache.tamaya.events.ConfigEvent; +import org.apache.tamaya.events.ConfigEventManager; import org.apache.tamaya.events.delta.ConfigurationContextChange; import org.apache.tamaya.events.delta.ConfigurationContextChangeBuilder; import org.apache.tamaya.format.ConfigurationData; @@ -212,7 +212,7 @@ public class ObservingPropertySourceProvider implements PropertySourceProvider, } ConfigurationContextChange changeEvent = b.build(); LOG.fine("Trigger Config Context Change: " + changeEvent); - ConfigEvent.fireEvent(changeEvent); + ConfigEventManager.fireEvent(changeEvent); } @Override http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/java/org/apache/tamaya/events/internal/DefaultConfigEventManagerSpi.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/events/internal/DefaultConfigEventManagerSpi.java b/src/main/java/org/apache/tamaya/events/internal/DefaultConfigEventManagerSpi.java new file mode 100644 index 0000000..6773c7d --- /dev/null +++ b/src/main/java/org/apache/tamaya/events/internal/DefaultConfigEventManagerSpi.java @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.events.internal; + +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.events.ConfigEventListener; +import org.apache.tamaya.events.spi.ConfigEventManagerSpi; +import org.apache.tamaya.spi.ServiceContextManager; + +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Default implementation of {@link DefaultConfigEventManagerSpi} just forwarding all + * events synchronously to the listeners. + */ +public class DefaultConfigEventManagerSpi implements ConfigEventManagerSpi { + + private static final Logger LOG = Logger.getLogger(DefaultConfigEventManagerSpi.class.getName()); + + private Map<Type, List<ConfigEventListener<?>>> listenerMap = new ConcurrentHashMap<>(); + + /** + * Constructor. Also loads all registered listeners. + */ + public DefaultConfigEventManagerSpi() { + try { + for (ConfigEventListener<?> l : ServiceContextManager.getServiceContext().getServices(ConfigEventListener.class)) { + try { + addListener(l); + } catch (Exception e) { + LOG.log(Level.WARNING, "Failed to load configured listener: " + l.getClass().getName(), e); + } + } + } catch (Exception e) { + LOG.log(Level.WARNING, "Failed to load configured listeners.", e); + } + } + + @Override + public <T> void addListener(ConfigEventListener<T> l) { + Type type = TypeLiteral.getGenericInterfaceTypeParameters(l.getClass(), ConfigEventListener.class)[0]; + List<ConfigEventListener<?>> listeners = listenerMap.get(type); + if (listeners == null) { + listeners = Collections.synchronizedList(new ArrayList<ConfigEventListener<?>>()); + listenerMap.put(type, listeners); + } + synchronized (listeners) { + if (!listeners.contains(l)) { + listeners.add(l); + } + } + } + + @Override + public <T> void removeListener(ConfigEventListener<T> l) { + Type type = TypeLiteral.getGenericInterfaceTypeParameters(l.getClass(), ConfigEventListener.class)[0]; + List<ConfigEventListener<?>> listeners = listenerMap.get(type); + if (listeners != null) { + synchronized (listeners) { + listeners.remove(l); + } + } + } + + @Override + public <T> void fireEvent(T event, Class<T> eventType) { + List<ConfigEventListener<?>> listeners = listenerMap.get(eventType); + if (listeners != null) { + synchronized (listeners) { + for (ConfigEventListener l : listeners) { + l.onConfigEvent(event); + } + } + } + } + + @Override + public <T> Collection<ConfigEventListener<T>> getListeners(Class<T> eventType) { + List<ConfigEventListener<?>> listeners = + listenerMap.get(eventType); + if (listeners != null) { + return Collection.class.cast(listeners); + } + return Collections.emptyList(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/java/org/apache/tamaya/events/internal/DefaultConfigEventSpi.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/events/internal/DefaultConfigEventSpi.java b/src/main/java/org/apache/tamaya/events/internal/DefaultConfigEventSpi.java deleted file mode 100644 index 0032c4d..0000000 --- a/src/main/java/org/apache/tamaya/events/internal/DefaultConfigEventSpi.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.events.internal; - -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.events.ConfigEventListener; -import org.apache.tamaya.events.spi.ConfigEventSpi; -import org.apache.tamaya.spi.ServiceContextManager; - -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Default implementation of {@link DefaultConfigEventSpi} just forwarding all - * events synchronously to the listeners. - */ -public class DefaultConfigEventSpi implements ConfigEventSpi { - - private static final Logger LOG = Logger.getLogger(DefaultConfigEventSpi.class.getName()); - - private Map<Type, List<ConfigEventListener>> listenerMap = new ConcurrentHashMap<>(); - - - /** - * Constructor. Also loads all registered listeners. - */ - public DefaultConfigEventSpi() { - try { - for (ConfigEventListener<?> l : ServiceContextManager.getServiceContext().getServices(ConfigEventListener.class)) { - try { - addListener(l); - } catch (Exception e) { - LOG.log(Level.WARNING, "Failed to load configured listener: " + l.getClass().getName(), e); - } - } - } catch (Exception e) { - LOG.log(Level.WARNING, "Failed to load configured listeners.", e); - } - } - - @Override - public <T> void addListener(ConfigEventListener<T> l) { - Type type = TypeLiteral.getGenericInterfaceTypeParameters(l.getClass(), ConfigEventListener.class)[0]; - List<ConfigEventListener> listeners = listenerMap.get(type); - if (listeners == null) { - listeners = Collections.synchronizedList(new ArrayList<ConfigEventListener>()); - listenerMap.put(type, listeners); - } - synchronized (listeners) { - if (!listeners.contains(l)) { - listeners.add(l); - } - } - } - - @Override - public <T> void removeListener(ConfigEventListener<T> l) { - Type type = TypeLiteral.getGenericInterfaceTypeParameters(l.getClass(), ConfigEventListener.class)[0]; - List<ConfigEventListener> listeners = listenerMap.get(type); - if (listeners != null) { - synchronized (listeners) { - listeners.remove(l); - } - } - } - - @Override - public <T> void fireEvent(T event, Class<T> eventType) { - List<ConfigEventListener> listeners = listenerMap.get(eventType); - if (listeners != null) { - synchronized (listeners) { - for (ConfigEventListener l : listeners) { - l.onConfigEvent(event); - } - } - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/java/org/apache/tamaya/events/internal/DefaultConfigObserverSpi.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/events/internal/DefaultConfigObserverSpi.java b/src/main/java/org/apache/tamaya/events/internal/DefaultConfigObserverSpi.java new file mode 100644 index 0000000..1340f2c --- /dev/null +++ b/src/main/java/org/apache/tamaya/events/internal/DefaultConfigObserverSpi.java @@ -0,0 +1,179 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.events.internal; + +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.events.ConfigListener; +import org.apache.tamaya.events.FrozenConfiguration; +import org.apache.tamaya.events.delta.ConfigurationChange; +import org.apache.tamaya.events.delta.ConfigurationChangeBuilder; +import org.apache.tamaya.events.spi.ConfigObserverSpi; +import org.apache.tamaya.spi.ServiceContextManager; + +import java.beans.PropertyChangeEvent; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Default implementation of {@link org.apache.tamaya.events.spi.ConfigObserverSpi} just forwarding all + * events synchronously to the listeners. + */ +public class DefaultConfigObserverSpi implements ConfigObserverSpi { + + private static final long START_DELAY = 5000L; + + private static final Logger LOG = Logger.getLogger(DefaultConfigObserverSpi.class.getName()); + + private Map<String, List<ConfigListener>> listenerMap = new ConcurrentHashMap<>(); + + private Timer timer = new Timer("ConfigurationObserver", true); + + private long checkPeriod = 2000L; + + private volatile FrozenConfiguration lastConfig; + + private ExecutorService publisher = Executors.newCachedThreadPool(); + + private volatile boolean running; + + /** + * Constructor. Also loads all registered listeners. + */ + public DefaultConfigObserverSpi() { + try { + for (ConfigListener l : ServiceContextManager.getServiceContext().getServices(ConfigListener.class)) { + addListener(l); + } + } catch (Exception e) { + LOG.log(Level.WARNING, "Failed to load configured listeners.", e); + } + timer.scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + if(running) { + checkConfigurationUpdate(); + } + } + }, START_DELAY, checkPeriod); + } + + public void checkConfigurationUpdate() { + LOG.finest("Checking configuration for changes..."); + FrozenConfiguration newConfig = FrozenConfiguration.of(ConfigurationProvider.getConfiguration()); + ConfigurationChange changes = null; + if(lastConfig==null){ + changes = ConfigurationChangeBuilder.of(newConfig).putAll(newConfig.getProperties()) + .build(); + }else{ + changes = ConfigurationChangeBuilder.of(lastConfig).addChanges(newConfig) + .build(); + } + 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(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); + } + + @Override + public synchronized <T> void removeListener(ConfigListener l) { + List<ConfigListener> items = listenerMap.get(l.getKeyExpression()); + if (items != null) { + items.remove(l); + } + } + + + @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; + } + + @Override + public long getCheckPeriod() { + return checkPeriod; + } + + @Override + public boolean isRunning(){ + return running; + } + + @Override + public void enableObservation(boolean enable){ + this.running = true; + } + + /** + * Tasks to inform observers on detected configuration changes. + */ + private static final class PublishConfigChangeTask implements Runnable{ + + private ConfigListener l; + private ConfigurationChange changes; + + public PublishConfigChangeTask(ConfigListener l, ConfigurationChange changes) { + this.l = Objects.requireNonNull(l); + this.changes = Objects.requireNonNull(changes); + } + + @Override + public void run() { + l.onConfigEvent(changes); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/java/org/apache/tamaya/events/internal/LoggingConfigListener.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/events/internal/LoggingConfigListener.java b/src/main/java/org/apache/tamaya/events/internal/LoggingConfigListener.java new file mode 100644 index 0000000..059d1b5 --- /dev/null +++ b/src/main/java/org/apache/tamaya/events/internal/LoggingConfigListener.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.events.internal; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.events.ChangeNotification; +import org.apache.tamaya.events.ConfigListener; + +import java.util.logging.Logger; + +/** + * Simple ConfigListener that simply logs any detected config changes to INFO level. + */ +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) { + LOG.info("Configuration changed: " + event); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/java/org/apache/tamaya/events/package-info.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/events/package-info.java b/src/main/java/org/apache/tamaya/events/package-info.java index 1cba8ce..33f5f25 100644 --- a/src/main/java/org/apache/tamaya/events/package-info.java +++ b/src/main/java/org/apache/tamaya/events/package-info.java @@ -18,6 +18,6 @@ */ /** * This package provides the main building blocks for handling configuration changes, such as - * {@link org.apache.tamaya.events.ConfigEvent}, {@link org.apache.tamaya.events.ConfigEventListener}. + * {@link org.apache.tamaya.events.ConfigEventManager}, {@link org.apache.tamaya.events.ConfigEventListener}. */ package org.apache.tamaya.events; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/java/org/apache/tamaya/events/spi/ConfigEventManagerSpi.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/events/spi/ConfigEventManagerSpi.java b/src/main/java/org/apache/tamaya/events/spi/ConfigEventManagerSpi.java new file mode 100644 index 0000000..7cb47a0 --- /dev/null +++ b/src/main/java/org/apache/tamaya/events/spi/ConfigEventManagerSpi.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.events.spi; + +import org.apache.tamaya.events.ConfigEventListener; + +import java.util.Collection; + +/** + * SPI interface to implement the {@link org.apache.tamaya.events.ConfigEventManager} singleton. + * Implementations of this interface must be registered with the current {@link org.apache.tamaya.spi.ServiceContext}, + * by default this equals to registering it with {@link java.util.ServiceLoader}. Add {@link javax.annotation.Priority} + * annotations for overriding (higher values overriden lower values). + */ +public interface ConfigEventManagerSpi { + /** + * Add a listener for observing events. References of this + * component to the listeners must be managed as weak references. + * + * @param l the listener not null. + */ + <T> void addListener(ConfigEventListener<T> l); + + + /** + * Removes a listener for observing events. + * + * @param l the listener not null. + */ + <T> void removeListener(ConfigEventListener<T> l); + + /** + * Publishes an event to all interested listeners. + * + * @param event the event, not null. + * @param eventType the event type. + */ + <T> void fireEvent(T event, Class<T> eventType); + + /** + * Access all known listeners for a given targe type. + * @param type the type. + * @param <T> the listener type. + * @return the items found, never null. + */ + <T> Collection<? extends ConfigEventListener<T>> getListeners(Class<T> type); +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/java/org/apache/tamaya/events/spi/ConfigEventSpi.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/events/spi/ConfigEventSpi.java b/src/main/java/org/apache/tamaya/events/spi/ConfigEventSpi.java deleted file mode 100644 index e963396..0000000 --- a/src/main/java/org/apache/tamaya/events/spi/ConfigEventSpi.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.events.spi; - -import org.apache.tamaya.events.ConfigEventListener; - -/** - * SPI interface to implement the {@link org.apache.tamaya.events.ConfigEvent} singleton. - * Implementations of this interface must be registered with the current {@link org.apache.tamaya.spi.ServiceContext}, - * by default this equals to registering it with {@link java.util.ServiceLoader}. Add {@link javax.annotation.Priority} - * annotations for overriding (higher values overriden lower values). - */ -public interface ConfigEventSpi { - /** - * Add a listener for observing events. References of this - * component to the listeners must be managed as weak references. - * - * @param l the listener not null. - */ - <T> void addListener(ConfigEventListener<T> l); - - - /** - * Removes a listener for observing events. - * - * @param l the listener not null. - */ - <T> void removeListener(ConfigEventListener<T> l); - - /** - * Publishes an event to all interested listeners. - * - * @param event the event, not null. - * @param eventType the event type. - */ - <T> void fireEvent(T event, Class<T> eventType); - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/java/org/apache/tamaya/events/spi/ConfigObserverSpi.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/events/spi/ConfigObserverSpi.java b/src/main/java/org/apache/tamaya/events/spi/ConfigObserverSpi.java new file mode 100644 index 0000000..17f5ccb --- /dev/null +++ b/src/main/java/org/apache/tamaya/events/spi/ConfigObserverSpi.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.events.spi; + +import org.apache.tamaya.events.ConfigListener; + +import java.util.Collection; + +/** + * SPI interface to implement the {@link org.apache.tamaya.events.ConfigurationObserver} singleton. + * Implementations of this interface must be registered with the current {@link org.apache.tamaya.spi.ServiceContext}, + * by default this equals to registering it with {@link java.util.ServiceLoader}. Add {@link javax.annotation.Priority} + * annotations for overriding (higher values overriden lower values). + */ +public interface ConfigObserverSpi { + /** + * Add a listener for observing events. References of this + * component to the listeners must be managed as weak references. + * + * @param l the listener not null. + */ + <T> void addListener(ConfigListener l); + + + /** + * Removes a listener for observing events. + * + * @param l the listener not null. + */ + <T> void removeListener(ConfigListener l); + + /** + * Access all registered ConfigEventListeners listening to the given event type. + * + * @return a list with the listeners found, never null. + */ + Collection<ConfigListener> getListeners(Collection<String> keys); + + /** + * Get the current check period to check for configuration changes. + * + * @return the check period in ms. + */ + long getCheckPeriod(); + + /** + * Check if the observer is running currently. + * + * @return true, if the observer is running. + */ + boolean isRunning(); + + /** + * Start/Stop the observer container. + */ + void enableObservation(boolean enable); + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/java/org/apache/tamaya/events/spi/package-info.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/events/spi/package-info.java b/src/main/java/org/apache/tamaya/events/spi/package-info.java index 7c4fd8f..63d2b3b 100644 --- a/src/main/java/org/apache/tamaya/events/spi/package-info.java +++ b/src/main/java/org/apache/tamaya/events/spi/package-info.java @@ -18,6 +18,6 @@ */ /** * This package contains the SPI to implement the - * {@link org.apache.tamaya.events.ConfigEvent} singleton. + * {@link org.apache.tamaya.events.ConfigEventManager} singleton. */ package org.apache.tamaya.events.spi; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/resources/META-INF/services/org.apache.tamaya.events.ConfigListener ---------------------------------------------------------------------- diff --git a/src/main/resources/META-INF/services/org.apache.tamaya.events.ConfigListener b/src/main/resources/META-INF/services/org.apache.tamaya.events.ConfigListener new file mode 100644 index 0000000..f3199f2 --- /dev/null +++ b/src/main/resources/META-INF/services/org.apache.tamaya.events.ConfigListener @@ -0,0 +1,18 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy current the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/resources/META-INF/services/org.apache.tamaya.events.spi.ConfigEventManagerSpi ---------------------------------------------------------------------- diff --git a/src/main/resources/META-INF/services/org.apache.tamaya.events.spi.ConfigEventManagerSpi b/src/main/resources/META-INF/services/org.apache.tamaya.events.spi.ConfigEventManagerSpi new file mode 100644 index 0000000..d45dc43 --- /dev/null +++ b/src/main/resources/META-INF/services/org.apache.tamaya.events.spi.ConfigEventManagerSpi @@ -0,0 +1,19 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy current the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +org.apache.tamaya.events.internal.DefaultConfigEventManagerSpi \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/main/resources/META-INF/services/org.apache.tamaya.events.spi.ConfigObserverSpi ---------------------------------------------------------------------- diff --git a/src/main/resources/META-INF/services/org.apache.tamaya.events.spi.ConfigObserverSpi b/src/main/resources/META-INF/services/org.apache.tamaya.events.spi.ConfigObserverSpi new file mode 100644 index 0000000..de5ee6f --- /dev/null +++ b/src/main/resources/META-INF/services/org.apache.tamaya.events.spi.ConfigObserverSpi @@ -0,0 +1,19 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy current the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +org.apache.tamaya.events.internal.DefaultConfigObserverSpi \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/test/java/org/apache/tamaya/events/ConfigEventManagerTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/tamaya/events/ConfigEventManagerTest.java b/src/test/java/org/apache/tamaya/events/ConfigEventManagerTest.java new file mode 100644 index 0000000..45c15d2 --- /dev/null +++ b/src/test/java/org/apache/tamaya/events/ConfigEventManagerTest.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.events; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests for {@link ConfigEventManager}. + */ +public class ConfigEventManagerTest { + + private String testAddListenerValue; + + @Test + public void testAddRemoveListener() throws Exception { + ConfigEventListener<String> testListener = new ConfigEventListener<String>() { + @Override + public void onConfigEvent(String event) { + testAddListenerValue = event; + } + }; + ConfigEventManager.addListener(testListener); + ConfigEventManager.fireEvent("Event1", String.class); + assertEquals(testAddListenerValue, "Event1"); + ConfigEventManager.removeListener(testListener); + ConfigEventManager.fireEvent("Event2", String.class); + assertEquals(testAddListenerValue, "Event1"); + } + + @Test + public void testFireEvent() throws Exception { + ConfigEventListener<String> testListener = new ConfigEventListener<String>() { + @Override + public void onConfigEvent(String event) { + testAddListenerValue = event; + } + }; + ConfigEventManager.addListener(testListener); + ConfigEventManager.fireEvent("Event1"); + assertEquals(testAddListenerValue, "Event1"); + ConfigEventManager.removeListener(testListener); + ConfigEventManager.fireEvent("Event2"); + assertEquals(testAddListenerValue, "Event1"); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/test/java/org/apache/tamaya/events/ConfigEventTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/tamaya/events/ConfigEventTest.java b/src/test/java/org/apache/tamaya/events/ConfigEventTest.java deleted file mode 100644 index 3398d3f..0000000 --- a/src/test/java/org/apache/tamaya/events/ConfigEventTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.events; - -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * Tests for {@link ConfigEvent}. - */ -public class ConfigEventTest { - - private String testAddListenerValue; - - @Test - public void testAddRemoveListener() throws Exception { - ConfigEventListener<String> testListener = new ConfigEventListener<String>() { - @Override - public void onConfigEvent(String event) { - testAddListenerValue = event; - } - }; - ConfigEvent.addListener(testListener); - ConfigEvent.fireEvent("Event1", String.class); - assertEquals(testAddListenerValue, "Event1"); - ConfigEvent.removeListener(testListener); - ConfigEvent.fireEvent("Event2", String.class); - assertEquals(testAddListenerValue, "Event1"); - } - - @Test - public void testFireEvent() throws Exception { - ConfigEventListener<String> testListener = new ConfigEventListener<String>() { - @Override - public void onConfigEvent(String event) { - testAddListenerValue = event; - } - }; - ConfigEvent.addListener(testListener); - ConfigEvent.fireEvent("Event1"); - assertEquals(testAddListenerValue, "Event1"); - ConfigEvent.removeListener(testListener); - ConfigEvent.fireEvent("Event2"); - assertEquals(testAddListenerValue, "Event1"); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/test/java/org/apache/tamaya/events/delta/ConfigurationChangeTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/tamaya/events/delta/ConfigurationChangeTest.java b/src/test/java/org/apache/tamaya/events/delta/ConfigurationChangeTest.java index 7d44b92..0a639ab 100644 --- a/src/test/java/org/apache/tamaya/events/delta/ConfigurationChangeTest.java +++ b/src/test/java/org/apache/tamaya/events/delta/ConfigurationChangeTest.java @@ -46,7 +46,7 @@ public class ConfigurationChangeTest { assertTrue(change.isEmpty()); for (Map.Entry<String, String> en : config.getProperties().entrySet()) { if (!"[meta]frozenAt".equals(en.getKey())) { - assertEquals("Error for " + en.getKey(), en.getValue(), change.getConfiguration().get(en.getKey())); + assertEquals("Error for " + en.getKey(), en.getValue(), change.getResource().get(en.getKey())); } } } @@ -74,9 +74,9 @@ public class ConfigurationChangeTest { public void testGetEvents() throws Exception { Configuration config = ConfigurationProvider.getConfiguration(); ConfigurationChange change = ConfigurationChangeBuilder.of(config).removeKey("key1", "key2").build(); - assertTrue(change.getEvents().size() == 2); + assertTrue(change.getChanges().size() == 2); change = ConfigurationChangeBuilder.of(config).addChange("key1Added", "value1Added").build(); - assertTrue(change.getEvents().size() == 1); + assertTrue(change.getChanges().size() == 1); } @Test @@ -127,11 +127,11 @@ public class ConfigurationChangeTest { public void testContainsKey() throws Exception { Configuration config = ConfigurationProvider.getConfiguration(); ConfigurationChange change = ConfigurationChangeBuilder.of(config).addChange("key1", "key2").build(); - assertTrue(change.containsKey("key1")); - assertFalse(change.containsKey("key2")); + assertTrue(change.isKeyAffected("key1")); + assertFalse(change.isKeyAffected("key2")); change = ConfigurationChangeBuilder.of(config).removeKey("java.version").build(); - assertFalse(change.containsKey("java.version")); - assertFalse(change.containsKey("key2")); + assertFalse(change.isKeyAffected("java.version")); + assertFalse(change.isKeyAffected("key2")); } @Test http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/test/java/org/apache/tamaya/events/delta/PropertySourceChangeTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/tamaya/events/delta/PropertySourceChangeTest.java b/src/test/java/org/apache/tamaya/events/delta/PropertySourceChangeTest.java index 2b44543..b3be028 100644 --- a/src/test/java/org/apache/tamaya/events/delta/PropertySourceChangeTest.java +++ b/src/test/java/org/apache/tamaya/events/delta/PropertySourceChangeTest.java @@ -47,7 +47,7 @@ public class PropertySourceChangeTest { @Test public void testGetPropertySource() throws Exception { PropertySourceChange change = PropertySourceChangeBuilder.of(myPS, ChangeType.DELETED).build(); - assertEquals(change.getPropertySource().getName(), myPS.getName()); + assertEquals(change.getResource().getName(), myPS.getName()); } @Test @@ -70,7 +70,7 @@ public class PropertySourceChangeTest { .addChanges( new EnvironmentPropertySource() ).build(); - assertTrue(change.getEvents().size()>0); + assertTrue(change.getChanges().size()>0); } @Test @@ -163,7 +163,7 @@ public class PropertySourceChangeTest { .addChanges( myPS ).build(); - assertTrue(change.containsKey("java.version")); + assertTrue(change.isKeyAffected("java.version")); } @Test http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/test/java/org/apache/tamaya/events/internal/DefaultConfigEventManagerSpiTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/tamaya/events/internal/DefaultConfigEventManagerSpiTest.java b/src/test/java/org/apache/tamaya/events/internal/DefaultConfigEventManagerSpiTest.java new file mode 100644 index 0000000..7bac25a --- /dev/null +++ b/src/test/java/org/apache/tamaya/events/internal/DefaultConfigEventManagerSpiTest.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.events.internal; + +import org.apache.tamaya.events.ConfigEventListener; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Tests for {@link DefaultConfigEventManagerSpi}. + */ +public class DefaultConfigEventManagerSpiTest { + + private DefaultConfigEventManagerSpi spi = new DefaultConfigEventManagerSpi(); + private String testAddListenerValue; + + @Test + public void testAddListener() throws Exception { + ConfigEventListener<String> testListener = new ConfigEventListener<String>() { + + @Override + public void onConfigEvent(String event) { + testAddListenerValue = event; + } + }; + spi.addListener(testListener); + spi.fireEvent("Event1", String.class); + assertEquals(testAddListenerValue, "Event1"); + spi.removeListener(testListener); + spi.fireEvent("Event2", String.class); + assertEquals(testAddListenerValue, "Event1"); + + } + + @Test + public void testRemoveListener() throws Exception { + ConfigEventListener<String> testListener = new ConfigEventListener<String>() { + + @Override + public void onConfigEvent(String event) { + testAddListenerValue = event; + } + }; + spi.removeListener(testListener); + spi.removeListener(testListener); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fb52d6cb/src/test/java/org/apache/tamaya/events/internal/DefaultConfigEventSpiTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/tamaya/events/internal/DefaultConfigEventSpiTest.java b/src/test/java/org/apache/tamaya/events/internal/DefaultConfigEventSpiTest.java deleted file mode 100644 index 0908d20..0000000 --- a/src/test/java/org/apache/tamaya/events/internal/DefaultConfigEventSpiTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.events.internal; - -import org.apache.tamaya.events.ConfigEventListener; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Tests for {@link DefaultConfigEventSpi}. - */ -public class DefaultConfigEventSpiTest { - - private DefaultConfigEventSpi spi = new DefaultConfigEventSpi(); - private String testAddListenerValue; - - @Test - public void testAddListener() throws Exception { - ConfigEventListener<String> testListener = new ConfigEventListener<String>() { - - @Override - public void onConfigEvent(String event) { - testAddListenerValue = event; - } - }; - spi.addListener(testListener); - spi.fireEvent("Event1", String.class); - assertEquals(testAddListenerValue, "Event1"); - spi.removeListener(testListener); - spi.fireEvent("Event2", String.class); - assertEquals(testAddListenerValue, "Event1"); - - } - - @Test - public void testRemoveListener() throws Exception { - ConfigEventListener<String> testListener = new ConfigEventListener<String>() { - - @Override - public void onConfigEvent(String event) { - testAddListenerValue = event; - } - }; - spi.removeListener(testListener); - spi.removeListener(testListener); - } - -} \ No newline at end of file