Repository: incubator-tamaya Updated Branches: refs/heads/master 75430495e -> 4de9e8b41 (forced update)
[TAMAYA-203] Readded the second example. It shows how to implement and to use a custom implementation of PropertySource and PropertySourceProvider. Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/4de9e8b4 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/4de9e8b4 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/4de9e8b4 Branch: refs/heads/master Commit: 4de9e8b410c44410c2a7d3282b4b7acdb44dbe1e Parents: ad770e6 Author: Oliver B. Fischer <[email protected]> Authored: Sat Dec 17 15:22:58 2016 +0100 Committer: Oliver B. Fischer <[email protected]> Committed: Sat Dec 17 15:37:25 2016 +0100 ---------------------------------------------------------------------- examples/02-custom-property-source/pom.xml | 51 +++++++++++ .../examples/custompropertysource/Main.java | 94 ++++++++++++++++++++ .../SimplePropertySource.java | 61 +++++++++++++ .../SimplePropertySourceProvider.java | 47 ++++++++++ .../custompropertysource/package-info.java | 19 ++++ .../META-INF/MyOtherConfigProperties.properties | 19 ++++ .../META-INF/javaconfiguration.properties | 23 +++++ .../org.apache.tamaya.spi.PropertySource | 19 ++++ ...org.apache.tamaya.spi.PropertySourceProvider | 19 ++++ .../src/main/resources/cfgOther/a.properties | 19 ++++ .../src/main/resources/cfgOther/b.properties | 19 ++++ .../src/main/resources/cfgOther/c.properties | 20 +++++ examples/pom.xml | 1 + 13 files changed, 411 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4de9e8b4/examples/02-custom-property-source/pom.xml ---------------------------------------------------------------------- diff --git a/examples/02-custom-property-source/pom.xml b/examples/02-custom-property-source/pom.xml new file mode 100644 index 0000000..31c624d --- /dev/null +++ b/examples/02-custom-property-source/pom.xml @@ -0,0 +1,51 @@ +<?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.examples</groupId> + <artifactId>examples</artifactId> + <version>0.3-incubating-SNAPSHOT</version> + </parent> + + <artifactId>02-custom-property-source</artifactId> + <name>Apache Tamaya Custom Propertysource Example</name> + + <dependencies> + <dependency> + <groupId>org.apache.tamaya</groupId> + <artifactId>tamaya-core</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>java-hamcrest</artifactId> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + +</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4de9e8b4/examples/02-custom-property-source/src/main/java/org/apache/tamaya/examples/custompropertysource/Main.java ---------------------------------------------------------------------- diff --git a/examples/02-custom-property-source/src/main/java/org/apache/tamaya/examples/custompropertysource/Main.java b/examples/02-custom-property-source/src/main/java/org/apache/tamaya/examples/custompropertysource/Main.java new file mode 100644 index 0000000..985a678 --- /dev/null +++ b/examples/02-custom-property-source/src/main/java/org/apache/tamaya/examples/custompropertysource/Main.java @@ -0,0 +1,94 @@ +/* + * 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.custompropertysource; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; + +import java.io.PrintStream; +import java.util.Map; +import java.util.TreeMap; +import java.util.logging.LogManager; +import java.util.logging.Logger; + +import static java.lang.String.format; + +/** + * Small example demonstrating the usage of a self-written {@link org.apache.tamaya.spi.PropertySource} + * and {@link org.apache.tamaya.spi.PropertySourceProvider}. + * + * <p> + * {@link SimplePropertySource} is a custom implementation of a + * {@link org.apache.tamaya.spi.PropertySource}. It reads its properties from a + * {@value SimplePropertySource#CONFIG_PROPERTIES_LOCATION}. As it is an implementation + * of {@code PropertySource} and it is listed as service implementation + * in {@code META-INF/services/org.apache.tamaya.spi.PropertySource} Tamaya is able + * to find and to use it through the Service Provider Interface service of Java. + * </p> + * + * <p> + * The same applies to {@link SimplePropertySourceProvider} which is an implementation + * of {@link org.apache.tamaya.spi.PropertySourceProvider}. Tamaya finds implementations + * of a {@link org.apache.tamaya.spi.PropertySourceProvider} also through the + * Service Provider Interface service of Java. Therefore it is listed in + * {@code META-INF/services/org.apache.tamaya.spi.PropertySourceProvider} file. + * </p> + */ +public class Main { + /* + * 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) { + Configuration cfg = ConfigurationProvider.getConfiguration(); + + System.out.println("*****************************************************"); + System.out.println("Simple Example (with a PropertySource and a Provider)"); + System.out.println("*****************************************************"); + System.out.println(); + System.out.println("Example Metadata:"); + System.out.println("\tType : " + cfg.get("example.type")); + System.out.println("\tName : " + cfg.get("example.name")); + System.out.println("\tDescription : " + cfg.get("example.description")); + System.out.println("\tVersion : " + cfg.get("example.version")); + System.out.println("\tAuthor : " + cfg.get("example.author")); + System.out.println(); + System.out.println("\tPath : " + cfg.get("Path")); + System.out.println("\taProp : " + cfg.get("aProp")); + System.out.println(); + + dump(cfg.getProperties(), System.out); + } + + private static void dump(Map<String, String> properties, PrintStream stream) { + stream.println("FULL DUMP:\n\n"); + + for (Map.Entry<String, String> en : new TreeMap<>(properties).entrySet()) { + stream.println(format("\t%s = %s", en.getKey(), en.getValue())); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4de9e8b4/examples/02-custom-property-source/src/main/java/org/apache/tamaya/examples/custompropertysource/SimplePropertySource.java ---------------------------------------------------------------------- diff --git a/examples/02-custom-property-source/src/main/java/org/apache/tamaya/examples/custompropertysource/SimplePropertySource.java b/examples/02-custom-property-source/src/main/java/org/apache/tamaya/examples/custompropertysource/SimplePropertySource.java new file mode 100644 index 0000000..6d05e19 --- /dev/null +++ b/examples/02-custom-property-source/src/main/java/org/apache/tamaya/examples/custompropertysource/SimplePropertySource.java @@ -0,0 +1,61 @@ +/* + * 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.custompropertysource; + +import org.apache.tamaya.core.propertysource.BasePropertySource; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +public class SimplePropertySource extends BasePropertySource { + + public static final String CONFIG_PROPERTIES_LOCATION = "META-INF/MyOtherConfigProperties.properties"; + private Map<String,String> props = new HashMap<>(); + + public SimplePropertySource() throws IOException { + URL url = ClassLoader.getSystemClassLoader().getResource(CONFIG_PROPERTIES_LOCATION); + Properties properties = new Properties(); + + try(InputStream is = url.openStream()){ + properties.load(is); + + for(Map.Entry en: properties.entrySet()){ + props.put(en.getKey().toString(), en.getValue().toString()); + } + } + finally{ + props = Collections.unmodifiableMap(props); + } + } + + @Override + public String getName() { + return CONFIG_PROPERTIES_LOCATION; + } + + @Override + public Map<String, String> getProperties() { + return props; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4de9e8b4/examples/02-custom-property-source/src/main/java/org/apache/tamaya/examples/custompropertysource/SimplePropertySourceProvider.java ---------------------------------------------------------------------- diff --git a/examples/02-custom-property-source/src/main/java/org/apache/tamaya/examples/custompropertysource/SimplePropertySourceProvider.java b/examples/02-custom-property-source/src/main/java/org/apache/tamaya/examples/custompropertysource/SimplePropertySourceProvider.java new file mode 100644 index 0000000..56fecf5 --- /dev/null +++ b/examples/02-custom-property-source/src/main/java/org/apache/tamaya/examples/custompropertysource/SimplePropertySourceProvider.java @@ -0,0 +1,47 @@ +/* + * 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.custompropertysource; + +import org.apache.tamaya.core.propertysource.SimplePropertySource; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertySourceProvider; + +import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +public class SimplePropertySourceProvider implements PropertySourceProvider { + private static final String[] RESOURCES = { + "cfgOther/a.properties", "cfgOther/b.properties", "cfgOther/c.properties" + }; + + @Override + public Collection<PropertySource> getPropertySources() { + List<PropertySource> propertySources = new ArrayList<>(); + + for (String res : RESOURCES) { + URL url = ClassLoader.getSystemClassLoader().getResource(res); + propertySources.add(new SimplePropertySource(url)); + } + + return Collections.unmodifiableList(propertySources); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4de9e8b4/examples/02-custom-property-source/src/main/java/org/apache/tamaya/examples/custompropertysource/package-info.java ---------------------------------------------------------------------- diff --git a/examples/02-custom-property-source/src/main/java/org/apache/tamaya/examples/custompropertysource/package-info.java b/examples/02-custom-property-source/src/main/java/org/apache/tamaya/examples/custompropertysource/package-info.java new file mode 100644 index 0000000..089f69c --- /dev/null +++ b/examples/02-custom-property-source/src/main/java/org/apache/tamaya/examples/custompropertysource/package-info.java @@ -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 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.custompropertysource; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4de9e8b4/examples/02-custom-property-source/src/main/resources/META-INF/MyOtherConfigProperties.properties ---------------------------------------------------------------------- diff --git a/examples/02-custom-property-source/src/main/resources/META-INF/MyOtherConfigProperties.properties b/examples/02-custom-property-source/src/main/resources/META-INF/MyOtherConfigProperties.properties new file mode 100644 index 0000000..a6f77e4 --- /dev/null +++ b/examples/02-custom-property-source/src/main/resources/META-INF/MyOtherConfigProperties.properties @@ -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. +# +example.description=A simple example now also loading its own files... \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4de9e8b4/examples/02-custom-property-source/src/main/resources/META-INF/javaconfiguration.properties ---------------------------------------------------------------------- diff --git a/examples/02-custom-property-source/src/main/resources/META-INF/javaconfiguration.properties b/examples/02-custom-property-source/src/main/resources/META-INF/javaconfiguration.properties new file mode 100644 index 0000000..b9845b6 --- /dev/null +++ b/examples/02-custom-property-source/src/main/resources/META-INF/javaconfiguration.properties @@ -0,0 +1,23 @@ +# +# 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. +# +example.type=standalone +example.name=simple-propertysource +example.description=A minimal example using an self written property source. +example.version=1 [email protected] http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4de9e8b4/examples/02-custom-property-source/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource ---------------------------------------------------------------------- diff --git a/examples/02-custom-property-source/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource b/examples/02-custom-property-source/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource new file mode 100644 index 0000000..219275b --- /dev/null +++ b/examples/02-custom-property-source/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource @@ -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.examples.custompropertysource.SimplePropertySource \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4de9e8b4/examples/02-custom-property-source/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider ---------------------------------------------------------------------- diff --git a/examples/02-custom-property-source/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider b/examples/02-custom-property-source/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider new file mode 100644 index 0000000..453126f --- /dev/null +++ b/examples/02-custom-property-source/src/main/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.apache.tamaya.examples.custompropertysource.SimplePropertySourceProvider \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4de9e8b4/examples/02-custom-property-source/src/main/resources/cfgOther/a.properties ---------------------------------------------------------------------- diff --git a/examples/02-custom-property-source/src/main/resources/cfgOther/a.properties b/examples/02-custom-property-source/src/main/resources/cfgOther/a.properties new file mode 100644 index 0000000..5457546 --- /dev/null +++ b/examples/02-custom-property-source/src/main/resources/cfgOther/a.properties @@ -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. +# +a=found A! http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4de9e8b4/examples/02-custom-property-source/src/main/resources/cfgOther/b.properties ---------------------------------------------------------------------- diff --git a/examples/02-custom-property-source/src/main/resources/cfgOther/b.properties b/examples/02-custom-property-source/src/main/resources/cfgOther/b.properties new file mode 100644 index 0000000..4dcc40d --- /dev/null +++ b/examples/02-custom-property-source/src/main/resources/cfgOther/b.properties @@ -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. +# +b=found B! http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4de9e8b4/examples/02-custom-property-source/src/main/resources/cfgOther/c.properties ---------------------------------------------------------------------- diff --git a/examples/02-custom-property-source/src/main/resources/cfgOther/c.properties b/examples/02-custom-property-source/src/main/resources/cfgOther/c.properties new file mode 100644 index 0000000..f282a20 --- /dev/null +++ b/examples/02-custom-property-source/src/main/resources/cfgOther/c.properties @@ -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. +# +c=found C! +aProp=from c.properties http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4de9e8b4/examples/pom.xml ---------------------------------------------------------------------- diff --git a/examples/pom.xml b/examples/pom.xml index 6c06676..a59fefa 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -39,6 +39,7 @@ under the License. <modules> <module>01-minimal</module> + <module>02-custom-property-source</module> </modules>
