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/03/24 22:20:19 UTC
[02/16] incubator-tamaya git commit: Simplified core property source
implementation. Added META-INF/javaconfiguration.propertie as default
property source.
Simplified core property source implementation.
Added META-INF/javaconfiguration.propertie as default property source.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/4b5c8297
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/4b5c8297
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/4b5c8297
Branch: refs/heads/master
Commit: 4b5c82974fd791e2fa4dc8c4ee559a2e05c19c5b
Parents: f859159
Author: anatole <an...@apache.org>
Authored: Tue Mar 24 15:51:19 2015 +0100
Committer: anatole <an...@apache.org>
Committed: Tue Mar 24 15:51:19 2015 +0100
----------------------------------------------------------------------
.../core/internal/PropertiesFileLoader.java | 204 +++++++++----------
.../core/propertysource/BasePropertySource.java | 63 +++---
.../core/propertysource/DefaultOrdinal.java | 54 -----
.../EnvironmentPropertySource.java | 20 +-
.../PropertiesFilePropertySource.java | 51 -----
.../PropertiesPropertySource.java | 51 -----
.../propertysource/SimplePropertySource.java | 127 ++++++++++++
.../propertysource/SystemPropertySource.java | 64 +++---
.../provider/JavaConfigurationProvider.java | 22 +-
.../org.apache.tamaya.spi.PropertySource | 21 ++
...org.apache.tamaya.spi.PropertySourceProvider | 19 ++
.../propertysource/BasePropertySourceTest.java | 10 +-
.../PropertiesFilePropertySourceTest.java | 31 ++-
.../SystemPropertySourceTest.java | 8 +-
.../testdata/TestPropertyDefaultSource.java | 2 +-
.../testdata/TestPropertySourceProvider.java | 2 +-
.../core/internal/PropertiesFileLoader.java | 87 --------
.../core/propertysource/BasePropertySource.java | 62 ++----
.../core/propertysource/DefaultOrdinal.java | 54 -----
.../EnvironmentPropertySource.java | 15 +-
.../PropertiesPropertySource.java | 51 -----
.../propertysource/SimplePropertySource.java | 108 ++++++++++
.../propertysource/SystemPropertySource.java | 59 +++---
.../provider/JavaConfigurationProvider.java | 20 +-
.../ConfigurationFromPropertiesFileIT.java | 90 --------
.../core/internal/PropertiesFileLoaderTest.java | 84 --------
.../propertysource/BasePropertySourceTest.java | 9 +-
.../PropertiesFilePropertySourceTest.java | 31 ++-
.../SystemPropertySourceTest.java | 6 +-
.../testdata/TestPropertyDefaultSource.java | 4 +-
.../testdata/TestPropertySourceProvider.java | 2 +-
31 files changed, 577 insertions(+), 854 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java7/core/src/main/java/org/apache/tamaya/core/internal/PropertiesFileLoader.java
----------------------------------------------------------------------
diff --git a/java7/core/src/main/java/org/apache/tamaya/core/internal/PropertiesFileLoader.java b/java7/core/src/main/java/org/apache/tamaya/core/internal/PropertiesFileLoader.java
index ffd9a41..cf2d637 100644
--- a/java7/core/src/main/java/org/apache/tamaya/core/internal/PropertiesFileLoader.java
+++ b/java7/core/src/main/java/org/apache/tamaya/core/internal/PropertiesFileLoader.java
@@ -1,102 +1,102 @@
-/*
- * 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.core.internal;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URL;
-import java.util.Enumeration;
-import java.util.HashSet;
-import java.util.Objects;
-import java.util.Properties;
-import java.util.Set;
-
-public final class PropertiesFileLoader {
-
-
- private PropertiesFileLoader() {
- // no instantiation
- }
-
-
- /**
- * loads all properties-files with the given name.
- * If the name do not end with {@code .properties} it will be appended
- *
- * @param name of the properties file
- *
- * @return URLs of properties-files or
- * an empty {@link java.util.Set} if no files has been found
- *
- * @throws java.io.IOException in case of problems loading the properties-files
- */
- public static Set<URL> resolvePropertiesFiles(String name) throws IOException {
- Objects.requireNonNull(name);
-
- if (!name.endsWith(".properties")) {
- name = name + ".properties";
- }
-
- Set<URL> urls = new HashSet<>();
-
- Enumeration<URL> files = Thread.currentThread().getContextClassLoader().getResources(name);
- while (files.hasMoreElements()) {
- urls.add(files.nextElement());
- }
-
- return urls;
- }
-
-
- /**
- * loads the Properties from the given URL
- *
- * @param propertiesFile {@link java.net.URL} to load Properties from
- *
- * @return loaded {@link java.util.Properties}
- *
- * @throws IllegalStateException in case of an error while reading properties-file
- */
- public static Properties load(URL propertiesFile) {
-
- Properties properties = new Properties();
-
- InputStream stream = null;
- try {
- stream = propertiesFile.openStream();
-
- if (stream != null) {
- properties.load(stream);
- }
- } catch (IOException e) {
- throw new IllegalStateException("Error loading Properties " + propertiesFile, e);
- } finally {
- if (stream != null) {
- try {
- stream.close();
- } catch (IOException e) {
- // bad luck -> stream is already closed
- }
- }
- }
-
- return properties;
- }
-
-}
+///*
+// * 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.core.internal;
+//
+//import java.io.IOException;
+//import java.io.InputStream;
+//import java.net.URL;
+//import java.util.Enumeration;
+//import java.util.HashSet;
+//import java.util.Objects;
+//import java.util.Properties;
+//import java.util.Set;
+//
+//public final class PropertiesFileLoader {
+//
+//
+// private PropertiesFileLoader() {
+// // no instantiation
+// }
+//
+//
+// /**
+// * loads all properties-files with the given name.
+// * If the name do not end with {@code .properties} it will be appended
+// *
+// * @param name of the properties file
+// *
+// * @return URLs of properties-files or
+// * an empty {@link java.util.Set} if no files has been found
+// *
+// * @throws java.io.IOException in case of problems loading the properties-files
+// */
+// public static Set<URL> resolvePropertiesFiles(String name) throws IOException {
+// Objects.requireNonNull(name);
+//
+// if (!name.endsWith(".properties")) {
+// name = name + ".properties";
+// }
+//
+// Set<URL> urls = new HashSet<>();
+//
+// Enumeration<URL> files = Thread.currentThread().getContextClassLoader().getResources(name);
+// while (files.hasMoreElements()) {
+// urls.add(files.nextElement());
+// }
+//
+// return urls;
+// }
+//
+//
+// /**
+// * loads the Properties from the given URL
+// *
+// * @param propertiesFile {@link java.net.URL} to load Properties from
+// *
+// * @return loaded {@link java.util.Properties}
+// *
+// * @throws IllegalStateException in case of an error while reading properties-file
+// */
+// public static Properties load(URL propertiesFile) {
+//
+// Properties properties = new Properties();
+//
+// InputStream stream = null;
+// try {
+// stream = propertiesFile.openStream();
+//
+// if (stream != null) {
+// properties.load(stream);
+// }
+// } catch (IOException e) {
+// throw new IllegalStateException("Error loading Properties " + propertiesFile, e);
+// } finally {
+// if (stream != null) {
+// try {
+// stream.close();
+// } catch (IOException e) {
+// // bad luck -> stream is already closed
+// }
+// }
+// }
+//
+// return properties;
+// }
+//
+//}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java7/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java
----------------------------------------------------------------------
diff --git a/java7/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java b/java7/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java
index 2172094..b78149f 100644
--- a/java7/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java
+++ b/java7/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java
@@ -20,63 +20,46 @@ package org.apache.tamaya.core.propertysource;
import org.apache.tamaya.spi.PropertySource;
-import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
- * A base class for {@link PropertySource}s. It provides a {@link #initializeOrdinal(int)} method that
- * reads the ordinal from the config source itself, allowing the ordinal to be "self-configured" by
- * the configuration read.
+ * Abstract {@link org.apache.tamaya.spi.PropertySource} that allows to set a default ordinal that will be used, if no
+ * ordinal is provided with the config.
*/
-public abstract class BasePropertySource implements PropertySource {
-
- private static final Logger LOG = Logger.getLogger(BasePropertySource.class.getName());
-
-
- private int ordinal = DefaultOrdinal.PROPERTY_SOURCE;
+public abstract class BasePropertySource implements PropertySource{
+ /** default ordinal that will be used, if no ordinal is provided with the config. */
+ private final int defaultOrdinal;
+ /**
+ * Constructor.
+ * @param defaultOrdinal default ordinal that will be used, if no ordinal is provided with the config.
+ */
+ protected BasePropertySource(int defaultOrdinal){
+ this.defaultOrdinal = defaultOrdinal;
+ }
@Override
public int getOrdinal() {
- return ordinal;
+ String configuredOrdinal = get(TAMAYA_ORDINAL);
+ if(configuredOrdinal!=null){
+ try{
+ return Integer.parseInt(configuredOrdinal);
+ } catch(Exception e){
+ Logger.getLogger(getClass().getName()).log(Level.WARNING,
+ "Configured Ordinal is not an int number: " + configuredOrdinal, e);
+ }
+ }
+ return defaultOrdinal;
}
-
@Override
public String get(String key) {
- Objects.requireNonNull(key, "key must not be null");
return getProperties().get(key);
}
@Override
- public boolean isScannable() {
+ public boolean isScannable(){
return true;
}
-
-
- /**
- * Initializing the ordinal of this {@link PropertySource} with the given defaultOrdinal.
- *
- * If {@link PropertySource#TAMAYA_ORDINAL} is present via {@link #get(String)} and the
- * value is a valid {@link Integer} then, the defaultOrdinal will be overridden.
- *
- * @param defaultOrdinal of the {@link PropertySource}
- */
- protected void initializeOrdinal(final int defaultOrdinal) {
- this.ordinal = defaultOrdinal;
-
- String ordinal = get(PropertySource.TAMAYA_ORDINAL);
- if (ordinal != null) {
-
- try {
- this.ordinal = Integer.parseInt(ordinal);
- } catch (NumberFormatException e) {
- LOG.log(Level.WARNING,
- "Specified {0} is not a valid Integer value: {1} - using defaultOrdinal {2}",
- new Object[]{PropertySource.TAMAYA_ORDINAL, ordinal, defaultOrdinal});
- }
- }
- }
-
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java7/core/src/main/java/org/apache/tamaya/core/propertysource/DefaultOrdinal.java
----------------------------------------------------------------------
diff --git a/java7/core/src/main/java/org/apache/tamaya/core/propertysource/DefaultOrdinal.java b/java7/core/src/main/java/org/apache/tamaya/core/propertysource/DefaultOrdinal.java
deleted file mode 100644
index ff35e3f..0000000
--- a/java7/core/src/main/java/org/apache/tamaya/core/propertysource/DefaultOrdinal.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.apache.tamaya.core.propertysource;
-
-
-/**
- * This interface defines the default ordinals for the 'standard'
- * {@link org.apache.tamaya.spi.PropertySource}s
- *
- * DefaultOrdinals can be overwritten via {@link org.apache.tamaya.spi.PropertySource#TAMAYA_ORDINAL}
- */
-public final class DefaultOrdinal {
-
- /** Private constructor. */
- private DefaultOrdinal(){}
-
- /**
- * default ordinal for {@link org.apache.tamaya.core.propertysource.BasePropertySource} if
- * not overriden in each class
- */
- public static final int PROPERTY_SOURCE = 1000;
-
- /**
- * default ordinal for {@link org.apache.tamaya.core.propertysource.SystemPropertySource}
- */
- public static final int SYSTEM_PROPERTIES = 400;
-
- /**
- * default ordinal for {@link org.apache.tamaya.core.propertysource.EnvironmentPropertySource}
- */
- public static final int ENVIRONMENT_PROPERTIES = 300;
-
- /**
- * default ordinal for {@link org.apache.tamaya.core.propertysource.PropertiesFilePropertySource}
- */
- public static final int FILE_PROPERTIES = 100;
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java7/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java
----------------------------------------------------------------------
diff --git a/java7/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java b/java7/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java
index 3f60b29..a8a1b11 100644
--- a/java7/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java
+++ b/java7/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java
@@ -18,6 +18,8 @@
*/
package org.apache.tamaya.core.propertysource;
+import org.apache.tamaya.spi.PropertySource;
+
import java.util.Map;
/**
@@ -26,12 +28,17 @@ import java.util.Map;
* {@code export myprop=myval} on UNIX Systems or<br />
* {@code set myprop=myval} on Windows
*/
-public class EnvironmentPropertySource extends BasePropertySource {
+public class EnvironmentPropertySource implements PropertySource {
- public EnvironmentPropertySource() {
- initializeOrdinal(DefaultOrdinal.ENVIRONMENT_PROPERTIES);
- }
+ /**
+ * default ordinal for {@link org.apache.tamaya.core.propertysource.EnvironmentPropertySource}
+ */
+ public static final int DEFAULT_ORDINAL = 300;
+ @Override
+ public int getOrdinal() {
+ return DEFAULT_ORDINAL;
+ }
@Override
public String getName() {
@@ -39,6 +46,11 @@ public class EnvironmentPropertySource extends BasePropertySource {
}
@Override
+ public String get(String key) {
+ return getProperties().get(key);
+ }
+
+ @Override
public Map<String, String> getProperties() {
return System.getenv(); // already a map and unmodifiable
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java7/core/src/main/java/org/apache/tamaya/core/propertysource/PropertiesFilePropertySource.java
----------------------------------------------------------------------
diff --git a/java7/core/src/main/java/org/apache/tamaya/core/propertysource/PropertiesFilePropertySource.java b/java7/core/src/main/java/org/apache/tamaya/core/propertysource/PropertiesFilePropertySource.java
deleted file mode 100644
index 86597f1..0000000
--- a/java7/core/src/main/java/org/apache/tamaya/core/propertysource/PropertiesFilePropertySource.java
+++ /dev/null
@@ -1,51 +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.core.propertysource;
-
-import org.apache.tamaya.core.internal.PropertiesFileLoader;
-
-import java.net.URL;
-
-/**
- * {@link org.apache.tamaya.spi.PropertySource} for properties-files
- */
-public class PropertiesFilePropertySource extends PropertiesPropertySource {
-
-
- private String fileName;
-
-
- public PropertiesFilePropertySource(URL propertiesFile) {
- super(PropertiesFileLoader.load(propertiesFile));
-
- initializeOrdinal(DefaultOrdinal.FILE_PROPERTIES);
- this.fileName = propertiesFile.toExternalForm();
- }
-
-
- @Override
- public String getName() {
- return fileName;
- }
-
- @Override
- public boolean isScannable() {
- return true;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java7/core/src/main/java/org/apache/tamaya/core/propertysource/PropertiesPropertySource.java
----------------------------------------------------------------------
diff --git a/java7/core/src/main/java/org/apache/tamaya/core/propertysource/PropertiesPropertySource.java b/java7/core/src/main/java/org/apache/tamaya/core/propertysource/PropertiesPropertySource.java
deleted file mode 100644
index cf68bd6..0000000
--- a/java7/core/src/main/java/org/apache/tamaya/core/propertysource/PropertiesPropertySource.java
+++ /dev/null
@@ -1,51 +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.core.propertysource;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Properties;
-
-/**
- * base class for {@link org.apache.tamaya.spi.PropertySource}s which are based on
- * {@link java.util.Properties}
- */
-abstract class PropertiesPropertySource extends BasePropertySource {
-
- protected Map<String, String> properties;
-
-
- // package private to not expose this class
- PropertiesPropertySource(Properties properties) {
- Map<String, String> props = new HashMap<>();
-
- for (String key : properties.stringPropertyNames()) {
- props.put(key, properties.getProperty(key));
- }
-
- this.properties = Collections.unmodifiableMap(props);
- }
-
-
- @Override
- public Map<String, String> getProperties() {
- return properties;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java7/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java
----------------------------------------------------------------------
diff --git a/java7/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java b/java7/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java
new file mode 100644
index 0000000..128cc52
--- /dev/null
+++ b/java7/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java
@@ -0,0 +1,127 @@
+/*
+ * 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.core.propertysource;
+
+import org.apache.tamaya.ConfigException;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Properties;
+
+/**
+ * Simple implementation of a {@link org.apache.tamaya.spi.PropertySource} for properties-files.
+ */
+public class SimplePropertySource extends BasePropertySource{
+ /** The property source name. */
+ private String name;
+ /** The current properties. */
+ private Map<String,String> properties;
+
+ /**
+ * Creates a new Properties based PropertySource based on the given URL.
+ * @param propertiesLocation the URL encoded location, not null.
+ */
+ public SimplePropertySource(File propertiesLocation) {
+ super(0);
+ try{
+ this.properties = load(propertiesLocation.toURI().toURL());
+ this.name = propertiesLocation.toString();
+ }
+ catch(IOException e){
+ throw new ConfigException("Failed to load properties from " + propertiesLocation,e);
+ }
+ }
+
+ /**
+ * Creates a new Properties based PropertySource based on the given URL.
+ * @param propertiesLocation the URL encoded location, not null.
+ */
+ public SimplePropertySource(URL propertiesLocation) {
+ super(0);
+ this.properties = load(propertiesLocation);
+ this.name = propertiesLocation.toExternalForm();
+ }
+
+ /**
+ * Creates a new Properties based PropertySource based on the given URL.
+ * @param name The property source name
+ * @param propertiesLocation the URL encoded location, not null.
+ */
+ public SimplePropertySource(String name, URL propertiesLocation) {
+ super(0);
+ this.properties = load(propertiesLocation);
+ this.name = Objects.requireNonNull(name);
+ }
+
+ @Override
+ public int getOrdinal() {
+ return 0;
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public String get(String key) {
+ return null;
+ }
+
+ @Override
+ public Map<String, String> getProperties() {
+ return this.properties;
+ }
+
+ @Override
+ public boolean isScannable() {
+ return false;
+ }
+
+ /**
+ * loads the Properties from the given URL
+ *
+ * @param propertiesFile {@link java.net.URL} to load Properties from
+ *
+ * @return loaded {@link java.util.Properties}
+ *
+ * @throws IllegalStateException in case of an error while reading properties-file
+ */
+ private Map<String,String> load(URL propertiesFile) {
+ Map<String,String> properties = new HashMap<>();
+ try (InputStream stream = propertiesFile.openStream()) {
+ Properties props = new Properties();
+ if (stream != null) {
+ props.load(stream);
+ }
+ for(Map.Entry<Object,Object> en:props.entrySet()){
+ properties.put(en.getKey().toString(), en.getValue().toString());
+ }
+ } catch (IOException e) {
+ throw new ConfigException("Error loading properties " + propertiesFile, e);
+ }
+ return properties;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java7/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java
----------------------------------------------------------------------
diff --git a/java7/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java b/java7/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java
index 962a6af..09f48bd 100644
--- a/java7/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java
+++ b/java7/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java
@@ -18,6 +18,8 @@
*/
package org.apache.tamaya.core.propertysource;
+import org.apache.tamaya.spi.PropertySource;
+
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -28,13 +30,14 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* This {@link org.apache.tamaya.spi.PropertySource} manages the system properties.
*/
-public class SystemPropertySource extends PropertiesPropertySource {
+public class SystemPropertySource implements PropertySource {
/**
- * Lock for internal synchronization.
+ * default ordinal for {@link org.apache.tamaya.core.propertysource.SystemPropertiesPropertySource}
*/
- private ReentrantReadWriteLock propertySourceLock = new ReentrantReadWriteLock();
+ public static final int DEFAULT_ORDINAL = 1000;
+ private volatile Map<String,String> cachedProperties;
/**
* previous System.getProperties().hashCode()
@@ -44,11 +47,23 @@ public class SystemPropertySource extends PropertiesPropertySource {
public SystemPropertySource() {
- super(System.getProperties());
+ cachedProperties = loadProperties();
previousHash = System.getProperties().hashCode();
- initializeOrdinal(DefaultOrdinal.SYSTEM_PROPERTIES);
}
+ private Map<String, String> loadProperties() {
+ Map<String,String> props = new HashMap<>();
+ Properties sysProps = System.getProperties();
+ for(Map.Entry<Object,Object> en: sysProps.entrySet()) {
+ props.put(en.toString(), en.toString());
+ }
+ return props;
+ }
+
+ @Override
+ public int getOrdinal() {
+ return DEFAULT_ORDINAL;
+ }
@Override
public String getName() {
@@ -56,32 +71,27 @@ public class SystemPropertySource extends PropertiesPropertySource {
}
@Override
- public Map<String, String> getProperties() {
+ public String get(String key) {
+ return getProperties().get(key);
+ }
- Lock writeLock = propertySourceLock.writeLock();
+ @Override
+ public Map<String, String> getProperties() {
// only need to reload and fill our map if something has changed
- try {
- writeLock.lock();
- if (previousHash != System.getProperties().hashCode()) {
-
- if (previousHash != System.getProperties().hashCode()) {
-
- Properties systemProperties = System.getProperties();
- Map<String, String> properties = new HashMap<>();
-
- for (String propertyName : systemProperties.stringPropertyNames()) {
- properties.put(propertyName, System.getProperty(propertyName));
- }
-
- this.properties = Collections.unmodifiableMap(properties);
- previousHash = systemProperties.hashCode();
- }
+ // synchonization was removed, Instance was marked as volatile. In the worst case it
+ // is reloaded twice, but the values will be the same.
+ if (previousHash != System.getProperties().hashCode()) {
+ Properties systemProperties = System.getProperties();
+ Map<String, String> properties = new HashMap<>();
+
+ for (String propertyName : systemProperties.stringPropertyNames()) {
+ properties.put(propertyName, System.getProperty(propertyName));
}
- } finally {
- writeLock.unlock();
- }
- return super.getProperties();
+ this.cachedProperties = Collections.unmodifiableMap(properties);
+ previousHash = systemProperties.hashCode();
+ }
+ return this.cachedProperties;
}
@Override
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java7/core/src/main/java/org/apache/tamaya/core/provider/JavaConfigurationProvider.java
----------------------------------------------------------------------
diff --git a/java7/core/src/main/java/org/apache/tamaya/core/provider/JavaConfigurationProvider.java b/java7/core/src/main/java/org/apache/tamaya/core/provider/JavaConfigurationProvider.java
index bb741de..bc6a4ec 100644
--- a/java7/core/src/main/java/org/apache/tamaya/core/provider/JavaConfigurationProvider.java
+++ b/java7/core/src/main/java/org/apache/tamaya/core/provider/JavaConfigurationProvider.java
@@ -18,10 +18,8 @@
*/
package org.apache.tamaya.core.provider;
-import static org.apache.tamaya.core.internal.PropertiesFileLoader.resolvePropertiesFiles;
-
import org.apache.tamaya.ConfigException;
-import org.apache.tamaya.core.propertysource.PropertiesFilePropertySource;
+import org.apache.tamaya.core.propertysource.SimplePropertySource;
import org.apache.tamaya.spi.PropertySource;
import org.apache.tamaya.spi.PropertySourceProvider;
@@ -30,29 +28,31 @@ import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.Enumeration;
import java.util.List;
/**
* Provider which reads all {@code javaconfiguration.properties} files from classpath
*/
public class JavaConfigurationProvider implements PropertySourceProvider {
-
+ /** Default location in the classpath, where Tamaya looks for configuration by default. */
+ public static final String DEFAULT_PROPERTIES_FILE_NAME="META-INF/javaconfiguration.properties";
@Override
public Collection<PropertySource> getPropertySources() {
-
List<PropertySource> propertySources = new ArrayList<>();
-
- //X TODO maybe put javaconf... in META-INF
-
try {
- for (URL url : resolvePropertiesFiles("javaconfiguration.properties")) {
- propertySources.add(new PropertiesFilePropertySource(url));
+ Enumeration<URL> urls = Thread.currentThread().getContextClassLoader()
+ .getResources(DEFAULT_PROPERTIES_FILE_NAME);
+ while (urls.hasMoreElements()) {
+ propertySources.add(new SimplePropertySource(urls.nextElement()));
}
+
} catch (IOException e) {
throw new ConfigException("Error while loading javaconfiguration.properties", e);
}
-
return Collections.unmodifiableList(propertySources);
}
+
+
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java7/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource
----------------------------------------------------------------------
diff --git a/java7/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource b/java7/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource
new file mode 100644
index 0000000..9f453f5
--- /dev/null
+++ b/java7/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource
@@ -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.
+#
+org.apache.tamaya.core.propertysource.EnvironmentPropertySource
+org.apache.tamaya.core.propertysource.SystemPropertySource
+
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java7/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider
----------------------------------------------------------------------
diff --git a/java7/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider b/java7/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider
new file mode 100644
index 0000000..4535a09
--- /dev/null
+++ b/java7/core/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.core.provider.JavaConfigurationProvider
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java7/core/src/test/java/org/apache/tamaya/core/test/propertysource/BasePropertySourceTest.java
----------------------------------------------------------------------
diff --git a/java7/core/src/test/java/org/apache/tamaya/core/test/propertysource/BasePropertySourceTest.java b/java7/core/src/test/java/org/apache/tamaya/core/test/propertysource/BasePropertySourceTest.java
index e6b789c..a8325c6 100644
--- a/java7/core/src/test/java/org/apache/tamaya/core/test/propertysource/BasePropertySourceTest.java
+++ b/java7/core/src/test/java/org/apache/tamaya/core/test/propertysource/BasePropertySourceTest.java
@@ -19,7 +19,6 @@
package org.apache.tamaya.core.test.propertysource;
import org.apache.tamaya.core.propertysource.BasePropertySource;
-import org.apache.tamaya.core.propertysource.DefaultOrdinal;
import org.apache.tamaya.spi.PropertySource;
import org.junit.Assert;
import org.junit.Test;
@@ -33,7 +32,7 @@ public class BasePropertySourceTest {
@Test
public void testGetOrdinal() {
- PropertySource defaultPropertySource = new BasePropertySource() {
+ PropertySource defaultPropertySource = new BasePropertySource(56) {
@Override
public String getName() {
@@ -51,7 +50,7 @@ public class BasePropertySourceTest {
}
};
- Assert.assertEquals(DefaultOrdinal.PROPERTY_SOURCE, defaultPropertySource.getOrdinal());
+ Assert.assertEquals(56, defaultPropertySource.getOrdinal());
Assert.assertEquals(1000, new OverriddenOrdinalPropertySource().getOrdinal());
// propertySource with invalid ordinal
@@ -66,7 +65,7 @@ public class BasePropertySourceTest {
private static class OverriddenOrdinalPropertySource extends BasePropertySource {
private OverriddenOrdinalPropertySource() {
- initializeOrdinal(250);
+ super(250);
}
@Override
@@ -80,13 +79,12 @@ public class BasePropertySourceTest {
map.put(PropertySource.TAMAYA_ORDINAL, "1000");
return map;
}
-
}
private static class OverriddenInvalidOrdinalPropertySource extends BasePropertySource {
private OverriddenInvalidOrdinalPropertySource() {
- initializeOrdinal(1);
+ super(1);
}
@Override
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java7/core/src/test/java/org/apache/tamaya/core/test/propertysource/PropertiesFilePropertySourceTest.java
----------------------------------------------------------------------
diff --git a/java7/core/src/test/java/org/apache/tamaya/core/test/propertysource/PropertiesFilePropertySourceTest.java b/java7/core/src/test/java/org/apache/tamaya/core/test/propertysource/PropertiesFilePropertySourceTest.java
index 6ad7790..9fef3be 100644
--- a/java7/core/src/test/java/org/apache/tamaya/core/test/propertysource/PropertiesFilePropertySourceTest.java
+++ b/java7/core/src/test/java/org/apache/tamaya/core/test/propertysource/PropertiesFilePropertySourceTest.java
@@ -18,34 +18,28 @@
*/
package org.apache.tamaya.core.test.propertysource;
-import org.apache.tamaya.core.internal.PropertiesFileLoader;
-import org.apache.tamaya.core.propertysource.DefaultOrdinal;
-import org.apache.tamaya.core.propertysource.PropertiesFilePropertySource;
+import org.apache.tamaya.core.propertysource.SimplePropertySource;
import org.apache.tamaya.spi.PropertySource;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
-import java.net.URL;
-import java.util.Map;
-import java.util.Properties;
-
public class PropertiesFilePropertySourceTest {
- private PropertiesFilePropertySource testfilePropertySource;
- private PropertiesFilePropertySource overrideOrdinalPropertySource;
+ private SimplePropertySource testfilePropertySource;
+ private SimplePropertySource overrideOrdinalPropertySource;
@Before
public void initTest() {
- testfilePropertySource = new PropertiesFilePropertySource(Thread.currentThread().getContextClassLoader().getResource("testfile.properties"));
- overrideOrdinalPropertySource = new PropertiesFilePropertySource(Thread.currentThread().getContextClassLoader().getResource("overrideOrdinal.properties"));
+ testfilePropertySource = new SimplePropertySource(Thread.currentThread().getContextClassLoader().getResource("testfile.properties"));
+ overrideOrdinalPropertySource = new SimplePropertySource(Thread.currentThread().getContextClassLoader().getResource("overrideOrdinal.properties"));
}
@Test
public void testGetOrdinal() {
- Assert.assertEquals(DefaultOrdinal.FILE_PROPERTIES, testfilePropertySource.getOrdinal());
+ Assert.assertEquals(0, testfilePropertySource.getOrdinal());
Assert.assertEquals(Integer.parseInt(overrideOrdinalPropertySource.get(PropertySource.TAMAYA_ORDINAL)), overrideOrdinalPropertySource.getOrdinal());
}
@@ -60,12 +54,11 @@ public class PropertiesFilePropertySourceTest {
@Test
public void testGetProperties() throws Exception {
- Properties expectedProperties = PropertiesFileLoader.load(new URL(testfilePropertySource.getName()));
-
- Assert.assertEquals(expectedProperties.size(), testfilePropertySource.getProperties().size());
-
- for (Map.Entry<String, String> entry : testfilePropertySource.getProperties().entrySet()) {
- Assert.assertEquals(expectedProperties.getProperty(entry.getKey()), entry.getValue());
- }
+ Assert.assertEquals(5, testfilePropertySource.getProperties().size());
+ Assert.assertTrue(testfilePropertySource.getProperties().containsKey("key1"));
+ Assert.assertTrue(testfilePropertySource.getProperties().containsKey("key2"));
+ Assert.assertTrue(testfilePropertySource.getProperties().containsKey("key3"));
+ Assert.assertTrue(testfilePropertySource.getProperties().containsKey("key4"));
+ Assert.assertTrue(testfilePropertySource.getProperties().containsKey("key5"));
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java7/core/src/test/java/org/apache/tamaya/core/test/propertysource/SystemPropertySourceTest.java
----------------------------------------------------------------------
diff --git a/java7/core/src/test/java/org/apache/tamaya/core/test/propertysource/SystemPropertySourceTest.java b/java7/core/src/test/java/org/apache/tamaya/core/test/propertysource/SystemPropertySourceTest.java
index 35ebc80..2eba79e 100644
--- a/java7/core/src/test/java/org/apache/tamaya/core/test/propertysource/SystemPropertySourceTest.java
+++ b/java7/core/src/test/java/org/apache/tamaya/core/test/propertysource/SystemPropertySourceTest.java
@@ -18,7 +18,6 @@
*/
package org.apache.tamaya.core.test.propertysource;
-import org.apache.tamaya.core.propertysource.DefaultOrdinal;
import org.apache.tamaya.core.propertysource.SystemPropertySource;
import org.apache.tamaya.spi.PropertySource;
import org.junit.Assert;
@@ -36,11 +35,12 @@ public class SystemPropertySourceTest {
public void testGetOrdinal() throws Exception {
// test the default ordinal
- Assert.assertEquals(DefaultOrdinal.SYSTEM_PROPERTIES, testPropertySource.getOrdinal());
+ Assert.assertEquals(SystemPropertySource.DEFAULT_ORDINAL, testPropertySource.getOrdinal());
// set the ordinal to 1000
System.setProperty(PropertySource.TAMAYA_ORDINAL, "1000");
- Assert.assertEquals(1000, new SystemPropertySource().getOrdinal()); // currently its not possible to change ordinal at runtime
+ Assert.assertEquals(1000, new SystemPropertySource().getOrdinal());
+ // currently its not possible to change ordinal at runtime
// reset it to not destroy other tests!!
System.clearProperty(PropertySource.TAMAYA_ORDINAL);
@@ -98,4 +98,4 @@ public class SystemPropertySourceTest {
}
}
-}
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java7/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSource.java
----------------------------------------------------------------------
diff --git a/java7/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSource.java b/java7/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSource.java
index 74f3aff..6c8751c 100644
--- a/java7/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSource.java
+++ b/java7/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSource.java
@@ -32,7 +32,7 @@ public class TestPropertyDefaultSource extends BasePropertySource{
private Map<String,String> properties = new HashMap<>();
public TestPropertyDefaultSource() {
- initializeOrdinal(100);
+ super(100);
properties.put("name","Anatole");
properties.put("name2","Sabine");
properties = Collections.unmodifiableMap(properties);
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java7/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertySourceProvider.java
----------------------------------------------------------------------
diff --git a/java7/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertySourceProvider.java b/java7/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertySourceProvider.java
index 7288b9f..5706478 100644
--- a/java7/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertySourceProvider.java
+++ b/java7/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertySourceProvider.java
@@ -51,7 +51,7 @@ public class TestPropertySourceProvider implements PropertySourceProvider {
private Map<String, String> properties = new HashMap<>();
public MyPropertySource() {
- initializeOrdinal(200);
+ super(200);
properties.put("name", "Robin");
properties.put("name3", "Lukas");
properties.put("name4", "Sereina");
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java8/core/src/main/java/org/apache/tamaya/core/internal/PropertiesFileLoader.java
----------------------------------------------------------------------
diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/PropertiesFileLoader.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/PropertiesFileLoader.java
deleted file mode 100644
index 3e2f550..0000000
--- a/java8/core/src/main/java/org/apache/tamaya/core/internal/PropertiesFileLoader.java
+++ /dev/null
@@ -1,87 +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.core.internal;
-
-import org.apache.tamaya.ConfigException;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URL;
-import java.util.Enumeration;
-import java.util.Objects;
-import java.util.Properties;
-
-/**
- * Minimalistic loader for property files from the classpath.
- */
-public final class PropertiesFileLoader {
-
-
- private PropertiesFileLoader() {
- // no instantiation
- }
-
-
- /**
- * loads all properties-files with the given name.
- * If the name do not end with {@code .properties} it will be appended
- *
- * @param name of the properties file
- *
- * @return {@link Enumeration} of {@link URL}s for properties-files
- *
- * @throws IOException in case of problems loading the properties-files
- */
- public static Enumeration<URL> resolvePropertiesFiles(String name) throws IOException {
- Objects.requireNonNull(name);
-
- if (!name.endsWith(".properties")) {
- name = name + ".properties";
- }
-
- return Thread.currentThread().getContextClassLoader().getResources(name);
- }
-
-
- /**
- * loads the Properties from the given URL
- *
- * @param propertiesFile {@link URL} to load Properties from
- *
- * @return loaded {@link Properties}
- *
- * @throws IllegalStateException in case of an error while reading properties-file
- */
- public static Properties load(URL propertiesFile) {
-
- Properties properties = new Properties();
-
- try (InputStream stream = propertiesFile.openStream()) {
-
- if (stream != null) {
- properties.load(stream);
- }
- } catch (IOException e) {
- throw new ConfigException("Error loading properties " + propertiesFile, e);
- }
-
- return properties;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java8/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java
----------------------------------------------------------------------
diff --git a/java8/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java b/java8/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java
index 23fb27c..f7404c8 100644
--- a/java8/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java
+++ b/java8/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java
@@ -20,58 +20,36 @@ package org.apache.tamaya.core.propertysource;
import org.apache.tamaya.spi.PropertySource;
-import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
- * A base class for {@link PropertySource}s. It provides a {@link #initializeOrdinal(int)} method that
- * reads the ordinal from the config source itself, allowing the ordinal to be "self-configured" by
- * the configuration read.
+ * Abstract {@link org.apache.tamaya.spi.PropertySource} that allows to set a default ordinal that will be used, if no
+ * ordinal is provided with the config.
*/
-public abstract class BasePropertySource implements PropertySource {
-
- private static final Logger LOG = Logger.getLogger(BasePropertySource.class.getName());
-
-
- private int ordinal = DefaultOrdinal.PROPERTY_SOURCE;
-
-
- @Override
- public int getOrdinal() {
- return ordinal;
- }
-
-
- @Override
- public String get(String key) {
- Objects.requireNonNull(key, "key must not be null");
- return getProperties().get(key);
- }
-
+public abstract class BasePropertySource implements PropertySource{
+ /** default ordinal that will be used, if no ordinal is provided with the config. */
+ private final int defaultOrdinal;
/**
- * Initializing the ordinal of this {@link PropertySource} with the given defaultOrdinal.
- *
- * If {@link PropertySource#TAMAYA_ORDINAL} is present via {@link #get(String)} and the
- * value is a valid {@link Integer} then, the defaultOrdinal will be overridden.
- *
- * @param defaultOrdinal of the {@link PropertySource}
+ * COnstructor.
+ * @param defaultOrdinal default ordinal that will be used, if no ordinal is provided with the config.
*/
- protected void initializeOrdinal(final int defaultOrdinal) {
- this.ordinal = defaultOrdinal;
-
- String ordinal = get(PropertySource.TAMAYA_ORDINAL);
- if (ordinal != null) {
+ protected BasePropertySource(int defaultOrdinal){
+ this.defaultOrdinal = defaultOrdinal;
+ }
- try {
- this.ordinal = Integer.parseInt(ordinal);
- } catch (NumberFormatException e) {
- LOG.log(Level.WARNING,
- "Specified {0} is not a valid Integer value: {1} - using defaultOrdinal {2}",
- new Object[]{PropertySource.TAMAYA_ORDINAL, ordinal, defaultOrdinal});
+ @Override
+ public int getOrdinal() {
+ String configuredOrdinal = get(TAMAYA_ORDINAL);
+ if(configuredOrdinal!=null){
+ try{
+ return Integer.parseInt(configuredOrdinal);
+ } catch(Exception e){
+ Logger.getLogger(getClass().getName()).log(Level.WARNING, e,
+ () -> "Configured Ordinal is not an int number: " + configuredOrdinal);
}
}
+ return defaultOrdinal;
}
-
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java8/core/src/main/java/org/apache/tamaya/core/propertysource/DefaultOrdinal.java
----------------------------------------------------------------------
diff --git a/java8/core/src/main/java/org/apache/tamaya/core/propertysource/DefaultOrdinal.java b/java8/core/src/main/java/org/apache/tamaya/core/propertysource/DefaultOrdinal.java
deleted file mode 100644
index ff35e3f..0000000
--- a/java8/core/src/main/java/org/apache/tamaya/core/propertysource/DefaultOrdinal.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.apache.tamaya.core.propertysource;
-
-
-/**
- * This interface defines the default ordinals for the 'standard'
- * {@link org.apache.tamaya.spi.PropertySource}s
- *
- * DefaultOrdinals can be overwritten via {@link org.apache.tamaya.spi.PropertySource#TAMAYA_ORDINAL}
- */
-public final class DefaultOrdinal {
-
- /** Private constructor. */
- private DefaultOrdinal(){}
-
- /**
- * default ordinal for {@link org.apache.tamaya.core.propertysource.BasePropertySource} if
- * not overriden in each class
- */
- public static final int PROPERTY_SOURCE = 1000;
-
- /**
- * default ordinal for {@link org.apache.tamaya.core.propertysource.SystemPropertySource}
- */
- public static final int SYSTEM_PROPERTIES = 400;
-
- /**
- * default ordinal for {@link org.apache.tamaya.core.propertysource.EnvironmentPropertySource}
- */
- public static final int ENVIRONMENT_PROPERTIES = 300;
-
- /**
- * default ordinal for {@link org.apache.tamaya.core.propertysource.PropertiesFilePropertySource}
- */
- public static final int FILE_PROPERTIES = 100;
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java8/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java
----------------------------------------------------------------------
diff --git a/java8/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java b/java8/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java
index 596ea73..b927274 100644
--- a/java8/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java
+++ b/java8/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java
@@ -18,6 +18,8 @@
*/
package org.apache.tamaya.core.propertysource;
+import org.apache.tamaya.spi.PropertySource;
+
import java.util.Map;
/**
@@ -26,12 +28,17 @@ import java.util.Map;
* {@code export myprop=myval} on UNIX Systems or<br />
* {@code set myprop=myval} on Windows
*/
-public class EnvironmentPropertySource extends BasePropertySource {
+public class EnvironmentPropertySource implements PropertySource {
- public EnvironmentPropertySource() {
- initializeOrdinal(DefaultOrdinal.ENVIRONMENT_PROPERTIES);
- }
+ /**
+ * default ordinal for {@link EnvironmentPropertySource}
+ */
+ public static final int DEFAULT_ORDINAL = 300;
+ @Override
+ public int getOrdinal() {
+ return DEFAULT_ORDINAL;
+ }
@Override
public String getName() {
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java8/core/src/main/java/org/apache/tamaya/core/propertysource/PropertiesPropertySource.java
----------------------------------------------------------------------
diff --git a/java8/core/src/main/java/org/apache/tamaya/core/propertysource/PropertiesPropertySource.java b/java8/core/src/main/java/org/apache/tamaya/core/propertysource/PropertiesPropertySource.java
deleted file mode 100644
index cf68bd6..0000000
--- a/java8/core/src/main/java/org/apache/tamaya/core/propertysource/PropertiesPropertySource.java
+++ /dev/null
@@ -1,51 +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.core.propertysource;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Properties;
-
-/**
- * base class for {@link org.apache.tamaya.spi.PropertySource}s which are based on
- * {@link java.util.Properties}
- */
-abstract class PropertiesPropertySource extends BasePropertySource {
-
- protected Map<String, String> properties;
-
-
- // package private to not expose this class
- PropertiesPropertySource(Properties properties) {
- Map<String, String> props = new HashMap<>();
-
- for (String key : properties.stringPropertyNames()) {
- props.put(key, properties.getProperty(key));
- }
-
- this.properties = Collections.unmodifiableMap(props);
- }
-
-
- @Override
- public Map<String, String> getProperties() {
- return properties;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java8/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java
----------------------------------------------------------------------
diff --git a/java8/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java b/java8/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java
new file mode 100644
index 0000000..9692574
--- /dev/null
+++ b/java8/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java
@@ -0,0 +1,108 @@
+/*
+ * 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.core.propertysource;
+
+import org.apache.tamaya.ConfigException;
+import org.apache.tamaya.spi.PropertySource;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Properties;
+
+/**
+ * Simple implementation of a {@link org.apache.tamaya.spi.PropertySource} for properties-files.
+ */
+public class SimplePropertySource implements PropertySource{
+ /** The property source name. */
+ private String name;
+ /** The current properties. */
+ private Map<String,String> properties;
+
+ /**
+ * Creates a new Properties based PropertySource based on the given URL.
+ * @param propertiesLocation the URL encoded location, not null.
+ */
+ public SimplePropertySource(File propertiesLocation) {
+ try{
+ this.properties = load(propertiesLocation.toURI().toURL());
+ this.name = propertiesLocation.toString();
+ }
+ catch(IOException e){
+ throw new ConfigException("Failed to load properties from " + propertiesLocation,e);
+ }
+ }
+
+ /**
+ * Creates a new Properties based PropertySource based on the given URL.
+ * @param propertiesLocation the URL encoded location, not null.
+ */
+ public SimplePropertySource(URL propertiesLocation) {
+ this.properties = load(propertiesLocation);
+ this.name = propertiesLocation.toExternalForm();
+ }
+
+ /**
+ * Creates a new Properties based PropertySource based on the given URL.
+ * @param name The property source name
+ * @param propertiesLocation the URL encoded location, not null.
+ */
+ public SimplePropertySource(String name, URL propertiesLocation) {
+ this.properties = load(propertiesLocation);
+ this.name = Objects.requireNonNull(name);
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public Map<String, String> getProperties() {
+ return this.properties;
+ }
+
+ /**
+ * loads the Properties from the given URL
+ *
+ * @param propertiesFile {@link URL} to load Properties from
+ *
+ * @return loaded {@link java.util.Properties}
+ *
+ * @throws IllegalStateException in case of an error while reading properties-file
+ */
+ private Map<String,String> load(URL propertiesFile) {
+ Map<String,String> properties = new HashMap<>();
+ try (InputStream stream = propertiesFile.openStream()) {
+ Properties props = new Properties();
+ if (stream != null) {
+ props.load(stream);
+ }
+ props.forEach((k,v) -> properties.put(k.toString(), v.toString()));
+ } catch (IOException e) {
+ throw new ConfigException("Error loading properties " + propertiesFile, e);
+ }
+ return properties;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java8/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java
----------------------------------------------------------------------
diff --git a/java8/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java b/java8/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java
index abfebeb..4e7708e 100644
--- a/java8/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java
+++ b/java8/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java
@@ -18,23 +18,24 @@
*/
package org.apache.tamaya.core.propertysource;
+import org.apache.tamaya.spi.PropertySource;
+
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.StampedLock;
/**
* This {@link org.apache.tamaya.spi.PropertySource} manages the system properties.
*/
-public class SystemPropertySource extends PropertiesPropertySource {
+public class SystemPropertySource implements PropertySource {
/**
- * Lock for internal synchronization.
+ * default ordinal for {@link SystemPropertySource}
*/
- private StampedLock propertySourceLock = new StampedLock();
+ public static final int DEFAULT_ORDINAL = 1000;
+ private volatile Map<String,String> cachedProperties;
/**
* previous System.getProperties().hashCode()
@@ -44,11 +45,21 @@ public class SystemPropertySource extends PropertiesPropertySource {
public SystemPropertySource() {
- super(System.getProperties());
+ cachedProperties = loadProperties();
previousHash = System.getProperties().hashCode();
- initializeOrdinal(DefaultOrdinal.SYSTEM_PROPERTIES);
}
+ private Map<String, String> loadProperties() {
+ Map<String,String> props = new HashMap<>();
+ Properties sysProps = System.getProperties();
+ sysProps.forEach((k,v) -> props.put(k.toString(),v.toString()));
+ return props;
+ }
+
+ @Override
+ public int getOrdinal() {
+ return DEFAULT_ORDINAL;
+ }
@Override
public String getName() {
@@ -57,30 +68,20 @@ public class SystemPropertySource extends PropertiesPropertySource {
@Override
public Map<String, String> getProperties() {
-
- Lock writeLock = propertySourceLock.asWriteLock();
// only need to reload and fill our map if something has changed
- try {
- writeLock.lock();
- if (previousHash != System.getProperties().hashCode()) {
-
- if (previousHash != System.getProperties().hashCode()) {
-
- Properties systemProperties = System.getProperties();
- Map<String, String> properties = new HashMap<>();
-
- for (String propertyName : systemProperties.stringPropertyNames()) {
- properties.put(propertyName, System.getProperty(propertyName));
- }
-
- this.properties = Collections.unmodifiableMap(properties);
- previousHash = systemProperties.hashCode();
- }
+ // synchonization was removed, Instance was marked as volatile. In the worst case it
+ // is reloaded twice, but the values will be the same.
+ if (previousHash != System.getProperties().hashCode()) {
+ Properties systemProperties = System.getProperties();
+ Map<String, String> properties = new HashMap<>();
+
+ for (String propertyName : systemProperties.stringPropertyNames()) {
+ properties.put(propertyName, System.getProperty(propertyName));
}
- } finally {
- writeLock.unlock();
- }
- return super.getProperties();
+ this.cachedProperties = Collections.unmodifiableMap(properties);
+ previousHash = systemProperties.hashCode();
+ }
+ return this.cachedProperties;
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java8/core/src/main/java/org/apache/tamaya/core/provider/JavaConfigurationProvider.java
----------------------------------------------------------------------
diff --git a/java8/core/src/main/java/org/apache/tamaya/core/provider/JavaConfigurationProvider.java b/java8/core/src/main/java/org/apache/tamaya/core/provider/JavaConfigurationProvider.java
index 4c3c956..bc6a4ec 100644
--- a/java8/core/src/main/java/org/apache/tamaya/core/provider/JavaConfigurationProvider.java
+++ b/java8/core/src/main/java/org/apache/tamaya/core/provider/JavaConfigurationProvider.java
@@ -19,8 +19,7 @@
package org.apache.tamaya.core.provider;
import org.apache.tamaya.ConfigException;
-import org.apache.tamaya.core.internal.PropertiesFileLoader;
-import org.apache.tamaya.core.propertysource.PropertiesFilePropertySource;
+import org.apache.tamaya.core.propertysource.SimplePropertySource;
import org.apache.tamaya.spi.PropertySource;
import org.apache.tamaya.spi.PropertySourceProvider;
@@ -36,27 +35,24 @@ import java.util.List;
* Provider which reads all {@code javaconfiguration.properties} files from classpath
*/
public class JavaConfigurationProvider implements PropertySourceProvider {
-
- public static final String DEFAULT_PROPERTIES_FILE_NAME="javaconfiguration.properties";
+ /** Default location in the classpath, where Tamaya looks for configuration by default. */
+ public static final String DEFAULT_PROPERTIES_FILE_NAME="META-INF/javaconfiguration.properties";
@Override
public Collection<PropertySource> getPropertySources() {
-
List<PropertySource> propertySources = new ArrayList<>();
-
- //X TODO maybe put javaconf... in META-INF
-
try {
-
- Enumeration<URL> urls = PropertiesFileLoader.resolvePropertiesFiles("META-INF/"+DEFAULT_PROPERTIES_FILE_NAME);
+ Enumeration<URL> urls = Thread.currentThread().getContextClassLoader()
+ .getResources(DEFAULT_PROPERTIES_FILE_NAME);
while (urls.hasMoreElements()) {
- propertySources.add(new PropertiesFilePropertySource(urls.nextElement()));
+ propertySources.add(new SimplePropertySource(urls.nextElement()));
}
} catch (IOException e) {
throw new ConfigException("Error while loading javaconfiguration.properties", e);
}
-
return Collections.unmodifiableList(propertySources);
}
+
+
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java8/core/src/test/java/org/apache/tamaya/core/internal/ConfigurationFromPropertiesFileIT.java
----------------------------------------------------------------------
diff --git a/java8/core/src/test/java/org/apache/tamaya/core/internal/ConfigurationFromPropertiesFileIT.java b/java8/core/src/test/java/org/apache/tamaya/core/internal/ConfigurationFromPropertiesFileIT.java
deleted file mode 100644
index bab9162..0000000
--- a/java8/core/src/test/java/org/apache/tamaya/core/internal/ConfigurationFromPropertiesFileIT.java
+++ /dev/null
@@ -1,90 +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.core.internal;
-
-import org.apache.tamaya.Configuration;
-import org.apache.tamaya.ConfigurationProvider;
-import org.apache.tamaya.core.propertysource.EnvironmentPropertySource;
-import org.apache.tamaya.core.propertysource.SystemPropertySource;
-import org.apache.tamaya.core.provider.JavaConfigurationProvider;
-import org.apache.tamaya.spi.ConfigurationContext;
-import org.apache.tamaya.spi.ConfigurationProviderSpi;
-import org.apache.tamaya.spi.PropertySource;
-import org.apache.tamaya.spi.PropertySourceProvider;
-import org.apache.tamaya.spi.ServiceContext;
-import org.hamcrest.CoreMatchers;
-import org.jboss.arquillian.container.test.api.Deployment;
-import org.jboss.arquillian.junit.Arquillian;
-import org.jboss.shrinkwrap.api.ShrinkWrap;
-import org.jboss.shrinkwrap.api.spec.JavaArchive;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import java.util.Optional;
-
-import static java.lang.System.getenv;
-import static org.hamcrest.CoreMatchers.not;
-import static org.hamcrest.Matchers.equalTo;
-import static org.hamcrest.Matchers.isEmptyOrNullString;
-import static org.hamcrest.Matchers.isEmptyString;
-import static org.hamcrest.Matchers.notNullValue;
-import static org.junit.Assert.assertThat;
-
-@RunWith(Arquillian.class)
-public class ConfigurationFromPropertiesFileIT {
-
- @Deployment
- public static JavaArchive createDeployment() {
- JavaArchive archive = ShrinkWrap.create(JavaArchive.class);
-
- archive.addPackage(ConfigurationProviderSpi.class.getPackage())
- .addPackage(ConfigurationProvider.class.getPackage());
-
- archive.addPackage(DefaultServiceContext.class.getPackage())
- .addPackage(JavaConfigurationProvider.class.getPackage())
- .addPackage(EnvironmentPropertySource.class.getPackage());
-
- archive.addAsServiceProvider(ServiceContext.class, DefaultServiceContext.class)
- .addAsServiceProvider(PropertySource.class, EnvironmentPropertySource.class,
- SystemPropertySource.class)
- .addAsServiceProvider(PropertySourceProvider.class, JavaConfigurationProvider.class)
- .addAsServiceProvider(ConfigurationContext.class, DefaultConfigurationContext.class)
- .addAsServiceProvider(ConfigurationProviderSpi.class, DefaultConfigurationProvider.class);
-
- archive.addAsManifestResource("x34.properties", JavaConfigurationProvider.DEFAULT_PROPERTIES_FILE_NAME);
-
- return archive;
- }
-
- @Test
- public void configurationIsBuildProperly() {
- Configuration config = ConfigurationProvider.getConfiguration();
-
- // Source is x34.properties
- assertThat(config.get("x34"), notNullValue());
- assertThat(config.get("x34"), equalTo("x34"));
- assertThat(config.get("x34.a.b.c"), notNullValue());
- assertThat(config.get("x34.a.b.c"), equalTo("C"));
-
- // Source is the environment USERNAME on Windows, USER on unix based systems
- assertThat(Optional.ofNullable(getenv("USERNAME")).orElse(getenv("USER")),
- not(isEmptyOrNullString()));
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java8/core/src/test/java/org/apache/tamaya/core/internal/PropertiesFileLoaderTest.java
----------------------------------------------------------------------
diff --git a/java8/core/src/test/java/org/apache/tamaya/core/internal/PropertiesFileLoaderTest.java b/java8/core/src/test/java/org/apache/tamaya/core/internal/PropertiesFileLoaderTest.java
deleted file mode 100644
index 436c0dd..0000000
--- a/java8/core/src/test/java/org/apache/tamaya/core/internal/PropertiesFileLoaderTest.java
+++ /dev/null
@@ -1,84 +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.core.internal;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-import java.net.URL;
-import java.util.Enumeration;
-import java.util.Properties;
-
-public class PropertiesFileLoaderTest {
-
- @Test
- public void resolveExistingPropertiesFileWithExtension() throws Exception {
- Properties expectedProperties = PropertiesFileLoader.load(Thread.currentThread()
- .getContextClassLoader()
- .getResource("testfile.properties"));
-
-
- // with .properties
- Enumeration<URL> urls = PropertiesFileLoader.resolvePropertiesFiles("testfile.properties");
- Assert.assertNotNull(urls);
- Assert.assertTrue(urls.hasMoreElements());
-
- Properties properties = PropertiesFileLoader.load(urls.nextElement());
- Assert.assertEquals(expectedProperties.size(), properties.size());
-
- Assert.assertFalse(urls.hasMoreElements());
- }
-
- @Test
- public void resolveExistingPropertiesFileWithoutExtension() throws Exception {
- Properties expectedProperties = PropertiesFileLoader.load(Thread.currentThread()
- .getContextClassLoader()
- .getResource("testfile.properties"));
-
- // without .properties
- Enumeration<URL> urls = PropertiesFileLoader.resolvePropertiesFiles("testfile");
- Assert.assertNotNull(urls);
- Assert.assertTrue(urls.hasMoreElements());
-
- Properties properties = PropertiesFileLoader.load(urls.nextElement());
- Assert.assertEquals(expectedProperties.size(), properties.size());
-
- Assert.assertFalse(urls.hasMoreElements());
- }
-
- @Test
- public void resolveNonExistingPropertiesFile() throws Exception {
- // with a while which doesn't exist
- Enumeration<URL> urls = PropertiesFileLoader.resolvePropertiesFiles("nonexistingfile.properties");
- Assert.assertNotNull(urls);
- Assert.assertFalse(urls.hasMoreElements());
- }
-
- @Test
- public void testLoad() {
- Properties properties = PropertiesFileLoader.load(Thread.currentThread().getContextClassLoader().getResource("testfile.properties"));
-
- Assert.assertNotNull(properties);
- Assert.assertEquals(5, properties.size());
-
- for (int i = 1; i < 6; i++) {
- Assert.assertEquals(properties.getProperty("key" + i), "val" + i);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java8/core/src/test/java/org/apache/tamaya/core/test/propertysource/BasePropertySourceTest.java
----------------------------------------------------------------------
diff --git a/java8/core/src/test/java/org/apache/tamaya/core/test/propertysource/BasePropertySourceTest.java b/java8/core/src/test/java/org/apache/tamaya/core/test/propertysource/BasePropertySourceTest.java
index 1a6a33c..a8325c6 100644
--- a/java8/core/src/test/java/org/apache/tamaya/core/test/propertysource/BasePropertySourceTest.java
+++ b/java8/core/src/test/java/org/apache/tamaya/core/test/propertysource/BasePropertySourceTest.java
@@ -19,7 +19,6 @@
package org.apache.tamaya.core.test.propertysource;
import org.apache.tamaya.core.propertysource.BasePropertySource;
-import org.apache.tamaya.core.propertysource.DefaultOrdinal;
import org.apache.tamaya.spi.PropertySource;
import org.junit.Assert;
import org.junit.Test;
@@ -33,7 +32,7 @@ public class BasePropertySourceTest {
@Test
public void testGetOrdinal() {
- PropertySource defaultPropertySource = new BasePropertySource() {
+ PropertySource defaultPropertySource = new BasePropertySource(56) {
@Override
public String getName() {
@@ -51,7 +50,7 @@ public class BasePropertySourceTest {
}
};
- Assert.assertEquals(DefaultOrdinal.PROPERTY_SOURCE, defaultPropertySource.getOrdinal());
+ Assert.assertEquals(56, defaultPropertySource.getOrdinal());
Assert.assertEquals(1000, new OverriddenOrdinalPropertySource().getOrdinal());
// propertySource with invalid ordinal
@@ -66,7 +65,7 @@ public class BasePropertySourceTest {
private static class OverriddenOrdinalPropertySource extends BasePropertySource {
private OverriddenOrdinalPropertySource() {
- initializeOrdinal(250);
+ super(250);
}
@Override
@@ -85,7 +84,7 @@ public class BasePropertySourceTest {
private static class OverriddenInvalidOrdinalPropertySource extends BasePropertySource {
private OverriddenInvalidOrdinalPropertySource() {
- initializeOrdinal(1);
+ super(1);
}
@Override
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java8/core/src/test/java/org/apache/tamaya/core/test/propertysource/PropertiesFilePropertySourceTest.java
----------------------------------------------------------------------
diff --git a/java8/core/src/test/java/org/apache/tamaya/core/test/propertysource/PropertiesFilePropertySourceTest.java b/java8/core/src/test/java/org/apache/tamaya/core/test/propertysource/PropertiesFilePropertySourceTest.java
index 6ad7790..9fef3be 100644
--- a/java8/core/src/test/java/org/apache/tamaya/core/test/propertysource/PropertiesFilePropertySourceTest.java
+++ b/java8/core/src/test/java/org/apache/tamaya/core/test/propertysource/PropertiesFilePropertySourceTest.java
@@ -18,34 +18,28 @@
*/
package org.apache.tamaya.core.test.propertysource;
-import org.apache.tamaya.core.internal.PropertiesFileLoader;
-import org.apache.tamaya.core.propertysource.DefaultOrdinal;
-import org.apache.tamaya.core.propertysource.PropertiesFilePropertySource;
+import org.apache.tamaya.core.propertysource.SimplePropertySource;
import org.apache.tamaya.spi.PropertySource;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
-import java.net.URL;
-import java.util.Map;
-import java.util.Properties;
-
public class PropertiesFilePropertySourceTest {
- private PropertiesFilePropertySource testfilePropertySource;
- private PropertiesFilePropertySource overrideOrdinalPropertySource;
+ private SimplePropertySource testfilePropertySource;
+ private SimplePropertySource overrideOrdinalPropertySource;
@Before
public void initTest() {
- testfilePropertySource = new PropertiesFilePropertySource(Thread.currentThread().getContextClassLoader().getResource("testfile.properties"));
- overrideOrdinalPropertySource = new PropertiesFilePropertySource(Thread.currentThread().getContextClassLoader().getResource("overrideOrdinal.properties"));
+ testfilePropertySource = new SimplePropertySource(Thread.currentThread().getContextClassLoader().getResource("testfile.properties"));
+ overrideOrdinalPropertySource = new SimplePropertySource(Thread.currentThread().getContextClassLoader().getResource("overrideOrdinal.properties"));
}
@Test
public void testGetOrdinal() {
- Assert.assertEquals(DefaultOrdinal.FILE_PROPERTIES, testfilePropertySource.getOrdinal());
+ Assert.assertEquals(0, testfilePropertySource.getOrdinal());
Assert.assertEquals(Integer.parseInt(overrideOrdinalPropertySource.get(PropertySource.TAMAYA_ORDINAL)), overrideOrdinalPropertySource.getOrdinal());
}
@@ -60,12 +54,11 @@ public class PropertiesFilePropertySourceTest {
@Test
public void testGetProperties() throws Exception {
- Properties expectedProperties = PropertiesFileLoader.load(new URL(testfilePropertySource.getName()));
-
- Assert.assertEquals(expectedProperties.size(), testfilePropertySource.getProperties().size());
-
- for (Map.Entry<String, String> entry : testfilePropertySource.getProperties().entrySet()) {
- Assert.assertEquals(expectedProperties.getProperty(entry.getKey()), entry.getValue());
- }
+ Assert.assertEquals(5, testfilePropertySource.getProperties().size());
+ Assert.assertTrue(testfilePropertySource.getProperties().containsKey("key1"));
+ Assert.assertTrue(testfilePropertySource.getProperties().containsKey("key2"));
+ Assert.assertTrue(testfilePropertySource.getProperties().containsKey("key3"));
+ Assert.assertTrue(testfilePropertySource.getProperties().containsKey("key4"));
+ Assert.assertTrue(testfilePropertySource.getProperties().containsKey("key5"));
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java8/core/src/test/java/org/apache/tamaya/core/test/propertysource/SystemPropertySourceTest.java
----------------------------------------------------------------------
diff --git a/java8/core/src/test/java/org/apache/tamaya/core/test/propertysource/SystemPropertySourceTest.java b/java8/core/src/test/java/org/apache/tamaya/core/test/propertysource/SystemPropertySourceTest.java
index 5495595..2eba79e 100644
--- a/java8/core/src/test/java/org/apache/tamaya/core/test/propertysource/SystemPropertySourceTest.java
+++ b/java8/core/src/test/java/org/apache/tamaya/core/test/propertysource/SystemPropertySourceTest.java
@@ -18,7 +18,6 @@
*/
package org.apache.tamaya.core.test.propertysource;
-import org.apache.tamaya.core.propertysource.DefaultOrdinal;
import org.apache.tamaya.core.propertysource.SystemPropertySource;
import org.apache.tamaya.spi.PropertySource;
import org.junit.Assert;
@@ -36,11 +35,12 @@ public class SystemPropertySourceTest {
public void testGetOrdinal() throws Exception {
// test the default ordinal
- Assert.assertEquals(DefaultOrdinal.SYSTEM_PROPERTIES, testPropertySource.getOrdinal());
+ Assert.assertEquals(SystemPropertySource.DEFAULT_ORDINAL, testPropertySource.getOrdinal());
// set the ordinal to 1000
System.setProperty(PropertySource.TAMAYA_ORDINAL, "1000");
- Assert.assertEquals(1000, new SystemPropertySource().getOrdinal()); // currently its not possible to change ordinal at runtime
+ Assert.assertEquals(1000, new SystemPropertySource().getOrdinal());
+ // currently its not possible to change ordinal at runtime
// reset it to not destroy other tests!!
System.clearProperty(PropertySource.TAMAYA_ORDINAL);
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/4b5c8297/java8/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSource.java
----------------------------------------------------------------------
diff --git a/java8/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSource.java b/java8/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSource.java
index b67d17e..0b82260 100644
--- a/java8/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSource.java
+++ b/java8/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSource.java
@@ -27,12 +27,12 @@ import java.util.Map;
/**
* Test provider reading properties from classpath:cfg/defaults/**.properties.
*/
-public class TestPropertyDefaultSource extends BasePropertySource{
+public class TestPropertyDefaultSource extends BasePropertySource {
private Map<String,String> properties = new HashMap<>();
public TestPropertyDefaultSource() {
- initializeOrdinal(100);
+ super(100);
properties.put("name","Anatole");
properties.put("name2","Sabine");
properties = Collections.unmodifiableMap(properties);