You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by an...@apache.org on 2015/05/21 09:40:28 UTC
[10/15] incubator-tamaya git commit: TAMAYA-79: removed dep on local
filesystem.
TAMAYA-79: removed dep on local filesystem.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/82b83f20
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/82b83f20
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/82b83f20
Branch: refs/heads/master
Commit: 82b83f20c1f1db835043bc7b389304c7d21d7e2e
Parents: 7a39d86
Author: anatole <an...@apache.org>
Authored: Thu May 21 06:34:55 2015 +0200
Committer: anatole <an...@apache.org>
Committed: Thu May 21 06:34:55 2015 +0200
----------------------------------------------------------------------
examples/6-fileobserver-example/pom.xml | 8 +-
.../src/data/test.properties | 21 ---
.../fileobserver/TestObservingProvider.java | 35 -----
...org.apache.tamaya.spi.PropertySourceProvider | 19 ---
.../fileobserver/ObservedConfigExampleTest.java | 145 +++++++++++++++++++
.../fileobserver/ObservedConfigTest.java | 54 -------
.../examples/fileobserver/TestConfigView.java | 90 ++++++++++++
.../fileobserver/TestObservingProvider.java | 37 +++++
...org.apache.tamaya.spi.PropertySourceProvider | 19 +++
.../src/test/resources/test.properties | 21 +++
10 files changed, 319 insertions(+), 130 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/82b83f20/examples/6-fileobserver-example/pom.xml
----------------------------------------------------------------------
diff --git a/examples/6-fileobserver-example/pom.xml b/examples/6-fileobserver-example/pom.xml
index fbe4af0..96bbba4 100644
--- a/examples/6-fileobserver-example/pom.xml
+++ b/examples/6-fileobserver-example/pom.xml
@@ -25,7 +25,8 @@ under the License.
<version>1.0-incubating-SNAPSHOT</version>
<name>Apache Tamaya Example: File Observer</name>
<description>This project contains a simple example observing a directory for (config) file changes, that updates
- the configuration correspondingly.</description>
+ the configuration correspondingly.
+ </description>
<packaging>jar</packaging>
@@ -46,6 +47,11 @@ under the License.
<version>4.12</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>2.4</version>
+ </dependency>
</dependencies>
</project>
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/82b83f20/examples/6-fileobserver-example/src/data/test.properties
----------------------------------------------------------------------
diff --git a/examples/6-fileobserver-example/src/data/test.properties b/examples/6-fileobserver-example/src/data/test.properties
deleted file mode 100644
index af06631..0000000
--- a/examples/6-fileobserver-example/src/data/test.properties
+++ /dev/null
@@ -1,21 +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 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.
-#
-testValue1=value
-test=test2
-a=b
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/82b83f20/examples/6-fileobserver-example/src/main/java/org/apache/tamaya/examples/fileobserver/TestObservingProvider.java
----------------------------------------------------------------------
diff --git a/examples/6-fileobserver-example/src/main/java/org/apache/tamaya/examples/fileobserver/TestObservingProvider.java b/examples/6-fileobserver-example/src/main/java/org/apache/tamaya/examples/fileobserver/TestObservingProvider.java
deleted file mode 100644
index b1f5dcf..0000000
--- a/examples/6-fileobserver-example/src/main/java/org/apache/tamaya/examples/fileobserver/TestObservingProvider.java
+++ /dev/null
@@ -1,35 +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.examples.fileobserver;
-
-import org.apache.tamaya.events.folderobserver.ObservingPropertySourceProvider;
-import org.apache.tamaya.format.formats.PropertiesFormat;
-
-import java.nio.file.Paths;
-
-/**
- * Test configuration property source provider that observes a directory and updated the config if necessary.
- */
-public class TestObservingProvider extends ObservingPropertySourceProvider{
-
- public TestObservingProvider(){
- super(Paths.get("C:\\Users\\Anatole\\IdeaProjects\\incubator-tamaya\\examples\\6-fileobserver-example\\src\\data"),
- new PropertiesFormat());
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/82b83f20/examples/6-fileobserver-example/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider
----------------------------------------------------------------------
diff --git a/examples/6-fileobserver-example/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider b/examples/6-fileobserver-example/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider
deleted file mode 100644
index 4792667..0000000
--- a/examples/6-fileobserver-example/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider
+++ /dev/null
@@ -1,19 +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 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.examples.fileobserver.TestObservingProvider
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/82b83f20/examples/6-fileobserver-example/src/test/java/org/aspache/tamaya/examples/fileobserver/ObservedConfigExampleTest.java
----------------------------------------------------------------------
diff --git a/examples/6-fileobserver-example/src/test/java/org/aspache/tamaya/examples/fileobserver/ObservedConfigExampleTest.java b/examples/6-fileobserver-example/src/test/java/org/aspache/tamaya/examples/fileobserver/ObservedConfigExampleTest.java
new file mode 100644
index 0000000..936d5ac
--- /dev/null
+++ b/examples/6-fileobserver-example/src/test/java/org/aspache/tamaya/examples/fileobserver/ObservedConfigExampleTest.java
@@ -0,0 +1,145 @@
+/*
+ * 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.aspache.tamaya.examples.fileobserver;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.tamaya.Configuration;
+import org.apache.tamaya.ConfigurationProvider;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Map;
+import java.util.TreeMap;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test (currently manual) to test configuration changes.
+ */
+public class ObservedConfigExampleTest {
+
+ @Before
+ public void setup() throws IOException {
+ // create some temporary config
+ Path tempDir = Files.createTempDirectory("observedFolder");
+
+ TestObservingProvider.propertyLocation = tempDir;
+
+ FileUtils.copyInputStreamToFile(
+ getClass().getResourceAsStream("/test.properties"),
+ new File(tempDir.toFile(), "test.properties"));
+ }
+
+
+// private static Path getSourceFile(String name) throws Exception {
+// URL url = ObservedConfigExampleTest.class.getResource("/data");
+// File testFile = new File(new File(url.toURI()), name);
+// return Paths.get(testFile.toURI());
+// }
+//
+// private static Path getTargetFile(String name) {
+// File testFile = new File(TestObservingProvider.getTestDirectory(), name);
+// return Paths.get(testFile.toURI());
+// }
+
+ /**
+ * Test method that periodically prints out what is happening.
+ */
+ public static void main() {
+ while (true) {
+ System.out.println("1: " + ConfigurationProvider.getConfiguration().get("1"));
+ System.out.println("2: " + ConfigurationProvider.getConfiguration().get("2"));
+ System.out.println("3: " + ConfigurationProvider.getConfiguration().get("3"));
+ System.out.println("4: " + ConfigurationProvider.getConfiguration().get("4"));
+ System.out.println("5: " + ConfigurationProvider.getConfiguration().get("5"));
+ System.out.println("6: " + ConfigurationProvider.getConfiguration().get("6"));
+ System.out.println("=======================================================================");
+ try {
+ Thread.sleep(2000L);
+ } catch (Exception e) {
+ // stop here...
+ }
+ }
+ }
+
+// @AfterClass
+// public static void cleanup() throws Exception {
+// // cleanup directory
+// Files.deleteIfExists(getTargetFile("test1.properties"));
+// Files.deleteIfExists(getTargetFile("test2.properties"));
+// Files.deleteIfExists(getTargetFile("test3.properties"));
+// }
+
+
+
+ public void testInitialConfig() throws IOException {
+ Configuration config = ConfigurationProvider.getConfiguration().with(TestConfigView.of());
+
+ Map<String, String> props = config.getProperties();
+
+ assertThat(props.get("test"), is("test2"));
+ assertThat(props.get("testValue1"), is("value"));
+ assertNull(props.get("a"));
+
+ }
+
+ @Test
+ public void testChangingConfig() throws IOException {
+ Configuration config = ConfigurationProvider.getConfiguration().with(TestConfigView.of());
+
+ Map<String, String> props = config.getProperties();
+ assertThat(props.get("test"), is("test2"));
+ assertThat(props.get("testValue1"), is("value"));
+ assertNull(props.get("testValue2"));
+
+ //insert a new properties file into the tempdirectory
+ FileUtils.writeStringToFile(
+ new File(TestObservingProvider.propertyLocation.toFile(), "test2.properties"),
+ "testValue2=anotherValue");
+
+ try {
+ Thread.sleep(10000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+
+ config = ConfigurationProvider.getConfiguration().with(TestConfigView.of());
+
+ props = config.getProperties();
+
+ assertThat(props.get("test"), is("test2"));
+ assertThat(props.get("testValue1"), is("value"));
+ assertThat(props.get("testValue2"), is("anotherValue"));
+ }
+
+ private static String dump(Map<String, String> properties) {
+ StringBuilder b = new StringBuilder();
+ new TreeMap<>(properties).forEach((k,v)->b.append(" " + k + " = " + v + '\n'));
+ return b.toString();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/82b83f20/examples/6-fileobserver-example/src/test/java/org/aspache/tamaya/examples/fileobserver/ObservedConfigTest.java
----------------------------------------------------------------------
diff --git a/examples/6-fileobserver-example/src/test/java/org/aspache/tamaya/examples/fileobserver/ObservedConfigTest.java b/examples/6-fileobserver-example/src/test/java/org/aspache/tamaya/examples/fileobserver/ObservedConfigTest.java
deleted file mode 100644
index 674a35d..0000000
--- a/examples/6-fileobserver-example/src/test/java/org/aspache/tamaya/examples/fileobserver/ObservedConfigTest.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.aspache.tamaya.examples.fileobserver;
-
-import org.apache.tamaya.ConfigurationProvider;
-import org.apache.tamaya.examples.fileobserver.TestConfigView;
-import org.junit.Test;
-
-import java.util.Map;
-import java.util.TreeMap;
-
-/**
- * Test (currently manual) to test configuration changes.
- */
-public class ObservedConfigTest {
-
- @Test
- public void testInitialConfig(){
- for(int i=0;i<100;i++){
- System.out.println(dump(ConfigurationProvider.getConfiguration().with(TestConfigView.of()).getProperties()));
- System.out.println("=======================================================================");
- try{
- Thread.sleep(2000L);
- }
- catch(Exception e){
- // ignore
- e.printStackTrace();
- }
- }
-
- }
-
- private static String dump(Map<String, String> properties) {
- StringBuilder b = new StringBuilder();
- new TreeMap<>(properties).forEach((k,v)->b.append(" " + k + " = " + v + '\n'));
- return b.toString();
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/82b83f20/examples/6-fileobserver-example/src/test/java/org/aspache/tamaya/examples/fileobserver/TestConfigView.java
----------------------------------------------------------------------
diff --git a/examples/6-fileobserver-example/src/test/java/org/aspache/tamaya/examples/fileobserver/TestConfigView.java b/examples/6-fileobserver-example/src/test/java/org/aspache/tamaya/examples/fileobserver/TestConfigView.java
new file mode 100644
index 0000000..0095f19
--- /dev/null
+++ b/examples/6-fileobserver-example/src/test/java/org/aspache/tamaya/examples/fileobserver/TestConfigView.java
@@ -0,0 +1,90 @@
+/*
+ * 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.aspache.tamaya.examples.fileobserver;
+
+import org.apache.tamaya.ConfigException;
+import org.apache.tamaya.ConfigOperator;
+import org.apache.tamaya.Configuration;
+import org.apache.tamaya.ConfigurationProvider;
+import org.apache.tamaya.TypeLiteral;
+import org.apache.tamaya.spi.PropertyConverter;
+
+import java.util.List;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+
+/**
+ * Created by Anatole on 24.03.2015.
+ */
+public class TestConfigView implements ConfigOperator{
+
+ private static final TestConfigView INSTANCE = new TestConfigView();
+
+ private TestConfigView(){}
+
+ public static ConfigOperator of(){
+ return INSTANCE;
+ }
+
+ @Override
+ public Configuration operate(Configuration config) {
+ return new Configuration() {
+ @Override
+ public Map<String, String> getProperties() {
+ return config.getProperties().entrySet().stream().filter(e -> e.getKey().startsWith("test")).collect(
+ Collectors.toMap(en -> en.getKey(), en -> en.getValue()));
+ }
+ /**
+ * Accesses the current String value for the given key and tries to convert it
+ * using the {@link org.apache.tamaya.spi.PropertyConverter} instances provided by the current
+ * {@link org.apache.tamaya.spi.ConfigurationContext}.
+ *
+ * @param key the property's absolute, or relative path, e.g. @code
+ * a/b/c/d.myProperty}.
+ * @param type The target type required, not null.
+ * @param <T> the value type
+ * @return the converted value, never null.
+ */
+ @Override
+ public <T> T get(String key, TypeLiteral<T> type) {
+ String value = get(key);
+ if (value != null) {
+ List<PropertyConverter<T>> converters = ConfigurationProvider.getConfigurationContext()
+ .getPropertyConverters(type);
+ for (PropertyConverter<T> converter : converters) {
+ try {
+ T t = converter.convert(value);
+ if (t != null) {
+ return t;
+ }
+ } catch (Exception e) {
+ Logger.getLogger(getClass().getName())
+ .log(Level.FINEST, "PropertyConverter: " + converter + " failed to convert value: "
+ + value, e);
+ }
+ }
+ throw new ConfigException("Unparseable config value for type: " + type.getRawType().getName() + ": " + key);
+ }
+ return null;
+ }
+ };
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/82b83f20/examples/6-fileobserver-example/src/test/java/org/aspache/tamaya/examples/fileobserver/TestObservingProvider.java
----------------------------------------------------------------------
diff --git a/examples/6-fileobserver-example/src/test/java/org/aspache/tamaya/examples/fileobserver/TestObservingProvider.java b/examples/6-fileobserver-example/src/test/java/org/aspache/tamaya/examples/fileobserver/TestObservingProvider.java
new file mode 100644
index 0000000..0588f13
--- /dev/null
+++ b/examples/6-fileobserver-example/src/test/java/org/aspache/tamaya/examples/fileobserver/TestObservingProvider.java
@@ -0,0 +1,37 @@
+/*
+ * 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.aspache.tamaya.examples.fileobserver;
+
+import org.apache.tamaya.events.folderobserver.ObservingPropertySourceProvider;
+import org.apache.tamaya.format.formats.PropertiesFormat;
+
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+/**
+ * Test configuration property source provider that observes a directory and updated the config if necessary.
+ */
+public class TestObservingProvider extends ObservingPropertySourceProvider{
+
+ public static Path propertyLocation;
+
+ public TestObservingProvider(){
+ super(propertyLocation, new PropertiesFormat());
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/82b83f20/examples/6-fileobserver-example/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider
----------------------------------------------------------------------
diff --git a/examples/6-fileobserver-example/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider b/examples/6-fileobserver-example/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider
new file mode 100644
index 0000000..7e19865
--- /dev/null
+++ b/examples/6-fileobserver-example/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider
@@ -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.aspache.tamaya.examples.fileobserver.TestObservingProvider
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/82b83f20/examples/6-fileobserver-example/src/test/resources/test.properties
----------------------------------------------------------------------
diff --git a/examples/6-fileobserver-example/src/test/resources/test.properties b/examples/6-fileobserver-example/src/test/resources/test.properties
new file mode 100644
index 0000000..af06631
--- /dev/null
+++ b/examples/6-fileobserver-example/src/test/resources/test.properties
@@ -0,0 +1,21 @@
+#
+# 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.
+#
+testValue1=value
+test=test2
+a=b
\ No newline at end of file