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 <[email protected]> Authored: Sat Feb 25 00:01:57 2017 +0100 Committer: Oliver B. Fischer <[email protected]> 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); } }
