Fixed versions and distribution.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/commit/c005f288
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/c005f288
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/c005f288
Branch: refs/heads/master
Commit: c005f2889ca6508d8eb35cc8df90bbdfbd6c67a7
Parents: b4586e1
Author: anatole <anatole@apache.org>
Authored: Tue Mar 8 17:07:53 2016 +0100
Committer: anatole <anatole@apache.org>
Committed: Tue Mar 8 17:07:53 2016 +0100
----------------------------------------------------------------------
pom.xml | 1 +
remote/pom.xml | 76 ++++++++++
.../tamaya/remote/BaseRemotePropertySource.java | 149 +++++++++++++++++++
3 files changed, 226 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/c005f288/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index ba43998..543b896 100644
--- a/pom.xml
+++ b/pom.xml
@@ -40,6 +40,7 @@ under the License.
<module>jodatime</module>
<module>sysprops</module>
<module>ui</module>
+ <module>remote</module>
</modules>
</project>
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/c005f288/remote/pom.xml
----------------------------------------------------------------------
diff --git a/remote/pom.xml b/remote/pom.xml
new file mode 100644
index 0000000..e47b2bf
--- /dev/null
+++ b/remote/pom.xml
@@ -0,0 +1,76 @@
+<!--
+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.
+-->
+<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</groupId>
+ <artifactId>tamaya-extensions</artifactId>
+ <version>0.2-incubating-SNAPSHOT</version>
+ <relativePath>..</relativePath>
+ </parent>
+
+ <artifactId>tamaya-remote</artifactId>
+ <name>Apache Tamaya Modules - Remote PropertySource</name>
+ <packaging>bundle</packaging>
+
+ <properties>
+ <jdkVersion>1.7</jdkVersion>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.tamaya</groupId>
+ <artifactId>tamaya-api</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>javax.servlet-api</artifactId>
+ <version>3.1.0</version>
+ </dependency>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>tamaya-json</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ <configuration>
+ <instructions>
+ <Export-Package>
+ org.apache.tamaya.remote
+ </Export-Package>
+ </instructions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/c005f288/remote/src/main/java/org/apache/tamaya/remote/BaseRemotePropertySource.java
----------------------------------------------------------------------
diff --git a/remote/src/main/java/org/apache/tamaya/remote/BaseRemotePropertySource.java b/remote/src/main/java/org/apache/tamaya/remote/BaseRemotePropertySource.java
new file mode 100644
index 0000000..0fd1c67
--- /dev/null
+++ b/remote/src/main/java/org/apache/tamaya/remote/BaseRemotePropertySource.java
@@ -0,0 +1,149 @@
+/*
+ * 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.remote;
+
+import org.apache.tamaya.format.ConfigurationData;
+import org.apache.tamaya.format.ConfigurationFormat;
+import org.apache.tamaya.json.JSONFormat;
+import org.apache.tamaya.spi.PropertySource;
+import org.apache.tamaya.spi.PropertyValue;
+
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Abstract base class for implementing a PropertySource that reads configuration data from
a remote resource. It uses
+ * by default the JSON format as defined by the JSON module.
+ */
+public abstract class BaseRemotePropertySource implements PropertySource{
+
+ private static final ConfigurationFormat DEFAULT_FORMAT = new JSONFormat();
+
+ private Map<String,String> properties = new HashMap<>();
+
+ protected BaseRemotePropertySource(){
+ reload();
+ }
+
+ @Override
+ public String getName() {
+ return getClass().getSimpleName();
+ }
+
+ @Override
+ public Map<String, String> getProperties() {
+ return properties;
+ }
+
+ /**
+ * Reloads the remote configuration. If reloads fails to whatever reasons the already
loaded configuration will
+ * stay untouched.
+ */
+ public void reload(){
+ ConfigurationFormat format = getConfigurationFormat();
+ for(URL url:getAccessURLs()) {
+ try(InputStream is = url.openStream()) {
+ ConfigurationData data = format.readConfiguration(url.toExternalForm(), is);
+ if(data!=null){
+ Map<String,String> newProperties = mapConfigurationData(data);
+ // the configs served by the tamaya server module has a 'data' root section
containing the
+ // config entries. if not present, we assume an alternate format, which
is sued as is...
+ if(!newProperties.isEmpty()){
+ this.properties = newProperties;
+ Logger.getLogger(getClass().getName()).info(
+ "Reloaded remote config from: " + url + ", entries read:
" + this.properties.size());
+ }
+ }
+ }
+ catch(Exception e){
+ Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Failed to load
config from url: " + url, e);
+ }
+ }
+ }
+
+ protected abstract Collection<URL> getAccessURLs();
+
+ protected ConfigurationFormat getConfigurationFormat(){
+ return DEFAULT_FORMAT;
+ }
+
+ protected Map<String,String> mapConfigurationData(ConfigurationData data){
+ Map<String,String> readProperties;
+ if(data!=null){
+ readProperties = data.getCombinedProperties();
+ if(readProperties!=null){
+ Map<String,String> newProperties = new HashMap<>();
+ for(Map.Entry<String,String> en:readProperties.entrySet()){
+ // filter data entries
+ newProperties.put(en.getKey(), en.getValue());
+ }
+ // the configs served by the tamaya server module has a 'data' root section
containing the
+ // config entries. if not present, we assume an alternate format, which
is sued as is...
+ if(newProperties.isEmpty()){
+ Logger.getLogger(getClass().getName()).info(
+ "Loaded remote config from: " + data.getResource() + ", does
not have a data section, using as is...");
+ newProperties = readProperties;
+ }
+ Logger.getLogger(getClass().getName()).info(
+ "Reloaded remote config from: " + data.getResource() + ", entriea
read: " + this.properties.size());
+ return newProperties;
+ }
+ }
+ return Collections.emptyMap();
+ }
+
+ @Override
+ public boolean isScannable(){
+ return true;
+ }
+
+ @Override
+ public PropertyValue get(String key) {
+ return PropertyValue.of(key,getProperties().get(key),getName());
+ }
+
+ @Override
+ public int getOrdinal(){
+ PropertyValue configuredOrdinal = get(TAMAYA_ORDINAL);
+ if(configuredOrdinal!=null){
+ try{
+ return Integer.parseInt(configuredOrdinal.getValue());
+ } catch(Exception e){
+ Logger.getLogger(getClass().getName()).log(Level.WARNING,
+ "Configured Ordinal is not an int number: " + configuredOrdinal,
e);
+ }
+ }
+ return getDefaultOrdinal();
+ }
+
+ /**
+ * Returns the default ordinal used, when no ordinal is set, or the ordinal was not
parseable to an int value.
+ * @return the default ordinal used, by default 0.
+ */
+ public int getDefaultOrdinal(){
+ return 0;
+ }
+
+}
|