http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONVisitor.java ---------------------------------------------------------------------- diff --git a/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONVisitor.java b/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONVisitor.java index 92c81fe..07208d9 100644 --- a/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONVisitor.java +++ b/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONVisitor.java @@ -18,10 +18,7 @@ */ package org.apache.tamaya.json; -import java.util.ArrayDeque; -import java.util.Deque; -import java.util.Iterator; -import java.util.Map; +import java.util.*; import javax.json.JsonArray; import javax.json.JsonObject; @@ -29,7 +26,6 @@ import javax.json.JsonString; import javax.json.JsonStructure; import javax.json.JsonValue; -import org.apache.tamaya.ConfigException; /** * Visitor implementation to read a JSON formatted input source. @@ -64,7 +60,7 @@ class JSONVisitor { case NUMBER: value = jsonValue.toString(); break; case STRING: value = ((JsonString) jsonValue).getString(); break; default: - throw new ConfigException("Internal failure while processing JSON document."); + throw new IllegalStateException("Internal failure while processing JSON document."); } targetStore.put(key, value); @@ -73,9 +69,11 @@ class JSONVisitor { JsonObject node = (JsonObject) current.getValue(); stack.push(new VisitingContext(node, key)); } else if (current.getValue() instanceof JsonArray) { - throw new ConfigException("Arrays are not supported at the moment."); + String key = stack.peek().getNSPrefix() + current.getKey(); + JsonArray array = (JsonArray) current.getValue(); + stack.push(new VisitingContext(array, key)); } else { - throw new ConfigException("Internal failure while processing JSON document."); + throw new IllegalStateException("Internal failure while processing JSON document."); } goOn = stack.peek().hasNext(); @@ -94,6 +92,7 @@ class JSONVisitor { private static class VisitingContext { private final String namespace; private final JsonObject node; + private final JsonArray array; private final Iterator<Map.Entry<String, JsonValue>> elements; public VisitingContext(JsonObject node) { @@ -103,9 +102,45 @@ class JSONVisitor { public VisitingContext(JsonObject rootNode, String currentNamespace) { namespace = currentNamespace; node = rootNode; + array = null; elements = node.entrySet().iterator(); } + public VisitingContext(JsonArray array, String currentNamespace) { + namespace = currentNamespace; + this.array = array; + this.node = null; + Map<String,JsonValue> arrayMap = new HashMap<>(); + arrayMap.put("[array]", formatArray(array.iterator())); + elements = arrayMap.entrySet().iterator(); + } + + private JsonValue formatArray(Iterator<JsonValue> iterator) { + StringBuilder b = new StringBuilder(); + iterator.forEachRemaining(r -> {b.append(r.toString().replace(",", "\\,")).append(',');}); + if(b.length()>0){ + b.setLength(b.length()-1); + } + String elemsAsString = b.toString(); + return new JsonString(){ + + @Override + public ValueType getValueType() { + return ValueType.STRING; + } + + @Override + public String getString() { + return elemsAsString; + } + + @Override + public CharSequence getChars() { + return elemsAsString; + } + }; + } + public Map.Entry<String, JsonValue> nextElement() { return elements.next(); } @@ -116,6 +151,9 @@ class JSONVisitor { } public String getNSPrefix() { + if(array!=null){ + return namespace; + } return namespace.isEmpty() ? namespace : namespace + "."; } }
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/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 209b438..73e20ed 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 @@ -26,7 +26,6 @@ import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonValue; -import org.apache.tamaya.ConfigException; import org.junit.Test; public class JSONVisitorTest { @@ -67,7 +66,7 @@ public class JSONVisitorTest { assertThat(targetStore).isEmpty(); } - @Test(expected = ConfigException.class) + @Test public void arraysAreNotSupported() { JsonObject startNode = Json.createObjectBuilder().// add("arrayKey", Json.createArrayBuilder().build()).// http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/formats/json/src/test/java/org/apache/tamaya/yaml/CommonJSONTestCaseCollection.java ---------------------------------------------------------------------- diff --git a/modules/formats/json/src/test/java/org/apache/tamaya/yaml/CommonJSONTestCaseCollection.java b/modules/formats/json/src/test/java/org/apache/tamaya/yaml/CommonJSONTestCaseCollection.java index e1f94cf..9636471 100644 --- a/modules/formats/json/src/test/java/org/apache/tamaya/yaml/CommonJSONTestCaseCollection.java +++ b/modules/formats/json/src/test/java/org/apache/tamaya/yaml/CommonJSONTestCaseCollection.java @@ -27,20 +27,20 @@ import static org.junit.Assert.assertTrue; import java.io.IOException; import java.net.URL; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; -import org.apache.tamaya.spisupport.PropertySourceComparator; +import org.apache.tamaya.base.configsource.ConfigSourceComparator; import org.hamcrest.CoreMatchers; import org.hamcrest.Matchers; import org.junit.Test; +import javax.config.spi.ConfigSource; + /** * Class with a collection of common test cases each JSON processing * class must be able to pass. */ public abstract class CommonJSONTestCaseCollection { - abstract PropertySource getPropertiesFrom(URL source) throws Exception; + abstract ConfigSource getPropertiesFrom(URL source) throws Exception; @Test public void canReadNonLatinCharacters() throws Exception { @@ -49,12 +49,12 @@ public abstract class CommonJSONTestCaseCollection { assertThat(configURL, Matchers.notNullValue()); - PropertySource propertySource = getPropertiesFrom(configURL); + ConfigSource propertySource = getPropertiesFrom(configURL); - assertThat(propertySource.get("name"), Matchers.notNullValue()); - assertThat(propertySource.get("name").getValue(), equalTo("\u041e\u043b\u0438\u0432\u0435\u0440")); - assertThat(propertySource.get("\u0444\u0430\u043c\u0438\u043b\u0438\u044f"), Matchers.notNullValue()); - assertThat(propertySource.get("\u0444\u0430\u043c\u0438\u043b\u0438\u044f").getValue(), Matchers.equalTo("Fischer")); + assertThat(propertySource.getValue("name"), Matchers.notNullValue()); + assertThat(propertySource.getValue("name"), equalTo("\u041e\u043b\u0438\u0432\u0435\u0440")); + assertThat(propertySource.getValue("\u0444\u0430\u043c\u0438\u043b\u0438\u044f"), Matchers.notNullValue()); + assertThat(propertySource.getValue("\u0444\u0430\u043c\u0438\u043b\u0438\u044f"), Matchers.equalTo("Fischer")); } @Test @@ -64,11 +64,11 @@ public abstract class CommonJSONTestCaseCollection { assertThat(configURL, Matchers.notNullValue()); - PropertySource propertySource = getPropertiesFrom(configURL); + ConfigSource propertySource = getPropertiesFrom(configURL); - assertThat(propertySource.get("onamae"), Matchers.notNullValue()); + assertThat(propertySource.getValue("onamae"), Matchers.notNullValue()); // éå± = Tamaya - assertThat(propertySource.get("onamae").getValue(), equalTo("\u970a\u5c4b")); + assertThat(propertySource.getValue("onamae"), equalTo("\u970a\u5c4b")); } @Test @@ -78,20 +78,20 @@ public abstract class CommonJSONTestCaseCollection { assertThat(configURL, CoreMatchers.notNullValue()); - PropertySource properties = getPropertiesFrom(configURL); + ConfigSource properties = getPropertiesFrom(configURL); assertTrue(properties.getProperties().keySet().size()>=5); - PropertyValue keyB = properties.get("b"); - PropertyValue keyDO = properties.get("d.o"); - PropertyValue keyDP = properties.get("d.p"); + String keyB = properties.getValue("b"); + String keyDO = properties.getValue("d.o"); + String keyDP = properties.getValue("d.p"); assertThat(keyB, notNullValue()); - assertThat(keyB.getValue(), equalTo("B")); + assertThat(keyB, equalTo("B")); assertThat(keyDO, notNullValue()); - assertThat(keyDO.getValue(), equalTo("O")); + assertThat(keyDO, equalTo("O")); assertThat(keyDP, Matchers.notNullValue()); - assertThat(keyDP.getValue(), is("P")); + assertThat(keyDP, is("P")); } @Test @@ -102,28 +102,28 @@ public abstract class CommonJSONTestCaseCollection { assertThat(configURL, CoreMatchers.notNullValue()); - PropertySource properties = getPropertiesFrom(configURL); + ConfigSource properties = getPropertiesFrom(configURL); assertTrue(properties.getProperties().keySet().size()>=4); - PropertyValue keyA = properties.get("a"); - PropertyValue keyDO = properties.get("b.o"); - PropertyValue keyDP = properties.get("b.p"); - PropertyValue keyC = properties.get("c"); + String keyA = properties.getValue("a"); + String keyDO = properties.getValue("b.o"); + String keyDP = properties.getValue("b.p"); + String keyC = properties.getValue("c"); assertThat(keyA, notNullValue()); - assertThat(keyA.getValue(), is("A")); + assertThat(keyA, is("A")); assertThat(keyC, notNullValue()); - assertThat(keyC.getValue(), equalTo("C")); + assertThat(keyC, equalTo("C")); assertThat(keyDO, notNullValue()); - assertThat(keyDO.getValue(), equalTo("O")); + assertThat(keyDO, equalTo("O")); assertThat(keyDP, notNullValue()); - assertThat(keyDP.getValue(), is("P")); + assertThat(keyDP, is("P")); } - @Test(expected = IOException.class) - public void canHandleIllegalJSONFileWhichContainsAnArray() throws Exception { - URL configURL = CommonJSONTestCaseCollection.class.getResource("/configs/invalid/with-array.json"); + @Test + public void canHandleJSONFileWhichContainsAnArray() throws Exception { + URL configURL = CommonJSONTestCaseCollection.class.getResource("/configs/valid/with-array.json"); assertThat(configURL, CoreMatchers.notNullValue()); @@ -154,9 +154,9 @@ public abstract class CommonJSONTestCaseCollection { assertThat(configURL, CoreMatchers.notNullValue()); - PropertySource properties = getPropertiesFrom(configURL); + ConfigSource properties = getPropertiesFrom(configURL); - assertThat(PropertySourceComparator.getOrdinal(properties), is(16784)); + assertThat(ConfigSourceComparator.getOrdinal(properties), is(16784)); } @Test @@ -165,20 +165,20 @@ public abstract class CommonJSONTestCaseCollection { assertThat(configURL, CoreMatchers.notNullValue()); - PropertySource properties = getPropertiesFrom(configURL); + ConfigSource properties = getPropertiesFrom(configURL); assertTrue(properties.getProperties().keySet().size()>=3); - PropertyValue keyA = properties.get("a"); - PropertyValue keyB = properties.get("b"); - PropertyValue keyC = properties.get("c"); + String keyA = properties.getValue("a"); + String keyB = properties.getValue("b"); + String keyC = properties.getValue("c"); assertThat(keyA, notNullValue()); - assertThat(keyA.getValue(), equalTo("A")); + assertThat(keyA, equalTo("A")); assertThat(keyB, notNullValue()); - assertThat(keyB.getValue(), is("B")); + assertThat(keyB, is("B")); assertThat(keyC, notNullValue()); - assertThat(keyC.getValue(), is("C")); + assertThat(keyC, is("C")); } @Test(expected = IOException.class) @@ -187,7 +187,7 @@ public abstract class CommonJSONTestCaseCollection { assertThat(configURL, CoreMatchers.notNullValue()); - PropertySource properties = getPropertiesFrom(configURL); + ConfigSource properties = getPropertiesFrom(configURL); properties.getProperties(); } @@ -198,7 +198,7 @@ public abstract class CommonJSONTestCaseCollection { assertThat(configURL, CoreMatchers.notNullValue()); - PropertySource properties = getPropertiesFrom(configURL); + ConfigSource properties = getPropertiesFrom(configURL); assertTrue(properties.getProperties().keySet().size()>=0); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONFormatTest.java ---------------------------------------------------------------------- diff --git a/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONFormatTest.java b/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONFormatTest.java index 216573e..ae1e6d2 100644 --- a/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONFormatTest.java +++ b/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONFormatTest.java @@ -20,11 +20,11 @@ package org.apache.tamaya.yaml; import org.apache.tamaya.format.ConfigurationData; -import org.apache.tamaya.format.MappedConfigurationDataPropertySource; +import org.apache.tamaya.format.MappedConfigurationDataConfigSource; import org.apache.tamaya.json.JSONFormat; -import org.apache.tamaya.spi.PropertySource; import org.junit.Test; +import javax.config.spi.ConfigSource; import java.io.InputStream; import java.net.URL; @@ -67,10 +67,10 @@ public class JSONFormatTest extends CommonJSONTestCaseCollection { } @Override - PropertySource getPropertiesFrom(URL source) throws Exception { + ConfigSource getPropertiesFrom(URL source) throws Exception { try (InputStream is = source.openStream()) { ConfigurationData data = format.readConfiguration(source.toString(), is); - return new MappedConfigurationDataPropertySource(data); + return new MappedConfigurationDataConfigSource(data); } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONPropertySourceTest.java ---------------------------------------------------------------------- diff --git a/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONPropertySourceTest.java b/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONPropertySourceTest.java index 0555bb8..3e9126a 100644 --- a/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONPropertySourceTest.java +++ b/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONPropertySourceTest.java @@ -24,11 +24,12 @@ import static org.junit.Assert.assertEquals; import java.io.IOException; import java.net.URL; -import org.apache.tamaya.json.JSONPropertySource; -import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.json.JSONConfigSource; import org.hamcrest.CoreMatchers; import org.junit.Test; +import javax.config.spi.ConfigSource; + public class JSONPropertySourceTest extends CommonJSONTestCaseCollection { @Test @@ -37,7 +38,7 @@ public class JSONPropertySourceTest extends CommonJSONTestCaseCollection { assertThat(configURL, CoreMatchers.notNullValue()); - JSONPropertySource source = new JSONPropertySource(configURL, 4); + JSONConfigSource source = new JSONConfigSource(configURL, 4); assertEquals(source.getOrdinal(), 16784); } @@ -47,11 +48,11 @@ public class JSONPropertySourceTest extends CommonJSONTestCaseCollection { assertThat(configURL, CoreMatchers.notNullValue()); - new JSONPropertySource(configURL); + new JSONConfigSource(configURL); } @Override - PropertySource getPropertiesFrom(URL source) throws Exception { - return new JSONPropertySource(source); + ConfigSource getPropertiesFrom(URL source) throws Exception { + return new JSONConfigSource(source); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/formats/json/src/test/resources/configs/invalid/with-array.json ---------------------------------------------------------------------- diff --git a/modules/formats/json/src/test/resources/configs/invalid/with-array.json b/modules/formats/json/src/test/resources/configs/invalid/with-array.json deleted file mode 100644 index e623e49..0000000 --- a/modules/formats/json/src/test/resources/configs/invalid/with-array.json +++ /dev/null @@ -1,27 +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. -*/ -{ - "a" : "A", - "b" : { - "c" : "C", - "d" : [ - "1", "2" - ] - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/formats/json/src/test/resources/configs/valid/with-array.json ---------------------------------------------------------------------- diff --git a/modules/formats/json/src/test/resources/configs/valid/with-array.json b/modules/formats/json/src/test/resources/configs/valid/with-array.json new file mode 100644 index 0000000..e623e49 --- /dev/null +++ b/modules/formats/json/src/test/resources/configs/valid/with-array.json @@ -0,0 +1,27 @@ +/* +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. +*/ +{ + "a" : "A", + "b" : { + "c" : "C", + "d" : [ + "1", "2" + ] + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/formats/json/src/test/resources/configs/valid/with-explicit-priority.json ---------------------------------------------------------------------- diff --git a/modules/formats/json/src/test/resources/configs/valid/with-explicit-priority.json b/modules/formats/json/src/test/resources/configs/valid/with-explicit-priority.json index ed7acc2..fe9b61b 100644 --- a/modules/formats/json/src/test/resources/configs/valid/with-explicit-priority.json +++ b/modules/formats/json/src/test/resources/configs/valid/with-explicit-priority.json @@ -20,6 +20,6 @@ under the License. /* some useful comment here */ - "tamaya.ordinal" : 16784, + "config_ordinal" : 16784, "a" : "A" // another comment } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/injection/cdi/pom.xml ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/pom.xml b/modules/injection/cdi/pom.xml index e1e59a5..f033574 100644 --- a/modules/injection/cdi/pom.xml +++ b/modules/injection/cdi/pom.xml @@ -100,13 +100,7 @@ under the License. </dependency> <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-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/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 20c3bbd..ffb3236 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 @@ -18,7 +18,6 @@ */ package org.apache.tamaya.cdi; -import org.apache.tamaya.ConfigException; import org.apache.tamaya.spi.ServiceContext; import javax.annotation.Priority; @@ -57,13 +56,17 @@ public class CDIAwareServiceContext implements ServiceContext { private ServiceContext defaultServiceContext = new ServiceLoaderServiceContext(); - @Override public <T> T getService(Class<T> serviceType) { + return getService(serviceType, ServiceContext.defaultClassLoader()); + } + + @Override + public <T> T getService(Class<T> serviceType, ClassLoader classLoader) { Object cached = singletons.get(serviceType); if (cached == null) { - Collection<T> services = getServices(serviceType); + Collection<T> services = getServices(serviceType, classLoader); if (services.isEmpty()) { cached = null; } else { @@ -78,7 +81,12 @@ public class CDIAwareServiceContext implements ServiceContext { @Override public <T> T create(Class<T> serviceType) { - T serv = getService(serviceType); + return create(serviceType, ServiceContext.defaultClassLoader()); + } + + @Override + public <T> T create(Class<T> serviceType, ClassLoader classLoader) { + T serv = getService(serviceType, classLoader); if(serv!=null){ try { return (T)serv.getClass().newInstance(); @@ -99,7 +107,19 @@ public class CDIAwareServiceContext implements ServiceContext { */ @Override public <T> List<T> getServices(final Class<T> serviceType) { - List<T> found = defaultServiceContext.getServices(serviceType); + return getServices(serviceType, ServiceContext.defaultClassLoader()); + } + + /** + * Loads and registers services. + * + * @param <T> the concrete type. + * @param serviceType The service type. + * @return the items found, never {@code null}. + */ + @Override + public <T> List<T> getServices(final Class<T> serviceType, ClassLoader classLoader) { + List<T> found = defaultServiceContext.getServices(serviceType, classLoader); BeanManager beanManager = TamayaCDIAccessor.getBeanManager(); Instance<T> cdiInstances = null; if(beanManager!=null){ @@ -159,7 +179,7 @@ public class CDIAwareServiceContext implements ServiceContext { * * @return the service with the highest {@link Priority#value()} * - * @throws ConfigException if there are multiple service implementations with the maximum priority + * @throws IllegalStateException if there are multiple service implementations with the maximum priority */ private <T> T getServiceWithHighestPriority(Collection<T> services, Class<T> serviceType) { @@ -184,7 +204,7 @@ public class CDIAwareServiceContext implements ServiceContext { } if (highestPriorityServiceCount > 1) { - throw new ConfigException(MessageFormat.format("Found {0} implementations for Service {1} with Priority {2}: {3}", + throw new IllegalStateException(MessageFormat.format("Found {0} implementations for Service {1} with Priority {2}: {3}", highestPriorityServiceCount, serviceType.getName(), highestPriority, http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIConfiguredField.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIConfiguredField.java b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIConfiguredField.java index 3331d9a..5ed58c5 100644 --- a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIConfiguredField.java +++ b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIConfiguredField.java @@ -16,9 +16,9 @@ */ package org.apache.tamaya.cdi; -import org.apache.tamaya.Configuration; import org.apache.tamaya.inject.spi.ConfiguredField; +import javax.config.Config; import javax.enterprise.inject.spi.InjectionPoint; import java.lang.reflect.Field; import java.util.ArrayList; @@ -66,7 +66,7 @@ class CDIConfiguredField implements ConfiguredField{ } @Override - public void configure(Object instance, Configuration config) { + public void configure(Object instance, Config config) { throw new UnsupportedOperationException("Use CDI annotations for configuration injection."); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIConfiguredMethod.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIConfiguredMethod.java b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIConfiguredMethod.java index c38fc23..8424f00 100644 --- a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIConfiguredMethod.java +++ b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIConfiguredMethod.java @@ -16,9 +16,9 @@ */ package org.apache.tamaya.cdi; -import org.apache.tamaya.Configuration; import org.apache.tamaya.inject.spi.ConfiguredMethod; +import javax.config.Config; import javax.enterprise.inject.spi.InjectionPoint; import java.lang.reflect.Method; import java.util.ArrayList; @@ -79,7 +79,7 @@ public class CDIConfiguredMethod implements ConfiguredMethod{ } @Override - public void configure(Object instance, Configuration config) { + public void configure(Object instance, Config config) { throw new UnsupportedOperationException("Use CDI annotations for configuration injection."); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIConfiguredType.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIConfiguredType.java b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIConfiguredType.java index 0c7e34a..c5fae2f 100644 --- a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIConfiguredType.java +++ b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/CDIConfiguredType.java @@ -16,11 +16,11 @@ */ package org.apache.tamaya.cdi; -import org.apache.tamaya.Configuration; import org.apache.tamaya.inject.spi.ConfiguredField; import org.apache.tamaya.inject.spi.ConfiguredMethod; import org.apache.tamaya.inject.spi.ConfiguredType; +import javax.config.Config; import javax.enterprise.inject.spi.InjectionPoint; import java.lang.reflect.Field; import java.lang.reflect.Member; @@ -65,7 +65,7 @@ class CDIConfiguredType implements ConfiguredType{ } @Override - public void configure(Object instance, Configuration config) { + public void configure(Object instance, Config config) { throw new UnsupportedOperationException("Use CDI annotations for configuration injection."); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/ConfigProducer.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/ConfigProducer.java b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/ConfigProducer.java new file mode 100644 index 0000000..10e92dc --- /dev/null +++ b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/ConfigProducer.java @@ -0,0 +1,200 @@ +/* + * 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.cdi; + +import org.apache.tamaya.base.convert.ConversionContext; +import org.apache.tamaya.base.convert.ConverterManager; +import org.apache.tamaya.functions.Supplier; +import org.apache.tamaya.inject.api.ConfigDefaultSections; +import org.apache.tamaya.inject.api.DynamicValue; +import org.apache.tamaya.inject.api.WithConverter; +import org.apache.tamaya.spi.ConfigContextSupplier; +import org.apache.tamaya.spi.TypeLiteral; + +import javax.config.Config; +import javax.config.ConfigProvider; +import javax.config.inject.ConfigProperty; +import javax.config.spi.ConfigBuilder; +import javax.config.spi.ConfigProviderResolver; +import javax.config.spi.Converter; +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Instance; +import javax.enterprise.inject.Produces; +import javax.enterprise.inject.spi.InjectionPoint; +import javax.inject.Provider; +import java.lang.reflect.*; +import java.util.List; +import java.util.Optional; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Producer bean for configuration properties. + */ +@ApplicationScoped +public class ConfigProducer { + + private static final Logger LOGGER = Logger.getLogger(ConfigProducer.class.getName()); + + private DynamicValue createDynamicValue(final InjectionPoint injectionPoint) { + Member member = injectionPoint.getMember(); + if (member instanceof Field) { + return DefaultDynamicValue.of(injectionPoint.getBean(), (Field) member, ConfigProvider.getConfig()); + } else if (member instanceof Method) { + return DefaultDynamicValue.of(injectionPoint.getBean(), (Method) member, ConfigProvider.getConfig()); + } + return null; + } + + @Produces + @ConfigProperty + public Object resolveAndConvert(final InjectionPoint injectionPoint) { + if (DynamicValue.class.equals(injectionPoint.getAnnotated().getBaseType())) { + return createDynamicValue(injectionPoint); + } + final ConfigProperty annotation = injectionPoint.getAnnotated().getAnnotation(ConfigProperty.class); + final ConfigDefaultSections typeAnnot = injectionPoint.getMember().getDeclaringClass().getAnnotation(ConfigDefaultSections.class); + final List<String> keys = TamayaCDIInjectionExtension.evaluateKeys(injectionPoint.getMember().getName(), + annotation != null ? new String[]{annotation.name()} : null, + typeAnnot != null ? typeAnnot.value() : null); + + Converter customConverter = null; + final WithConverter withConverterAnnot = injectionPoint.getAnnotated().getAnnotation(WithConverter.class); + if (withConverterAnnot != null) { + customConverter = TamayaCDIInjectionExtension.CUSTOM_CONVERTERS.get(withConverterAnnot.value()); + } + String defaultTextValue = null; + if(annotation!=null && !annotation.defaultValue().equals(ConfigProperty.UNCONFIGURED_VALUE)){ + defaultTextValue = annotation.defaultValue(); + } + Config config = ConfigProvider.getConfig(); +// final WithConfigOperator withOperatorAnnot = injectionPoint.getAnnotated().getAnnotation(WithConfigOperator.class); +// ConfigOperator operator = null; +// if (withOperatorAnnot != null) { +// operator = TamayaCDIInjectionExtension.CUSTOM_OPERATORS.get(withOperatorAnnot.value()); +// } +// config = Objects.requireNonNull(operator.apply(config)); + + Optional<String> textValue = Optional.empty(); + // Try to esolve using type, non present is possible, conversion issues are errors. + String keyFound = null; + for(String key:keys) { + textValue = config.getOptionalValue(key, String.class); + if(textValue.isPresent()) { + keyFound = key; + break; + } + } + LOGGER.info("Converting config value found for " + injectionPoint ); + ConversionContext conversionContext = createConversionContext(keyFound, keys, config, injectionPoint); + + Object value = convertValue(textValue.orElse(defaultTextValue), conversionContext, customConverter); + if (value == null) { + throw new IllegalArgumentException(String.format( + "Can't resolve any of the possible config keys: %s to the required target type: %s, supported formats: %s", + keys, conversionContext.getTargetType(), conversionContext.getSupportedFormats().toString())); + } + LOGGER.finest(String.format("Injecting %s for key %s in class %s", keyFound, value.toString(), injectionPoint.toString())); + if(TypeLiteral.of(injectionPoint.getAnnotated().getBaseType()).getRawType().equals(Optional.class)){ + return Optional.ofNullable(value); + } + return value; + } + +// private Class getClass(Type baseType) { +// if(baseType instanceof Class){ +// return Class.class.cast(baseType); +// }else if(baseType instanceof ParameterizedType){ +// return getClass(((ParameterizedType)baseType).getRawType()); +// }else{ +// try { +// return Class.forName(baseType.getTypeName()); +// } catch (ClassNotFoundException e) { +// throw new IllegalArgumentException("Not a class tape: " + baseType.getTypeName()); +// } +// } +// } + + static ConversionContext createConversionContext(String key, List<String> keys, Config config, InjectionPoint injectionPoint) { + final Type targetType = resolveTargetType(injectionPoint.getAnnotated().getBaseType()); + ConversionContext.Builder builder = new ConversionContext.Builder(config, key, targetType); + if (injectionPoint.getMember() instanceof Field) { + Field annotated = (Field)injectionPoint.getMember(); + if(annotated.isAnnotationPresent(ConfigProperty.class)) { + builder.setAnnotatedElement(annotated); + } + }else if(injectionPoint.getMember() instanceof Method){ + Method method = (Method)injectionPoint.getMember(); + for(Type type:method.getParameterTypes()){ + if(type instanceof AnnotatedElement){ + AnnotatedElement annotated = (AnnotatedElement)type; + if(annotated.isAnnotationPresent(ConfigProperty.class)) { + builder.setAnnotatedElement(annotated); + } + } + } + } + return builder.build(); + } + + private static <T> T convertValue(String textValue, ConversionContext conversionContext, + Converter<T> customConverter) { + try { + ConversionContext.setContext(conversionContext); + if(customConverter!=null) { + return customConverter.convert(textValue); + } + + if(conversionContext.getConfiguration() instanceof ConfigContextSupplier){ + try { + return ConverterManager.defaultInstance().convertValue(textValue, conversionContext.getTargetType(), + ((ConfigContextSupplier) conversionContext.getConfiguration()).getConfigContext() + .getConverters(conversionContext.getTargetType())); + }catch(IllegalArgumentException e){ + return null; + } + } + return ConverterManager.defaultInstance().convertValue(textValue, conversionContext.getTargetType()); + }finally{ + ConversionContext.reset(); + } + } + + private static Type resolveTargetType(Type targetType) { + if(targetType instanceof ParameterizedType){ + ParameterizedType pt = (ParameterizedType)targetType; + if(Provider.class.equals(pt.getRawType()) || Supplier.class.equals(pt.getRawType()) + || Instance.class.equals(pt.getRawType()) + || Optional.class.equals(pt.getRawType())){ + return pt.getActualTypeArguments()[0]; + } + } + return targetType; + } + + + @Produces + public Config getConfig(){ + return ConfigProvider.getConfig(); + } + + @Produces + public ConfigBuilder getConfigBuilder(){ + return ConfigProviderResolver.instance().getBuilder(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/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 deleted file mode 100644 index fdcf995..0000000 --- a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/ConfigurationProducer.java +++ /dev/null @@ -1,202 +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.cdi; - -import org.apache.tamaya.*; -import org.apache.tamaya.inject.api.*; -import org.apache.tamaya.spi.*; - -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.inject.Instance; -import javax.enterprise.inject.Produces; -import javax.enterprise.inject.spi.InjectionPoint; -import javax.inject.Provider; -import java.lang.reflect.*; -import java.util.List; -import java.util.Optional; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Producer bean for configuration properties. - */ -@ApplicationScoped -public class ConfigurationProducer { - - private static final Logger LOGGER = Logger.getLogger(ConfigurationProducer.class.getName()); - - private DynamicValue createDynamicValue(final InjectionPoint injectionPoint) { - Member member = injectionPoint.getMember(); - if (member instanceof Field) { - return DefaultDynamicValue.of(injectionPoint.getBean(), (Field) member, ConfigurationProvider.getConfiguration()); - } else if (member instanceof Method) { - return DefaultDynamicValue.of(injectionPoint.getBean(), (Method) member, ConfigurationProvider.getConfiguration()); - } - return null; - } - - @Produces - @Config - public Object resolveAndConvert(final InjectionPoint injectionPoint) { - if (DynamicValue.class.equals(injectionPoint.getAnnotated().getBaseType())) { - return createDynamicValue(injectionPoint); - } - final Config annotation = injectionPoint.getAnnotated().getAnnotation(Config.class); - final ConfigDefaultSections typeAnnot = injectionPoint.getMember().getDeclaringClass().getAnnotation(ConfigDefaultSections.class); - final List<String> keys = TamayaCDIInjectionExtension.evaluateKeys(injectionPoint.getMember().getName(), - annotation != null ? annotation.value() : null, - typeAnnot != null ? typeAnnot.value() : null); - - final WithConfigOperator withOperatorAnnot = injectionPoint.getAnnotated().getAnnotation(WithConfigOperator.class); - ConfigOperator operator = null; - if (withOperatorAnnot != null) { - operator = TamayaCDIInjectionExtension.CUSTOM_OPERATORS.get(withOperatorAnnot.value()); - } - PropertyConverter customConverter = null; - final WithPropertyConverter withConverterAnnot = injectionPoint.getAnnotated().getAnnotation(WithPropertyConverter.class); - if (withConverterAnnot != null) { - customConverter = TamayaCDIInjectionExtension.CUSTOM_CONVERTERS.get(withConverterAnnot.value()); - } - - // unless the extension is not installed, this should never happen because the extension - // enforces the resolvability of the config - - String defaultTextValue = annotation.defaultValue().equals(Config.UNCONFIGURED_VALUE) ? null : annotation.defaultValue(); - String textValue = null; - Configuration config = ConfigurationProvider.getConfiguration(); - if(operator!=null) { - config = config.with(operator); - } - String keyFound = null; - for(String key:keys) { - textValue = config.get(key); - if(textValue!=null) { - keyFound = key; - break; - } - } - if(textValue==null) { - LOGGER.info("Using default value: '" + defaultTextValue + "' for IP: " + injectionPoint ); - textValue = defaultTextValue; - } - ConversionContext conversionContext = createConversionContext(keyFound, keys, injectionPoint); - Object value = convertValue(textValue, conversionContext, injectionPoint, customConverter); - if (value == null) { - throw new ConfigException(String.format( - "Can't resolve any of the possible config keys: %s to the required target type: %s, supported formats: %s", - keys, conversionContext.getTargetType(), conversionContext.getSupportedFormats().toString())); - } - LOGGER.finest(String.format("Injecting %s for key %s in class %s", keyFound, value.toString(), injectionPoint.toString())); - return value; - } - - static ConversionContext createConversionContext(String key, List<String> keys, InjectionPoint injectionPoint) { - final Type targetType = injectionPoint.getAnnotated().getBaseType(); - Configuration config = ConfigurationProvider.getConfiguration(); - ConversionContext.Builder builder = new ConversionContext.Builder(config, - ConfigurationProvider.getConfiguration().getContext(), key, TypeLiteral.of(targetType)); - // builder.setKeys(keys); - if(targetType instanceof ParameterizedType){ - ParameterizedType pt = (ParameterizedType)targetType; - if(pt.getRawType().equals(Provider.class)) { - builder.setTargetType( - TypeLiteral.of(pt.getActualTypeArguments()[0])); - } - } - if (injectionPoint.getMember() instanceof Field) { - Field annotated = (Field)injectionPoint.getMember(); - if(annotated.isAnnotationPresent(Config.class)) { - builder.setAnnotatedElement(annotated); - } - }else if(injectionPoint.getMember() instanceof Method){ - Method method = (Method)injectionPoint.getMember(); - for(Type type:method.getParameterTypes()){ - if(type instanceof AnnotatedElement){ - AnnotatedElement annotated = (AnnotatedElement)type; - if(annotated.isAnnotationPresent(Config.class)) { - builder.setAnnotatedElement(annotated); - } - } - } - } - return builder.build(); - } - - static Object convertValue(String textValue, ConversionContext conversionContext, InjectionPoint injectionPoint, - PropertyConverter customConverter) { - if (customConverter != null) { - return customConverter.convert(textValue, conversionContext); - } - if(String.class.equals(conversionContext.getTargetType().getRawType())){ - return textValue; - } - Object value = null; - ParameterizedType pt = null; - Type toType = injectionPoint.getAnnotated().getBaseType(); - if(toType instanceof ParameterizedType){ - pt = (ParameterizedType)toType; - if(Provider.class.equals(pt.getRawType()) || Instance.class.equals(pt.getRawType()) - || Optional.class.equals(pt.getRawType())){ - toType = pt.getActualTypeArguments()[0]; - } - if(toType.equals(String.class)){ - value = textValue; - } - } - List<PropertyConverter<Object>> converters = ConfigurationProvider.getConfiguration().getContext() - .getPropertyConverters(TypeLiteral.of(toType)); - for (PropertyConverter<Object> converter : converters) { - try { - value = converter.convert(textValue, conversionContext); - if (value != null) { - LOGGER.log(Level.INFO, "Parsed value from '" + textValue + "' into " + - injectionPoint); - break; - } - } catch (Exception e) { - LOGGER.log(Level.INFO, "Failed to convert value '" + textValue + "' for " + - injectionPoint, e); - } - } - if(pt != null && Optional.class.equals(pt.getRawType())){ - return Optional.ofNullable(value); - } - return value; - } - - @Produces - public Configuration getConfiguration(){ - return ConfigurationProvider.getConfiguration(); - } - - @Produces - public ConfigurationContext getConfigurationContext(){ - return ConfigurationProvider.getConfiguration().getContext(); - } - - @Deprecated - @Produces - public ConfigurationContextBuilder getConfigurationContextBuilder(){ - return ConfigurationProvider.getConfigurationContextBuilder(); - } - - @Produces - public ConfigurationBuilder getConfigurationBuilder(){ - return ConfigurationProvider.getConfigurationBuilder(); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/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 8c6dfee..6b31196 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 @@ -18,17 +18,15 @@ */ package org.apache.tamaya.cdi; -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.Configuration; -import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.inject.api.DynamicValue; import org.apache.tamaya.inject.api.LoadPolicy; import org.apache.tamaya.inject.api.UpdatePolicy; -import org.apache.tamaya.inject.api.WithPropertyConverter; +import org.apache.tamaya.inject.api.WithConverter; import org.apache.tamaya.inject.spi.BaseDynamicValue; -import org.apache.tamaya.inject.spi.InjectionUtils; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.inject.spi.InjectionEvaluator; +import javax.config.Config; +import javax.config.spi.Converter; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; @@ -55,12 +53,12 @@ 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; + private final Config configuration; /** * The property converter to be applied, may be null. In the ladder case targetType is not null. */ - private final PropertyConverter<T> customConverter; + private final Converter<T> customConverter; /** * Load policy. */ @@ -75,8 +73,8 @@ final class DefaultDynamicValue<T> extends BaseDynamicValue<T> { * @param targetType the target type, not null. * @param customConverter the optional converter to be used. */ - private DefaultDynamicValue(Object owner, String propertyName, Configuration configuration, TypeLiteral<T> targetType, - PropertyConverter<T> customConverter, List<String> keys, LoadPolicy loadPolicy, + private DefaultDynamicValue(Object owner, String propertyName, Config configuration, Type targetType, + Converter<T> customConverter, List<String> keys, LoadPolicy loadPolicy, UpdatePolicy updatePolicy) { super(owner, propertyName, targetType, keys); this.configuration = Objects.requireNonNull(configuration); @@ -88,103 +86,104 @@ final class DefaultDynamicValue<T> extends BaseDynamicValue<T> { } } - public static DynamicValue of(Object owner, Field annotatedField, Configuration configuration) { + public static DynamicValue of(Object owner, Field annotatedField, Config configuration) { return of(owner, annotatedField, configuration, LoadPolicy.ALWAYS, UpdatePolicy.IMMEDIATE); } - public static DynamicValue of(Object owner, Field annotatedField, Configuration configuration, LoadPolicy loadPolicy) { + public static DynamicValue of(Object owner, Field annotatedField, Config configuration, LoadPolicy loadPolicy) { return of(owner, annotatedField, configuration, loadPolicy, UpdatePolicy.IMMEDIATE); } - public static DynamicValue of(Object owner, Field annotatedField, Configuration configuration, UpdatePolicy updatePolicy) { + public static DynamicValue of(Object owner, Field annotatedField, Config configuration, UpdatePolicy updatePolicy) { return of(owner, annotatedField, configuration, LoadPolicy.ALWAYS, updatePolicy); } - public static DynamicValue of(Object owner, Field annotatedField, Configuration configuration, LoadPolicy loadPolicy, UpdatePolicy updatePolicy) { + public static DynamicValue of(Object owner, Field annotatedField, Config configuration, LoadPolicy loadPolicy, + UpdatePolicy updatePolicy) { // Check for adapter/filter Type targetType = annotatedField.getGenericType(); if (targetType == null) { - throw new ConfigException("Failed to evaluate target type for " + annotatedField.getDeclaringClass().getName() + throw new IllegalArgumentException("Failed to evaluate target type for " + annotatedField.getDeclaringClass().getName() + '.' + annotatedField.getName()); } if (targetType instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) targetType; Type[] types = pt.getActualTypeArguments(); if (types.length != 1) { - throw new ConfigException("Failed to evaluate target type for " + annotatedField.getDeclaringClass().getName() + throw new IllegalArgumentException("Failed to evaluate target type for " + annotatedField.getDeclaringClass().getName() + '.' + annotatedField.getName()); } targetType = types[0]; } - PropertyConverter<?> propertyConverter = null; - WithPropertyConverter annot = annotatedField.getAnnotation(WithPropertyConverter.class); + Converter<?> propertyConverter = null; + WithConverter annot = annotatedField.getAnnotation(WithConverter.class); if (annot != null) { try { propertyConverter = annot.value().newInstance(); } catch (Exception e) { - throw new ConfigException("Failed to instantiate annotated PropertyConverter on " + + throw new IllegalArgumentException("Failed to instantiate annotated Converter on " + annotatedField.getDeclaringClass().getName() + '.' + annotatedField.getName(), e); } } - List<String> keys = InjectionUtils.getKeys(annotatedField); + List<String> keys = InjectionEvaluator.getKeys(annotatedField); return new DefaultDynamicValue(owner, annotatedField.getName(), configuration, - TypeLiteral.of(targetType), propertyConverter, keys, loadPolicy, updatePolicy); + targetType, propertyConverter, keys, loadPolicy, updatePolicy); } - public static DynamicValue of(Object owner, Method method, Configuration configuration) { + public static DynamicValue of(Object owner, Method method, Config configuration) { return of(owner, method, configuration, LoadPolicy.ALWAYS, UpdatePolicy.IMMEDIATE); } - public static DynamicValue of(Object owner, Method method, Configuration configuration, UpdatePolicy updatePolicy) { + public static DynamicValue of(Object owner, Method method, Config configuration, UpdatePolicy updatePolicy) { return of(owner, method, configuration, LoadPolicy.ALWAYS, updatePolicy); } - public static DynamicValue of(Object owner, Method method, Configuration configuration, LoadPolicy loadPolicy) { + public static DynamicValue of(Object owner, Method method, Config configuration, LoadPolicy loadPolicy) { return of(owner, method, configuration, loadPolicy, UpdatePolicy.IMMEDIATE); } - public static DynamicValue of(Object owner, Method method, Configuration configuration, LoadPolicy loadPolicy, UpdatePolicy updatePolicy) { + public static DynamicValue of(Object owner, Method method, Config configuration, LoadPolicy loadPolicy, UpdatePolicy updatePolicy) { // Check for adapter/filter Type targetType = method.getGenericReturnType(); if (targetType == null) { - throw new ConfigException("Failed to evaluate target type for " + method.getDeclaringClass() + throw new IllegalArgumentException("Failed to evaluate target type for " + method.getDeclaringClass() .getName() + '.' + method.getName()); } if (targetType instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) targetType; Type[] types = pt.getActualTypeArguments(); if (types.length != 1) { - throw new ConfigException("Failed to evaluate target type for " + method.getDeclaringClass() + throw new IllegalArgumentException("Failed to evaluate target type for " + method.getDeclaringClass() .getName() + '.' + method.getName()); } targetType = types[0]; } - PropertyConverter<Object> propertyConverter = null; - WithPropertyConverter annot = method.getAnnotation(WithPropertyConverter.class); + Converter<Object> propertyConverter = null; + WithConverter annot = method.getAnnotation(WithConverter.class); if (annot != null) { try { - propertyConverter = (PropertyConverter<Object>) annot.value().newInstance(); + propertyConverter = (Converter<Object>) annot.value().newInstance(); } catch (Exception e) { - throw new ConfigException("Failed to instantiate annotated PropertyConverter on " + + throw new IllegalArgumentException("Failed to instantiate annotated Converter on " + method.getDeclaringClass().getName() + '.' + method.getName(), e); } } return new DefaultDynamicValue<>(owner, method.getName(), - configuration, TypeLiteral.of(targetType), propertyConverter, InjectionUtils.getKeys(method), + configuration, targetType, propertyConverter, InjectionEvaluator.getKeys(method), loadPolicy, updatePolicy); } @Override - protected Configuration getConfiguration() { + protected Config getConfiguration() { return configuration; } @Override - protected PropertyConverter<T> getCustomConverter() { + protected Converter<T> getCustomConverter() { return customConverter; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/ServiceLoaderServiceContext.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/ServiceLoaderServiceContext.java b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/ServiceLoaderServiceContext.java index f5a5f6c..f56c199 100644 --- a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/ServiceLoaderServiceContext.java +++ b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/ServiceLoaderServiceContext.java @@ -18,9 +18,8 @@ */ package org.apache.tamaya.cdi; -import org.apache.tamaya.ConfigException; +import org.apache.tamaya.base.PriorityServiceComparator; import org.apache.tamaya.spi.ServiceContext; -import org.apache.tamaya.spisupport.PriorityServiceComparator; import javax.annotation.Priority; import java.io.IOException; @@ -49,6 +48,11 @@ final class ServiceLoaderServiceContext implements ServiceContext { @Override public <T> T getService(Class<T> serviceType) { + return getService(serviceType, ServiceContext.defaultClassLoader()); + } + + @Override + public <T> T getService(Class<T> serviceType, ClassLoader classLoader) { Object cached = singletons.get(serviceType); if (cached == null) { cached = create(serviceType); @@ -61,9 +65,15 @@ final class ServiceLoaderServiceContext implements ServiceContext { @Override public <T> T create(Class<T> serviceType) { + return create(serviceType, ServiceContext.defaultClassLoader()); + } + + @Override + public <T> T create(Class<T> serviceType, ClassLoader classLoader) { + @SuppressWarnings("unchecked") Class<? extends T> implType = factoryTypes.get(serviceType); if(implType==null) { - Collection<T> services = getServices(serviceType); + Collection<T> services = getServices(serviceType, classLoader); if (services.isEmpty()) { return null; } else { @@ -87,13 +97,25 @@ final class ServiceLoaderServiceContext implements ServiceContext { */ @Override public <T> List<T> getServices(final Class<T> serviceType) { + return getServices(serviceType, ServiceContext.defaultClassLoader()); + } + + /** + * Loads and registers services. + * + * @param <T> the concrete type. + * @param serviceType The service type. + * @return the items found, never {@code null}. + */ + @Override + public <T> List<T> getServices(final Class<T> serviceType, ClassLoader classLoader) { List<T> found = (List<T>) servicesLoaded.get(serviceType); if (found != null) { return found; } List<T> services = new ArrayList<>(); try { - for (T t : ServiceLoader.load(serviceType)) { + for (T t : ServiceLoader.load(serviceType, classLoader)) { services.add(t); } Collections.sort(services, PriorityServiceComparator.getInstance()); @@ -130,7 +152,7 @@ final class ServiceLoaderServiceContext implements ServiceContext { * * @return the service with the highest {@link Priority#value()} * - * @throws ConfigException if there are multiple service implementations with the maximum priority + * @throws IllegalStateException if there are multiple service implementations with the maximum priority */ private <T> T getServiceWithHighestPriority(Collection<T> services, Class<T> serviceType) { T highestService = null; @@ -156,7 +178,7 @@ final class ServiceLoaderServiceContext implements ServiceContext { } if (highestPriorityServiceCount > 1) { - throw new ConfigException(MessageFormat.format("Found {0} implementations for Service {1} with Priority {2}: {3}", + throw new IllegalStateException(MessageFormat.format("Found {0} implementations for Service {1} with Priority {2}: {3}", highestPriorityServiceCount, serviceType.getName(), highestPriority, http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/TamayaCDIInjectionExtension.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/TamayaCDIInjectionExtension.java b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/TamayaCDIInjectionExtension.java index 2485f05..2d95401 100644 --- a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/TamayaCDIInjectionExtension.java +++ b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/TamayaCDIInjectionExtension.java @@ -16,14 +16,11 @@ */ package org.apache.tamaya.cdi; -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.ConfigOperator; -import org.apache.tamaya.inject.api.Config; import org.apache.tamaya.inject.api.ConfigDefaultSections; -import org.apache.tamaya.inject.api.WithConfigOperator; -import org.apache.tamaya.inject.api.WithPropertyConverter; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.inject.api.WithConverter; +import javax.config.inject.ConfigProperty; +import javax.config.spi.Converter; import javax.enterprise.context.spi.CreationalContext; import javax.enterprise.event.Observes; import javax.enterprise.inject.Instance; @@ -39,16 +36,15 @@ import java.util.logging.Logger; /** * CDI Extension module that adds injection mechanism for configuration. * - * @see Config + * @see javax.config.inject.ConfigProperty * @see ConfigDefaultSections - * @see ConfigException */ public class TamayaCDIInjectionExtension implements Extension { private static final Logger LOG = Logger.getLogger(TamayaCDIInjectionExtension.class.getName()); - static final Map<Class, ConfigOperator> CUSTOM_OPERATORS = new ConcurrentHashMap<>(); - static final Map<Class, PropertyConverter> CUSTOM_CONVERTERS = new ConcurrentHashMap<>(); +// static final Map<Class, ConfigOperator> CUSTOM_OPERATORS = new ConcurrentHashMap<>(); + static final Map<Class, Converter> CUSTOM_CONVERTERS = new ConcurrentHashMap<>(); private final Set<Type> types = new HashSet<>(); private Bean<?> tamayaProducerBean; @@ -72,18 +68,18 @@ public class TamayaCDIInjectionExtension implements Extension { boolean configured = false; for (InjectionPoint injectionPoint : ips) { - if (injectionPoint.getAnnotated().isAnnotationPresent(Config.class)) { + if (injectionPoint.getAnnotated().isAnnotationPresent(ConfigProperty.class)) { LOG.fine("Configuring: " + injectionPoint); - final Config annotation = injectionPoint.getAnnotated().getAnnotation(Config.class); + final ConfigProperty annotation = injectionPoint.getAnnotated().getAnnotation(ConfigProperty.class); final ConfigDefaultSections typeAnnot = injectionPoint.getMember().getDeclaringClass().getAnnotation(ConfigDefaultSections.class); final List<String> keys = evaluateKeys(injectionPoint.getMember().getName(), - annotation!=null?annotation.value():null, + (annotation!=null && !annotation.name().isEmpty())?new String[]{annotation.name()}:null, typeAnnot!=null?typeAnnot.value():null); - final WithConfigOperator withOperatorAnnot = injectionPoint.getAnnotated().getAnnotation(WithConfigOperator.class); - if(withOperatorAnnot!=null){ - tryLoadOpererator(withOperatorAnnot.value()); - } - final WithPropertyConverter withConverterAnnot = injectionPoint.getAnnotated().getAnnotation(WithPropertyConverter.class); +// final WithConfigOperator withOperatorAnnot = injectionPoint.getAnnotated().getAnnotation(WithConfigOperator.class); +// if(withOperatorAnnot!=null){ +// tryLoadOpererator(withOperatorAnnot.value()); +// } + final WithConverter withConverterAnnot = injectionPoint.getAnnotated().getAnnotation(WithConverter.class); if(withConverterAnnot!=null){ tryLoadConverter(withConverterAnnot.value()); } @@ -102,7 +98,7 @@ public class TamayaCDIInjectionExtension implements Extension { public void captureConvertBean(@Observes final ProcessProducerMethod<?, ?> ppm) { - if (ppm.getAnnotated().isAnnotationPresent(Config.class)) { + if (ppm.getAnnotated().isAnnotationPresent(ConfigProperty.class)) { tamayaProducerBean = ppm.getBean(); } } @@ -123,23 +119,23 @@ public class TamayaCDIInjectionExtension implements Extension { return type; } - private void tryLoadOpererator(Class<? extends ConfigOperator> operatorClass) { - Objects.requireNonNull(operatorClass); - if(ConfigOperator.class == operatorClass){ - return; - } - try{ - if(!CUSTOM_OPERATORS.containsKey(operatorClass)) { - CUSTOM_OPERATORS.put(operatorClass, operatorClass.newInstance()); - } - } catch(Exception e){ - throw new ConfigException("Custom ConfigOperator could not be loaded: " + operatorClass.getName(), e); - } - } - - private void tryLoadConverter(Class<? extends PropertyConverter> converterClass) { +// private void tryLoadOpererator(Class<? extends ConfigOperator> operatorClass) { +// Objects.requireNonNull(operatorClass); +// if(ConfigOperator.class == operatorClass){ +// return; +// } +// try{ +// if(!CUSTOM_OPERATORS.containsKey(operatorClass)) { +// CUSTOM_OPERATORS.put(operatorClass, operatorClass.newInstance()); +// } +// } catch(Exception e){ +// throw new ConfigException("Custom ConfigOperator could not be loaded: " + operatorClass.getName(), e); +// } +// } + + private void tryLoadConverter(Class<? extends Converter> converterClass) { Objects.requireNonNull(converterClass); - if(PropertyConverter.class == converterClass){ + if(Converter.class == converterClass){ return; } try{ @@ -147,7 +143,7 @@ public class TamayaCDIInjectionExtension implements Extension { CUSTOM_CONVERTERS.put(converterClass, converterClass.newInstance()); } } catch(Exception e){ - throw new ConfigException("Custom PropertyConverter could not be loaded: " + converterClass.getName(), e); + throw new IllegalArgumentException("Custom PropertyConverter could not be loaded: " + converterClass.getName(), e); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/TamayaSEInjectionExtension.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/TamayaSEInjectionExtension.java b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/TamayaSEInjectionExtension.java index 024b895..6ab73b0 100644 --- a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/TamayaSEInjectionExtension.java +++ b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/TamayaSEInjectionExtension.java @@ -20,9 +20,9 @@ package org.apache.tamaya.cdi; import org.apache.tamaya.inject.ConfigurationInjection; -import org.apache.tamaya.inject.api.Config; import org.apache.tamaya.inject.api.ConfigDefaultSections; +import javax.config.inject.ConfigProperty; import javax.enterprise.context.spi.CreationalContext; import javax.enterprise.event.Observes; import javax.enterprise.inject.Vetoed; @@ -92,13 +92,13 @@ public final class TamayaSEInjectionExtension implements Extension { } // if no class level annotation is there we might have field level annotations only for (Field field : type.getDeclaredFields()) { - if (field.isAnnotationPresent(Config.class) && !field.isAnnotationPresent(Inject.class)) { + if (field.isAnnotationPresent(ConfigProperty.class) && !field.isAnnotationPresent(Inject.class)) { return true; } } // if no class level annotation is there we might have method level annotations only for (Method method : type.getDeclaredMethods()) { - if(method.isAnnotationPresent(Config.class)) { + if(method.isAnnotationPresent(ConfigProperty.class)) { return true; } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/extra/ConfiguredVetoExtension.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/extra/ConfiguredVetoExtension.java b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/extra/ConfiguredVetoExtension.java index e86ee89..93fbdd2 100644 --- a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/extra/ConfiguredVetoExtension.java +++ b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/extra/ConfiguredVetoExtension.java @@ -18,8 +18,7 @@ */ package org.apache.tamaya.cdi.extra; -import org.apache.tamaya.ConfigurationProvider; - +import javax.config.ConfigProvider; import javax.enterprise.event.Observes; import javax.enterprise.inject.spi.Extension; import javax.enterprise.inject.spi.ProcessAnnotatedType; @@ -31,7 +30,7 @@ import javax.enterprise.inject.spi.ProcessAnnotatedType; public class ConfiguredVetoExtension implements Extension { public void observesBean(@Observes ProcessAnnotatedType<?> type) { - String vetoedTypesVal = ConfigurationProvider.getConfiguration().get("javax.enterprise.inject.vetoed"); + String vetoedTypesVal = ConfigProvider.getConfig().getValue("javax.enterprise.inject.vetoed", String.class); String[] vetoedTypes = vetoedTypesVal.split(","); for (String typeExpr : vetoedTypes) { String typeExprTrimmed = typeExpr.trim(); http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/BaseTestConfiguration.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/BaseTestConfiguration.java b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/BaseTestConfiguration.java index ad762a7..0365195 100644 --- a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/BaseTestConfiguration.java +++ b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/BaseTestConfiguration.java @@ -34,10 +34,10 @@ abstract class BaseTestConfiguration { return ShrinkWrap.create(WebArchive.class) .addClasses(ConfiguredTest.class, ConfiguredClass.class, InjectedClass.class, AdditionalMatchers.class, NotFoundNoDefault.class, - ConfigurationProducer.class) + ConfigProducer.class) .addAsServiceProvider(Extension.class, TamayaCDIInjectionExtension.class) .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") - .addAsWebInfResource("META-INF/javaconfiguration.properties", "META-INF/javaconfiguration.properties"); + .addAsWebInfResource("META-INF/javaconfig.properties", "META-INF/javaconfig.properties"); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfigurationProducerTest.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfigurationProducerTest.java b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfigurationProducerTest.java index b3459b7..bf7e55a 100644 --- a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfigurationProducerTest.java +++ b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfigurationProducerTest.java @@ -23,11 +23,11 @@ import static org.junit.Assert.assertTrue; import java.io.File; import java.util.Optional; +import javax.config.inject.ConfigProperty; import javax.enterprise.inject.spi.Extension; import javax.inject.Inject; import javax.inject.Provider; -import org.apache.tamaya.inject.api.Config; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.shrinkwrap.api.Archive; @@ -45,10 +45,10 @@ public class ConfigurationProducerTest { return ShrinkWrap.create(WebArchive.class) .addClasses(ConfiguredClass.class, InjectedClass.class, TamayaCDIInjectionExtension.class, TamayaCDIAccessor.class, - org.apache.tamaya.cdi.ConfigurationProducer.class) + org.apache.tamaya.cdi.ConfigProducer.class) .addAsServiceProvider(Extension.class, TamayaCDIInjectionExtension.class) .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") - .addAsWebInfResource("META-INF/javaconfiguration.properties", "META-INF/javaconfiguration.properties"); + .addAsWebInfResource("META-INF/javaconfig.properties", "META-INF/javaconfig.properties"); } @Inject @@ -116,51 +116,51 @@ public class ConfigurationProducerTest { private Provider<Integer> providerIntegerAsMethodParam; @Inject - @Config(value = "string.value", defaultValue = "defaultString") + @ConfigProperty(name = "string.value", defaultValue = "defaultString") private String string; @Inject - @Config(value = "string.value", defaultValue = "defaultString") + @ConfigProperty(name = "string.value", defaultValue = "defaultString") private Optional<String> optionalString; @Inject - @Config(value = "string.value", defaultValue = "defaultString") + @ConfigProperty(name = "string.value", defaultValue = "defaultString") private Provider<String> providerString; @Inject - @Config(value = "defaultString.value", defaultValue = "defaultString") + @ConfigProperty(name = "defaultString.value", defaultValue = "defaultString") private String defaultString; @Inject - @Config(value = "file.value", defaultValue = "./") + @ConfigProperty(name = "file.value", defaultValue = "./") private File file; @Inject - @Config(value = "defaultFile.value", defaultValue = "./") + @ConfigProperty(name = "defaultFile.value", defaultValue = "./") private File defaultFile; @Inject - @Config(value = "boolean.value", defaultValue = "true") + @ConfigProperty(name = "boolean.value", defaultValue = "true") private Boolean aBoolean; @Inject - @Config(value = "defaultBoolean.value", defaultValue = "true") + @ConfigProperty(name = "defaultBoolean.value", defaultValue = "true") private Boolean defaultBoolean; @Inject - @Config(value = "integer.value", defaultValue = "45") + @ConfigProperty(name = "integer.value", defaultValue = "45") private Integer integer; @Inject - @Config(value = "defaultInteger.value", defaultValue = "45") + @ConfigProperty(name = "defaultInteger.value", defaultValue = "45") private Integer defaultInteger; @Inject - @Config(value = "integer.value", defaultValue = "45") + @ConfigProperty(name = "integer.value", defaultValue = "45") private Optional<Integer> optionalInteger; @Inject - @Config(value = "integer.value", defaultValue = "45") + @ConfigProperty(name = "integer.value", defaultValue = "45") private Provider<Integer> providerInteger; public String getString() { @@ -197,32 +197,32 @@ public class ConfigurationProducerTest { @Inject - public void setStringAsMethodParam(@Config(value = "string.value", defaultValue = "defaultString") String stringAsMethodParam) { + public void setStringAsMethodParam(@ConfigProperty(name = "string.value", defaultValue = "defaultString") String stringAsMethodParam) { this.stringAsMethodParam = stringAsMethodParam; } @Inject - public void setIntegerAsMethodParam(@Config(value = "integer.value", defaultValue = "45")Integer integerAsMethodParam) { + public void setIntegerAsMethodParam(@ConfigProperty(name = "integer.value", defaultValue = "45")Integer integerAsMethodParam) { this.integerAsMethodParam = integerAsMethodParam; } @Inject - public void setOptionalStringAsMethodParam(@Config(value = "string.value", defaultValue = "defaultString") Optional<String> optionalStringAsMethodParam) { + public void setOptionalStringAsMethodParam(@ConfigProperty(name = "string.value", defaultValue = "defaultString") Optional<String> optionalStringAsMethodParam) { this.optionalStringAsMethodParam = optionalStringAsMethodParam; } @Inject - public void setOptionalIntegerAsMethodParam(@Config(value = "integer.value", defaultValue = "45") Optional<Integer> optionalIntegerAsMethodParam) { + public void setOptionalIntegerAsMethodParam(@ConfigProperty(name = "integer.value", defaultValue = "45") Optional<Integer> optionalIntegerAsMethodParam) { this.optionalIntegerAsMethodParam = optionalIntegerAsMethodParam; } @Inject - public void setProviderStringAsMethodParam(@Config(value = "string.value", defaultValue = "defaultString") Provider<String> providerStringAsMethodParam) { + public void setProviderStringAsMethodParam(@ConfigProperty(name = "string.value", defaultValue = "defaultString") Provider<String> providerStringAsMethodParam) { this.providerStringAsMethodParam = providerStringAsMethodParam; } @Inject - public void setProviderIntegerAsMethodParam(@Config(value = "integer.value", defaultValue = "45") Provider<Integer> providerIntegerAsMethodParam) { + public void setProviderIntegerAsMethodParam(@ConfigProperty(name = "integer.value", defaultValue = "45") Provider<Integer> providerIntegerAsMethodParam) { this.providerIntegerAsMethodParam = providerIntegerAsMethodParam; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfiguredClass.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfiguredClass.java b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfiguredClass.java index 5d71d5d..29c20c0 100644 --- a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfiguredClass.java +++ b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfiguredClass.java @@ -20,8 +20,9 @@ */ package org.apache.tamaya.cdi; -import org.apache.tamaya.inject.api.Config; +import org.apache.tamaya.inject.api.ConfigFallbackKeys; +import javax.config.inject.ConfigProperty; import javax.inject.Singleton; import java.math.BigDecimal; @@ -31,34 +32,36 @@ import java.math.BigDecimal; @Singleton public class ConfiguredClass{ - @Config + @ConfigProperty private String testProperty; - @Config(value = {"a.b.c.key1","a.b.c.key2","a.b.c.key3"}, defaultValue = "The current \\${JAVA_HOME} env property is ${env:JAVA_HOME}.") + @ConfigProperty(name = "a.b.c.key1", defaultValue = "The current \\${JAVA_HOME} env property is ${env:JAVA_HOME}.") + @ConfigFallbackKeys({"a.b.c.key2","a.b.c.key3"}) String value1; - @Config({"foo","a.b.c.key2"}) + @ConfigProperty(name="foo") + @ConfigFallbackKeys({"a.b.c.key2"}) private String value2; - @Config(defaultValue = "N/A") + @ConfigProperty(defaultValue = "N/A") private String runtimeVersion; - @Config(defaultValue = "${sys:java.version}") + @ConfigProperty(defaultValue = "${sys:java.version}") private String javaVersion2; - @Config(defaultValue = "5") + @ConfigProperty(defaultValue = "5") private Integer int1; - @Config + @ConfigProperty private int int2; - @Config + @ConfigProperty private boolean booleanT; - @Config("BD") + @ConfigProperty(name="BD") private BigDecimal bigNumber; - @Config("double1") + @ConfigProperty(name="double1") private double doubleValue; public String getTestProperty() { http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfiguredTest.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfiguredTest.java b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfiguredTest.java index 8143d95..95435e7 100644 --- a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfiguredTest.java +++ b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfiguredTest.java @@ -52,7 +52,7 @@ public class ConfiguredTest extends BaseTestConfiguration { assertNotNull(injectedClass.builder1); assertNotNull(injectedClass.builder2); assertNotNull(injectedClass.config); - assertNotNull(injectedClass.configContext); + assertNotNull(injectedClass.config2); } @Test @@ -67,12 +67,6 @@ public class ConfiguredTest extends BaseTestConfiguration { assertTrue(injectedClass.config == injectedClass.config2); } - @Test - public void test_Injected_configContexts_are_same(){ - InjectedClass injectedClass = CDI.current().select(InjectedClass.class).get(); - assertTrue(injectedClass.configContext == injectedClass.configContext2); - } - @Test(expected=Exception.class) public void test_error_Injection() { NotFoundNoDefault injectedClass = CDI.current().select(NotFoundNoDefault.class).get(); http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cfb364cd/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/InjectedClass.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/InjectedClass.java b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/InjectedClass.java index 3c6ca51..e3d6fa2 100644 --- a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/InjectedClass.java +++ b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/InjectedClass.java @@ -19,10 +19,8 @@ */ package org.apache.tamaya.cdi; -import org.apache.tamaya.Configuration; -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.ConfigurationBuilder; - +import javax.config.Config; +import javax.config.spi.ConfigBuilder; import javax.inject.Inject; import javax.inject.Singleton; @@ -33,28 +31,21 @@ import javax.inject.Singleton; public class InjectedClass { @Inject - Configuration config; - - @Inject - Configuration config2; - - @Inject - ConfigurationContext configContext; + Config config; @Inject - ConfigurationContext configContext2; + Config config2; @Inject - ConfigurationBuilder builder1; + ConfigBuilder builder1; @Inject - ConfigurationBuilder builder2; + ConfigBuilder builder2; @Override public String toString() { return "InjectedClass{" + "config=" + config + - ", configContext=" + configContext + ", builder1=" + builder1 + ", builder2=" + builder2 + '}';