http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java ---------------------------------------------------------------------- diff --git a/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java b/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java index 32428de..eec8963 100644 --- a/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java +++ b/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java @@ -23,7 +23,6 @@ import org.apache.tamaya.spi.PropertyValue; import java.io.IOException; import java.io.InputStream; import java.net.URL; -import java.util.Collection; /** * <p>Implementations current this class encapsulate the mechanism how to read a @@ -64,9 +63,9 @@ public interface ConfigurationFormat { * different levels of data: * <ul> * <li>Only a <i>default</i> section is returned, since the configuration format does not support - * hierarchies, e.g. a root {@link PropertyValue} with a number of direct getChildren.</li> - * <li>Hierarchical formats such as INI, XML, YAML and JSON can have both object mapped childs as well as arrays/list - * childs. With {@link PropertyValue#asMap()} a default mapping to a property based representation is + * hierarchies, e.g. a root {@link PropertyValue} with a number of direct getList.</li> + * <li>Hierarchical formats such as INI, XML, YAML and JSON can have both createObject mapped childs as well as arrays/createList + * childs. With {@link PropertyValue#toMap()} a default mapping to a property based representation is * available.</li> * </ul> *
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/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 f098b94..9020093 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 @@ -32,6 +32,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.apache.tamaya.ConfigException; +import org.apache.tamaya.functions.Predicate; import org.apache.tamaya.spi.PropertySource; import org.apache.tamaya.spi.ServiceContextManager; @@ -45,10 +46,31 @@ public final class ConfigurationFormats { */ private static final Logger LOG = Logger.getLogger(ConfigurationFormats.class.getName()); + private ClassLoader classLoader; + /** * Singleton constructor. */ - private ConfigurationFormats() { + private ConfigurationFormats(ClassLoader classLoader) { + this.classLoader = Objects.requireNonNull(classLoader); + } + + /** + * Access the format manager using the default classloader. + * @return the corresponding format manager, not null. + * @see ServiceContextManager#getDefaultClassLoader() + */ + public static ConfigurationFormats getInstance(){ + return getInstance(ServiceContextManager.getDefaultClassLoader()); + } + + /** + * Access the format manager targeting the given classloader. + * @return the corresponding format manager, not null. + */ + public static ConfigurationFormats getInstance(ClassLoader classLoader){ + return ServiceContextManager.getServiceContext(classLoader) + .getService(ConfigurationFormats.class, () -> new ConfigurationFormats(classLoader)); } /** @@ -56,8 +78,8 @@ public final class ConfigurationFormats { * * @return the currently available formats, never null. */ - public static List<ConfigurationFormat> getFormats() { - return ServiceContextManager.getServiceContext().getServices(ConfigurationFormat.class); + public List<ConfigurationFormat> getFormats() { + return ServiceContextManager.getServiceContext(classLoader).getServices(ConfigurationFormat.class); } /** @@ -66,7 +88,7 @@ public final class ConfigurationFormats { * @param formatNames available formats to be ordered. * @return the currently available formats, never null. */ - public static List<ConfigurationFormat> getFormats(String... formatNames) { + public List<ConfigurationFormat> getFormats(String... formatNames) { final List<ConfigurationFormat> result = new ArrayList<>(); final Set<String> names = new HashSet<>(Arrays.asList(formatNames)); for (final ConfigurationFormat f : getFormats()) { @@ -77,29 +99,13 @@ public final class ConfigurationFormats { return result; } - // Activate for JDK 8... -// /** -// * Get all currently available formats, ordered by priority. -// * -// * @return the currently available formats, never null. -// */ -// public static List<ConfigurationFormat> getFormats(Predicate<String> namePredicate) { -// List<ConfigurationFormat> result = new ArrayList<>(); -// for(ConfigurationFormat f:getFormats()){ -// if(namePredicate.test(f.name()){ -// result.addNode(f); -// } -// } -// return result; -// } - /** * Get all currently available formats, ordered by priority. * * @param url source to read configuration from. * @return the currently available formats, never null. */ - public static List<ConfigurationFormat> getFormats(final URL url) { + public List<ConfigurationFormat> getFormats(final URL url) { final List<ConfigurationFormat> formats = getFormats(); final List<ConfigurationFormat> result = new ArrayList<>(); for (final ConfigurationFormat f : formats) { @@ -118,7 +124,7 @@ public final class ConfigurationFormats { * @return the ConfigurationData read, or null. * @throws IOException if the resource cannot be read. */ - public static ConfigurationData readConfigurationData(final URL url) throws IOException { + public ConfigurationData readConfigurationData(final URL url) throws IOException { final List<ConfigurationFormat> formats = getFormats(url); return readConfigurationData(url, formats.toArray(new ConfigurationFormat[formats.size()])); } @@ -131,7 +137,7 @@ public final class ConfigurationFormats { * @return the ConfigurationData read, or null. * @throws IOException if the resource cannot be read. */ - public static ConfigurationData readConfigurationData(URL url, ConfigurationFormat... formats) throws IOException { + public ConfigurationData readConfigurationData(URL url, ConfigurationFormat... formats) throws IOException { return readConfigurationData(url, Arrays.asList(formats)); } @@ -143,7 +149,7 @@ public final class ConfigurationFormats { * @return the ConfigurationData read, or null. * @throws IOException if the resource cannot be read. */ - public static ConfigurationData readConfigurationData(URL url, Collection<ConfigurationFormat> formats) throws IOException { + public ConfigurationData readConfigurationData(URL url, Collection<ConfigurationFormat> formats) throws IOException { return readConfigurationData(url.toString(), url.openStream(), formats); } @@ -153,7 +159,7 @@ public final class ConfigurationFormats { * @return the {@link org.apache.tamaya.format.ConfigurationData} of the files successfully decoded by the * given formats. */ - public static Collection<ConfigurationData> readConfigurationData(Collection<URL> urls, ConfigurationFormat... formats) { + public Collection<ConfigurationData> readConfigurationData(Collection<URL> urls, ConfigurationFormat... formats) { return readConfigurationData(urls, Arrays.asList(formats)); } @@ -163,7 +169,7 @@ public final class ConfigurationFormats { * @return the {@link org.apache.tamaya.format.ConfigurationData} of the files successfully decoded by the * given formats. */ - public static Collection<ConfigurationData> readConfigurationData(Collection<URL> urls, Collection<ConfigurationFormat> formats) { + public Collection<ConfigurationData> readConfigurationData(Collection<URL> urls, Collection<ConfigurationFormat> formats) { final List<ConfigurationData> dataRead = new ArrayList<>(); for (final URL url : urls) { try { @@ -187,7 +193,7 @@ public final class ConfigurationFormats { * @return the ConfigurationData read, or null. * @throws IOException if the resource cannot be read. */ - public static ConfigurationData readConfigurationData(String resource, InputStream inputStream, + public ConfigurationData readConfigurationData(String resource, InputStream inputStream, ConfigurationFormat... formats) throws IOException { return readConfigurationData(resource, inputStream, Arrays.asList(formats)); } @@ -202,9 +208,12 @@ public final class ConfigurationFormats { * @throws ConfigException if the resource cannot be read. * @throws IOException if reading the input fails. */ - public static ConfigurationData readConfigurationData(String resource, InputStream inputStream, + public ConfigurationData readConfigurationData(String resource, InputStream inputStream, Collection<ConfigurationFormat> formats) throws IOException { Objects.requireNonNull(resource, "Config resource required for traceability."); + if(formats.isEmpty()){ + formats = getFormats(); + } try(InputStreamFactory isFactory = new InputStreamFactory(Objects.requireNonNull(inputStream))) { for (final ConfigurationFormat format : formats) { try (InputStream is = isFactory.createInputStream()) { @@ -232,7 +241,7 @@ public final class ConfigurationFormats { * @throws ConfigException 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{ + public PropertySource createPropertySource(URL url, ConfigurationFormat... formats)throws IOException{ return createPropertySource(url.toString(), url.openStream(), formats); } @@ -247,7 +256,7 @@ public final class ConfigurationFormats { * @throws ConfigException 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{ + public PropertySource createPropertySource(URL url, Collection<ConfigurationFormat> formats)throws IOException{ return createPropertySource(url.toString(), url.openStream(), formats); } @@ -262,7 +271,7 @@ public final class ConfigurationFormats { * @return a corresponding property source, or null. * @throws ConfigException if the resource cannot be read. */ - public static PropertySource createPropertySource(String resource, InputStream inputStream, + public PropertySource createPropertySource(String resource, InputStream inputStream, ConfigurationFormat... formats){ return createPropertySource(resource, inputStream, Arrays.asList(formats)); } @@ -279,7 +288,7 @@ public final class ConfigurationFormats { * @return a corresponding property source, or null. * @throws ConfigException if the resource cannot be read. */ - public static PropertySource createPropertySource(String resource, InputStream inputStream, + public PropertySource createPropertySource(String resource, InputStream inputStream, Collection<ConfigurationFormat> formats) { Objects.requireNonNull(resource, "Config resource required for traceability."); try(InputStreamFactory isFactory = new InputStreamFactory(Objects.requireNonNull(inputStream))) { http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/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 index eeee6a6..2fff0ee 100644 --- 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 @@ -43,7 +43,7 @@ public class MappedConfigurationDataPropertySource extends BasePropertySource { * 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>}. + * contained in a section as {@code Entry<section.propertyName,createValue>}. * @see ConfigurationData#getCombinedProperties() */ public MappedConfigurationDataPropertySource(String name, final Supplier<ConfigurationData> dataSupplier) { @@ -54,7 +54,7 @@ public class MappedConfigurationDataPropertySource extends BasePropertySource { * 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>}. + * contained in a section as {@code Entry<section.propertyName,createValue>}. * @see ConfigurationData#getCombinedProperties() */ public MappedConfigurationDataPropertySource(final ConfigurationData data) { @@ -65,7 +65,7 @@ public class MappedConfigurationDataPropertySource extends BasePropertySource { * 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>}. + * contained in a section as {@code Entry<section.propertyName,createValue>}. * @see ConfigurationData#getCombinedProperties() */ public MappedConfigurationDataPropertySource(int defaultOrdinal, final ConfigurationData data) { @@ -76,7 +76,7 @@ public class MappedConfigurationDataPropertySource extends BasePropertySource { * 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>}. + * contained in a section as {@code Entry<section.propertyName,createValue>}. * @see ConfigurationData#getCombinedProperties() */ public MappedConfigurationDataPropertySource(String name, int defaultOrdinal, Supplier<ConfigurationData> dataSupplier) { http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/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 2f7fc42..7c4b911 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 @@ -21,6 +21,7 @@ package org.apache.tamaya.format.formats; import org.apache.tamaya.ConfigException; import org.apache.tamaya.format.ConfigurationData; import org.apache.tamaya.format.ConfigurationFormat; +import org.apache.tamaya.spi.ObjectValue; import org.apache.tamaya.spi.PropertyValue; import org.osgi.service.component.annotations.Component; @@ -52,7 +53,7 @@ public class IniConfigurationFormat implements ConfigurationFormat { @Override public ConfigurationData readConfiguration(String resource, InputStream inputStream) throws IOException{ - PropertyValue data = PropertyValue.create(); + PropertyValue data = PropertyValue.createObject(); data.setMeta("resource", resource); try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"))) { String line = reader.readLine(); @@ -81,17 +82,17 @@ public class IniConfigurationFormat implements ConfigurationFormat { String value = line.substring(sep + 1); if (section != null) { final String sectionName = section; - PropertyValue sectionPV = sections.computeIfAbsent(section, - s -> PropertyValue.of(sectionName, "", resource) + ObjectValue sectionPV = (ObjectValue)sections.computeIfAbsent(section, + s -> PropertyValue.createObject(sectionName) .setMeta(ConfigurationFormat.class, this)); - sectionPV.addChild(PropertyValue.of(key, value, resource) - .setMeta(ConfigurationFormat.class, this)); + sectionPV.setField(key, value).setMeta("source", resource) + .setMeta(ConfigurationFormat.class, this); } else { String finalSection = "default"; - PropertyValue sectionBuilder = sections.computeIfAbsent(section, - s -> PropertyValue.of(finalSection, "", resource)); - sectionBuilder.addChild(PropertyValue.of(key, value, resource) - .setMeta(ConfigurationFormat.class, this)); + ObjectValue sectionBuilder = (ObjectValue)sections.computeIfAbsent(section, + s -> PropertyValue.createObject(finalSection).setMeta("source", resource)); + sectionBuilder.setField(key, value).setMeta("source", resource) + .setMeta(ConfigurationFormat.class, this); } } line = reader.readLine(); http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/formats/base/src/test/java/org/apache/tamaya/format/ConfigurationFormatsTest.java ---------------------------------------------------------------------- diff --git a/modules/formats/base/src/test/java/org/apache/tamaya/format/ConfigurationFormatsTest.java b/modules/formats/base/src/test/java/org/apache/tamaya/format/ConfigurationFormatsTest.java index 7d42dfb..611f75d 100644 --- a/modules/formats/base/src/test/java/org/apache/tamaya/format/ConfigurationFormatsTest.java +++ b/modules/formats/base/src/test/java/org/apache/tamaya/format/ConfigurationFormatsTest.java @@ -34,17 +34,17 @@ public class ConfigurationFormatsTest { @org.junit.Test public void testGetFormats() throws Exception { - List<ConfigurationFormat> formats = ConfigurationFormats.getFormats(); + List<ConfigurationFormat> formats = ConfigurationFormats.getInstance().getFormats(); assertNotNull(formats); assertEquals(formats.size(), 3); } @org.junit.Test public void testReadConfigurationData() throws Exception { - List<ConfigurationFormat> formats = ConfigurationFormats.getFormats(getClass().getResource("/Test.ini")); + List<ConfigurationFormat> formats = ConfigurationFormats.getInstance().getFormats(getClass().getResource("/Test.ini")); assertNotNull(formats); assertEquals(formats.size(), 1); - formats = ConfigurationFormats.getFormats(getClass().getResource("/Test.properties")); + formats = ConfigurationFormats.getInstance().getFormats(getClass().getResource("/Test.properties")); assertNotNull(formats); assertEquals(formats.size(), 1); @@ -52,26 +52,26 @@ public class ConfigurationFormatsTest { @org.junit.Test public void testReadConfigurationData_URL() throws Exception { - ConfigurationData data = ConfigurationFormats.readConfigurationData( + ConfigurationData data = ConfigurationFormats.getInstance().readConfigurationData( getClass().getResource("/Test.ini")); assertNotNull(data); - data = ConfigurationFormats.readConfigurationData(getClass().getResource("/Test.properties")); + data = ConfigurationFormats.getInstance().readConfigurationData(getClass().getResource("/Test.properties")); assertNotNull(data); } @org.junit.Test public void testReadConfigurationData_URL_ConfiguratonFormat() throws Exception { - ConfigurationData data = ConfigurationFormats.readConfigurationData( + ConfigurationData data = ConfigurationFormats.getInstance().readConfigurationData( getClass().getResource("/Test.ini"), - ConfigurationFormats.getFormats("ini")); + ConfigurationFormats.getInstance().getFormats("ini")); assertNotNull(data); } @org.junit.Test public void testReadConfigurationData_URL_CollectionOfConfiguratonFormat() throws Exception { List<ConfigurationFormat> formats = new ArrayList<>(); - formats.add(ConfigurationFormats.getFormats("ini").get(0)); - ConfigurationData data = ConfigurationFormats.readConfigurationData( + formats.add(ConfigurationFormats.getInstance().getFormats("ini").get(0)); + ConfigurationData data = ConfigurationFormats.getInstance().readConfigurationData( getClass().getResource("/Test.ini"), formats); assertNotNull(data); @@ -82,8 +82,8 @@ public class ConfigurationFormatsTest { List<URL> urls = new ArrayList<>(); urls.add(getClass().getResource("/Test.ini")); List<ConfigurationFormat> formats = new ArrayList<>(); - formats.add(ConfigurationFormats.getFormats("ini").get(0)); - Collection<ConfigurationData> data = ConfigurationFormats.readConfigurationData( + formats.add(ConfigurationFormats.getInstance().getFormats("ini").get(0)); + Collection<ConfigurationData> data = ConfigurationFormats.getInstance().readConfigurationData( urls, formats); assertNotNull(data); @@ -94,27 +94,27 @@ public class ConfigurationFormatsTest { public void testReadConfigurationData_CollectionOfURL_ConfiguratonFormat() throws Exception { List<URL> urls = new ArrayList<>(); urls.add(getClass().getResource("/Test.ini")); - Collection<ConfigurationData> data = ConfigurationFormats.readConfigurationData( + Collection<ConfigurationData> data = ConfigurationFormats.getInstance().readConfigurationData( urls, - ConfigurationFormats.getFormats("ini").get(0)); + ConfigurationFormats.getInstance().getFormats("ini").get(0)); assertNotNull(data); assertTrue(data.size()==1); } @org.junit.Test public void testReadConfigurationData_String_InputStream_ConfiguratonFormat() throws Exception { - ConfigurationData data = ConfigurationFormats.readConfigurationData( + ConfigurationData data = ConfigurationFormats.getInstance().readConfigurationData( "Test.ini", getClass().getResource("/Test.ini").openStream(), - ConfigurationFormats.getFormats("ini")); + ConfigurationFormats.getInstance().getFormats("ini")); assertNotNull(data); } @org.junit.Test public void testReadConfigurationData_String_InputStream_CollectionOfConfiguratonFormat() throws Exception { List<ConfigurationFormat> formats = new ArrayList<>(); - formats.add(ConfigurationFormats.getFormats("ini").get(0)); - ConfigurationData data = ConfigurationFormats.readConfigurationData( + formats.add(ConfigurationFormats.getInstance().getFormats("ini").get(0)); + ConfigurationData data = ConfigurationFormats.getInstance().readConfigurationData( "Test.ini", getClass().getResource("/Test.ini").openStream(), formats); @@ -123,8 +123,8 @@ public class ConfigurationFormatsTest { @org.junit.Test public void testReadConfigurationData2() throws Exception { - List<ConfigurationFormat> formats = ConfigurationFormats.getFormats(); - ConfigurationData data = ConfigurationFormats.readConfigurationData( + List<ConfigurationFormat> formats = ConfigurationFormats.getInstance().getFormats(); + ConfigurationData data = ConfigurationFormats.getInstance().readConfigurationData( getClass().getResource("/Test.ini"), formats.toArray(new ConfigurationFormat[formats.size()])); assertNotNull(data); http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/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 index 3a4ec16..20fbc05 100644 --- 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 @@ -29,7 +29,7 @@ import static org.junit.Assert.*; public class FormatPropertySourceProviderTest extends BaseFormatPropertySourceProvider { public FormatPropertySourceProviderTest() { - super(ConfigurationFormats.getFormats(), "Test.ini", "Test.properties"); + super(ConfigurationFormats.getInstance().getFormats(), "Test.ini", "Test.properties"); } @Test http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/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 8645bd1..aa2e88a 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 @@ -47,7 +47,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(PropertySource.TAMAYA_ORDINAL, String.createValue(ordinal)) // .addSectionProperty("section2", "sectionKey1", "sectionValue21").build(); // } // @@ -67,32 +67,32 @@ public class MappedConfigurationDataPropertySourceTest { // @Test // public void testGet() throws Exception { // MappedConfigurationDataPropertySource ps = new MappedConfigurationDataPropertySource(createConfigurationData("test2")); -// assertEquals("aValue", ps.getChild("a").value()); -// assertNotNull(ps.getChild("section1.sectionKey1").value()); -// assertNotNull(ps.getChild("section2.sectionKey1").value()); -// assertNull(ps.getChild("sectionKey1")); +// assertEquals("aValue", ps.getField("a").createValue()); +// assertNotNull(ps.getField("section1.sectionKey1").createValue()); +// assertNotNull(ps.getField("section2.sectionKey1").createValue()); +// assertNull(ps.getField("sectionKey1")); // ps = new MappedConfigurationDataPropertySource(createConfigurationDataNoDefault("test2")); -// assertEquals("sectionValue11", ps.getChild("section1.sectionKey1").value()); -// assertEquals("sectionValue21", ps.getChild("section2.sectionKey1").value()); -// assertNull(ps.getChild("a")); -// assertNull(ps.getChild("section1")); +// assertEquals("sectionValue11", ps.getField("section1.sectionKey1").createValue()); +// assertEquals("sectionValue21", ps.getField("section2.sectionKey1").createValue()); +// assertNull(ps.getField("a")); +// assertNull(ps.getField("section1")); // } // // @Test // public void testGetProperties() throws Exception { // MappedConfigurationDataPropertySource ps = new MappedConfigurationDataPropertySource(createConfigurationData("test3")); // assertNotNull(ps.getProperties()); -// assertEquals("aValue", ps.getProperties().getChild("a").value()); -// assertNotNull(ps.getProperties().getChild("section1.sectionKey1")); -// assertNotNull(ps.getProperties().getChild("section2.sectionKey1")); -// assertNull(ps.getProperties().getChild("section1.sectionKey2")); -// assertNull(ps.getProperties().getChild("section2.sectionKey2")); -// assertNull(ps.getProperties().getChild("sectionKey1")); -// assertNull(ps.getProperties().getChild("sectionKey2")); +// assertEquals("aValue", ps.getProperties().getField("a").createValue()); +// assertNotNull(ps.getProperties().getField("section1.sectionKey1")); +// assertNotNull(ps.getProperties().getField("section2.sectionKey1")); +// assertNull(ps.getProperties().getField("section1.sectionKey2")); +// assertNull(ps.getProperties().getField("section2.sectionKey2")); +// assertNull(ps.getProperties().getField("sectionKey1")); +// assertNull(ps.getProperties().getField("sectionKey2")); // ps = new MappedConfigurationDataPropertySource(createConfigurationDataNoDefault("test3")); // assertNotNull(ps.getProperties()); -// assertEquals("sectionValue11", ps.getProperties().getChild("section1.sectionKey1").value()); -// assertEquals("sectionValue21", ps.getProperties().getChild("section2.sectionKey1").value()); -// assertNull(ps.getChild("section1")); +// assertEquals("sectionValue11", ps.getProperties().getField("section1.sectionKey1").createValue()); +// assertEquals("sectionValue21", ps.getProperties().getField("section2.sectionKey1").createValue()); +// assertNull(ps.getField("section1")); // } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONDataBuilder.java ---------------------------------------------------------------------- diff --git a/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONDataBuilder.java b/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONDataBuilder.java index 6bd73d7..6c4e065 100644 --- a/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONDataBuilder.java +++ b/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONDataBuilder.java @@ -24,46 +24,51 @@ import javax.json.JsonString; import javax.json.JsonValue; import org.apache.tamaya.ConfigException; +import org.apache.tamaya.spi.ListValue; +import org.apache.tamaya.spi.ObjectValue; import org.apache.tamaya.spi.PropertyValue; +import java.util.Objects; + /** * Visitor implementation to read a JSON asString input source. */ class JSONDataBuilder { - private PropertyValue data = PropertyValue.create(); + private String resource; + private PropertyValue data; private JsonValue root; JSONDataBuilder(String resource, JsonValue root) { - data.setMeta("resource", resource); - data.setMeta("format", "json"); + this.resource = Objects.requireNonNull(resource); this.root = root; } - private void addJsonObject(JsonObject jsonObject, PropertyValue parent, String objectKey){ - PropertyValue dataNode = objectKey==null?parent:parent.getOrCreateChild(objectKey); + private void addJsonObject(JsonObject jsonObject, ObjectValue dataNode){ jsonObject.forEach((key,val) -> { switch(val.getValueType()) { case FALSE: - dataNode.addProperty(key, Boolean.FALSE.toString()); + dataNode.setField(key, Boolean.FALSE.toString()); break; case TRUE: - dataNode.addProperty(key, Boolean.TRUE.toString()); + dataNode.setField(key, Boolean.TRUE.toString()); break; case NUMBER: - dataNode.addProperty(key, val.toString()); + dataNode.setField(key, val.toString()); break; case STRING: - dataNode.addProperty(key, ((JsonString) val).getString()); + dataNode.setField(key, ((JsonString) val).getString()); break; case NULL: - dataNode.addProperty(key, null); + dataNode.setField(key, null); break; case OBJECT: - addJsonObject((JsonObject)val, dataNode, key); + ObjectValue oval = dataNode.setFieldObject(key); + addJsonObject((JsonObject)val, oval); break; case ARRAY: - addArray((JsonArray)val, dataNode, key); + ListValue aval = dataNode.setFieldList(key); + addArray((JsonArray)val, aval); break; default: throw new ConfigException("Internal failure while processing JSON document."); @@ -71,29 +76,30 @@ class JSONDataBuilder { }); } - private void addArray(JsonArray array, PropertyValue parent, String arrayKey) { + private void addArray(JsonArray array, ListValue dataNode) { array.forEach(val -> { - PropertyValue dataNode = parent.createChild(arrayKey, true); switch(val.getValueType()) { case NULL: break; case FALSE: - dataNode.setValue(Boolean.FALSE.toString()); + dataNode.addValue(Boolean.FALSE.toString()); break; case TRUE: - dataNode.setValue(Boolean.TRUE.toString()); + dataNode.addValue(Boolean.TRUE.toString()); break; case NUMBER: - dataNode.setValue(val.toString()); + dataNode.addValue(val.toString()); break; case STRING: - dataNode.setValue(((JsonString) val).getString()); + dataNode.addValue(((JsonString) val).getString()); break; case OBJECT: - addJsonObject((JsonObject)val, dataNode, null); + ObjectValue oval = dataNode.addObject(); + addJsonObject((JsonObject)val, oval); break; case ARRAY: - addArray((JsonArray)val, dataNode, ""); + ListValue aval = dataNode.addList(); + addArray((JsonArray)val, aval); break; default: throw new ConfigException("Internal failure while processing JSON document."); @@ -103,13 +109,17 @@ class JSONDataBuilder { public PropertyValue build() { if (root instanceof JsonObject) { - addJsonObject((JsonObject)root, data, null); + data = PropertyValue.createObject(""); + addJsonObject((JsonObject)root, (ObjectValue) data); } else if (root instanceof JsonArray) { JsonArray array = (JsonArray)root; - addArray(array, data, ""); + data = PropertyValue.createList(""); + addArray(array, (ListValue)data); } else { throw new ConfigException("Unknown JsonType encountered: " + root.getClass().getName()); } + data.setMeta("resource", resource); + data.setMeta("format", "json"); return data; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/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 index 2b3aa06..06209e6 100644 --- 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 @@ -34,7 +34,6 @@ 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; @@ -132,7 +131,7 @@ public class JSONPropertySource implements PropertySource { JsonStructure root = this.readerFactory.createReader(is, Charset.forName("UTF-8")).read(); JSONDataBuilder visitor = new JSONDataBuilder(urlResource.toString(), root); - Map<String, String> values = visitor.build().asMap(); + Map<String, String> values = visitor.build().toMap(); 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())); http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/formats/json/src/test/java/org/apache/tamaya/json/JSONVisitorTest.java ---------------------------------------------------------------------- diff --git a/modules/formats/json/src/test/java/org/apache/tamaya/json/JSONVisitorTest.java b/modules/formats/json/src/test/java/org/apache/tamaya/json/JSONVisitorTest.java index 8d12eff..d3a4dbc 100644 --- a/modules/formats/json/src/test/java/org/apache/tamaya/json/JSONVisitorTest.java +++ b/modules/formats/json/src/test/java/org/apache/tamaya/json/JSONVisitorTest.java @@ -29,6 +29,7 @@ import javax.json.JsonArray; import javax.json.JsonObject; import javax.json.JsonValue; +import org.apache.tamaya.spi.ObjectValue; import org.apache.tamaya.spi.PropertyValue; import org.junit.Test; @@ -37,7 +38,7 @@ public class JSONVisitorTest { @Test public void ensureJSONisParsedProperlyWithDifferentValueTypesFilteringOutEmptyValues() { JsonObject startNode = Json.createObjectBuilder().// - add("key.sub", "value").// + add("key.sub", "createValue").// add("anotherKey", true).// add("notAnotherKey", false).// add("number", 4711).// @@ -49,13 +50,14 @@ public class JSONVisitorTest { PropertyValue data = visitor.build(); assertThat(data).isNotNull(); - assertThat(data.getChildren().size() == 6); - assertEquals(data.getNumChilds(), 6); - assertThat(data.asMap()).containsKeys("key.sub", "anotherKey", "notAnotherKey", "number", "null"); - assertThat(data.asMap()).containsEntry("key.sub", "value"); - assertThat(data.asMap()).containsEntry("null", null); - assertThat(data.asMap()).containsEntry("anotherKey", "true"); - assertThat(data.asMap()).containsEntry("empty", null); + ObjectValue ov = data.toObjectValue(); + assertThat(ov.getFields().size() == 6); + assertEquals(data.getSize(), 6); + assertThat(data.toMap()).containsKeys("key.sub", "anotherKey", "notAnotherKey", "number", "null"); + assertThat(data.toMap()).containsEntry("key.sub", "createValue"); + assertThat(data.toMap()).containsEntry("null", null); + assertThat(data.toMap()).containsEntry("anotherKey", "true"); + assertThat(data.toMap()).doesNotContainEntry("empty", null); } @Test http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/YAMLFormat.java ---------------------------------------------------------------------- diff --git a/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/YAMLFormat.java b/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/YAMLFormat.java index b90bc65..d8b59bd 100644 --- a/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/YAMLFormat.java +++ b/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/YAMLFormat.java @@ -21,6 +21,8 @@ package org.apache.tamaya.yaml; import org.apache.tamaya.ConfigException; import org.apache.tamaya.format.ConfigurationData; import org.apache.tamaya.format.ConfigurationFormat; +import org.apache.tamaya.spi.ListValue; +import org.apache.tamaya.spi.ObjectValue; import org.apache.tamaya.spi.PropertyValue; import org.yaml.snakeyaml.Yaml; @@ -61,14 +63,18 @@ public class YAMLFormat implements ConfigurationFormat { public ConfigurationData readConfiguration(String resource, InputStream inputStream) { try{ Yaml yaml = new Yaml(); - PropertyValue data = PropertyValue.create(); - data.setMeta("resource", resource); - data.setMeta("format", "yaml"); + PropertyValue data; Object config = yaml.load(inputStream); if(config instanceof Map){ - addObject((Map)config, data, null); + data = PropertyValue.createObject(""); + data.setMeta("resource", resource); + data.setMeta("format", "yaml"); + addObject((Map)config, (ObjectValue)data); }else if(config instanceof List){ - addArray((List)config, data, null); + data = PropertyValue.createList(""); + data.setMeta("resource", resource); + data.setMeta("format", "yaml"); + addList((List)config, (ListValue)data); }else { throw new ConfigException("Unknown YamlType encountered: " + config.getClass().getName()); } @@ -83,26 +89,28 @@ public class YAMLFormat implements ConfigurationFormat { } - private void addObject(Map<String,Object> object, PropertyValue parent, String objectKey){ - PropertyValue dataNode = objectKey==null?parent:parent.getOrCreateChild(objectKey); - object.entrySet().forEach(en -> { + private void addObject(Map<String,Object> values, ObjectValue dataNode){ + values.entrySet().forEach(en -> { if (en.getValue() instanceof List) { - addArray((List) en.getValue(), dataNode, en.getKey()); + ListValue list = dataNode.setFieldList(en.getKey()); + addList((List) en.getValue(), list); } else if (en.getValue() instanceof Map) { - addObject((Map) en.getValue(), dataNode, en.getKey()); + ObjectValue object = dataNode.setFieldObject(en.getKey()); + addObject((Map) en.getValue(), object); } else{ - dataNode.createChild(en.getKey(), String.valueOf(en.getValue())); + dataNode.setField(en.getKey(), String.valueOf(en.getValue())); } }); } - private void addArray(List<Object> array, PropertyValue parent, String arrayKey) { - array.forEach(val -> { - PropertyValue dataNode = parent.createChild(arrayKey, true); + private void addList(List<Object> values, ListValue dataNode) { + values.forEach(val -> { if (val instanceof List) { - addArray((List) val, dataNode, ""); + ListValue list = dataNode.addList(); + addList((List) val, list); } else if (val instanceof Map) { - addObject((Map) val, dataNode, null); + ObjectValue ov = dataNode.addObject(); + addObject((Map) val, ov); } else{ dataNode.setValue(String.valueOf(val)); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/YAMLPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/YAMLPropertySource.java b/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/YAMLPropertySource.java index ec168bc..2f94d43 100644 --- a/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/YAMLPropertySource.java +++ b/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/YAMLPropertySource.java @@ -64,11 +64,11 @@ public class YAMLPropertySource implements PropertySource { this.ordinal = defaultOrdinal; // may be overriden by read... ConfigurationData data = format.readConfiguration(urlResource.toString(), resource.openStream()); this.values = new HashMap<>(); - for(Map.Entry<String,String> en:data.getData().get(0).asMap().entrySet()){ + for(Map.Entry<String,String> en:data.getData().get(0).toMap().entrySet()){ this.values.put(en.getKey(), PropertyValue.of(en.getKey(), en.getValue(), getName())); } - if (data.getData().get(0).asMap().containsKey(TAMAYA_ORDINAL)) { - this.ordinal = Integer.parseInt(data.getData().get(0).asMap().get(TAMAYA_ORDINAL)); + if (data.getData().get(0).toMap().containsKey(TAMAYA_ORDINAL)) { + this.ordinal = Integer.parseInt(data.getData().get(0).toMap().get(TAMAYA_ORDINAL)); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/formats/yaml/src/test/java/org/apache/tamaya/yaml/YAMLFormatTest.java ---------------------------------------------------------------------- diff --git a/modules/formats/yaml/src/test/java/org/apache/tamaya/yaml/YAMLFormatTest.java b/modules/formats/yaml/src/test/java/org/apache/tamaya/yaml/YAMLFormatTest.java index b4540ef..52f7149 100644 --- a/modules/formats/yaml/src/test/java/org/apache/tamaya/yaml/YAMLFormatTest.java +++ b/modules/formats/yaml/src/test/java/org/apache/tamaya/yaml/YAMLFormatTest.java @@ -60,7 +60,7 @@ public class YAMLFormatTest { assertTrue(format.accepts(configURL)); ConfigurationData data = format.readConfiguration(configURL.toString(), configURL.openStream()); assertNotNull(data); - for(Map.Entry<String,String> en:data.getData().get(0).asMap().entrySet()) { + for(Map.Entry<String,String> en:data.getData().get(0).toMap().entrySet()) { System.out.println(en.getKey() + " -> " + en.getValue()); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/functions/pom.xml ---------------------------------------------------------------------- diff --git a/modules/functions/pom.xml b/modules/functions/pom.xml index 839b67e..2b34b52 100644 --- a/modules/functions/pom.xml +++ b/modules/functions/pom.xml @@ -37,7 +37,7 @@ under the License. <artifactId>tamaya-api</artifactId> <version>${tamaya-apicore.version}</version> </dependency> - <!-- Test scope only, do not create a code dependency! --> + <!-- Test scope only, do not createObject a code dependency! --> <dependency> <groupId>org.apache.tamaya</groupId> <artifactId>tamaya-core</artifactId> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/functions/src/main/java/org/apache/tamaya/functions/CombinedConfiguration.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/main/java/org/apache/tamaya/functions/CombinedConfiguration.java b/modules/functions/src/main/java/org/apache/tamaya/functions/CombinedConfiguration.java index 72b5b3a..28fed46 100644 --- a/modules/functions/src/main/java/org/apache/tamaya/functions/CombinedConfiguration.java +++ b/modules/functions/src/main/java/org/apache/tamaya/functions/CombinedConfiguration.java @@ -18,8 +18,6 @@ */ package org.apache.tamaya.functions; -import org.apache.tamaya.ConfigOperator; -import org.apache.tamaya.ConfigQuery; import org.apache.tamaya.Configuration; import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.ConfigurationContext; @@ -90,7 +88,7 @@ class CombinedConfiguration implements Configuration{ public <T> T getOrDefault(String key, Class<T> type, T defaultValue) { Objects.requireNonNull(type, "Type must be given."); Objects.requireNonNull(key, "Key must be given."); - Objects.requireNonNull(defaultValue, "Default value must be given."); + Objects.requireNonNull(defaultValue, "Default createValue must be given."); T val = get(key, type); if(val==null){ @@ -127,7 +125,7 @@ class CombinedConfiguration implements Configuration{ public <T> T getOrDefault(String key, TypeLiteral<T> type, T defaultValue) { Objects.requireNonNull(key, "Key must be given."); Objects.requireNonNull(type, "Type must be given."); - Objects.requireNonNull(defaultValue, "Default value must be given."); + Objects.requireNonNull(defaultValue, "Default createValue must be given."); T val = get(key, type); if(val==null){ @@ -146,20 +144,6 @@ class CombinedConfiguration implements Configuration{ } @Override - public Configuration with(ConfigOperator operator) { - Objects.requireNonNull(operator, "Operator must be given."); - - return operator.operate(this); - } - - @Override - public <T> T query(ConfigQuery<T> query) { - Objects.requireNonNull(query, "Query must be given."); - - return query.query(this); - } - - @Override public ConfigurationContext getContext() { // TODO thjink on combining the participating contexts... return configurations.get(0).getContext(); http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/functions/src/main/java/org/apache/tamaya/functions/EnrichedConfiguration.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/main/java/org/apache/tamaya/functions/EnrichedConfiguration.java b/modules/functions/src/main/java/org/apache/tamaya/functions/EnrichedConfiguration.java index a223a45..2b6aac1 100644 --- a/modules/functions/src/main/java/org/apache/tamaya/functions/EnrichedConfiguration.java +++ b/modules/functions/src/main/java/org/apache/tamaya/functions/EnrichedConfiguration.java @@ -77,7 +77,7 @@ class EnrichedConfiguration implements Configuration { @Override public String getOrDefault(String key, String defaultValue) { Objects.requireNonNull(key, "Key must be given."); - Objects.requireNonNull(defaultValue, "Default value must be given."); + Objects.requireNonNull(defaultValue, "Default createValue must be given."); String val = get(key); if (val == null) { @@ -90,7 +90,7 @@ class EnrichedConfiguration implements Configuration { public <T> T getOrDefault(String key, Class<T> type, T defaultValue) { Objects.requireNonNull(key, "Key not given."); Objects.requireNonNull(type, "Class not given."); - Objects.requireNonNull(defaultValue, "Default value not given."); + Objects.requireNonNull(defaultValue, "Default createValue not given."); T val = get(key, type); if (val == null) { @@ -128,7 +128,7 @@ class EnrichedConfiguration implements Configuration { public <T> T getOrDefault(String key, TypeLiteral<T> type, T defaultValue) { Objects.requireNonNull(key, "Key not given."); Objects.requireNonNull(type, "Type not given."); - Objects.requireNonNull(defaultValue, "Default value not given."); + Objects.requireNonNull(defaultValue, "Default createValue not given."); T val = get(key, type); if (val == null) { http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/functions/src/main/java/org/apache/tamaya/functions/FilteredConfiguration.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/main/java/org/apache/tamaya/functions/FilteredConfiguration.java b/modules/functions/src/main/java/org/apache/tamaya/functions/FilteredConfiguration.java index e8acaaa..2f441dc 100644 --- a/modules/functions/src/main/java/org/apache/tamaya/functions/FilteredConfiguration.java +++ b/modules/functions/src/main/java/org/apache/tamaya/functions/FilteredConfiguration.java @@ -18,8 +18,6 @@ */ package org.apache.tamaya.functions; -import org.apache.tamaya.ConfigOperator; -import org.apache.tamaya.ConfigQuery; import org.apache.tamaya.Configuration; import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.ConfigurationContext; @@ -101,16 +99,6 @@ class FilteredConfiguration implements Configuration { } @Override - public Configuration with(ConfigOperator operator) { - return null; - } - - @Override - public <T> T query(ConfigQuery<T> query) { - return query.query(this); - } - - @Override public ConfigurationContext getContext() { return baseConfiguration.getContext(); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/functions/src/main/java/org/apache/tamaya/functions/MappedConfiguration.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/main/java/org/apache/tamaya/functions/MappedConfiguration.java b/modules/functions/src/main/java/org/apache/tamaya/functions/MappedConfiguration.java index dd2547f..49125ed 100644 --- a/modules/functions/src/main/java/org/apache/tamaya/functions/MappedConfiguration.java +++ b/modules/functions/src/main/java/org/apache/tamaya/functions/MappedConfiguration.java @@ -18,8 +18,6 @@ */ package org.apache.tamaya.functions; -import org.apache.tamaya.ConfigOperator; -import org.apache.tamaya.ConfigQuery; import org.apache.tamaya.Configuration; import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.ConfigurationContext; @@ -110,16 +108,6 @@ class MappedConfiguration implements Configuration { } @Override - public Configuration with(ConfigOperator operator) { - return operator.operate(this); - } - - @Override - public <T> T query(ConfigQuery<T> query) { - return query.query(this); - } - - @Override public ConfigurationContext getContext() { return baseConfiguration.getContext(); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/functions/src/main/java/org/apache/tamaya/functions/MappedPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/main/java/org/apache/tamaya/functions/MappedPropertySource.java b/modules/functions/src/main/java/org/apache/tamaya/functions/MappedPropertySource.java index 74c5d09..17dd478 100644 --- a/modules/functions/src/main/java/org/apache/tamaya/functions/MappedPropertySource.java +++ b/modules/functions/src/main/java/org/apache/tamaya/functions/MappedPropertySource.java @@ -90,8 +90,8 @@ class MappedPropertySource implements PropertySource { * </p> * * @param key the property's key, not {@code null}. - * @return the property value map, where {@code map.current(key) == value}, - * including also any metadata. In case a value is {@code null}, + * @return the property createValue map, where {@code map.current(key) == createValue}, + * including also any metadata. In case a createValue is {@code null}, * simply return {@code null}. */ @Override @@ -107,7 +107,7 @@ class MappedPropertySource implements PropertySource { if (mappedKey.equals(newKey)) { String mappedName = getName(); - return property.setKey(newKey) + return PropertyValue.createValue(newKey, property.getValue()) .setMeta("source", mappedName); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/functions/src/main/java/org/apache/tamaya/functions/PropertyMapper.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/main/java/org/apache/tamaya/functions/PropertyMapper.java b/modules/functions/src/main/java/org/apache/tamaya/functions/PropertyMapper.java index dc5bd30..3e7dad4 100644 --- a/modules/functions/src/main/java/org/apache/tamaya/functions/PropertyMapper.java +++ b/modules/functions/src/main/java/org/apache/tamaya/functions/PropertyMapper.java @@ -26,10 +26,10 @@ package org.apache.tamaya.functions; public interface PropertyMapper { /** - * Maps the given key/value to another value. + * Maps the given key/createValue to another createValue. * @param key the key, not null. - * @param value the value, not null. - * @return the new value, not null. + * @param value the createValue, not null. + * @return the new createValue, not null. */ String mapProperty(String key, String value); http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/functions/src/main/java/org/apache/tamaya/functions/PropertyMatcher.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/main/java/org/apache/tamaya/functions/PropertyMatcher.java b/modules/functions/src/main/java/org/apache/tamaya/functions/PropertyMatcher.java index cfb207e..d2ea759 100644 --- a/modules/functions/src/main/java/org/apache/tamaya/functions/PropertyMatcher.java +++ b/modules/functions/src/main/java/org/apache/tamaya/functions/PropertyMatcher.java @@ -29,7 +29,7 @@ public interface PropertyMatcher { * Evaluates this predicate on the given arguments. * * @param key the key, not null - * @param value the value, not null + * @param value the createValue, not null * @return {@code true} if the entry should match the predicate */ boolean test(String key, String value); http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/functions/src/main/java/org/apache/tamaya/functions/PropertySourceFunctions.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/main/java/org/apache/tamaya/functions/PropertySourceFunctions.java b/modules/functions/src/main/java/org/apache/tamaya/functions/PropertySourceFunctions.java index 408726f..c349370 100644 --- a/modules/functions/src/main/java/org/apache/tamaya/functions/PropertySourceFunctions.java +++ b/modules/functions/src/main/java/org/apache/tamaya/functions/PropertySourceFunctions.java @@ -18,9 +18,10 @@ */ package org.apache.tamaya.functions; -import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.Configuration; import org.apache.tamaya.spi.PropertySource; import org.apache.tamaya.spi.PropertyValue; +import org.apache.tamaya.spi.ServiceContextManager; import java.util.*; @@ -360,11 +361,24 @@ public final class PropertySourceFunctions { * {@link org.apache.tamaya.spi.ConfigurationContext} that are assignable to the given type. * * @param expression the regular expression to match the source's name. - * @return the list of all {@link PropertySource} instances matching, never null. + * @return the createList of all {@link PropertySource} instances matching, never null. */ public static Collection<? extends PropertySource> findPropertySourcesByName(String expression) { + return findPropertySourcesByName(ServiceContextManager.getDefaultClassLoader(), expression); + } + + /** + * Find all {@link PropertySource} instances managed by the current + * {@link org.apache.tamaya.spi.ConfigurationContext} that are assignable to the given type. + * + * @param classLoader the target classloader, not null. + * @param expression the regular expression to match the source's name. + * @param classLoader the target classloader, not null. + * @return the createList of all {@link PropertySource} instances matching, never null. + */ + public static Collection<? extends PropertySource> findPropertySourcesByName(ClassLoader classLoader, String expression) { List result = new ArrayList<>(); - for (PropertySource src : ConfigurationProvider.getConfigurationContext().getPropertySources()) { + for (PropertySource src : Configuration.current(classLoader).getContext().getPropertySources()) { if (src.getName().matches(expression)) { result.add(src); } @@ -373,16 +387,30 @@ public final class PropertySourceFunctions { } /** - * Get a list of all {@link PropertySource} instances managed by the current + * Get a createList of all {@link PropertySource} instances managed by the current + * {@link org.apache.tamaya.spi.ConfigurationContext} that are assignable to the given type. + * + * @param <T> the type of the property source instances requested + * @param type target type to filter for property sources. + * @return the createList of all {@link PropertySource} instances matching, never null. + * @see ServiceContextManager#getDefaultClassLoader() + */ + public static <T> Collection<T> getPropertySources(Class<T> type) { + return getPropertySources(ServiceContextManager.getDefaultClassLoader(), type); + } + + /** + * Get a createList of all {@link PropertySource} instances managed by the current * {@link org.apache.tamaya.spi.ConfigurationContext} that are assignable to the given type. * * @param <T> the type of the property source instances requested * @param type target type to filter for property sources. - * @return the list of all {@link PropertySource} instances matching, never null. + * @param classLoader the target classloader, not null. + * @return the createList of all {@link PropertySource} instances matching, never null. */ - public static <T> Collection<T> getPropertySources(Class<T> type) { + public static <T> Collection<T> getPropertySources(ClassLoader classLoader, Class<T> type) { List<T> result = new ArrayList<>(); - for (PropertySource src : ConfigurationProvider.getConfigurationContext().getPropertySources()) { + for (PropertySource src : Configuration.current().getContext().getPropertySources()) { if (type.isAssignableFrom(src.getClass())) { result.add((T) src); } @@ -391,15 +419,28 @@ public final class PropertySourceFunctions { } /** - * Get a list of all {@link PropertySource} instances managed by the current + * Get a createList of all {@link PropertySource} instances managed by the current * {@link org.apache.tamaya.spi.ConfigurationContext} that are assignable to the given type. * * @param <T> the type of the property source instances requested - * @param type target type to filter for property sources. - * @return the list of all {@link PropertySource} instances matching, never null. + * @param type target type to filter for property sources. + * @return the createList of all {@link PropertySource} instances matching, never null. */ public static <T> T getPropertySource(Class<T> type) { - for (PropertySource src : ConfigurationProvider.getConfigurationContext().getPropertySources()) { + return getPropertySource(ServiceContextManager.getDefaultClassLoader(), type); + } + + /** + * Get a createList of all {@link PropertySource} instances managed by the current + * {@link org.apache.tamaya.spi.ConfigurationContext} that are assignable to the given type. + * + * @param <T> the type of the property source instances requested + * @param type target type to filter for property sources. + * @param classLoader the target classloader, not null. + * @return the createList of all {@link PropertySource} instances matching, never null. + */ + public static <T> T getPropertySource(ClassLoader classLoader, Class<T> type) { + for (PropertySource src : Configuration.current(classLoader).getContext().getPropertySources()) { if (type.isAssignableFrom(src.getClass())) { return (T) src; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/functions/src/test/java/org/apache/tamaya/functions/CombinedConfigurationTest.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/test/java/org/apache/tamaya/functions/CombinedConfigurationTest.java b/modules/functions/src/test/java/org/apache/tamaya/functions/CombinedConfigurationTest.java index a8a2094..a850493 100644 --- a/modules/functions/src/test/java/org/apache/tamaya/functions/CombinedConfigurationTest.java +++ b/modules/functions/src/test/java/org/apache/tamaya/functions/CombinedConfigurationTest.java @@ -210,7 +210,7 @@ public class CombinedConfigurationTest { cc.getOrDefault("a", TypeLiteral.of(Integer.class), null); } }).isInstanceOf(NullPointerException.class) - .hasMessage("Default value must be given."); + .hasMessage("Default createValue must be given."); } @Test @@ -284,7 +284,7 @@ public class CombinedConfigurationTest { cc.getOrDefault("a", Integer.class, null); } }).isInstanceOf(NullPointerException.class) - .hasMessage("Default value must be given."); + .hasMessage("Default createValue must be given."); } @Test http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/functions/src/test/java/org/apache/tamaya/functions/EnrichedConfigurationTest.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/test/java/org/apache/tamaya/functions/EnrichedConfigurationTest.java b/modules/functions/src/test/java/org/apache/tamaya/functions/EnrichedConfigurationTest.java index 2ab9d13..6c9a3ce 100644 --- a/modules/functions/src/test/java/org/apache/tamaya/functions/EnrichedConfigurationTest.java +++ b/modules/functions/src/test/java/org/apache/tamaya/functions/EnrichedConfigurationTest.java @@ -204,7 +204,7 @@ public class EnrichedConfigurationTest { sut.getOrDefault("v", null); } }).isInstanceOf(NullPointerException.class) - .hasMessage("Default value must be given."); + .hasMessage("Default createValue must be given."); } @Test @@ -293,7 +293,7 @@ public class EnrichedConfigurationTest { sut.getOrDefault("b", String.class, null); } }).isInstanceOf(NullPointerException.class) - .hasMessage("Default value not given."); + .hasMessage("Default createValue not given."); } @Test @@ -321,7 +321,7 @@ public class EnrichedConfigurationTest { sut.getOrDefault("b", String.class, null); } }).isInstanceOf(NullPointerException.class) - .hasMessage("Default value not given."); + .hasMessage("Default createValue not given."); } @Test @@ -442,7 +442,7 @@ public class EnrichedConfigurationTest { sut.getOrDefault("b", TypeLiteral.<String>of(String.class), null); } }).isInstanceOf(NullPointerException.class) - .hasMessage("Default value not given."); + .hasMessage("Default createValue not given."); } @@ -472,7 +472,7 @@ public class EnrichedConfigurationTest { sut.getOrDefault("b", TypeLiteral.<String>of(String.class), null); } }).isInstanceOf(NullPointerException.class) - .hasMessage("Default value not given."); + .hasMessage("Default createValue not given."); } @Test http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/functions/src/test/java/org/apache/tamaya/functions/InMemoryConfiguration.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/test/java/org/apache/tamaya/functions/InMemoryConfiguration.java b/modules/functions/src/test/java/org/apache/tamaya/functions/InMemoryConfiguration.java index deb544c..8b08e27 100644 --- a/modules/functions/src/test/java/org/apache/tamaya/functions/InMemoryConfiguration.java +++ b/modules/functions/src/test/java/org/apache/tamaya/functions/InMemoryConfiguration.java @@ -27,8 +27,8 @@ class InMemoryConfiguration extends DefaultConfiguration { } // private Map<String, String> entries = new TreeMap<>(); -// public InMemoryConfiguration addEntry(String key, String value) { -// entries.put(key, value); +// public InMemoryConfiguration addEntry(String key, String createValue) { +// entries.put(key, createValue); // // return this; // } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIAwareServiceContext.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIAwareServiceContext.java b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIAwareServiceContext.java index 2f54867..9a4d831 100644 --- a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIAwareServiceContext.java +++ b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIAwareServiceContext.java @@ -19,6 +19,7 @@ package org.apache.tamaya.cdi; import org.apache.tamaya.ConfigException; +import org.apache.tamaya.spi.ClassloaderAware; import org.apache.tamaya.spi.ServiceContext; import javax.annotation.Priority; @@ -30,6 +31,7 @@ import java.net.URL; import java.text.MessageFormat; import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Supplier; import java.util.logging.Level; import java.util.logging.Logger; @@ -48,7 +50,7 @@ import java.util.logging.Logger; * <p>This class uses an ordinal of {@code 10}, so it overrides any default {@link ServiceContext} implementations * provided with the Tamaya core modules.</p> */ -public class CDIAwareServiceContext implements ServiceContext { +public class CDIAwareServiceContext implements ServiceContext, ClassloaderAware { private static final Logger LOG = Logger.getLogger(CDIAwareServiceContext.class.getName()); /** @@ -60,16 +62,19 @@ public class CDIAwareServiceContext implements ServiceContext { @Override - public <T> T getService(Class<T> serviceType) { + public <T> T getService(Class<T> serviceType, Supplier<T> supplier) { Object cached = singletons.get(serviceType); if (cached == null) { - Collection<T> services = getServices(serviceType); + Collection<T> services = getServices(serviceType, null); if (services.isEmpty()) { cached = null; } else { cached = getServiceWithHighestPriority(services, serviceType); } + if(cached==null && supplier!=null){ + cached = supplier.get(); + } if (cached != null) { singletons.put(serviceType, cached); } @@ -78,16 +83,19 @@ public class CDIAwareServiceContext implements ServiceContext { } @Override - public <T> T create(Class<T> serviceType) { + public <T> T create(Class<T> serviceType, Supplier<T> supplier) { T serv = getService(serviceType); if(serv!=null){ try { return (T)serv.getClass().newInstance(); } catch (Exception e) { Logger.getLogger(getClass().getName()) - .log(Level.SEVERE, "Failed to create new instance of: " +serviceType.getName(), e); + .log(Level.SEVERE, "Failed to createObject new instance of: " +serviceType.getName(), e); } } + if(supplier!=null){ + return supplier.get(); + } return null; } @@ -99,16 +107,20 @@ public class CDIAwareServiceContext implements ServiceContext { * @return the items found, never {@code null}. */ @Override - public <T> List<T> getServices(final Class<T> serviceType) { + public <T> List<T> getServices(final Class<T> serviceType, Supplier<List<T>> supplier) { List<T> found = defaultServiceContext.getServices(serviceType); try { BeanManager beanManager = TamayaCDIAccessor.getBeanManager(); Instance<T> cdiInstances = null; if(beanManager!=null) { - Set<Bean<?>> instanceBeans = beanManager.getBeans(Instance.class); - Bean<?> bean = instanceBeans.iterator().next(); - cdiInstances = (Instance<T>) beanManager.getReference(bean, Instance.class, - beanManager.createCreationalContext(bean)); + try { + Set<Bean<?>> instanceBeans = beanManager.getBeans(Instance.class); + Bean<?> bean = instanceBeans.iterator().next(); + cdiInstances = (Instance<T>) beanManager.getReference(bean, Instance.class, + beanManager.createCreationalContext(bean)); + }catch(Exception e){ + LOG.log(Level.WARNING, "Failed to access BeanManager", e); + } } if(cdiInstances!=null){ for(T t:cdiInstances.select(serviceType)){ @@ -118,6 +130,9 @@ public class CDIAwareServiceContext implements ServiceContext { }catch(Exception e){ LOG.log(Level.SEVERE, "Failed to access BeanManager.", e); } + if(found.isEmpty() && supplier!=null){ + return supplier.get(); + } return found; } @@ -131,8 +146,18 @@ public class CDIAwareServiceContext implements ServiceContext { return defaultServiceContext.getResource(resource); } + @Override + public <T> T register(Class<T> type, T instance, boolean force) { + return defaultServiceContext.register(type, instance, force); + } + + @Override + public <T> List<T> register(Class<T> type, List<T> instances, boolean force) { + return defaultServiceContext.register(type, instances, force); + } + /** - * Checks the given instance for a @Priority annotation. If present the annotation's value s evaluated. If no such + * Checks the given instance for a @Priority annotation. If present the annotation's createValue s evaluated. If no such * annotation is present, a default priority is returned (1); * @param o the instance, not null. * @return a priority, by default 1. @@ -156,7 +181,7 @@ public class CDIAwareServiceContext implements ServiceContext { */ private <T> T getServiceWithHighestPriority(Collection<T> services, Class<T> serviceType) { - // we do not need the priority stuff if the list contains only one element + // we do not need the priority stuff if the createList contains only one element if (services.size() == 1) { return services.iterator().next(); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/ConfigurationProducer.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/ConfigurationProducer.java b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/ConfigurationProducer.java index 57d6e7e..9944678 100644 --- a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/ConfigurationProducer.java +++ b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/ConfigurationProducer.java @@ -92,7 +92,7 @@ public class ConfigurationProducer { } } if(textValue==null) { - LOGGER.info("Using default value: '" + defaultTextValue + "' for IP: " + injectionPoint ); + LOGGER.info("Using default createValue: '" + defaultTextValue + "' for IP: " + injectionPoint ); textValue = defaultTextValue; } ConversionContext conversionContext = createConversionContext(keyFound, keys, injectionPoint); @@ -166,12 +166,12 @@ public class ConfigurationProducer { try { value = converter.convert(textValue); if (value != null) { - LOGGER.log(Level.INFO, "Parsed value from '" + textValue + "' into " + + LOGGER.log(Level.INFO, "Parsed createValue from '" + textValue + "' into " + injectionPoint); break; } } catch (Exception e) { - LOGGER.log(Level.INFO, "Failed to convert value '" + textValue + "' for " + + LOGGER.log(Level.INFO, "Failed to convert createValue '" + textValue + "' for " + injectionPoint, e); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36c32fcf/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/DefaultDynamicValue.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/DefaultDynamicValue.java b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/DefaultDynamicValue.java index b0dc0db..5f31715 100644 --- a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/DefaultDynamicValue.java +++ b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/DefaultDynamicValue.java @@ -36,10 +36,10 @@ import java.lang.reflect.Type; import java.util.*; /** - * A accessor for a single configured value. This can be used to support values that may change during runtime, + * A accessor for a single configured createValue. This can be used to support values that may change during runtime, * reconfigured or final. Hereby external code (could be Tamaya configuration listeners or client code), can setCurrent a - * new value. Depending on the {@link UpdatePolicy} the new value is immediately active or it requires an active commit - * by client code. Similarly an instance also can ignore all later changes to the value. + * new createValue. Depending on the {@link UpdatePolicy} the new createValue is immediately active or it requires an active commit + * by client code. Similarly an instance also can ignore all later changes to the createValue. * <h3>Implementation Details</h3> * This class is * <ul> @@ -47,17 +47,11 @@ import java.util.*; * <li>Thread safe</li> * </ul> * - * @param <T> The type of the value. + * @param <T> The type of the createValue. */ final class DefaultDynamicValue<T> extends BaseDynamicValue<T> { /** - * Back reference to the base configuration instance. This reference is used reevalaute the given property and - * compare the result with the previous value after a configuration change was triggered. - */ - private final Configuration configuration; - - /** * The property converter to be applied, may be null. In the ladder case targetType is not null. */ private final PropertyConverter<T> customConverter; @@ -78,8 +72,7 @@ final class DefaultDynamicValue<T> extends BaseDynamicValue<T> { private DefaultDynamicValue(Object owner, String propertyName, Configuration configuration, TypeLiteral<T> targetType, PropertyConverter<T> customConverter, List<String> keys, LoadPolicy loadPolicy, UpdatePolicy updatePolicy) { - super(owner, propertyName, targetType, keys); - this.configuration = Objects.requireNonNull(configuration); + super(owner, propertyName, targetType, keys, configuration); this.customConverter = customConverter; this.loadPolicy = Objects.requireNonNull(loadPolicy); setUpdatePolicy(updatePolicy); @@ -176,13 +169,6 @@ final class DefaultDynamicValue<T> extends BaseDynamicValue<T> { loadPolicy, updatePolicy); } - - @Override - protected Configuration getConfiguration() { - return configuration; - } - - @Override protected PropertyConverter<T> getCustomConverter() { return customConverter; @@ -191,11 +177,11 @@ final class DefaultDynamicValue<T> extends BaseDynamicValue<T> { @Override public String toString() { return "DefaultDynamicValue{" + - "configuration=" + configuration + + "configuration=" + getConfiguration() + ", customConverter=" + customConverter + ", loadPolicy=" + loadPolicy + - ", value=" + value + - ", newValue=" + newValue + + ", createValue=" + value + + ", createValue=" + newValue + ", defaultValue=" + getDefaultValue() + ", discarded=" + discarded + ", keys=" + getKeys() +
