TAMAYA-72; Unified parameter ordering. Updated changes from core library.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/commit/9f3e3cf0 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/9f3e3cf0 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/9f3e3cf0 Branch: refs/heads/master Commit: 9f3e3cf080f0bb0621f54eeb58474852ea412c5e Parents: 9d34667 Author: anatole <[email protected]> Authored: Mon Nov 7 01:44:04 2016 +0100 Committer: anatole <[email protected]> Committed: Mon Nov 7 01:45:08 2016 +0100 ---------------------------------------------------------------------- modules/injection/cdi-se/pom.xml | 7 + .../tamaya/spisupport/BasePropertySource.java | 40 +++++- .../tamaya/spisupport/CLIPropertySource.java | 55 ++++++-- .../spisupport/EnvironmentPropertySource.java | 129 +++++++++++++---- .../tamaya/spisupport/MapPropertySource.java | 56 ++------ .../PropertiesResourcePropertySource.java | 65 ++++++--- .../tamaya/spisupport/SimplePropertySource.java | 30 ++-- .../tamaya/spisupport/SystemPropertySource.java | 141 ++++++++++++++----- 8 files changed, 360 insertions(+), 163 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f3e3cf0/modules/injection/cdi-se/pom.xml ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/pom.xml b/modules/injection/cdi-se/pom.xml index 848b95e..36b1b77 100644 --- a/modules/injection/cdi-se/pom.xml +++ b/modules/injection/cdi-se/pom.xml @@ -118,6 +118,13 @@ under the License. <scope>provided</scope> </dependency> <dependency> + <groupId>org.apache.geronimo.specs</groupId> + <artifactId>geronimo-atinject_1.0_spec</artifactId> + <version>${geronimo-atinject-1.0-spec.version}</version> + <scope>provided</scope> + <optional>true</optional> + </dependency> + <dependency> <groupId>org.apache.deltaspike.modules</groupId> <artifactId>deltaspike-test-control-module-api</artifactId> <version>${ds.version}</version> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f3e3cf0/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BasePropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BasePropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BasePropertySource.java index 0d90c8c..a79df5e 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BasePropertySource.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BasePropertySource.java @@ -20,7 +20,9 @@ package org.apache.tamaya.spisupport; import org.apache.tamaya.spi.PropertySource; import org.apache.tamaya.spi.PropertyValue; +import org.apache.tamaya.spi.PropertyValueBuilder; +import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -32,6 +34,9 @@ public abstract class BasePropertySource implements PropertySource{ /** default ordinal that will be used, if no ordinal is provided with the config. */ private final int defaultOrdinal; + /** Used if the ordinal has been set explicitly. */ + private volatile Integer ordinal; + /** * Constructor. * @param defaultOrdinal default ordinal that will be used, if no ordinal is provided with the config. @@ -52,13 +57,28 @@ public abstract class BasePropertySource implements PropertySource{ return getClass().getSimpleName(); } + /** + * Allows to set the ordinal of this property source explcitly. This will override any evaluated + * ordinal, or default ordinal. To reset an explcit ordinal call {@code setOrdinal(null);}. + * @param ordinal the explicit ordinal, or null. + */ + public void setOrdinal(Integer ordinal){ + this.ordinal = ordinal; + } + @Override public int getOrdinal() { + Integer ordinal = this.ordinal; + if(ordinal!=null){ + Logger.getLogger(getClass().getName()).finest( + "Using explicit ordinal '"+ordinal+"' for property source: " + getName()); + return ordinal; + } PropertyValue configuredOrdinal = get(TAMAYA_ORDINAL); if(configuredOrdinal!=null){ - try{ - return Integer.parseInt(configuredOrdinal.get(TAMAYA_ORDINAL)); - } catch(Exception e){ + 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); } @@ -76,7 +96,19 @@ public abstract class BasePropertySource implements PropertySource{ @Override public PropertyValue get(String key) { - return PropertyValue.of(key, getProperties().get(key), getName()); + Map<String,String> properties = getProperties(); + String val = properties.get(key); + if(val==null){ + return null; + } + PropertyValueBuilder b = new PropertyValueBuilder(key, val, getName()); + String metaKeyStart = "_" + key + "."; + for(Map.Entry<String,String> en:properties.entrySet()) { + if(en.getKey().startsWith(metaKeyStart) && en.getValue()!=null){ + b.addContextData(en.getKey().substring(metaKeyStart.length()), en.getValue()); + } + } + return b.build(); } @Override http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f3e3cf0/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/CLIPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/CLIPropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/CLIPropertySource.java index ec0a532..edcbafe 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/CLIPropertySource.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/CLIPropertySource.java @@ -1,20 +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 of the License at + * 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 + * 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. + * 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.spisupport; @@ -40,11 +40,36 @@ public class CLIPropertySource extends BasePropertySource{ initMainArgs(args); } - /** * Creates a new instance. */ - public CLIPropertySource(){} + public CLIPropertySource(){ + this(null); + } + + /** + * Creates a new instance, allows optionally to pass the main arguments. + * @param args the args, or null. + */ + public CLIPropertySource(String... args){ + if(args!=null){ + initMainArgs(args); + } + } + + /** + * Creates a new instance, allows optionally to pass the main arguments. + * @param args the args, or null. + * @param ordinal the ordinal to be applied. + */ + public CLIPropertySource(int ordinal, String... args){ + if(args!=null){ + initMainArgs(args); + } + setOrdinal(ordinal); + } + + /** * Configure the main arguments, hereby parsing and mapping the main arguments into http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f3e3cf0/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnvironmentPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnvironmentPropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnvironmentPropertySource.java index dca8060..7717c11 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnvironmentPropertySource.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnvironmentPropertySource.java @@ -1,24 +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 of the License at + * 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 + * 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. + * 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.spisupport; -import org.apache.tamaya.spi.PropertySource; import org.apache.tamaya.spi.PropertyValue; import java.util.Collections; @@ -27,13 +26,13 @@ import java.util.Map; import java.util.logging.Logger; /** - * This {@link PropertySource} provides all Properties which are set + * This {@link org.apache.tamaya.spi.PropertySource} provides all Properties which are set * via * {@code export myprop=myval} on UNIX Systems or * {@code set myprop=myval} on Windows. You can disable this feature by setting {@code tamaya.envprops.disable} * or {@code tamaya.defaults.disable}. */ -public class EnvironmentPropertySource implements PropertySource { +public class EnvironmentPropertySource extends BasePropertySource { private static final Logger LOG = Logger.getLogger(EnvironmentPropertySource.class.getName()); @@ -42,10 +41,45 @@ public class EnvironmentPropertySource implements PropertySource { */ public static final int DEFAULT_ORDINAL = 300; - private final boolean disabled = evaluateDisabled(); + /** + * Prefix that allows environment properties to virtually be mapped on specified sub section. + */ + private String prefix; + + /** + * If true, this property source does not return any properties. This is useful since this + * property source is applied by default, but can be switched off by setting the + * {@code tamaya.envprops.disable} system/environment property to {@code true}. + */ + private boolean disabled = false; + + + /** + * Creates a new instance. Also initializes the {@code prefix} and {@code disabled} properties + * from the system-/ environment properties: + * <pre> + * tamaya.envprops.prefix + * tamaya.envprops.disable + * </pre> + */ + public EnvironmentPropertySource(){ + initFromSystemProperties(); + } - private boolean evaluateDisabled() { - String value = System.getProperty("tamaya.envprops.disable"); + /** + * Initializes the {@code prefix} and {@code disabled} properties from the system-/ + * environment properties: + * <pre> + * tamaya.envprops.prefix + * tamaya.envprops.disable + * </pre> + */ + private void initFromSystemProperties() { + String value = System.getProperty("tamaya.envprops.prefix"); + if(value==null){ + prefix = System.getenv("tamaya.envprops.prefix"); + } + value = System.getProperty("tamaya.envprops.disable"); if(value==null){ value = System.getenv("tamaya.envprops.disable"); } @@ -55,14 +89,39 @@ public class EnvironmentPropertySource implements PropertySource { if(value==null){ value = System.getenv("tamaya.defaults.disable"); } - if(value==null){ - return false; + if(value!=null && !value.isEmpty()) { + this.disabled = Boolean.parseBoolean(value); } - return value.isEmpty() || Boolean.parseBoolean(value); + } + + /** + * Creates a new instance using a fixed ordinal value. + * @param ordinal the ordinal number. + */ + public EnvironmentPropertySource(int ordinal){ + this(null, ordinal); + } + + /** + * Creates a new instance. + * @param prefix the prefix to be used, or null. + * @param ordinal the ordinal to be used. + */ + public EnvironmentPropertySource(String prefix, int ordinal){ + this.prefix = prefix; + setOrdinal(ordinal); + } + + /** + * Creates a new instance. + * @param prefix the prefix to be used, or null. + */ + public EnvironmentPropertySource(String prefix){ + this.prefix = prefix; } @Override - public int getOrdinal() { + public int getDefaultOrdinal() { return DEFAULT_ORDINAL; } @@ -79,7 +138,11 @@ public class EnvironmentPropertySource implements PropertySource { if(disabled){ return null; } - return PropertyValue.of(key, System.getenv(key), getName()); + String prefix = this.prefix; + if(prefix==null) { + return PropertyValue.of(key, System.getenv(key), getName()); + } + return PropertyValue.of(key, System.getenv(key.substring(prefix.length())), getName()); } @Override @@ -87,11 +150,21 @@ public class EnvironmentPropertySource implements PropertySource { if(disabled){ return Collections.emptyMap(); } - Map<String, String> entries = new HashMap<>(System.getenv()); - for (Map.Entry<String, String> entry : System.getenv().entrySet()) { - entries.put("_" + entry.getKey() + ".source", getName()); + String prefix = this.prefix; + if(prefix==null) { + Map<String, String> entries = new HashMap<>(System.getenv()); + for (Map.Entry<String, String> entry : System.getenv().entrySet()) { + entries.put("_" + entry.getKey() + ".source", getName()); + } + return entries; + }else{ + Map<String, String> entries = new HashMap<>(); + for (Map.Entry<String, String> entry : System.getenv().entrySet()) { + entries.put(prefix + entry.getKey(), entry.getValue()); + entries.put("_" + prefix + entry.getKey() + ".source", getName()); + } + return entries; } - return entries; } @Override http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f3e3cf0/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/MapPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/MapPropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/MapPropertySource.java index ec4abc9..5580c24 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/MapPropertySource.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/MapPropertySource.java @@ -32,11 +32,6 @@ public class MapPropertySource extends BasePropertySource { private final String name; /** - * The Property Sources priority, a fixed priority should be used. - */ - private final Integer priority; - - /** * The current properties. */ private final Map<String, String> props = new HashMap<>(); @@ -49,42 +44,23 @@ public class MapPropertySource extends BasePropertySource { * @param props the properties */ public MapPropertySource(String name, Map<String, String> props) { - this(name, props, null, null); - } - - /** - * Creates a new instance, hereby using the default mechanism for evaluating the property source's - * priority, but applying a custom mapping {@code rootContext} to the entries provided. - * - * @param name unique name of this source. - * @param props the properties - * @param rootContext the root context mapping, or null (for no mapping). - */ - public MapPropertySource(String name, Map<String, String> props, String rootContext) { - this(name, props, rootContext, null); + this(name, props, null); } /** * Creates a new instance, hereby using the default mechanism for evaluating the property source's - * priority, but applying a custom mapping {@code rootContext} to the entries provided. + * priority, but applying a custom mapping {@code prefix} to the entries provided. * - * @param name unique name of this source. + * @param name unique name of this source. * @param props the properties - * @param rootContext the root context mapping, or null (for no mapping). - * @param priority the (optional) fixed priority. If null, the default priority - * evaluation is used. + * @param prefix the prefix context mapping, or null (for no mapping). */ - public MapPropertySource(String name, Map<String, String> props, String rootContext, Integer priority) { - this.priority = priority; + public MapPropertySource(String name, Map<String, String> props, String prefix) { this.name = Objects.requireNonNull(name); - if (rootContext == null) { + if (prefix == null) { this.props.putAll(props); } else { for (Map.Entry<String, String> en : props.entrySet()) { - String prefix = rootContext; - if (!prefix.endsWith(".") && prefix.length() > 0) { - prefix += "."; - } this.props.put(prefix + en.getKey(), en.getValue()); } } @@ -96,12 +72,10 @@ public class MapPropertySource extends BasePropertySource { * * @param name unique name of this source. * @param props the properties - * @param rootContext the root context mapping, or null (for no mapping). - * @param priority the (optional) fixed priority. If null, the default priority - * evaluation is used. + * @param prefix the prefix context mapping, or null (for no mapping). */ - public MapPropertySource(String name, Properties props, String rootContext, Integer priority) { - this(name, getMap(props), rootContext, priority); + public MapPropertySource(String name, Properties props, String prefix) { + this(name, getMap(props), prefix); } /** @@ -129,18 +103,10 @@ public class MapPropertySource extends BasePropertySource { } @Override - public int getOrdinal() { - if(priority!=null) { - return priority; - } - return super.getOrdinal(); - } - - @Override public String toString() { - return "SimplePropertiesPropertySource{" + + return "MapPropertySource{" + "name=" + name + ", " + - "priority=" + priority + + "ordinal=" + getOrdinal() + '}'; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f3e3cf0/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertiesResourcePropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertiesResourcePropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertiesResourcePropertySource.java index bb8ba0b..3744c94 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertiesResourcePropertySource.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertiesResourcePropertySource.java @@ -32,27 +32,53 @@ import java.util.logging.Logger; public class PropertiesResourcePropertySource extends MapPropertySource { /** The logger used. */ private static final Logger LOGGER = Logger.getLogger(PropertiesResourcePropertySource.class.getName()); - /** The resource loaded. */ - private final URL url; /** * Creates a new instance. * @param url the resource URL, not null. - * @param priority the optional (fixed) priority ordinal. */ - public PropertiesResourcePropertySource(URL url, Integer priority){ - this(null, url, priority); + public PropertiesResourcePropertySource(URL url){ + this(url, null); } /** * Creates a new instance. - * @param rootContext the (optional) root context for mapping (prefixing) the properties loaded. + * @param prefix the (optional) prefix context for mapping (prefixing) the properties loaded. * @param url the resource URL, not null. - * @param priority the optional (fixed) priority ordinal. */ - public PropertiesResourcePropertySource(String rootContext, URL url, Integer priority){ - super(url.toExternalForm(), loadProps(url), rootContext, priority); - this.url = url; + public PropertiesResourcePropertySource(URL url, String prefix){ + super(url.toExternalForm(), loadProps(url), prefix); + } + + /** + * Creates a new instance. + * @param prefix the (optional) prefix context for mapping (prefixing) the properties loaded. + * @param path the resource path, not null. + */ + public PropertiesResourcePropertySource(String path, String prefix){ + super(path, loadProps(path, null), prefix); + } + + /** + * Creates a new instance. + * @param prefix the (optional) prefix context for mapping (prefixing) the properties loaded. + * @param path the resource path, not null. + */ + public PropertiesResourcePropertySource(String path, String prefix, ClassLoader cl){ + super(path, loadProps(path, cl), prefix); + } + + /** + * Loads the properties using the JDK's Property loading mechanism. + * @param path the resource classpath, not null. + * @return the loaded properties. + */ + private static Map<String, String> loadProps(String path, ClassLoader cl) { + if(cl==null){ + cl = PropertiesResourcePropertySource.class.getClassLoader(); + } + URL url = cl.getResource(path); + return loadProps(url); } /** @@ -62,15 +88,18 @@ public class PropertiesResourcePropertySource extends MapPropertySource { */ private static Map<String, String> loadProps(URL url) { Map<String,String> result = new HashMap<>(); - try(InputStream is = url.openStream()){ - Properties props = new Properties(); - props.load(is); - for(Map.Entry en: props.entrySet()){ - result.put(en.getKey().toString(), en.getValue().toString()); + if(url!=null) { + try (InputStream is = url.openStream()) { + Properties props = new Properties(); + props.load(is); + for (Map.Entry en : props.entrySet()) { + result.put(en.getKey().toString(), en.getValue().toString()); + } + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Failed to read properties from " + url, e); } - } - catch(Exception e){ - LOGGER.log(Level.WARNING, "Failed to read properties from " + url, e); + }else{ + LOGGER.log(Level.WARNING, "No properties found at " + url); } return result; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f3e3cf0/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SimplePropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SimplePropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SimplePropertySource.java index 6c15e35..8f4f69b 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SimplePropertySource.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SimplePropertySource.java @@ -1,20 +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 of the License at + * 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 + * 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. + * 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.spisupport; @@ -111,8 +111,8 @@ public class SimplePropertySource extends BasePropertySource { /** * loads the Properties from the given URL * - * @param propertiesFile {@link URL} to load Properties from - * @return loaded {@link Properties} + * @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) { http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f3e3cf0/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SystemPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SystemPropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SystemPropertySource.java index 5f48738..ea10e93 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SystemPropertySource.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SystemPropertySource.java @@ -1,24 +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 of the License at + * 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 + * 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. + * 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.spisupport; -import org.apache.tamaya.spi.PropertySource; import org.apache.tamaya.spi.PropertyValue; import java.util.Collections; @@ -27,10 +26,10 @@ import java.util.Map; import java.util.Properties; /** - * This {@link PropertySource} manages the system properties. You can disable this feature by + * This {@link org.apache.tamaya.spi.PropertySource} manages the system properties. You can disable this feature by * setting {@code tamaya.envprops.disable} or {@code tamaya.defaults.disable}. */ -public class SystemPropertySource implements PropertySource { +public class SystemPropertySource extends BasePropertySource { /** * default ordinal for {@link org.apache.tamaya.core.propertysource.SystemPropertySource} @@ -43,12 +42,49 @@ public class SystemPropertySource implements PropertySource { * previous System.getProperties().hashCode() * so we can check if we need to reload */ - private int previousHash; + private volatile int previousHash; - private final boolean disabled = evaluateDisabled(); + /** + * Prefix that allows system properties to virtually be mapped on specified sub section. + */ + private String prefix; + + /** + * If true, this property source does not return any properties. This is useful since this + * property source is applied by default, but can be switched off by setting the + * {@code tamaya.envprops.disable} system/environment property to {@code true}. + */ + private boolean disabled = false; + + /** + * Creates a new instance. Also initializes the {@code prefix} and {@code disabled} properties + * from the system-/ environment properties: + * <pre> + * tamaya.envprops.prefix + * tamaya.envprops.disable + * </pre> + */ + public SystemPropertySource(){ + initFromSystemProperties(); + if(!disabled){ + cachedProperties = Collections.unmodifiableMap(loadProperties()); + } + } - private boolean evaluateDisabled() { - String value = System.getProperty("tamaya.sysprops.disable"); + /** + * Initializes the {@code prefix} and {@code disabled} properties from the system-/ + * environment properties: + * <pre> + * tamaya.envprops.prefix + * tamaya.envprops.disable + * </pre> + */ + private void initFromSystemProperties() { + String value = System.getProperty("tamaya.sysprops.prefix"); + if(value==null){ + prefix = System.getenv("tamaya.sysprops.prefix"); + } + value = System.getProperty("tamaya.sysprops.disable"); if(value==null){ value = System.getenv("tamaya.sysprops.disable"); } @@ -58,34 +94,60 @@ public class SystemPropertySource implements PropertySource { if(value==null){ value = System.getenv("tamaya.defaults.disable"); } - if(value==null){ - return false; + if(value!=null && !value.isEmpty()) { + this.disabled = Boolean.parseBoolean(value); } - return value.isEmpty() || Boolean.parseBoolean(value); } + /** + * Creates a new instance using a fixed ordinal value. + * @param ordinal the ordinal number. + */ + public SystemPropertySource(int ordinal){ + this(null, ordinal); + } - - public SystemPropertySource() { - cachedProperties = loadProperties(); - previousHash = System.getProperties().hashCode(); + /** + * Creates a new instance. + * @param prefix the prefix to be used, or null. + * @param ordinal the ordinal to be used. + */ + public SystemPropertySource(String prefix, int ordinal){ + this.prefix = prefix; + setOrdinal(ordinal); } - private Map<String, String> loadProperties() { - Map<String,String> props = new HashMap<>(); - Properties sysProps = System.getProperties(); - for(String name: sysProps.stringPropertyNames()) { - props.put(name,sysProps.getProperty(name)); - props.put("_"+name+".source",getName()); - } - return props; + /** + * Creates a new instance. + * @param prefix the prefix to be used, or null. + */ + public SystemPropertySource(String prefix){ + this.prefix = prefix; } @Override - public int getOrdinal() { + public int getDefaultOrdinal() { return DEFAULT_ORDINAL; } + + private Map<String, String> loadProperties() { + Properties sysProps = System.getProperties(); + previousHash = System.getProperties().hashCode(); + final String prefix = this.prefix; + Map<String, String> entries = new HashMap<>(); + for (Map.Entry<Object,Object> entry : sysProps.entrySet()) { + if(prefix==null) { + entries.put("_" + entry.getKey() + ".source", getName()); + entries.put((String) entry.getKey(), (String) entry.getValue()); + }else { + entries.put(prefix + entry.getKey(), (String)entry.getValue()); + entries.put("_" + prefix + entry.getKey() + ".source", getName()); + } + } + return entries; + } + @Override public String getName() { if(disabled){ @@ -99,7 +161,11 @@ public class SystemPropertySource implements PropertySource { if(disabled){ return null; } - return PropertyValue.of(key, System.getProperty(key), getName()); + String prefix = this.prefix; + if(prefix==null) { + return PropertyValue.of(key, System.getProperty(key), getName()); + } + return PropertyValue.of(key, System.getProperty(key.substring(prefix.length())), getName()); } @Override @@ -113,7 +179,6 @@ public class SystemPropertySource implements PropertySource { if (previousHash != System.getProperties().hashCode()) { Map<String, String> properties = loadProperties(); this.cachedProperties = Collections.unmodifiableMap(properties); - previousHash = System.getProperties().hashCode(); } return this.cachedProperties; }
