Added full JSR support. Signed-off-by: Anatole Tresch <[email protected]>
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/06f29e1a Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/06f29e1a Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/06f29e1a Branch: refs/heads/configjsr Commit: 06f29e1a51a4b11d8eb7e2dcdaae3611951bcba6 Parents: 4af5f5f Author: Anatole Tresch <[email protected]> Authored: Wed Dec 13 22:44:40 2017 +0100 Committer: Anatole Tresch <[email protected]> Committed: Wed Dec 13 22:44:40 2017 +0100 ---------------------------------------------------------------------- modules/resources/pom.xml | 2 +- .../AbstractPathConfigSourceProvider.java | 206 +++++++++++++++++++ .../AbstractPathPropertySourceProvider.java | 205 ------------------ .../apache/tamaya/resource/ConfigResources.java | 7 +- .../AbstractPathPropertySourceProviderTest.java | 31 ++- .../internal/PathBasedConfigSourceProvider.java | 87 ++++++++ .../PathBasedPropertySourceProvider.java | 94 --------- 7 files changed, 310 insertions(+), 322 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/06f29e1a/modules/resources/pom.xml ---------------------------------------------------------------------- diff --git a/modules/resources/pom.xml b/modules/resources/pom.xml index e482618..f0aa1c2 100644 --- a/modules/resources/pom.xml +++ b/modules/resources/pom.xml @@ -34,7 +34,7 @@ under the License. <dependencies> <dependency> <groupId>org.apache.tamaya</groupId> - <artifactId>tamaya-api</artifactId> + <artifactId>tamaya-base</artifactId> <version>${tamaya-apicore.version}</version> </dependency> <dependency> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/06f29e1a/modules/resources/src/main/java/org/apache/tamaya/resource/AbstractPathConfigSourceProvider.java ---------------------------------------------------------------------- diff --git a/modules/resources/src/main/java/org/apache/tamaya/resource/AbstractPathConfigSourceProvider.java b/modules/resources/src/main/java/org/apache/tamaya/resource/AbstractPathConfigSourceProvider.java new file mode 100644 index 0000000..a536be9 --- /dev/null +++ b/modules/resources/src/main/java/org/apache/tamaya/resource/AbstractPathConfigSourceProvider.java @@ -0,0 +1,206 @@ +/* + * 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.resource; + +import org.apache.tamaya.spi.ServiceContext; + +import java.io.InputStream; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.Properties; +import java.util.logging.Level; +import java.util.logging.Logger; + + +import javax.config.spi.ConfigSource; +import javax.config.spi.ConfigSourceProvider; + +/** + * Abstract base class that uses a descriptive resource path to define the locations of configuration files to be + * included into the configuration. This is especially useful, when the current configuration policy in place + * does not define the exact file names, but the file locations, where configuration can be provided. + */ +public abstract class AbstractPathConfigSourceProvider implements ConfigSourceProvider{ + /** The log used. */ + private static final Logger LOG = Logger.getLogger(AbstractPathConfigSourceProvider.class.getName()); + /** The resource paths. */ + private String[] resourcePaths; + + + /** + * Creates a new instance using the given resource paths. + * @param resourcePaths the resource paths, not null, not empty. + */ + public AbstractPathConfigSourceProvider(String... resourcePaths){ + if(resourcePaths.length==0){ + throw new IllegalArgumentException("At least one resource path should be configured."); + } + + setResourcePaths(resourcePaths); + } + + @Override + public Collection<ConfigSource> getConfigSources(ClassLoader classLoader) { + List<ConfigSource> propertySources = new ArrayList<>(); + if(classLoader == null){ + classLoader = ServiceContext.defaultClassLoader(); + } + for (String resource : getResourcePaths()) { + try { + // TODO Get a resource resolver for a certain classloader + Collection<URL> resources = ConfigResources.getResourceResolver().getResources(resource); + for (URL url : resources) { + try { + Collection<ConfigSource> propertySourcesToInclude = getConfigSources(url); + if(propertySourcesToInclude!=null){ + propertySources.addAll(propertySourcesToInclude); + } + } catch (Exception e) { + LOG.log(Level.WARNING, "Failed to read configuration from " + url, e); + } + } + } catch (Exception e) { + LOG.log(Level.SEVERE, "Invalid resource path: " + resource, e); + } + } + return propertySources; + } + + protected String[] getResourcePaths() { + return resourcePaths; + } + + protected void setResourcePaths(String[] paths) { + resourcePaths = paths.clone(); + } + + /** + * Factory method that creates a {@link ConfigSource} based on the URL found by + * the resource locator. + * @param url the URL, not null. + * @return the {@link ConfigSource}s to be included into the current provider's sources + * list. It is safe to return {@code null} here, in case the content of the URL has shown to be not relevant + * as configuration input. In case the input is not valid or accessible an exception can be thrown or logged. + */ + protected abstract Collection<ConfigSource> getConfigSources(URL url); + + /** + * Utility method that reads a .properties file from the given url and creates a corresponding + * {@link ConfigSource}. + * @param url the url to read, not null. + * @return the corresponding PropertySource, or null. + */ + public static ConfigSource createConfigSource(URL url) { + Properties props = new Properties(); + try (InputStream is = url.openStream()){ + props.load(is); + return new PropertiesBasedConfigSource(url.toString(), props); + } + catch (Exception e){ + LOG.log(Level.WARNING, "Failed to read properties from " + url, e); + return null; + } + } + + /** + * Minimal {@link ConfigSource} implementation based on {@link Properties} or + * {@link Map}. + */ + private final static class PropertiesBasedConfigSource implements ConfigSource{ + /** The property source's name. */ + private final String name; + /** The properties. */ + private final Map<String,String> properties = new HashMap<>(); + + /** + * Constructor for a simple properties configuration. + * @param name the source's name, not null + * @param props the properties, not null + */ + public PropertiesBasedConfigSource(String name, Properties props) { + this.name = Objects.requireNonNull(name); + for (Entry<Object, Object> en : props.entrySet()) { + this.properties.put(en.getKey().toString(), + String.valueOf(en.getValue())); + } + } + + /** + * Constructor for a simple properties configuration. + * @param name the source's name, not null + * @param props the properties, not null + */ + public PropertiesBasedConfigSource(String name, Map<String,String> props) { + this.name = Objects.requireNonNull(name); + for (Entry<String, String> en : props.entrySet()) { + this.properties.put(en.getKey(), + en.getValue()); + } + } + + public int getOrdinal() { + String configuredOrdinal = getValue(CONFIG_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 getDefaultOrdinal(); + } + + /** + * Returns the default ordinal used, when no ordinal is set, or the ordinal was not parseable to an int value. + * + * @return the default ordinal used, by default 0. + */ + public int getDefaultOrdinal() { + return 0; + } + + @Override + public String getName() { + return name; + } + + @Override + public String getValue(String key) { + return this.properties.get(key); + } + + @Override + public Map<String, String> getProperties() { + return properties; + } + + @Override + public String toString(){ + return "PropertiesBasedConfigSource["+name+']'; + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/06f29e1a/modules/resources/src/main/java/org/apache/tamaya/resource/AbstractPathPropertySourceProvider.java ---------------------------------------------------------------------- diff --git a/modules/resources/src/main/java/org/apache/tamaya/resource/AbstractPathPropertySourceProvider.java b/modules/resources/src/main/java/org/apache/tamaya/resource/AbstractPathPropertySourceProvider.java deleted file mode 100644 index 760e688..0000000 --- a/modules/resources/src/main/java/org/apache/tamaya/resource/AbstractPathPropertySourceProvider.java +++ /dev/null @@ -1,205 +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.resource; - -import java.io.InputStream; -import java.net.URL; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Objects; -import java.util.Properties; -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertySourceProvider; -import org.apache.tamaya.spi.PropertyValue; - -/** - * Abstract base class that uses a descriptive resource path to define the locations of configuration files to be - * included into the configuration. This is especially useful, when the current configuration policy in place - * does not define the exact file names, but the file locations, where configuration can be provided. - */ -public abstract class AbstractPathPropertySourceProvider implements PropertySourceProvider{ - /** The log used. */ - private static final Logger LOG = Logger.getLogger(AbstractPathPropertySourceProvider.class.getName()); - /** The resource paths. */ - private String[] resourcePaths; - - - /** - * Creates a new instance using the given resource paths. - * @param resourcePaths the resource paths, not null, not empty. - */ - public AbstractPathPropertySourceProvider(String... resourcePaths){ - if(resourcePaths.length==0){ - throw new IllegalArgumentException("At least one resource path should be configured."); - } - - setResourcePaths(resourcePaths); - } - - @Override - public Collection<PropertySource> getPropertySources() { - List<PropertySource> propertySources = new ArrayList<>(); - for (String resource : getResourcePaths()) { - try { - Collection<URL> resources = ConfigResources.getResourceResolver().getResources(resource); - for (URL url : resources) { - try { - Collection<PropertySource> propertySourcesToInclude = getPropertySources(url); - if(propertySourcesToInclude!=null){ - propertySources.addAll(propertySourcesToInclude); - } - } catch (Exception e) { - LOG.log(Level.WARNING, "Failed to read configuration from " + url, e); - } - } - } catch (Exception e) { - LOG.log(Level.SEVERE, "Invalid resource path: " + resource, e); - } - } - return propertySources; - } - - protected String[] getResourcePaths() { - return resourcePaths; - } - - protected void setResourcePaths(String[] paths) { - resourcePaths = paths.clone(); - } - - /** - * Factory method that creates a {@link org.apache.tamaya.spi.PropertySource} based on the URL found by - * the resource locator. - * @param url the URL, not null. - * @return the {@link org.apache.tamaya.spi.PropertySource}s to be included into the current provider's sources - * list. It is safe to return {@code null} here, in case the content of the URL has shown to be not relevant - * as configuration input. In case the input is not valid or accessible an exception can be thrown or logged. - */ - protected abstract Collection<PropertySource> getPropertySources(URL url); - - /** - * Utility method that reads a .properties file from the given url and creates a corresponding - * {@link org.apache.tamaya.spi.PropertySource}. - * @param url the url to read, not null. - * @return the corresponding PropertySource, or null. - */ - public static PropertySource createPropertiesPropertySource(URL url) { - Properties props = new Properties(); - try (InputStream is = url.openStream()){ - props.load(is); - return new PropertiesBasedPropertySource(url.toString(), props); - } - catch (Exception e){ - LOG.log(Level.WARNING, "Failed to read properties from " + url, e); - return null; - } - } - - /** - * Minimal {@link PropertySource} implementation based on {@link Properties} or - * {@link Map}. - */ - private final static class PropertiesBasedPropertySource implements PropertySource{ - /** The property source's name. */ - private final String name; - /** The properties. */ - private final Map<String,PropertyValue> properties = new HashMap<>(); - - /** - * Constructor for a simple properties configuration. - * @param name the source's name, not null - * @param props the properties, not null - */ - public PropertiesBasedPropertySource(String name, Properties props) { - this.name = Objects.requireNonNull(name); - for (Entry<Object, Object> en : props.entrySet()) { - this.properties.put(en.getKey().toString(), - PropertyValue.of(en.getKey().toString(), String.valueOf(en.getValue()), name)); - } - } - - /** - * Constructor for a simple properties configuration. - * @param name the source's name, not null - * @param props the properties, not null - */ - public PropertiesBasedPropertySource(String name, Map<String,String> props) { - this.name = Objects.requireNonNull(name); - for (Entry<String, String> en : props.entrySet()) { - this.properties.put(en.getKey(), - PropertyValue.of(en.getKey(), en.getValue(), name)); - } - } - - public int getOrdinal() { - PropertyValue configuredOrdinal = get(TAMAYA_ORDINAL); - if (configuredOrdinal != null) { - try { - return Integer.parseInt(configuredOrdinal.getValue()); - } catch (Exception e) { - Logger.getLogger(getClass().getName()).log(Level.WARNING, - "Configured Ordinal is not an int number: " + configuredOrdinal, e); - } - } - return getDefaultOrdinal(); - } - - /** - * Returns the default ordinal used, when no ordinal is set, or the ordinal was not parseable to an int value. - * - * @return the default ordinal used, by default 0. - */ - public int getDefaultOrdinal() { - return 0; - } - - @Override - public String getName() { - return name; - } - - @Override - public PropertyValue get(String key) { - return this.properties.get(key); - } - - @Override - public Map<String, PropertyValue> getProperties() { - return properties; - } - - @Override - public boolean isScannable() { - return false; - } - - @Override - public String toString(){ - return "PropertiesBasedPropertySource["+name+']'; - } - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/06f29e1a/modules/resources/src/main/java/org/apache/tamaya/resource/ConfigResources.java ---------------------------------------------------------------------- diff --git a/modules/resources/src/main/java/org/apache/tamaya/resource/ConfigResources.java b/modules/resources/src/main/java/org/apache/tamaya/resource/ConfigResources.java index ae08148..fc40260 100644 --- a/modules/resources/src/main/java/org/apache/tamaya/resource/ConfigResources.java +++ b/modules/resources/src/main/java/org/apache/tamaya/resource/ConfigResources.java @@ -18,7 +18,6 @@ */ package org.apache.tamaya.resource; -import org.apache.tamaya.ConfigException; import org.apache.tamaya.spi.ServiceContextManager; @@ -35,14 +34,14 @@ public final class ConfigResources { /** * <p>Access the current ResourceResolver.</p> * - * @throws ConfigException if no ResourceResolver is available (should not happen). + * @throws IllegalStateException if no ResourceResolver is available (should not happen). * * @return the current ResourceResolver instance, never null. */ - public static ResourceResolver getResourceResolver() throws ConfigException { + public static ResourceResolver getResourceResolver() { ResourceResolver resolver = ServiceContextManager.getServiceContext().getService(ResourceResolver.class); if (resolver == null) { - throw new ConfigException("ResourceResolver not available."); + throw new IllegalStateException("ResourceResolver not available."); } return resolver; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/06f29e1a/modules/resources/src/test/java/org/apache/tamaya/resource/AbstractPathPropertySourceProviderTest.java ---------------------------------------------------------------------- diff --git a/modules/resources/src/test/java/org/apache/tamaya/resource/AbstractPathPropertySourceProviderTest.java b/modules/resources/src/test/java/org/apache/tamaya/resource/AbstractPathPropertySourceProviderTest.java index 0dc6c91..3ad3e93 100644 --- a/modules/resources/src/test/java/org/apache/tamaya/resource/AbstractPathPropertySourceProviderTest.java +++ b/modules/resources/src/test/java/org/apache/tamaya/resource/AbstractPathPropertySourceProviderTest.java @@ -18,10 +18,9 @@ */ package org.apache.tamaya.resource; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; import org.junit.Test; +import javax.config.spi.ConfigSource; import java.net.URL; import java.util.ArrayList; import java.util.Collection; @@ -36,30 +35,30 @@ import static org.junit.Assert.assertTrue; public class AbstractPathPropertySourceProviderTest { - private final AbstractPathPropertySourceProvider myProvider = new AbstractPathPropertySourceProvider("*.properties") { + private final AbstractPathConfigSourceProvider myProvider = new AbstractPathConfigSourceProvider("*.properties") { @Override - protected Collection<PropertySource> getPropertySources(URL url) { - List<PropertySource> result = new ArrayList<>(); - result.add(new EmptyPropertySource()); + protected Collection<ConfigSource> getConfigSources(URL url) { + List<ConfigSource> result = new ArrayList<>(); + result.add(new EmptyConfigSource()); return result; } }; @Test - public void testGetPropertySources() throws Exception { - assertNotNull(myProvider.getPropertySources()); + public void testGetConfigSources() throws Exception { + assertNotNull(myProvider.getConfigSources((ClassLoader)null)); } @Test public void testCreatePropertiesPropertySource() throws Exception { - PropertySource ps = AbstractPathPropertySourceProvider.createPropertiesPropertySource( + ConfigSource ps = AbstractPathConfigSourceProvider.createConfigSource( ClassLoader.getSystemClassLoader().getResource("test.properties") ); assertNotNull(ps); assertTrue(ps.getProperties().isEmpty()); } - private static final class EmptyPropertySource implements PropertySource { + private static final class EmptyConfigSource implements ConfigSource { /** * Lookup order: * TODO rethink whole default PropertySources and ordering: @@ -88,10 +87,10 @@ public class AbstractPathPropertySourceProviderTest { * @return the 'importance' aka ordinal of the configured values. The higher, the more important. */ public int getOrdinal() { - PropertyValue configuredOrdinal = get(TAMAYA_ORDINAL); + String configuredOrdinal = getValue(CONFIG_ORDINAL); if (configuredOrdinal != null) { try { - return Integer.parseInt(configuredOrdinal.getValue()); + return Integer.parseInt(configuredOrdinal); } catch (Exception e) { Logger.getLogger(getClass().getName()).log(Level.WARNING, "Configured Ordinal is not an int number: " + configuredOrdinal, e); @@ -115,18 +114,14 @@ public class AbstractPathPropertySourceProviderTest { } @Override - public PropertyValue get(String key) { + public String getValue(String key) { return null; } @Override - public Map<String, PropertyValue> getProperties() { + public Map<String, String> getProperties() { return Collections.emptyMap(); } - @Override - public boolean isScannable() { - return true; - } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/06f29e1a/modules/resources/src/test/java/org/apache/tamaya/resource/internal/PathBasedConfigSourceProvider.java ---------------------------------------------------------------------- diff --git a/modules/resources/src/test/java/org/apache/tamaya/resource/internal/PathBasedConfigSourceProvider.java b/modules/resources/src/test/java/org/apache/tamaya/resource/internal/PathBasedConfigSourceProvider.java new file mode 100644 index 0000000..db1c72a --- /dev/null +++ b/modules/resources/src/test/java/org/apache/tamaya/resource/internal/PathBasedConfigSourceProvider.java @@ -0,0 +1,87 @@ +/* + * 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.resource.internal; + +import org.apache.tamaya.resource.AbstractPathConfigSourceProvider; + +import javax.config.spi.ConfigSource; +import java.io.InputStream; +import java.net.URL; +import java.util.*; + +/** + * Created by Anatole on 03.03.2015. + */ +public class PathBasedConfigSourceProvider extends AbstractPathConfigSourceProvider { + + public PathBasedConfigSourceProvider() { + super("META-INF/cfg/**/*.properties"); + } + + @Override + protected Collection<ConfigSource> getConfigSources(URL url) { + List<ConfigSource> list = new ArrayList<>(); + Properties props = new Properties(); + try(InputStream is = url.openStream()){ + props.load(is); + list.add(new PropertiesBasedConfigSource(url.toString(), props)); + } + catch(Exception e){ + e.printStackTrace(); + return null; + } + return list; + } + + + private final static class PropertiesBasedConfigSource implements ConfigSource{ + + private final String name; + private final Map<String,String> properties = new HashMap<>(); + + public PropertiesBasedConfigSource(String name, Properties props) { + this.name = Objects.requireNonNull(name); + for (Map.Entry en : props.entrySet()) { + this.properties.put(en.getKey().toString(), + String.valueOf(en.getValue())); + } + } + + @Override + public int getOrdinal() { + return 0; + } + + @Override + public String getName() { + return name; + } + + @Override + public String getValue(String key) { + return properties.get(key); + } + + @Override + public Map<String, String> getProperties() { + return properties; + } + + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/06f29e1a/modules/resources/src/test/java/org/apache/tamaya/resource/internal/PathBasedPropertySourceProvider.java ---------------------------------------------------------------------- diff --git a/modules/resources/src/test/java/org/apache/tamaya/resource/internal/PathBasedPropertySourceProvider.java b/modules/resources/src/test/java/org/apache/tamaya/resource/internal/PathBasedPropertySourceProvider.java deleted file mode 100644 index 7e2f622..0000000 --- a/modules/resources/src/test/java/org/apache/tamaya/resource/internal/PathBasedPropertySourceProvider.java +++ /dev/null @@ -1,94 +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.resource.internal; - -import org.apache.tamaya.resource.AbstractPathPropertySourceProvider; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; - -import java.io.InputStream; -import java.net.URL; -import java.util.*; - -/** - * Created by Anatole on 03.03.2015. - */ -public class PathBasedPropertySourceProvider extends AbstractPathPropertySourceProvider{ - - public PathBasedPropertySourceProvider() { - super("META-INF/cfg/**/*.properties"); - } - - @Override - protected Collection<PropertySource> getPropertySources(URL url) { - List<PropertySource> list = new ArrayList<>(); - Properties props = new Properties(); - try(InputStream is = url.openStream()){ - props.load(is); - list.add(new PropertiesBasedPropertySource(url.toString(), props)); - } - catch(Exception e){ - e.printStackTrace(); - return null; - } - return list; - } - - - private final static class PropertiesBasedPropertySource implements PropertySource{ - - private final String name; - private final Map<String,PropertyValue> properties = new HashMap<>(); - - public PropertiesBasedPropertySource(String name, Properties props) { - this.name = Objects.requireNonNull(name); - for (Map.Entry en : props.entrySet()) { - this.properties.put(en.getKey().toString(), - PropertyValue.of(en.getKey().toString(), - String.valueOf(en.getValue()), - name)); - } - } - - @Override - public int getOrdinal() { - return 0; - } - - @Override - public String getName() { - return name; - } - - @Override - public PropertyValue get(String key) { - return properties.get(key); - } - - @Override - public Map<String, PropertyValue> getProperties() { - return properties; - } - - @Override - public boolean isScannable() { - return false; - } - } -}
