http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/filter/src/test/java/org/apache/tamaya/filter/ProgrammableFilterTest.java ---------------------------------------------------------------------- diff --git a/modules/filter/src/test/java/org/apache/tamaya/filter/ProgrammableFilterTest.java b/modules/filter/src/test/java/org/apache/tamaya/filter/ProgrammableFilterTest.java deleted file mode 100644 index b6e7bb7..0000000 --- a/modules/filter/src/test/java/org/apache/tamaya/filter/ProgrammableFilterTest.java +++ /dev/null @@ -1,149 +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.filter; - -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.PropertyFilter; -import org.apache.tamaya.spi.PropertyValue; -import org.apache.tamaya.spisupport.DefaultConfigurationContextBuilder; -import org.apache.tamaya.spisupport.RegexPropertyFilter; -import org.junit.Test; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -import static org.junit.Assert.*; - -/** - * Tests for {@link FilterContext}. Created by atsticks on 11.02.16. - */ -public class ProgrammableFilterTest { - - private static ConfigurationContext context = new DefaultConfigurationContextBuilder().build(); - private static PropertyValue test1Property = PropertyValue.of("test1","test1","test"); - private static PropertyValue test2Property = PropertyValue.of("test2","test2","test"); - private static PropertyValue test3Property = PropertyValue.of("test.test3","test.test3","test"); - - @Test - public void testAddRemoveFilter() throws Exception { - FilterContext filter = new FilterContext(); - Map<String,PropertyValue> map = new HashMap<>(); - assertEquals(filter.filterProperty(test1Property, new org.apache.tamaya.spi.FilterContext(test1Property, map, context)), test1Property); - assertEquals(filter.filterProperty(test2Property, new org.apache.tamaya.spi.FilterContext(test2Property, map, context)), test2Property); - assertEquals(filter.filterProperty(test3Property, new org.apache.tamaya.spi.FilterContext(test3Property, map, context)), test3Property); - RegexPropertyFilter regexFilter = new RegexPropertyFilter(); - regexFilter.setIncludes("test\\..*"); - filter.addFilter(regexFilter); - assertNull(filter.filterProperty(test1Property, new org.apache.tamaya.spi.FilterContext(test1Property, map, context))); - assertNull(filter.filterProperty(test2Property, new org.apache.tamaya.spi.FilterContext(test2Property, map, context))); - assertEquals(filter.filterProperty(test3Property, new org.apache.tamaya.spi.FilterContext(test3Property, map, context)), test3Property); - filter.removeFilter(0); - assertEquals(filter.filterProperty(test1Property, new org.apache.tamaya.spi.FilterContext(test1Property, map, context)), test1Property); - assertEquals(filter.filterProperty(test2Property, new org.apache.tamaya.spi.FilterContext(test2Property, map, context)), test2Property); - assertEquals(filter.filterProperty(test3Property, new org.apache.tamaya.spi.FilterContext(test3Property, map, context)), test3Property); - filter.addFilter(0, regexFilter); - assertNull(filter.filterProperty(test1Property, new org.apache.tamaya.spi.FilterContext(test1Property, map, context))); - assertNull(filter.filterProperty(test2Property, new org.apache.tamaya.spi.FilterContext(test2Property, map, context))); - assertEquals(filter.filterProperty(test3Property, new org.apache.tamaya.spi.FilterContext(test3Property, map, context)), test3Property); - } - - @Test - public void testClearFilters() throws Exception { - FilterContext filter = new FilterContext(); - RegexPropertyFilter regexFilter = new RegexPropertyFilter(); - regexFilter.setIncludes("test1.*"); - Map<String,String> map = new HashMap<>(); - map.put("test1", "test1"); - map.put("test2", "test2"); - map.put("test.test3", "test.test3"); - assertEquals(filter.filterProperty(test1Property, new org.apache.tamaya.spi.FilterContext(test1Property, context)), test1Property); - assertEquals(filter.filterProperty(test2Property, new org.apache.tamaya.spi.FilterContext(test2Property, context)), test2Property); - assertEquals(filter.filterProperty(test3Property, new org.apache.tamaya.spi.FilterContext(test3Property, context)), test3Property); - filter.addFilter(regexFilter); - assertEquals(filter.filterProperty(test1Property, new org.apache.tamaya.spi.FilterContext(test1Property, context)), test1Property); - assertNull(filter.filterProperty(test2Property, new org.apache.tamaya.spi.FilterContext(test2Property, context))); - assertNull(filter.filterProperty(test3Property, new org.apache.tamaya.spi.FilterContext(test3Property, context))); - filter.clearFilters(); - assertEquals(filter.filterProperty(test1Property, new org.apache.tamaya.spi.FilterContext(test1Property, context)), test1Property); - assertEquals(filter.filterProperty(test2Property, new org.apache.tamaya.spi.FilterContext(test2Property, context)), test2Property); - assertEquals(filter.filterProperty(test3Property, new org.apache.tamaya.spi.FilterContext(test3Property, context)), test3Property); - } - - @Test - public void testSetFilters() throws Exception { - FilterContext filter = new FilterContext(); - RegexPropertyFilter regexFilter = new RegexPropertyFilter(); - regexFilter.setIncludes("test\\..*"); - Map<String,PropertyValue> map = new HashMap<>(); - map.put("test1", test1Property); - map.put("test2", test1Property); - map.put("test.test3", test3Property); - assertEquals(filter.filterProperty(test1Property, new org.apache.tamaya.spi.FilterContext(test1Property, context)), test1Property); - assertEquals(filter.filterProperty(test2Property, new org.apache.tamaya.spi.FilterContext(test2Property, map, context)), test2Property); - assertEquals(filter.filterProperty(test3Property, new org.apache.tamaya.spi.FilterContext(test3Property, map, context)), test3Property); - filter.setFilters(regexFilter); - assertEquals(filter.filterProperty(test3Property, new org.apache.tamaya.spi.FilterContext(test3Property, map, context)), test3Property); - assertNull(filter.filterProperty(test2Property, new org.apache.tamaya.spi.FilterContext(test2Property, map, context))); - assertNull(filter.filterProperty(test1Property, new org.apache.tamaya.spi.FilterContext(test1Property, map, context))); - } - - @Test - public void testSetFilters1() throws Exception { - FilterContext filter = new FilterContext(); - RegexPropertyFilter regexFilter = new RegexPropertyFilter(); - regexFilter.setIncludes("test1.*"); - Map<String,String> map = new HashMap<>(); - assertEquals(filter.filterProperty(test1Property, new org.apache.tamaya.spi.FilterContext(test1Property, context)), test1Property); - assertEquals(filter.filterProperty(test2Property, new org.apache.tamaya.spi.FilterContext(test2Property, context)), test2Property); - assertEquals(filter.filterProperty(test3Property, new org.apache.tamaya.spi.FilterContext(test3Property, context)), test3Property); - filter.setFilters(Arrays.asList(new PropertyFilter[]{regexFilter})); - assertEquals(filter.filterProperty(test1Property, new org.apache.tamaya.spi.FilterContext(test1Property, context)), test1Property); - assertNull(filter.filterProperty(test2Property, new org.apache.tamaya.spi.FilterContext(test2Property, context))); - assertNull(filter.filterProperty(test3Property, new org.apache.tamaya.spi.FilterContext(test3Property, context))); - } - - @Test - public void testGetFilters() throws Exception { - FilterContext filter = new FilterContext(); - assertNotNull(filter.getFilters()); - assertTrue(filter.getFilters().isEmpty()); - RegexPropertyFilter regexFilter = new RegexPropertyFilter(); - regexFilter.setIncludes("test\\..*"); - filter.addFilter(regexFilter); - assertNotNull(filter.getFilters()); - assertFalse(filter.getFilters().isEmpty()); - assertEquals(1, filter.getFilters().size()); - assertEquals(regexFilter, filter.getFilters().get(0)); - } - - @Test - public void testToString() throws Exception { - FilterContext filter = new FilterContext(); - assertFalse(filter.toString().contains("test\\..*")); - assertTrue(filter.toString().contains("ProgrammableFilter")); - assertFalse(filter.toString().contains("RegexPropertyFilter")); - RegexPropertyFilter regexFilter = new RegexPropertyFilter(); - regexFilter.setIncludes("test\\..*"); - filter.addFilter(regexFilter); - assertTrue(filter.toString().contains("test\\..*")); - assertTrue(filter.toString().contains("ProgrammableFilter")); - assertTrue(filter.toString().contains("RegexPropertyFilter")); - } -} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/filter/src/test/java/org/apache/tamaya/spisupport/filter/ConfigurationFilterTest.java ---------------------------------------------------------------------- diff --git a/modules/filter/src/test/java/org/apache/tamaya/spisupport/filter/ConfigurationFilterTest.java b/modules/filter/src/test/java/org/apache/tamaya/spisupport/filter/ConfigurationFilterTest.java new file mode 100644 index 0000000..e0f24ef --- /dev/null +++ b/modules/filter/src/test/java/org/apache/tamaya/spisupport/filter/ConfigurationFilterTest.java @@ -0,0 +1,95 @@ +/* + * 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.spisupport.filter; + +import org.apache.tamaya.filter.ConfigurationFilter; +import org.apache.tamaya.spi.ConfigValue; +import org.apache.tamaya.spi.Filter; +import org.junit.Test; + +import javax.config.Config; +import javax.config.ConfigProvider; + +import static org.junit.Assert.*; + +/** + * Tests for {@link ConfigurationFilter}. Created by atsticks on 11.02.16. + */ +public class ConfigurationFilterTest { + + @Test + public void testMetadataFiltered() throws Exception { + ConfigurationFilter.setMetadataFiltered(true); + assertTrue(ConfigurationFilter.isMetadataFiltered()); + ConfigurationFilter.setMetadataFiltered(false); + assertFalse(ConfigurationFilter.isMetadataFiltered()); + } + + @Test + public void testGetFilters() throws Exception { + Config config = ConfigProvider.getConfig(); + assertNotNull(ConfigurationFilter.getFilters()); + Filter testFilter = (k,v) -> k + ":testGetSingleFilters"; + ConfigurationFilter.addFilter(testFilter); + assertTrue(ConfigurationFilter.getFilters().contains(testFilter)); + } + + @Test + public void testFiltering() throws Exception { + Config config = ConfigProvider.getConfig(); + Filter testFilter = (k,v) -> k + ":testGetMapFilters"; + ConfigurationFilter.addFilter(testFilter); + assertEquals("user.home:testGetMapFilters", config.getValue("user.home", String.class)); + ConfigurationFilter.removeFilter(testFilter); + assertNotSame("user.home:testGetSingleFilters", config.getValue("user.home", String.class)); + } + + @Test + public void testRemove() throws Exception { + Config config = ConfigProvider.getConfig(); + Filter testFilter = (k,v) -> k + ":testGetMapFilters"; + ConfigurationFilter.addFilter(testFilter); + assertTrue(ConfigurationFilter.getFilters().contains(testFilter)); + ConfigurationFilter.removeFilter(testFilter); + assertFalse(ConfigurationFilter.getFilters().contains(testFilter)); + } + + @Test + public void testRemoveFilterAt0() throws Exception { + Config config = ConfigProvider.getConfig(); + Filter testFilter = (k,v) -> k + ":testGetMapFilters"; + ConfigurationFilter.addFilter(testFilter); + assertTrue(ConfigurationFilter.getFilters().contains(testFilter)); + ConfigurationFilter.removeFilter(0); + assertFalse(ConfigurationFilter.getFilters().contains(testFilter)); + } + + @Test + public void testClearFilters() throws Exception { + Config config = ConfigProvider.getConfig(); + Filter testFilter = (k,v) -> k + ":testGetSingleFilters"; + ConfigurationFilter.addFilter(testFilter); + assertTrue(ConfigurationFilter.getFilters().contains(testFilter)); + assertEquals("user.home:testGetSingleFilters", config.getValue("user.home", String.class)); + ConfigurationFilter.cleanupFilterContext(); + assertFalse(ConfigurationFilter.getFilters().contains(testFilter)); + assertNotSame("user.home:testGetSingleFilters", config.getValue("user.home", String.class)); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/filter/src/test/java/org/apache/tamaya/spisupport/filter/ProgrammableFilterTest.java ---------------------------------------------------------------------- diff --git a/modules/filter/src/test/java/org/apache/tamaya/spisupport/filter/ProgrammableFilterTest.java b/modules/filter/src/test/java/org/apache/tamaya/spisupport/filter/ProgrammableFilterTest.java new file mode 100644 index 0000000..15e54e6 --- /dev/null +++ b/modules/filter/src/test/java/org/apache/tamaya/spisupport/filter/ProgrammableFilterTest.java @@ -0,0 +1,183 @@ +/* + * 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.spisupport.filter; + +import org.apache.tamaya.base.filter.FilterContext; +import org.apache.tamaya.base.filter.RegexPropertyFilter; +import org.apache.tamaya.filter.Context; +import org.apache.tamaya.spi.Filter; +import org.junit.Test; + +import javax.config.Config; +import javax.config.ConfigProvider; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.*; + +/** + * Tests for {@link Context}. Created by atsticks on 11.02.16. + */ +public class ProgrammableFilterTest { + + private static Config config = ConfigProvider.getConfig(); + private static String test1Property = "test1"; + private static String test2Property = "test2"; + private static String test3Property = "test.test3"; + + @Test + public void testAddRemoveFilter() throws Exception { + Context filter = new Context(); + Map<String,String> map = new HashMap<>(); + FilterContext.setContext(new FilterContext(map, config)); + assertEquals(filter.filterProperty(test1Property,test1Property), test1Property); + FilterContext.setContext(new FilterContext(map, config)); + assertEquals(filter.filterProperty(test2Property, test2Property), test2Property); + FilterContext.setContext(new FilterContext(map, config)); + assertEquals(filter.filterProperty(test3Property, test3Property), test3Property); + RegexPropertyFilter regexFilter = new RegexPropertyFilter(); + regexFilter.setIncludes("test\\..*"); + filter.addFilter(regexFilter); + FilterContext.setContext(new FilterContext(map, config)); + assertNull(filter.filterProperty(test1Property, test1Property)); + FilterContext.setContext(new FilterContext(map, config)); + assertNull(filter.filterProperty(test2Property, test2Property)); + FilterContext.setContext(new FilterContext(map, config)); + assertEquals(filter.filterProperty(test3Property, test3Property), test3Property); + filter.removeFilter(0); + FilterContext.setContext(new FilterContext(map, config)); + assertEquals(filter.filterProperty(test1Property, test1Property), test1Property); + FilterContext.setContext(new FilterContext(map, config)); + assertEquals(filter.filterProperty(test2Property, test2Property), test2Property); + FilterContext.setContext(new FilterContext(map, config)); + assertEquals(filter.filterProperty(test3Property, test3Property), test3Property); + filter.addFilter(0, regexFilter); + FilterContext.setContext(new FilterContext(map, config)); + assertNull(filter.filterProperty(test1Property, test1Property)); + FilterContext.setContext(new FilterContext(map, config)); + assertNull(filter.filterProperty(test2Property, test2Property)); + FilterContext.setContext(new FilterContext(map, config)); + assertEquals(filter.filterProperty(test3Property, test3Property), test3Property); + } + + @Test + public void testClearFilters() throws Exception { + Context filter = new Context(); + RegexPropertyFilter regexFilter = new RegexPropertyFilter(); + regexFilter.setIncludes("test1.*"); + Map<String,String> map = new HashMap<>(); + map.put("test1", "test1"); + map.put("test2", "test2"); + map.put("test.test3", "test.test3"); + FilterContext.setContext(new FilterContext(config)); + assertEquals(filter.filterProperty(test1Property, test1Property), test1Property); + FilterContext.setContext(new FilterContext(config)); + assertEquals(filter.filterProperty(test2Property, test2Property), test2Property); + FilterContext.setContext(new FilterContext(config)); + assertEquals(filter.filterProperty(test3Property, test3Property), test3Property); + filter.addFilter(regexFilter); + FilterContext.setContext(new FilterContext(config)); + assertEquals(filter.filterProperty(test1Property, test1Property), test1Property); + FilterContext.setContext(new FilterContext(config)); + assertNull(filter.filterProperty(test2Property, test2Property)); + FilterContext.setContext(new FilterContext(config)); + assertNull(filter.filterProperty(test3Property, test3Property)); + filter.clearFilters(); + FilterContext.setContext(new FilterContext(config)); + assertEquals(filter.filterProperty(test1Property, test1Property), test1Property); + FilterContext.setContext(new FilterContext(config)); + assertEquals(filter.filterProperty(test2Property, test2Property), test2Property); + FilterContext.setContext(new FilterContext(config)); + assertEquals(filter.filterProperty(test3Property, test3Property), test3Property); + } + + @Test + public void testSetFilters() throws Exception { + Context filter = new Context(); + RegexPropertyFilter regexFilter = new RegexPropertyFilter(); + regexFilter.setIncludes("test\\..*"); + Map<String,String> map = new HashMap<>(); + map.put("test1", test1Property); + map.put("test2", test1Property); + map.put("test.test3", test3Property); + FilterContext.setContext(new FilterContext(map, config)); + assertEquals(filter.filterProperty(test1Property, test1Property), test1Property); + FilterContext.setContext(new FilterContext(map, config)); + assertEquals(filter.filterProperty(test2Property, test2Property), test2Property); + FilterContext.setContext(new FilterContext(map, config)); + assertEquals(filter.filterProperty(test3Property, test3Property), test3Property); + filter.setFilters(regexFilter); + FilterContext.setContext(new FilterContext(map, config)); + assertEquals(filter.filterProperty(test3Property, test3Property), test3Property); + FilterContext.setContext(new FilterContext(map, config)); + assertNull(filter.filterProperty(test2Property, test2Property)); + FilterContext.setContext(new FilterContext(map, config)); + assertNull(filter.filterProperty(test1Property, test1Property)); + } + + @Test + public void testSetFilters1() throws Exception { + Context filter = new Context(); + RegexPropertyFilter regexFilter = new RegexPropertyFilter(); + regexFilter.setIncludes("test1.*"); + Map<String,String> map = new HashMap<>(); + FilterContext.setContext(new FilterContext(map, config)); + assertEquals(filter.filterProperty(test1Property, test1Property), test1Property); + FilterContext.setContext(new FilterContext(map, config)); + assertEquals(filter.filterProperty(test2Property, test2Property), test2Property); + FilterContext.setContext(new FilterContext(map, config)); + assertEquals(filter.filterProperty(test3Property, test3Property), test3Property); + filter.setFilters(Arrays.asList(new Filter[]{regexFilter})); + FilterContext.setContext(new FilterContext(config)); + assertEquals(filter.filterProperty(test1Property, test1Property), test1Property); + FilterContext.setContext(new FilterContext(config)); + assertNull(filter.filterProperty(test2Property, test2Property)); + FilterContext.setContext(new FilterContext(config)); + assertNull(filter.filterProperty(test3Property, test3Property)); + } + + @Test + public void testGetFilters() throws Exception { + Context filter = new Context(); + assertNotNull(filter.getFilters()); + assertTrue(filter.getFilters().isEmpty()); + RegexPropertyFilter regexFilter = new RegexPropertyFilter(); + regexFilter.setIncludes("test\\..*"); + filter.addFilter(regexFilter); + assertNotNull(filter.getFilters()); + assertFalse(filter.getFilters().isEmpty()); + assertEquals(1, filter.getFilters().size()); + assertEquals(regexFilter, filter.getFilters().get(0)); + } + + @Test + public void testToString() throws Exception { + Context filter = new Context(); + assertFalse(filter.toString().contains("test\\..*")); + assertTrue(filter.toString().contains("ProgrammableFilter")); + assertFalse(filter.toString().contains("RegexPropertyFilter")); + RegexPropertyFilter regexFilter = new RegexPropertyFilter(); + regexFilter.setIncludes("test\\..*"); + filter.addFilter(regexFilter); + assertTrue(filter.toString().contains("test\\..*")); + assertTrue(filter.toString().contains("ProgrammableFilter")); + assertTrue(filter.toString().contains("RegexPropertyFilter")); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/formats/base/pom.xml ---------------------------------------------------------------------- diff --git a/modules/formats/base/pom.xml b/modules/formats/base/pom.xml index a2007ce..20d4ab0 100644 --- a/modules/formats/base/pom.xml +++ b/modules/formats/base/pom.xml @@ -34,19 +34,13 @@ under the License. <dependencies> <dependency> - <groupId>org.apache.tamaya</groupId> - <artifactId>tamaya-api</artifactId> - <version>${tamaya-apicore.version}</version> - <scope>provided</scope> - </dependency> - <dependency> <groupId>org.apache.tamaya.ext</groupId> <artifactId>tamaya-resources</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.apache.tamaya</groupId> - <artifactId>tamaya-spisupport</artifactId> + <artifactId>tamaya-base</artifactId> <version>${project.version}</version> </dependency> <dependency> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/formats/base/src/main/java/org/apache/tamaya/format/BaseFormatConfigSourceProvider.java ---------------------------------------------------------------------- diff --git a/modules/formats/base/src/main/java/org/apache/tamaya/format/BaseFormatConfigSourceProvider.java b/modules/formats/base/src/main/java/org/apache/tamaya/format/BaseFormatConfigSourceProvider.java new file mode 100644 index 0000000..86cf909 --- /dev/null +++ b/modules/formats/base/src/main/java/org/apache/tamaya/format/BaseFormatConfigSourceProvider.java @@ -0,0 +1,159 @@ +/* + * 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.format; + +import javax.config.spi.ConfigSource; +import javax.config.spi.ConfigSourceProvider; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Enumeration; +import java.util.List; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Implementation of a {@link ConfigSourceProvider} that reads configuration from some given resource paths + * and using the given formats. The resource path are resolved as classpath resources. This can be changed by + * overriding {@link #getConfigSources(ConfigurationData)}. + * For each resource found the configuration formats passed get a chance to read the resource, if they succeed the + * result is taken as the providers PropertySources to be exposed. + */ +public abstract class BaseFormatConfigSourceProvider implements ConfigSourceProvider { + /** + * The logger used. + */ + private static final Logger LOG = Logger.getLogger(BaseFormatConfigSourceProvider.class.getName()); + /** + * The config formats supported for the given location/resource paths. + */ + private final List<ConfigurationFormat> configFormats = new ArrayList<>(); + /** + * The paths to be evaluated. + */ + private final Collection<URL> paths = new ArrayList<>(); + + /** + * Creates a new instance. + * + * @param formats the formats to be used, not null, not empty. + * @param paths the paths to be resolved, not null, not empty. + */ + public BaseFormatConfigSourceProvider( + List<ConfigurationFormat> formats, + URL... paths) { + this.configFormats.addAll(Objects.requireNonNull(formats)); + this.paths.addAll(Arrays.asList(Objects.requireNonNull(paths))); + } + + /** + * Creates a new instance, hereby using the current thread context classloader, or if not available the classloader + * that loaded this class. + * @param formats the formats to be used, not null, not empty. + * @param paths the paths to be resolved, not null, not empty. + */ + public BaseFormatConfigSourceProvider( + List<ConfigurationFormat> formats, String... paths) { + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + if(cl==null){ + cl = getClass().getClassLoader(); + } + this.configFormats.addAll(Objects.requireNonNull(formats)); + for(String path:paths) { + Enumeration<URL> urls; + try { + urls = cl.getResources(path); + } catch (IOException e) { + LOG.log(Level.WARNING, "Failed to read resource: " + path, e); + continue; + } + while(urls.hasMoreElements()) { + this.paths.add(urls.nextElement()); + } + } + } + + /** + * Creates a new instance. + * + * @param classLoader the ClassLoader to be used, not null, not empty. + * @param formats the formats to be used, not null, not empty. + * @param paths the paths to be resolved, not null, not empty. + */ + public BaseFormatConfigSourceProvider( + List<ConfigurationFormat> formats, + ClassLoader classLoader, String... paths) { + this.configFormats.addAll(Objects.requireNonNull(formats)); + for(String path:paths) { + Enumeration<URL> urls; + try { + urls = classLoader.getResources(path); + } catch (IOException e) { + LOG.log(Level.WARNING, "Failed to read resource: " + path, e); + continue; + } + while(urls.hasMoreElements()) { + this.paths.add(urls.nextElement()); + } + } + } + + + /** + * Method to create a {@link ConfigSource} based on the given entries read. + * + * @param data the configuration data, not null. + * @return the {@link ConfigSource} instance ready to be registered. + */ + protected abstract Collection<ConfigSource> getConfigSources(ConfigurationData data); + + /** + * This method does dynamically resolve the paths using the current ClassLoader set. If no ClassLoader was + * explcitly set during creation the current Thread context ClassLoader is used. If none of the supported + * formats is able to parse a resource a WARNING log is written. + * + * @return the PropertySources successfully read + */ + @Override + public Iterable<ConfigSource> getConfigSources(ClassLoader classLoader) { + List<ConfigSource> propertySources = new ArrayList<>(); + for (URL res : this.paths) { + try{ + for (ConfigurationFormat format : configFormats) { + try (InputStream inputStream = res.openStream()){ + if (format.accepts(res)) { + ConfigurationData data = format.readConfiguration(res.toString(), inputStream); + propertySources.addAll(getConfigSources(data)); + } + } catch (Exception e) { + LOG.log(Level.WARNING, "Failed to put resource based config: " + res, e); + } + } + } catch (Exception e) { + LOG.log(Level.WARNING, "Failed to put resource based config: " + res, e); + } + } + return propertySources; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/formats/base/src/main/java/org/apache/tamaya/format/BaseFormatPropertySourceProvider.java ---------------------------------------------------------------------- diff --git a/modules/formats/base/src/main/java/org/apache/tamaya/format/BaseFormatPropertySourceProvider.java b/modules/formats/base/src/main/java/org/apache/tamaya/format/BaseFormatPropertySourceProvider.java deleted file mode 100644 index a0f1fdd..0000000 --- a/modules/formats/base/src/main/java/org/apache/tamaya/format/BaseFormatPropertySourceProvider.java +++ /dev/null @@ -1,160 +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.format; - -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertySourceProvider; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Enumeration; -import java.util.List; -import java.util.Objects; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Implementation of a {@link PropertySourceProvider} that reads configuration from some given resource paths - * and using the given formats. The resource path are resolved as classpath resources. This can be changed by - * overriding {@link #getPropertySources()}. - * For each resource found the configuration formats passed get a chance to read the resource, if they succeed the - * result is taken as the providers PropertySources to be exposed. - */ -public abstract class BaseFormatPropertySourceProvider implements PropertySourceProvider { - /** - * The logger used. - */ - private static final Logger LOG = Logger.getLogger(BaseFormatPropertySourceProvider.class.getName()); - /** - * The config formats supported for the given location/resource paths. - */ - private final List<ConfigurationFormat> configFormats = new ArrayList<>(); - /** - * The paths to be evaluated. - */ - private final Collection<URL> paths = new ArrayList<>(); - - /** - * Creates a new instance. - * - * @param formats the formats to be used, not null, not empty. - * @param paths the paths to be resolved, not null, not empty. - */ - public BaseFormatPropertySourceProvider( - List<ConfigurationFormat> formats, - URL... paths) { - this.configFormats.addAll(Objects.requireNonNull(formats)); - this.paths.addAll(Arrays.asList(Objects.requireNonNull(paths))); - } - - /** - * Creates a new instance, hereby using the current thread context classloader, or if not available the classloader - * that loaded this class. - * @param formats the formats to be used, not null, not empty. - * @param paths the paths to be resolved, not null, not empty. - */ - public BaseFormatPropertySourceProvider( - List<ConfigurationFormat> formats, String... paths) { - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - if(cl==null){ - cl = getClass().getClassLoader(); - } - this.configFormats.addAll(Objects.requireNonNull(formats)); - for(String path:paths) { - Enumeration<URL> urls; - try { - urls = cl.getResources(path); - } catch (IOException e) { - LOG.log(Level.WARNING, "Failed to read resource: " + path, e); - continue; - } - while(urls.hasMoreElements()) { - this.paths.add(urls.nextElement()); - } - } - } - - /** - * Creates a new instance. - * - * @param classLoader the ClassLoader to be used, not null, not empty. - * @param formats the formats to be used, not null, not empty. - * @param paths the paths to be resolved, not null, not empty. - */ - public BaseFormatPropertySourceProvider( - List<ConfigurationFormat> formats, - ClassLoader classLoader, String... paths) { - this.configFormats.addAll(Objects.requireNonNull(formats)); - for(String path:paths) { - Enumeration<URL> urls; - try { - urls = classLoader.getResources(path); - } catch (IOException e) { - LOG.log(Level.WARNING, "Failed to read resource: " + path, e); - continue; - } - while(urls.hasMoreElements()) { - this.paths.add(urls.nextElement()); - } - } - } - - - /** - * Method to create a {@link org.apache.tamaya.spi.PropertySource} based on the given entries read. - * - * @param data the configuration data, not null. - * @return the {@link org.apache.tamaya.spi.PropertySource} instance ready to be registered. - */ - protected abstract Collection<PropertySource> getPropertySources(ConfigurationData data); - - /** - * This method does dynamically resolve the paths using the current ClassLoader set. If no ClassLoader was - * explcitly set during creation the current Thread context ClassLoader is used. If none of the supported - * formats is able to parse a resource a WARNING log is written. - * - * @return the PropertySources successfully read - */ - @Override - public Collection<PropertySource> getPropertySources() { - List<PropertySource> propertySources = new ArrayList<>(); - for (URL res : this.paths) { - try{ - for (ConfigurationFormat format : configFormats) { - try (InputStream inputStream = res.openStream()){ - if (format.accepts(res)) { - ConfigurationData data = format.readConfiguration(res.toString(), inputStream); - propertySources.addAll(getPropertySources(data)); - } - } catch (Exception e) { - LOG.log(Level.WARNING, "Failed to put resource based config: " + res, e); - } - } - } catch (Exception e) { - LOG.log(Level.WARNING, "Failed to put resource based config: " + res, e); - } - } - return propertySources; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java ---------------------------------------------------------------------- diff --git a/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java b/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java index f08e967..804147e 100644 --- a/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java +++ b/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java @@ -31,10 +31,10 @@ import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.spi.PropertySource; import org.apache.tamaya.spi.ServiceContextManager; +import javax.config.spi.ConfigSource; + /** * Small accessor and management class dealing with {@link org.apache.tamaya.format.ConfigurationFormat} * instances. @@ -199,7 +199,7 @@ public final class ConfigurationFormats { * @param inputStream the inputStream from where to read, not null. * @param formats the formats to try. * @return the ConfigurationData read, or null. - * @throws ConfigException if the resource cannot be read. + * @throws IllegalArgumentException if the resource cannot be read. */ public static ConfigurationData readConfigurationData(String resource, InputStream inputStream, Collection<ConfigurationFormat> formats) throws IOException { @@ -222,64 +222,64 @@ public final class ConfigurationFormats { /** * Tries to read configuration data from a given URL, hereby explicitly trying all given formats - * in order and transforms it into a {@link PropertySource} using a default mapping. + * in order and transforms it into a {@link javax.config.spi.ConfigSource} using a default mapping. * * @param url the URL to read, not null. * @param formats the formats to try. If not formats are passed explicitly, all known formats * are tried. * @return a corresponding property source, or null. - * @throws ConfigException if the resource cannot be read. + * @throws IllegalArgumentException if the resource cannot be read. * @throws IOException if the URL's stream can not be opened. */ - public static PropertySource createPropertySource(URL url, ConfigurationFormat... formats)throws IOException{ - return createPropertySource(url.toString(), url.openStream(), formats); + public static ConfigSource createConfigSource(URL url, ConfigurationFormat... formats)throws IOException{ + return createConfigSource(url.toString(), url.openStream(), formats); } /** * Tries to read configuration data from a given URL, hereby explicitly trying all given formats - * in order and transforms it into a {@link PropertySource} using a default mapping. + * in order and transforms it into a {@link ConfigSource} using a default mapping. * * @param url the URL to read, not null. * @param formats the formats to try. If not formats are passed explicitly, all known formats * are tried. * @return a corresponding property source, or null. - * @throws ConfigException if the resource cannot be read. + * @throws IllegalArgumentException if the resource cannot be read. * @throws IOException if the URL's stream can not be opened. */ - public static PropertySource createPropertySource(URL url, Collection<ConfigurationFormat> formats)throws IOException{ - return createPropertySource(url.toString(), url.openStream(), formats); + public static ConfigSource createConfigSource(URL url, Collection<ConfigurationFormat> formats)throws IOException{ + return createConfigSource(url.toString(), url.openStream(), formats); } /** * Tries to read configuration data from a given URL, hereby explicitly trying all given formats - * in order and transforms it into a {@link PropertySource} using a default mapping. + * in order and transforms it into a {@link ConfigSource} using a default mapping. * * @param resource a descriptive name for the resource, since an InputStream does not have any * @param inputStream the inputStream from where to read, not null. * @param formats the formats to try. If not formats are passed explicitly, all known formats * are tried. * @return a corresponding property source, or null. - * @throws ConfigException if the resource cannot be read. + * @throws IllegalArgumentException if the resource cannot be read. */ - public static PropertySource createPropertySource(String resource, InputStream inputStream, - ConfigurationFormat... formats){ - return createPropertySource(resource, inputStream, Arrays.asList(formats)); + public static ConfigSource createConfigSource(String resource, InputStream inputStream, + ConfigurationFormat... formats){ + return createConfigSource(resource, inputStream, Arrays.asList(formats)); } /** * Tries to read configuration data from a given URL, hereby explicitly trying all given formats - * in order and transforms it into a {@link PropertySource} using a default mapping. + * in order and transforms it into a {@link ConfigSource} using a default mapping. * * @param resource a descriptive name for the resource, since an InputStream does not have any * @param inputStream the inputStream from where to read, not null. * @param formats the formats to try. If not formats are passed explicitly, all known formats * are tried. * @return a corresponding property source, or null. - * @throws ConfigException if the resource cannot be read. + * @throws IllegalArgumentException if the resource cannot be read. */ - public static PropertySource createPropertySource(String resource, InputStream inputStream, - Collection<ConfigurationFormat> formats) { + public static ConfigSource createConfigSource(String resource, InputStream inputStream, + Collection<ConfigurationFormat> formats) { Objects.requireNonNull(resource, "Config resource required for traceability."); try(InputStreamFactory isFactory = new InputStreamFactory(Objects.requireNonNull(inputStream))) { if(formats.isEmpty()){ @@ -289,7 +289,7 @@ public final class ConfigurationFormats { try (InputStream is = isFactory.createInputStream()) { final ConfigurationData data = format.readConfiguration(resource, is); if (data != null) { - return new MappedConfigurationDataPropertySource(data); + return new MappedConfigurationDataConfigSource(data); } } catch (final Exception e) { LOG.log(Level.INFO, @@ -297,9 +297,9 @@ public final class ConfigurationFormats { } } }catch(IOException ioe){ - throw new ConfigException("Failed to read from input stream for "+resource, ioe); + throw new IllegalArgumentException("Failed to read from input stream for "+resource, ioe); } - throw new ConfigException("No matching format found for "+resource+", tried: "+ formats); + throw new IllegalArgumentException("No matching format found for "+resource+", tried: "+ formats); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/formats/base/src/main/java/org/apache/tamaya/format/MappedConfigurationDataConfigSource.java ---------------------------------------------------------------------- diff --git a/modules/formats/base/src/main/java/org/apache/tamaya/format/MappedConfigurationDataConfigSource.java b/modules/formats/base/src/main/java/org/apache/tamaya/format/MappedConfigurationDataConfigSource.java new file mode 100644 index 0000000..3c45747 --- /dev/null +++ b/modules/formats/base/src/main/java/org/apache/tamaya/format/MappedConfigurationDataConfigSource.java @@ -0,0 +1,152 @@ +/* + * 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.format; + +import org.apache.tamaya.base.configsource.BaseConfigSource; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Supplier; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Mapped PropertySource that uses the flattened config data read from an URL by a + * {@link org.apache.tamaya.format.ConfigurationFormat}. Use of a {@link Supplier} + * allows deferring the load until a resource is available. + */ +public class MappedConfigurationDataConfigSource extends BaseConfigSource { + private static final Logger LOG = Logger.getLogger(MappedConfigurationDataConfigSource.class.getName()); + private Map<String, String> properties; + private final Supplier<ConfigurationData> dataSupplier; + + /* + * Constructor, uses hereby the flattened config data read from an URL by a + * ${@link org.apache.tamaya.format.ConfigurationFormat}. + * Hereby it reads the <i>default</i> properties as is and adds properties + * contained in a section as {@code Entry<section.propertyName,value>}. + * @see ConfigurationData#getCombinedProperties() + */ + public MappedConfigurationDataConfigSource(String name, final Supplier<ConfigurationData> dataSupplier) { + this(name, 0, dataSupplier); + } + + /* + * Constructor, uses hereby the flattened config data read from an URL by a + * ${@link org.apache.tamaya.format.ConfigurationFormat}. + * Hereby it reads the <i>default</i> properties as is and adds properties + * contained in a section as {@code Entry<section.propertyName,value>}. + * @see ConfigurationData#getCombinedProperties() + */ + public MappedConfigurationDataConfigSource(final ConfigurationData data) { + this(data.getResource(), 0, new Supplier<ConfigurationData>(){ + @Override + public ConfigurationData get() { + return data; + } + }); + } + + /* + * Constructor, uses hereby the flattened config data read from an URL by a + * ${@link org.apache.tamaya.format.ConfigurationFormat}. + * Hereby it reads the <i>default</i> properties as is and adds properties + * contained in a section as {@code Entry<section.propertyName,value>}. + * @see ConfigurationData#getCombinedProperties() + */ + public MappedConfigurationDataConfigSource(int defaultOrdinal, final ConfigurationData data) { + this(data.getResource(), defaultOrdinal, new Supplier<ConfigurationData>() { + @Override + public ConfigurationData get() { + return data; + } + }); + } + + /* + * Constructor, uses hereby the flattened config data read from an URL by a + * ${@link org.apache.tamaya.format.ConfigurationFormat}. + * Hereby it reads the <i>default</i> properties as is and adds properties + * contained in a section as {@code Entry<section.propertyName,value>}. + * @see ConfigurationData#getCombinedProperties() + */ + public MappedConfigurationDataConfigSource(String name, int defaultOrdinal, Supplier<ConfigurationData> dataSupplier) { + super(defaultOrdinal); + setName(name); + this.dataSupplier = dataSupplier; + load(); + } + + public void load(){ + try{ + this.properties = populateData(dataSupplier.get()); + }catch(Exception e){ + LOG.log(Level.INFO, "Failed to load property source: " + getName(), e); + if(this.properties==null) { + this.properties = new HashMap<>(); + } + this.properties.put("_exception", e.getLocalizedMessage()); + this.properties.put("_state", "ERROR"); + }finally{ + this.properties.put("_timestamp", String.valueOf(System.currentTimeMillis())); + } + } + + /** + * Method that copies and converts the properties read from the data instance + * provided. + * @param data the data returned from the format, not null. + * @return the final properties to be included. + */ + protected Map<String, String> populateData(ConfigurationData data) { + Map<String, String> result = new HashMap<>(); + if(data!=null) { + for (String section : data.getSectionNames()) { + for (Map.Entry<String, String> en : data.getSection(section).entrySet()) { + if ("default".equals(section)) { + result.put(en.getKey(), en.getValue()); + } else { + result.put(section + '.' + en.getKey(), en.getValue()); + } + } + } + result.put("_propertySource", getName()); + result.put("_source", data.getResource()); + } + return result; + } + + @Override + public String getValue(String key) { + return properties.get(key); + } + + @Override + public Map<String, String> getProperties() { + return Collections.unmodifiableMap(this.properties); + } + + @Override + protected String toStringValues() { + return super.toStringValues() + + " dataSupplier=" + dataSupplier + '\n'; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/formats/base/src/main/java/org/apache/tamaya/format/MappedConfigurationDataPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/formats/base/src/main/java/org/apache/tamaya/format/MappedConfigurationDataPropertySource.java b/modules/formats/base/src/main/java/org/apache/tamaya/format/MappedConfigurationDataPropertySource.java deleted file mode 100644 index 5bd6444..0000000 --- a/modules/formats/base/src/main/java/org/apache/tamaya/format/MappedConfigurationDataPropertySource.java +++ /dev/null @@ -1,157 +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.format; - -import org.apache.tamaya.functions.Supplier; -import org.apache.tamaya.spi.PropertyValue; -import org.apache.tamaya.spisupport.propertysource.BasePropertySource; - -import java.util.HashMap; -import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Mapped PropertySource that uses the flattened config data read from an URL by a - * {@link org.apache.tamaya.format.ConfigurationFormat}. Use of a {@link Supplier} - * allows deferring the load until a resource is available. - */ -public class MappedConfigurationDataPropertySource extends BasePropertySource { - private static final Logger LOG = Logger.getLogger(MappedConfigurationDataPropertySource.class.getName()); - private Map<String, String> properties; - private final Supplier<ConfigurationData> dataSupplier; - - /* - * Constructor, uses hereby the flattened config data read from an URL by a - * ${@link org.apache.tamaya.format.ConfigurationFormat}. - * Hereby it reads the <i>default</i> properties as is and adds properties - * contained in a section as {@code Entry<section.propertyName,value>}. - * @see ConfigurationData#getCombinedProperties() - */ - public MappedConfigurationDataPropertySource(String name, final Supplier<ConfigurationData> dataSupplier) { - this(name, 0, dataSupplier); - } - - /* - * Constructor, uses hereby the flattened config data read from an URL by a - * ${@link org.apache.tamaya.format.ConfigurationFormat}. - * Hereby it reads the <i>default</i> properties as is and adds properties - * contained in a section as {@code Entry<section.propertyName,value>}. - * @see ConfigurationData#getCombinedProperties() - */ - public MappedConfigurationDataPropertySource(final ConfigurationData data) { - this(data.getResource(), 0, new Supplier<ConfigurationData>(){ - @Override - public ConfigurationData get() { - return data; - } - }); - } - - /* - * Constructor, uses hereby the flattened config data read from an URL by a - * ${@link org.apache.tamaya.format.ConfigurationFormat}. - * Hereby it reads the <i>default</i> properties as is and adds properties - * contained in a section as {@code Entry<section.propertyName,value>}. - * @see ConfigurationData#getCombinedProperties() - */ - public MappedConfigurationDataPropertySource(int defaultOrdinal, final ConfigurationData data) { - this(data.getResource(), defaultOrdinal, new Supplier<ConfigurationData>() { - @Override - public ConfigurationData get() { - return data; - } - }); - } - - /* - * Constructor, uses hereby the flattened config data read from an URL by a - * ${@link org.apache.tamaya.format.ConfigurationFormat}. - * Hereby it reads the <i>default</i> properties as is and adds properties - * contained in a section as {@code Entry<section.propertyName,value>}. - * @see ConfigurationData#getCombinedProperties() - */ - public MappedConfigurationDataPropertySource(String name, int defaultOrdinal, Supplier<ConfigurationData> dataSupplier) { - super(defaultOrdinal); - setName(name); - this.dataSupplier = dataSupplier; - load(); - } - - public void load(){ - try{ - this.properties = populateData(dataSupplier.get()); - }catch(Exception e){ - LOG.log(Level.INFO, "Failed to load property source: " + getName(), e); - if(this.properties==null) { - this.properties = new HashMap<>(); - } - this.properties.put("_exception", e.getLocalizedMessage()); - this.properties.put("_state", "ERROR"); - }finally{ - this.properties.put("_timestamp", String.valueOf(System.currentTimeMillis())); - } - } - - /** - * Method that copies and converts the properties read from the data instance - * provided. - * @param data the data returned from the format, not null. - * @return the final properties to be included. - */ - protected Map<String, String> populateData(ConfigurationData data) { - Map<String, String> result = new HashMap<>(); - if(data!=null) { - for (String section : data.getSectionNames()) { - for (Map.Entry<String, String> en : data.getSection(section).entrySet()) { - if ("default".equals(section)) { - result.put(en.getKey(), en.getValue()); - } else { - result.put(section + '.' + en.getKey(), en.getValue()); - } - } - } - result.put("_propertySource", getName()); - result.put("_source", data.getResource()); - } - return result; - } - - @Override - public PropertyValue get(String key) { - String val = properties.get(key); - return PropertyValue.of(key, val, getName()); - } - - @Override - public Map<String, PropertyValue> getProperties() { - Map<String, PropertyValue> result = new HashMap<>(); - for(Map.Entry<String,String> en:this.properties.entrySet()) { - result.put(en.getKey(), PropertyValue.of(en.getKey(), en.getValue(), getName())); - } - return result; - } - - @Override - protected String toStringValues() { - return super.toStringValues() + - " dataSupplier=" + dataSupplier + '\n'; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/formats/base/src/main/java/org/apache/tamaya/format/formats/IniConfigurationFormat.java ---------------------------------------------------------------------- diff --git a/modules/formats/base/src/main/java/org/apache/tamaya/format/formats/IniConfigurationFormat.java b/modules/formats/base/src/main/java/org/apache/tamaya/format/formats/IniConfigurationFormat.java index 6fe82c6..b1f2267 100644 --- a/modules/formats/base/src/main/java/org/apache/tamaya/format/formats/IniConfigurationFormat.java +++ b/modules/formats/base/src/main/java/org/apache/tamaya/format/formats/IniConfigurationFormat.java @@ -18,7 +18,6 @@ */ package org.apache.tamaya.format.formats; -import org.apache.tamaya.ConfigException; import org.apache.tamaya.format.ConfigurationData; import org.apache.tamaya.format.ConfigurationDataBuilder; import org.apache.tamaya.format.ConfigurationFormat; @@ -65,7 +64,7 @@ public class IniConfigurationFormat implements ConfigurationFormat { if (line.startsWith("[")) { int end = line.indexOf(']'); if (end < 0) { - throw new ConfigException( + throw new IllegalArgumentException( "Invalid INI-Format, ']' expected, at " + lineNum + " in " + resource); } section = line.substring(1, end); http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/formats/base/src/test/java/org/apache/tamaya/format/FormatConfigSourceProviderTest.java ---------------------------------------------------------------------- diff --git a/modules/formats/base/src/test/java/org/apache/tamaya/format/FormatConfigSourceProviderTest.java b/modules/formats/base/src/test/java/org/apache/tamaya/format/FormatConfigSourceProviderTest.java new file mode 100644 index 0000000..881a817 --- /dev/null +++ b/modules/formats/base/src/test/java/org/apache/tamaya/format/FormatConfigSourceProviderTest.java @@ -0,0 +1,58 @@ +/* + * 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.format; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; + +import org.junit.Test; + +import javax.config.spi.ConfigSource; +import javax.config.spi.ConfigSourceProvider; + +import static org.junit.Assert.*; + +public class FormatConfigSourceProviderTest + extends BaseFormatConfigSourceProvider { + public FormatConfigSourceProviderTest() { + super(ConfigurationFormats.getFormats(), "Test.ini", "Test.properties"); + } + + @Test + public void getConfigSourcesTest() { + ConfigSourceProvider provider = new FormatConfigSourceProviderTest(); + Iterable<ConfigSource> sources = provider.getConfigSources(null); + + Iterator iter = sources.iterator(); + assertTrue(iter.hasNext()); + iter.next(); + assertTrue(iter.hasNext()); + iter.next(); + assertFalse(iter.hasNext()); + } + + @Override + protected Collection<ConfigSource> getConfigSources(ConfigurationData data) { + ConfigSource ps = new MappedConfigurationDataConfigSource(data); + ArrayList<ConfigSource> result = new ArrayList<ConfigSource>(); + result.add(ps); + return result; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/formats/base/src/test/java/org/apache/tamaya/format/FormatPropertySourceProviderTest.java ---------------------------------------------------------------------- diff --git a/modules/formats/base/src/test/java/org/apache/tamaya/format/FormatPropertySourceProviderTest.java b/modules/formats/base/src/test/java/org/apache/tamaya/format/FormatPropertySourceProviderTest.java deleted file mode 100644 index 3a4ec16..0000000 --- a/modules/formats/base/src/test/java/org/apache/tamaya/format/FormatPropertySourceProviderTest.java +++ /dev/null @@ -1,50 +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.format; - -import java.util.ArrayList; -import java.util.Collection; - -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertySourceProvider; -import org.junit.Test; -import static org.junit.Assert.*; - -public class FormatPropertySourceProviderTest - extends BaseFormatPropertySourceProvider { - public FormatPropertySourceProviderTest() { - super(ConfigurationFormats.getFormats(), "Test.ini", "Test.properties"); - } - - @Test - public void getPropertySourcesTest() { - PropertySourceProvider provider = new FormatPropertySourceProviderTest(); - Collection<PropertySource> sources = provider.getPropertySources(); - - assertEquals(2, sources.size()); - } - - @Override - protected Collection<PropertySource> getPropertySources(ConfigurationData data) { - PropertySource ps = new MappedConfigurationDataPropertySource(data); - ArrayList<PropertySource> result = new ArrayList<PropertySource>(); - result.add(ps); - return result; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/formats/base/src/test/java/org/apache/tamaya/format/MappedConfigurationDataPropertySourceTest.java ---------------------------------------------------------------------- diff --git a/modules/formats/base/src/test/java/org/apache/tamaya/format/MappedConfigurationDataPropertySourceTest.java b/modules/formats/base/src/test/java/org/apache/tamaya/format/MappedConfigurationDataPropertySourceTest.java index 1d26d3a..619240f 100644 --- a/modules/formats/base/src/test/java/org/apache/tamaya/format/MappedConfigurationDataPropertySourceTest.java +++ b/modules/formats/base/src/test/java/org/apache/tamaya/format/MappedConfigurationDataPropertySourceTest.java @@ -19,21 +19,22 @@ package org.apache.tamaya.format; import org.apache.tamaya.format.formats.PropertiesFormat; -import org.apache.tamaya.spi.PropertySource; import org.junit.Test; +import javax.config.spi.ConfigSource; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; /** - * Tests for {@link MappedConfigurationDataPropertySource}. + * Tests for {@link MappedConfigurationDataConfigSource}. */ public class MappedConfigurationDataPropertySourceTest { @Test public void testGetName() throws Exception { - MappedConfigurationDataPropertySource ps = new MappedConfigurationDataPropertySource(createConfigurationData("test1")); + MappedConfigurationDataConfigSource ps = new MappedConfigurationDataConfigSource(createConfigurationData("test1")); assertEquals("test1", ps.getName()); } @@ -47,7 +48,7 @@ public class MappedConfigurationDataPropertySourceTest { private ConfigurationData createConfigurationData(String sourceName, int ordinal) { return ConfigurationDataBuilder.of(sourceName, new PropertiesFormat()) .addDefaultProperty("a", "aValue").addSectionProperty("section1", "sectionKey1", "sectionValue11") - .addSections("section1", "section12").addDefaultProperty(PropertySource.TAMAYA_ORDINAL, String.valueOf(ordinal)) + .addSections("section1", "section12").addDefaultProperty(ConfigSource.CONFIG_ORDINAL, String.valueOf(ordinal)) .addSectionProperty("section2", "sectionKey1", "sectionValue21").build(); } @@ -60,39 +61,39 @@ public class MappedConfigurationDataPropertySourceTest { @Test public void testGetOrdinal() throws Exception { - MappedConfigurationDataPropertySource ps = new MappedConfigurationDataPropertySource(createConfigurationData("test1", 11)); + MappedConfigurationDataConfigSource ps = new MappedConfigurationDataConfigSource(createConfigurationData("test1", 11)); assertEquals(11, ps.getOrdinal()); } @Test public void testGet() throws Exception { - MappedConfigurationDataPropertySource ps = new MappedConfigurationDataPropertySource(createConfigurationData("test2")); - assertEquals("aValue", ps.get("a").getValue()); - assertNotNull(ps.get("section1.sectionKey1").getValue()); - assertNotNull(ps.get("section2.sectionKey1").getValue()); - assertNull(ps.get("sectionKey1")); - ps = new MappedConfigurationDataPropertySource(createConfigurationDataNoDefault("test2")); - assertEquals("sectionValue11", ps.get("section1.sectionKey1").getValue()); - assertEquals("sectionValue21", ps.get("section2.sectionKey1").getValue()); - assertNull(ps.get("a")); - assertNull(ps.get("section1")); + MappedConfigurationDataConfigSource ps = new MappedConfigurationDataConfigSource(createConfigurationData("test2")); + assertEquals("aValue", ps.getValue("a")); + assertNotNull(ps.getValue("section1.sectionKey1")); + assertNotNull(ps.getValue("section2.sectionKey1")); + assertNull(ps.getValue("sectionKey1")); + ps = new MappedConfigurationDataConfigSource(createConfigurationDataNoDefault("test2")); + assertEquals("sectionValue11", ps.getValue("section1.sectionKey1")); + assertEquals("sectionValue21", ps.getValue("section2.sectionKey1")); + assertNull(ps.getValue("a")); + assertNull(ps.getValue("section1")); } @Test public void testGetProperties() throws Exception { - MappedConfigurationDataPropertySource ps = new MappedConfigurationDataPropertySource(createConfigurationData("test3")); + MappedConfigurationDataConfigSource ps = new MappedConfigurationDataConfigSource(createConfigurationData("test3")); assertNotNull(ps.getProperties()); - assertEquals("aValue", ps.getProperties().get("a").getValue()); + assertEquals("aValue", ps.getProperties().get("a")); assertNotNull(ps.getProperties().get("section1.sectionKey1")); assertNotNull(ps.getProperties().get("section2.sectionKey1")); assertNull(ps.getProperties().get("section1.sectionKey2")); assertNull(ps.getProperties().get("section2.sectionKey2")); assertNull(ps.getProperties().get("sectionKey1")); assertNull(ps.getProperties().get("sectionKey2")); - ps = new MappedConfigurationDataPropertySource(createConfigurationDataNoDefault("test3")); + ps = new MappedConfigurationDataConfigSource(createConfigurationDataNoDefault("test3")); assertNotNull(ps.getProperties()); - assertEquals("sectionValue11", ps.getProperties().get("section1.sectionKey1").getValue()); - assertEquals("sectionValue21", ps.getProperties().get("section2.sectionKey1").getValue()); - assertNull(ps.get("section1")); + assertEquals("sectionValue11", ps.getProperties().get("section1.sectionKey1")); + assertEquals("sectionValue21", ps.getProperties().get("section2.sectionKey1")); + assertNull(ps.getValue("section1")); } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/formats/json/pom.xml ---------------------------------------------------------------------- diff --git a/modules/formats/json/pom.xml b/modules/formats/json/pom.xml index 49360ca..75a27f7 100644 --- a/modules/formats/json/pom.xml +++ b/modules/formats/json/pom.xml @@ -37,16 +37,9 @@ under the License. <dependencies> <dependency> <groupId>org.apache.tamaya</groupId> - <artifactId>tamaya-api</artifactId> - <version>${tamaya-apicore.version}</version> - <scope>provided</scope> - </dependency> - - <dependency> - <groupId>org.apache.tamaya</groupId> <artifactId>tamaya-core</artifactId> <version>${tamaya-apicore.version}</version> - <scope>provided</scope> + <scope>test</scope> </dependency> <dependency> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONConfigSource.java ---------------------------------------------------------------------- diff --git a/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONConfigSource.java b/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONConfigSource.java new file mode 100644 index 0000000..2f73fb0 --- /dev/null +++ b/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONConfigSource.java @@ -0,0 +1,148 @@ +/* + * 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.json; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.nio.charset.Charset; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.config.spi.ConfigSource; +import javax.json.Json; +import javax.json.JsonObject; +import javax.json.JsonReaderFactory; +import javax.json.JsonStructure; + +import static java.lang.String.format; + +/** + * Property source based on a JSON file. + */ +public class JSONConfigSource implements ConfigSource { + /** Constant for enabling comments in Johnzon. */ + public static final String JOHNZON_SUPPORTS_COMMENTS_PROP = "org.apache.johnzon.supports-comments"; + + /** The underlying resource. */ + private final URL urlResource; + /** The values read. */ + private final Map<String, String> values; + /** The evaluated ordinal. */ + private int ordinal; + /** The JSON reader factory used. */ + private JsonReaderFactory readerFactory = initReaderFactory(); + + /** Initializes the factory to be used for creating readers. */ + private JsonReaderFactory initReaderFactory() { + Map<String, Object> config = new HashMap<>(); + config.put(JOHNZON_SUPPORTS_COMMENTS_PROP, true); + return Json.createReaderFactory(config); + } + + /** + * Constructor, hereby using 0 as the default ordinal. + * @param resource the resource modelled as URL, not null. + */ + public JSONConfigSource(URL resource)throws IOException { + this(resource, 0); + } + + /** + * Constructor. + * @param resource the resource modelled as URL, not null. + * @param defaultOrdinal the defaultOrdinal to be used. + */ + public JSONConfigSource(URL resource, int defaultOrdinal)throws IOException { + urlResource = Objects.requireNonNull(resource); + this.ordinal = defaultOrdinal; // may be overriden by read... + this.values = readConfig(urlResource); + if (this.values.containsKey(CONFIG_ORDINAL)) { + this.ordinal = Integer.parseInt(this.values.get(CONFIG_ORDINAL)); + } + Map<String, Object> config = new HashMap<>(); + config.put(JOHNZON_SUPPORTS_COMMENTS_PROP, true); + this.readerFactory = Json.createReaderFactory(config); + } + + + 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 ordinal; + } + + @Override + public String getName() { + return urlResource.toExternalForm(); + } + + @Override + public String getValue(String key) { + return getProperties().get(key); + } + + @Override + public Map<String, String> getProperties() { + + return Collections.unmodifiableMap(values); + } + + /** + * Reads the configuration. + * @param urlResource soure of the configuration. + * @return the configuration read from the given resource URL. + * @throws IllegalArgumentException if resource URL cannot be read. + */ + protected Map<String, String> readConfig(URL urlResource) throws IOException{ + try (InputStream is = urlResource.openStream()) { + JsonStructure root = this.readerFactory.createReader(is, Charset.forName("UTF-8")).read(); + + // Test added. H. Saly, 15. Aug. 2015 + if (!(root instanceof JsonObject)) { + throw new IllegalArgumentException("Currently only JSON objects are supported"); + } + + Map<String, String> values = new HashMap<>(); + JSONVisitor visitor = new JSONVisitor((JsonObject)root, values); + visitor.run(); + Map<String, String> result = new HashMap<>(); + for(Map.Entry<String,String> en:values.entrySet()){ + result.put(en.getKey(), en.getValue()); + } + return result; + }catch(IOException ioe){ + throw ioe; + }catch (Exception t) { + throw new IOException(format("Failed to read properties from %s", urlResource.toExternalForm()), t); + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONPropertySource.java b/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONPropertySource.java deleted file mode 100644 index 5934210..0000000 --- a/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONPropertySource.java +++ /dev/null @@ -1,155 +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.json; - -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.nio.charset.Charset; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; -import java.util.logging.Level; -import java.util.logging.Logger; - -import javax.json.Json; -import javax.json.JsonObject; -import javax.json.JsonReaderFactory; -import javax.json.JsonStructure; - -import static java.lang.String.format; - -/** - * Property source based on a JSON file. - */ -public class JSONPropertySource implements PropertySource { - /** Constant for enabling comments in Johnzon. */ - public static final String JOHNZON_SUPPORTS_COMMENTS_PROP = "org.apache.johnzon.supports-comments"; - - /** The underlying resource. */ - private final URL urlResource; - /** The values read. */ - private final Map<String, PropertyValue> values; - /** The evaluated ordinal. */ - private int ordinal; - /** The JSON reader factory used. */ - private JsonReaderFactory readerFactory = initReaderFactory(); - - /** Initializes the factory to be used for creating readers. */ - private JsonReaderFactory initReaderFactory() { - Map<String, Object> config = new HashMap<>(); - config.put(JOHNZON_SUPPORTS_COMMENTS_PROP, true); - return Json.createReaderFactory(config); - } - - /** - * Constructor, hereby using 0 as the default ordinal. - * @param resource the resource modelled as URL, not null. - */ - public JSONPropertySource(URL resource)throws IOException { - this(resource, 0); - } - - /** - * Constructor. - * @param resource the resource modelled as URL, not null. - * @param defaultOrdinal the defaultOrdinal to be used. - */ - public JSONPropertySource(URL resource, int defaultOrdinal)throws IOException { - urlResource = Objects.requireNonNull(resource); - this.ordinal = defaultOrdinal; // may be overriden by read... - this.values = readConfig(urlResource); - if (this.values.containsKey(TAMAYA_ORDINAL)) { - this.ordinal = Integer.parseInt(this.values.get(TAMAYA_ORDINAL).getValue()); - } - Map<String, Object> config = new HashMap<>(); - config.put(JOHNZON_SUPPORTS_COMMENTS_PROP, true); - this.readerFactory = Json.createReaderFactory(config); - } - - - 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 ordinal; - } - - @Override - public String getName() { - return urlResource.toExternalForm(); - } - - @Override - public PropertyValue get(String key) { - return getProperties().get(key); - } - - @Override - public Map<String, PropertyValue> getProperties() { - - return Collections.unmodifiableMap(values); - } - - /** - * Reads the configuration. - * @param urlResource soure of the configuration. - * @return the configuration read from the given resource URL. - * @throws ConfigException if resource URL cannot be read. - */ - protected Map<String, PropertyValue> readConfig(URL urlResource) throws IOException{ - try (InputStream is = urlResource.openStream()) { - JsonStructure root = this.readerFactory.createReader(is, Charset.forName("UTF-8")).read(); - - // Test added. H. Saly, 15. Aug. 2015 - if (!(root instanceof JsonObject)) { - throw new ConfigException("Currently only JSON objects are supported"); - } - - Map<String, String> values = new HashMap<>(); - JSONVisitor visitor = new JSONVisitor((JsonObject)root, values); - visitor.run(); - Map<String, PropertyValue> result = new HashMap<>(); - for(Map.Entry<String,String> en:values.entrySet()){ - result.put(en.getKey(), PropertyValue.of(en.getKey(), en.getValue(), getName())); - } - return result; - }catch(IOException ioe){ - throw ioe; - }catch (Exception t) { - throw new IOException(format("Failed to read properties from %s", urlResource.toExternalForm()), t); - } - } - - @Override - public boolean isScannable() { - return true; - } -}
