You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by pl...@apache.org on 2017/02/24 23:16:09 UTC
[1/3] incubator-tamaya-extensions git commit: [TAMAYA-206] Re-added
the file observer example, but overhauled it.
Repository: incubator-tamaya-extensions
Updated Branches:
refs/heads/master e82d3bd98 -> e8db5e48c
[TAMAYA-206] Re-added the file observer example, but overhauled it.
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/c985c778
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/c985c778
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/c985c778
Branch: refs/heads/master
Commit: c985c77855378f7ff06d8360b86180062c639279
Parents: 2c2a55a
Author: Oliver B. Fischer <pl...@apache.org>
Authored: Sat Feb 25 00:01:57 2017 +0100
Committer: Oliver B. Fischer <pl...@apache.org>
Committed: Sat Feb 25 00:02:59 2017 +0100
----------------------------------------------------------------------
examples/04-fileobserver-example/pom.xml | 72 +++++++++
.../FilePropertySourceProvider.java | 72 +++++++++
.../tamaya/ext/examples/fileobserver/Main.java | 160 +++++++++++++++++++
...org.apache.tamaya.spi.PropertySourceProvider | 20 +++
examples/pom.xml | 1 +
.../internal/DefaultConfigChangeObserver.java | 14 +-
6 files changed, 336 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c985c778/examples/04-fileobserver-example/pom.xml
----------------------------------------------------------------------
diff --git a/examples/04-fileobserver-example/pom.xml b/examples/04-fileobserver-example/pom.xml
new file mode 100644
index 0000000..e191eb7
--- /dev/null
+++ b/examples/04-fileobserver-example/pom.xml
@@ -0,0 +1,72 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.tamaya.ext.examples</groupId>
+ <artifactId>examples</artifactId>
+ <version>0.3-incubating-SNAPSHOT</version>
+ </parent>
+
+ <groupId>org.apache.tamaya.ext.examples</groupId>
+ <artifactId>04-fileobserver-example</artifactId>
+ <version>0.3-incubating-SNAPSHOT</version>
+
+ <name>Apache Tamaya File Observer Example</name>
+
+ <description>This project contains a simple example observing a directory
+ for (config) file changes, that updates the configuration correspondingly.
+ </description>
+
+ <dependencies>
+ <dependency>
+ <groupId>joda-time</groupId>
+ <artifactId>joda-time</artifactId>
+ <version>2.8.2</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.tamaya</groupId>
+ <artifactId>tamaya-core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.tamaya.ext</groupId>
+ <artifactId>tamaya-events</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.tamaya.ext</groupId>
+ <artifactId>tamaya-formats</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>java-hamcrest</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ </dependency>
+ </dependencies>
+
+</project>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c985c778/examples/04-fileobserver-example/src/main/java/org/apache/tamaya/ext/examples/fileobserver/FilePropertySourceProvider.java
----------------------------------------------------------------------
diff --git a/examples/04-fileobserver-example/src/main/java/org/apache/tamaya/ext/examples/fileobserver/FilePropertySourceProvider.java b/examples/04-fileobserver-example/src/main/java/org/apache/tamaya/ext/examples/fileobserver/FilePropertySourceProvider.java
new file mode 100644
index 0000000..28441e7
--- /dev/null
+++ b/examples/04-fileobserver-example/src/main/java/org/apache/tamaya/ext/examples/fileobserver/FilePropertySourceProvider.java
@@ -0,0 +1,72 @@
+/*
+ * 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.ext.examples.fileobserver;
+
+
+import org.apache.tamaya.ConfigException;
+import org.apache.tamaya.core.propertysource.BasePropertySource;
+import org.apache.tamaya.resource.AbstractPathPropertySourceProvider;
+import org.apache.tamaya.spi.PropertySource;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.*;
+
+import static org.apache.tamaya.ext.examples.fileobserver.Main.getPropertiesFilePath;
+
+public class FilePropertySourceProvider extends AbstractPathPropertySourceProvider {
+ public FilePropertySourceProvider() {
+ super(getPropertiesFilePath().toString());
+ }
+
+ @Override
+ protected Collection<PropertySource> getPropertySources(URL url) {
+ return Arrays.asList(new PropertySource[] { new DumbReloadingPropertySource(url) });
+ }
+
+ public static class DumbReloadingPropertySource extends BasePropertySource {
+ private final URL propertiesFile;
+
+ public DumbReloadingPropertySource(URL url) {
+ propertiesFile = url;
+ }
+
+ @Override
+ public Map<String, String> getProperties() {
+
+ Map<String, String> properties = new HashMap<>();
+ try (InputStream stream = propertiesFile.openStream()) {
+ Properties props = new Properties();
+ if (stream != null) {
+ props.load(stream);
+ }
+
+ for (String key : props.stringPropertyNames()) {
+ properties.put(key, props.getProperty(key));
+ }
+ } catch (IOException e) {
+ throw new ConfigException("Error loading properties from " + propertiesFile, e);
+ }
+
+ return properties;
+
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c985c778/examples/04-fileobserver-example/src/main/java/org/apache/tamaya/ext/examples/fileobserver/Main.java
----------------------------------------------------------------------
diff --git a/examples/04-fileobserver-example/src/main/java/org/apache/tamaya/ext/examples/fileobserver/Main.java b/examples/04-fileobserver-example/src/main/java/org/apache/tamaya/ext/examples/fileobserver/Main.java
new file mode 100644
index 0000000..3f1ba49
--- /dev/null
+++ b/examples/04-fileobserver-example/src/main/java/org/apache/tamaya/ext/examples/fileobserver/Main.java
@@ -0,0 +1,160 @@
+/*
+ * 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.ext.examples.fileobserver;
+
+import org.apache.tamaya.Configuration;
+import org.apache.tamaya.ConfigurationProvider;
+import org.apache.tamaya.events.ConfigEvent;
+import org.apache.tamaya.events.ConfigEventListener;
+import org.apache.tamaya.events.ConfigEventManager;
+import org.apache.tamaya.events.ConfigurationChange;
+import org.joda.time.DateTime;
+import org.joda.time.Duration;
+
+import java.beans.PropertyChangeEvent;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.file.*;
+import java.util.*;
+import java.util.logging.LogManager;
+import java.util.logging.Logger;
+
+import static java.lang.System.out;
+import static java.nio.file.StandardOpenOption.CREATE;
+import static java.util.Arrays.asList;
+
+public class Main {
+ private static final Duration EXAMPLE_RUNTIME = Duration.standardSeconds(30L);
+
+ /*
+ * Turns off all logging.
+ */
+ static {
+ LogManager.getLogManager().reset();
+ Logger globalLogger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
+ globalLogger.setLevel(java.util.logging.Level.OFF);
+ }
+
+ private Main() {
+ }
+
+ public static void main(String[] args) throws Exception {
+ DateTime end = DateTime.now().plus(EXAMPLE_RUNTIME);
+
+ Timer timer = new Timer();
+ timer.schedule(new PropertiesFileWritingTask(), 0L, 5_000L);
+
+ installCleanupHook(getPropertiesFilePath());
+
+ ConfigEventManager.addListener(new ConfigurationChangeListener());
+ ConfigEventManager.setChangeMonitoringPeriod(1_000L);
+ ConfigEventManager.enableChangeMonitoring(true);
+
+ out.println("****************************************************");
+ out.println("File observer example");
+ out.println("****************************************************");
+ out.println();
+ out.println("Configuration source is: " + getPropertiesFilePath());
+ out.println();
+
+ Thread.sleep(EXAMPLE_RUNTIME.getMillis());
+
+ timer.cancel();
+ }
+
+ private static void installCleanupHook(final Path path) {
+ Runtime.getRuntime().addShutdownHook(new Thread() {
+ @Override
+ public void run() {
+ try {
+ Files.delete(path);
+ out.println("Removed " + path);
+ } catch (IOException e) {
+ throw new RuntimeException("Failed to delete " + path, e);
+ }
+ }
+ });
+ }
+
+ static Path getPropertiesFilePath() {
+ String tempDir = System.getProperty("java.io.tmpdir");
+
+ return Paths.get(tempDir, "fileobserver.properties");
+ }
+
+ private static class ConfigurationChangeListener implements ConfigEventListener {
+ @Override
+ public void onConfigEvent(ConfigEvent<?> event) {
+
+ ConfigurationChange c = (ConfigurationChange) event;
+
+ if (c.isKeyAffected("a")) {
+ PropertyChangeEvent change = c.getChanges().iterator().next();
+ Object oldValue = change.getOldValue();
+ Object newValue = change.getNewValue();
+
+ if (oldValue != null) {
+ out.println("Value for key a changed (" + oldValue + " (old) => " + newValue + " (new))");
+ } else {
+ out.println("Value for key a changed (" + newValue + " (new))");
+ }
+
+ }
+
+ }
+ }
+
+ private static class ContentProvider implements Iterable<String> {
+ private long value;
+
+ public void setValue(long val) {
+ this.value = val;
+ }
+
+ public long getValue() {
+ return value;
+ }
+
+ @Override
+ public Iterator<String> iterator() {
+ List<String> list = asList("# Generated file", "a="+ getValue());
+
+ return list.iterator();
+ }
+ }
+
+ private static class PropertiesFileWritingTask extends TimerTask {
+ private static ContentProvider contentProvider = new ContentProvider();
+
+ @Override
+ public void run() {
+
+ try {
+ contentProvider.setValue(System.currentTimeMillis());
+
+ Files.write(getPropertiesFilePath(), contentProvider,
+ Charset.defaultCharset(), StandardOpenOption.CREATE);
+ }
+ catch (IOException e) {
+ throw new RuntimeException("Failed to write properties file.", e);
+ }
+
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c985c778/examples/04-fileobserver-example/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider
----------------------------------------------------------------------
diff --git a/examples/04-fileobserver-example/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider b/examples/04-fileobserver-example/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider
new file mode 100644
index 0000000..fb21cb9
--- /dev/null
+++ b/examples/04-fileobserver-example/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider
@@ -0,0 +1,20 @@
+#
+# 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.ext.examples.fileobserver.FilePropertySourceProvider
+
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c985c778/examples/pom.xml
----------------------------------------------------------------------
diff --git a/examples/pom.xml b/examples/pom.xml
index 113be1e..b1bc219 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -40,6 +40,7 @@ under the License.
<module>01-resources-example</module>
<module>02-resolver-example</module>
<module>03-injection-example</module>
+ <module>04-fileobserver-example</module>
</modules>
</project>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c985c778/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigChangeObserver.java
----------------------------------------------------------------------
diff --git a/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigChangeObserver.java b/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigChangeObserver.java
index 6d8c694..95259b7 100644
--- a/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigChangeObserver.java
+++ b/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigChangeObserver.java
@@ -52,9 +52,14 @@ public class DefaultConfigChangeObserver {
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
- if(running) {
- checkConfigurationUpdate();
- }
+ System.out.println("*******");
+ if (running) {
+ System.out.println("Observer l�uft!");
+ checkConfigurationUpdate();
+ } else {
+ System.out.println("Observer l�uft nicht.");
+ }
+
}
}, START_DELAY, checkPeriod);
}
@@ -74,6 +79,9 @@ public class DefaultConfigChangeObserver {
}
if(!changes.isEmpty()) {
LOG.info("Identified configuration changes, publishing changes:\n" + changes);
+// System.out.println("UPD detected with size=" + changes.getUpdatedSize());
+// System.out.println("ADD detected with size=" + changes.getAddedSize());
+// System.out.println("REM detected with size=" + changes.getRemovedSize());
ConfigEventManager.fireEvent(changes);
}
}
[3/3] incubator-tamaya-extensions git commit: [TAMAYA-232] This
commits fixes the breaking change introduced by a git pull -r.
Posted by pl...@apache.org.
[TAMAYA-232] This commits fixes the breaking change introduced by a git pull -r.
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/e8db5e48
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/e8db5e48
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/e8db5e48
Branch: refs/heads/master
Commit: e8db5e48c6398b725a23a57f42a489cb43fc76f6
Parents: c985c77
Author: Oliver B. Fischer <pl...@apache.org>
Authored: Sat Feb 25 00:15:15 2017 +0100
Committer: Oliver B. Fischer <pl...@apache.org>
Committed: Sat Feb 25 00:15:15 2017 +0100
----------------------------------------------------------------------
.../org/apache/tamaya/events/ConfigurationChangeBuilder.java | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/e8db5e48/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationChangeBuilder.java
----------------------------------------------------------------------
diff --git a/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationChangeBuilder.java b/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationChangeBuilder.java
index e622869..37232c7 100644
--- a/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationChangeBuilder.java
+++ b/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationChangeBuilder.java
@@ -106,12 +106,12 @@ public final class ConfigurationChangeBuilder {
changes.add(new PropertyChangeEvent(original, en.getKey(), val, en.getValue()));
}
}
- for (Map.Entry<String, String> en : map2.getProperties().entrySet()) {
- String val = map1.get(en.getKey());
+ for (Map.Entry<String, String> en : target.getProperties().entrySet()) {
+ String val = original.get(en.getKey());
if (val == null) {
- changes.add(new PropertyChangeEvent(map1, en.getKey(), null, en.getValue()));
+ changes.add(new PropertyChangeEvent(original, en.getKey(), null, en.getValue()));
} else if (!val.equals(en.getValue())) {
- changes.add(new PropertyChangeEvent(map1, en.getKey(), val, en.getValue()));
+ changes.add(new PropertyChangeEvent(original, en.getKey(), val, en.getValue()));
}
}
return changes;
[2/3] incubator-tamaya-extensions git commit: [TAMAYA-232] Removed
meta-data from the frozen configuration.
Posted by pl...@apache.org.
[TAMAYA-232] Removed meta-data from the frozen configuration.
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/2c2a55a9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/2c2a55a9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/2c2a55a9
Branch: refs/heads/master
Commit: 2c2a55a9809da7e243115c132931589730966363
Parents: e82d3bd
Author: Oliver B. Fischer <pl...@apache.org>
Authored: Sun Feb 12 13:32:15 2017 +0100
Committer: Oliver B. Fischer <pl...@apache.org>
Committed: Sat Feb 25 00:02:59 2017 +0100
----------------------------------------------------------------------
modules/events/pom.xml | 4 +
.../events/ConfigurationChangeBuilder.java | 18 ++---
.../tamaya/events/FrozenConfiguration.java | 45 +++++++++--
.../internal/DefaultConfigEventManagerSpi.java | 2 +-
.../tamaya/events/FrozenConfigurationTest.java | 80 ++++++++++++++++++++
5 files changed, 133 insertions(+), 16 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/2c2a55a9/modules/events/pom.xml
----------------------------------------------------------------------
diff --git a/modules/events/pom.xml b/modules/events/pom.xml
index bd96532..b29d680 100644
--- a/modules/events/pom.xml
+++ b/modules/events/pom.xml
@@ -78,6 +78,10 @@ under the License.
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ </dependency>
</dependencies>
</project>
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/2c2a55a9/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationChangeBuilder.java
----------------------------------------------------------------------
diff --git a/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationChangeBuilder.java b/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationChangeBuilder.java
index 93726d8..e622869 100644
--- a/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationChangeBuilder.java
+++ b/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationChangeBuilder.java
@@ -90,20 +90,20 @@ public final class ConfigurationChangeBuilder {
/**
* Compares the two property config/configurations and creates a collection with all changes
- * that must be applied to render {@code map1} into {@code map2}.
+ * that must be applied to render {@code original} into {@code target}.
*
- * @param map1 the source map, not null.
- * @param map2 the target map, not null.
- * @return a collection current change events, never null.
+ * @param original the original map, not null.
+ * @param target the target map, not null.
+ * @return a collection current change events, never {@code null}.
*/
- public static Collection<PropertyChangeEvent> compare(Configuration map1, Configuration map2) {
+ public static Collection<PropertyChangeEvent> compare(Configuration original, Configuration target) {
List<PropertyChangeEvent> changes = new ArrayList<>();
- for (Map.Entry<String, String> en : map1.getProperties().entrySet()) {
- String val = map2.get(en.getKey());
+ for (Map.Entry<String, String> en : original.getProperties().entrySet()) {
+ String val = target.get(en.getKey());
if (val == null) {
- changes.add(new PropertyChangeEvent(map1, en.getKey(), null, en.getValue()));
+ changes.add(new PropertyChangeEvent(original, en.getKey(), null, en.getValue()));
} else if (!val.equals(en.getValue())) {
- changes.add(new PropertyChangeEvent(map1, en.getKey(), val, en.getValue()));
+ changes.add(new PropertyChangeEvent(original, en.getKey(), val, en.getValue()));
}
}
for (Map.Entry<String, String> en : map2.getProperties().entrySet()) {
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/2c2a55a9/modules/events/src/main/java/org/apache/tamaya/events/FrozenConfiguration.java
----------------------------------------------------------------------
diff --git a/modules/events/src/main/java/org/apache/tamaya/events/FrozenConfiguration.java b/modules/events/src/main/java/org/apache/tamaya/events/FrozenConfiguration.java
index 2a4540c..295a609 100644
--- a/modules/events/src/main/java/org/apache/tamaya/events/FrozenConfiguration.java
+++ b/modules/events/src/main/java/org/apache/tamaya/events/FrozenConfiguration.java
@@ -41,10 +41,13 @@ import java.util.logging.Logger;
*/
public final class FrozenConfiguration implements Configuration, Serializable {
private static final long serialVersionUID = -6373137316556444171L;
+
/**
* The properties frozen.
*/
private Map<String, String> properties = new HashMap<>();
+ private long frozenAt = System.nanoTime();
+ private UUID id = UUID.randomUUID();
/**
* Constructor.
@@ -53,10 +56,6 @@ public final class FrozenConfiguration implements Configuration, Serializable {
*/
private FrozenConfiguration(Configuration config) {
this.properties.putAll(config.getProperties());
- this.properties.put("_frozenAt", String.valueOf(System.currentTimeMillis()));
- if(!this.properties.containsKey("_id")) {
- this.properties.put("_id", UUID.randomUUID().toString());
- }
this.properties = Collections.unmodifiableMap(this.properties);
}
@@ -176,19 +175,53 @@ public final class FrozenConfiguration implements Configuration, Serializable {
if (o == null || getClass() != o.getClass()) {
return false;
}
+
FrozenConfiguration that = (FrozenConfiguration) o;
- return properties.equals(that.properties);
+
+ if (frozenAt != that.frozenAt) {
+ return false;
+ }
+ if (properties != null ? !properties.equals(that.properties) : that.properties != null) {
+ return false;
+ }
+ return id != null ? id.equals(that.id) : that.id == null;
}
@Override
public int hashCode() {
- return properties.hashCode();
+ int result = properties != null ? properties.hashCode() : 0;
+ result = 31 * result + (int) (frozenAt ^ (frozenAt >>> 32));
+ result = 31 * result + (id != null ? id.hashCode() : 0);
+ return result;
}
@Override
public String toString() {
return "FrozenConfiguration{" +
+ "id=" + getId() + "," +
+ "frozenAt=" + getFrozenAt() + "," +
"properties=" + properties +
'}';
}
+
+ /**
+ * <p>Returns the moment in time when this frozen configuration has been created.</p>
+ *
+ * <p>The time is taken from {@linkplain System#currentTimeMillis()}</p>
+ *
+ * @see {@linkplain System#currentTimeMillis()}
+ * @return the moment in time when this configruration has been created
+ */
+ public long getFrozenAt() {
+ return frozenAt;
+ }
+
+ /**
+ * <p>Returns the unique id of this frozen configuration.</p>
+ *
+ * @return the unique id of this frozen configuration, never {@code null}
+ */
+ public UUID getId() {
+ return id;
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/2c2a55a9/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigEventManagerSpi.java
----------------------------------------------------------------------
diff --git a/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigEventManagerSpi.java b/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigEventManagerSpi.java
index 586df5c..c92b7e8 100644
--- a/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigEventManagerSpi.java
+++ b/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigEventManagerSpi.java
@@ -184,7 +184,7 @@ public class DefaultConfigEventManagerSpi implements ConfigEventManagerSpi {
/**
* Tasks to inform observers on detected configuration changes.
*/
- private static final class PublishConfigChangeTask implements Runnable{
+ private static final class PublishConfigChangeTask implements Runnable {
private final ConfigEventListener l;
private final ConfigEvent<?> changes;
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/2c2a55a9/modules/events/src/test/java/org/apache/tamaya/events/FrozenConfigurationTest.java
----------------------------------------------------------------------
diff --git a/modules/events/src/test/java/org/apache/tamaya/events/FrozenConfigurationTest.java b/modules/events/src/test/java/org/apache/tamaya/events/FrozenConfigurationTest.java
new file mode 100644
index 0000000..dbb2ead
--- /dev/null
+++ b/modules/events/src/test/java/org/apache/tamaya/events/FrozenConfigurationTest.java
@@ -0,0 +1,80 @@
+/*
+ * 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;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.doReturn;
+
+public class FrozenConfigurationTest {
+
+ @Test
+ public void getFrozenAtReturnsTheCorrectTimestamp() {
+ Configuration source = Mockito.mock(Configuration.class);
+
+ long poiStart = System.nanoTime();
+
+ FrozenConfiguration fc = FrozenConfiguration.of(source);
+
+ long poiEnd = System.nanoTime();
+
+ assertThat(fc.getFrozenAt()).isGreaterThan(poiStart)
+ .isLessThan(poiEnd);
+ }
+
+
+ @Test
+ public void idMustBeNotNull() {
+ Configuration source = Mockito.mock(Configuration.class);
+
+ FrozenConfiguration fc = FrozenConfiguration.of(source);
+
+ assertThat(fc.getId()).isNotNull();
+ }
+
+ /*
+ * All tests for equals() and hashCode() go here...
+ */
+
+ @Test
+ public void twoFrozenAreDifferentIfTheyHaveADifferentIdAndFrozenAtTimestamp() {
+ Map<String, String> properties = new HashMap<>();
+ properties.put("key", "value");
+
+ Configuration configuration = Mockito.mock(Configuration.class);
+ doReturn(properties).when(configuration).getProperties();
+
+ FrozenConfiguration fcA = FrozenConfiguration.of(configuration);
+ FrozenConfiguration fcB = FrozenConfiguration.of(configuration);
+
+ assertThat(fcA.getId()).isNotEqualTo(fcB.getId());
+ assertThat(fcA).isNotEqualTo(fcB);
+ }
+
+ /*
+ * END OF ALL TESTS for equals() and hashCode()
+ */
+}
\ No newline at end of file