http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/ConfiguredTest.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/ConfiguredTest.java b/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/ConfiguredTest.java new file mode 100644 index 0000000..69e8ef9 --- /dev/null +++ b/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/ConfiguredTest.java @@ -0,0 +1,83 @@ +/* + * 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 current 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.integration.cdi; + +import org.apache.deltaspike.testcontrol.api.TestControl; +import org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner; +import org.hamcrest.MatcherAssert; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.spi.CDI; +import javax.inject.Singleton; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * Tests for CDI integration. + */ +@RunWith(CdiTestRunner.class) +@TestControl(startScopes = {ApplicationScoped.class, Singleton.class}) +public class ConfiguredTest{ + + @Test + public void test_Configuration_is_injected_correctly(){ + ConfiguredClass item = CDI.current().select(ConfiguredClass.class).get(); + System.out.println("********************************************"); + System.out.println(item); + System.out.println("********************************************"); + double actual = 1234.5678; + MatcherAssert.assertThat(item.getDoubleValue(), is(actual)); + } + + @Test + public void test_Default_injections_are_accessible(){ + InjectedClass injectedClass = CDI.current().select(InjectedClass.class).get(); + System.out.println("********************************************"); + System.out.println(injectedClass); + System.out.println("********************************************"); + assertNotNull(injectedClass.builder1); + assertNotNull(injectedClass.builder2); + assertNotNull(injectedClass.config); + assertNotNull(injectedClass.configContext); + } + + @Test + public void test_Injected_builders_are_notSame(){ + InjectedClass injectedClass = CDI.current().select(InjectedClass.class).get(); + assertTrue(injectedClass.builder1 != injectedClass.builder2); + } + + @Test + public void test_Injected_configs_are_same(){ + InjectedClass injectedClass = CDI.current().select(InjectedClass.class).get(); + 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); + } + +}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/InjectedClass.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/InjectedClass.java b/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/InjectedClass.java new file mode 100644 index 0000000..9b7bd23 --- /dev/null +++ b/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/InjectedClass.java @@ -0,0 +1,62 @@ +/* + * 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 current 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.integration.cdi; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.spi.ConfigurationContext; +import org.apache.tamaya.spi.ConfigurationContextBuilder; + +import javax.inject.Inject; +import javax.inject.Singleton; + +/** + * Class loaded by CDI to test correct injection of Configuration API artifacts. + */ +@Singleton +public class InjectedClass { + + @Inject + Configuration config; + + @Inject + Configuration config2; + + @Inject + ConfigurationContext configContext; + + @Inject + ConfigurationContext configContext2; + + @Inject + ConfigurationContextBuilder builder1; + + @Inject + ConfigurationContextBuilder builder2; + + @Override + public String toString() { + return "InjectedClass{" + + "config=" + config + + ", configContext=" + configContext + + ", builder1=" + builder1 + + ", builder2=" + builder2 + + '}'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/ProvidedPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/ProvidedPropertySource.java b/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/ProvidedPropertySource.java new file mode 100644 index 0000000..f7e3c6d --- /dev/null +++ b/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/ProvidedPropertySource.java @@ -0,0 +1,67 @@ +/* + * 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 current 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.integration.cdi.cfg; + +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertyValue; + +import javax.enterprise.inject.Vetoed; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by Anatole on 17.09.2015. + */ +@Vetoed +class ProvidedPropertySource implements PropertySource{ + + final Map<String,String> config = new HashMap<>(); + + public ProvidedPropertySource(){ + config.put("a.b.c.key3", "keys current a.b.c.key3"); + config.put("a.b.c.key4", "keys current a.b.c.key4"); + config.put("{meta}source.type:"+getClass().getName(), "PropertySourceProvider"); + } + + @Override + public int getOrdinal() { + return 10; + } + + @Override + public String getName() { + return getClass().getName(); + } + + @Override + public PropertyValue get(String key) { + return PropertyValue.of(key, config.get(key), getName()); + } + + @Override + public Map<String, String> getProperties() { + return config; + } + + @Override + public boolean isScannable() { + return true; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/TestConfigProvider.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/TestConfigProvider.java b/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/TestConfigProvider.java new file mode 100644 index 0000000..7b89fba --- /dev/null +++ b/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/TestConfigProvider.java @@ -0,0 +1,45 @@ +/* + * 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 current 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.integration.cdi.cfg; + +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertySourceProvider; + +import javax.enterprise.context.ApplicationScoped; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * Created by Anatole on 29.09.2014. + */ +@ApplicationScoped +public class TestConfigProvider implements PropertySourceProvider { + + private List<PropertySource> configs = new ArrayList<>(); + + public TestConfigProvider(){ + configs.add(new ProvidedPropertySource()); + } + + @Override + public Collection<PropertySource> getPropertySources() { + return configs; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/TestPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/TestPropertySource.java b/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/TestPropertySource.java new file mode 100644 index 0000000..75c55ca --- /dev/null +++ b/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/TestPropertySource.java @@ -0,0 +1,79 @@ +/* + * 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 current 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.integration.cdi.cfg; + +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertyValue; + +import javax.inject.Singleton; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by Anatole on 17.09.2015. + */ +@Singleton +public class TestPropertySource implements PropertySource{ + + final Map<String,String> config = new HashMap<>(); + + public TestPropertySource(){ + config.put("a.b.c.key1", "keys current a.b.c.key1"); + config.put("a.b.c.key2", "keys current a.b.c.key2"); + config.put("a.b.key3", "keys current a.b.key3"); + config.put("a.b.key4", "keys current a.b.key4"); + config.put("a.key5", "keys current a.key5"); + config.put("a.key6", "keys current a.key6"); + config.put("int1", "123456"); + config.put("int2", "111222"); + config.put("testProperty", "testPropertyValue!"); + config.put("booleanT", "true"); + config.put("double1", "1234.5678"); + config.put("BD", "123456789123456789123456789123456789.123456789123456789123456789123456789"); + config.put("testProperty", "keys current testProperty"); + config.put("runtimeVersion", "${java.version}"); + config.put("{meta}source.type:"+getClass().getName(), "PropertySource"); + } + + @Override + public int getOrdinal() { + return 10; + } + + @Override + public String getName() { + return getClass().getName(); + } + + @Override + public PropertyValue get(String key) { + return PropertyValue.of(key, config.get(key), getName()); + } + + @Override + public Map<String, String> getProperties() { + return config; + } + + @Override + public boolean isScannable() { + return true; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/cdi-se/src/test/resources/META-INF/beans.xml ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/src/test/resources/META-INF/beans.xml b/modules/injection/cdi-se/src/test/resources/META-INF/beans.xml new file mode 100644 index 0000000..adee378 --- /dev/null +++ b/modules/injection/cdi-se/src/test/resources/META-INF/beans.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +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 current 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. +--> +<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://jboss.org/schema/cdi/beans_1_0.xsd"> + +</beans> + http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/cdi-se/src/test/resources/META-INF/javaconfiguration.properties ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/src/test/resources/META-INF/javaconfiguration.properties b/modules/injection/cdi-se/src/test/resources/META-INF/javaconfiguration.properties new file mode 100644 index 0000000..362ce0b --- /dev/null +++ b/modules/injection/cdi-se/src/test/resources/META-INF/javaconfiguration.properties @@ -0,0 +1,20 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# 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. +# +double1=1234.5678 +int2=13 +booleanT=y \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/injection-api/pom.xml ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/pom.xml b/modules/injection/injection-api/pom.xml new file mode 100644 index 0000000..32e300d --- /dev/null +++ b/modules/injection/injection-api/pom.xml @@ -0,0 +1,81 @@ +<!-- +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 current 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.tamaya.ext</groupId> + <artifactId>tamaya-injection-all</artifactId> + <version>0.3-incubating-SNAPSHOT</version> + <relativePath>..</relativePath> + </parent> + <artifactId>tamaya-injection-api</artifactId> + <name>Apache Tamaya Modules - Injection API</name> + <packaging>bundle</packaging> + + <properties> + <jdkVersion>1.7</jdkVersion> + <geronimo-atinject-1.0-spec.version>1.0</geronimo-atinject-1.0-spec.version> + <geronimo-jcdi-1.1-spec.version>1.0</geronimo-jcdi-1.1-spec.version> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.tamaya</groupId> + <artifactId>tamaya-api</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.geronimo.specs</groupId> + <artifactId>geronimo-atinject_1.0_spec</artifactId> + <version>${geronimo-atinject-1.0-spec.version}</version> + <scope>provided</scope> + <optional>true</optional> + </dependency> + <dependency> + <groupId>org.apache.geronimo.specs</groupId> + <artifactId>geronimo-jcdi_1.1_spec</artifactId> + <version>${geronimo-jcdi-1.1-spec.version}</version> + <scope>provided</scope> + <optional>true</optional> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <instructions> + <Export-Package> + org.apache.tamaya.inject.api, + org.apache.tamaya.inject.spi + </Export-Package> + </instructions> + </configuration> + </plugin> + </plugins> + </build> + +</project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/BaseDynamicValue.java ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/BaseDynamicValue.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/BaseDynamicValue.java new file mode 100644 index 0000000..2f8c559 --- /dev/null +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/BaseDynamicValue.java @@ -0,0 +1,125 @@ +/* + * 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.inject.api; + + +import java.io.Serializable; + +/** + * <p>A accessor for a single configured value. This can be used to support values that may change during runtime, + * reconfigured or final. Hereby external code (could be Tamaya configuration listners or client code), can set a + * new value. Depending on the {@link UpdatePolicy} the new value is immedeately active or it requires an active commit + * by client code. Similarly an instance also can ignore all later changes to the value.</p> + * + * <p>Types of this interface can be used as injection targets in injected beans or as template resiult on configuration + * templates.</p> + * + * <h3>Implementation Specification</h3> + * Implementation of this interface must be + * <ul> + * <li>Serializable, when also the item stored is serializable</li> + * <li>Thread safe</li> + * </ul> + * + * @param <T> The type of the value. + */ +public abstract class BaseDynamicValue<T> implements DynamicValue<T>, Serializable { + + private static final long serialVersionUID = 1L; + + /** + * Performs a commit, if necessary, and returns the current value. + * + * @return the non-null value held by this {@code DynamicValue} + * @throws org.apache.tamaya.ConfigException if there is no value present + * @see DynamicValue#isPresent() + */ + public T commitAndGet() { + commit(); + return get(); + } + + /** + * Return {@code true} if there is a value present, otherwise {@code false}. + * + * @return {@code true} if there is a value present, otherwise {@code false} + */ + public boolean isPresent() { + return get() != null; + } + + + /** + * Return the value if present, otherwise return {@code other}. + * + * @param other the value to be returned if there is no value present, may + * be null + * @return the value, if present, otherwise {@code other} + */ + public T orElse(T other) { + T value = get(); + if (value == null) { + return other; + } + return value; + } + + /** + * Return the value if present, otherwise invoke {@code other} and return + * the result of that invocation. + * + * @param other a {@code ConfiguredItemSupplier} whose result is returned if no value + * is present + * @return the value if present otherwise the result of {@code other.get()} + * @throws NullPointerException if value is not present and {@code other} is + * null + */ + public T orElseGet(ConfiguredItemSupplier<? extends T> other) { + T value = get(); + if (value == null) { + return other.get(); + } + return value; + } + + /** + * Return the contained value, if present, otherwise throw an exception + * to be created by the provided supplier. + * <p> + * NOTE A method reference to the exception constructor with an empty + * argument list can be used as the supplier. For example, + * {@code IllegalStateException::new} + * + * @param <X> Type of the exception to be thrown + * @param exceptionSupplier The supplier which will return the exception to + * be thrown + * @return the present value + * @throws X if there is no value present + * @throws NullPointerException if no value is present and + * {@code exceptionSupplier} is null + */ + public <X extends Throwable> T orElseThrow(ConfiguredItemSupplier<? extends X> exceptionSupplier) throws X { + T value = get(); + if (value == null) { + throw exceptionSupplier.get(); + } + return value; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/Config.java ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/Config.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/Config.java new file mode 100644 index 0000000..2484934 --- /dev/null +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/Config.java @@ -0,0 +1,93 @@ +/* + * 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.inject.api; + + +import javax.enterprise.util.Nonbinding; +import javax.inject.Qualifier; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** todo The author of this should fix this invalid Javadoc. Oliver B. Fischer, 2015-12-27 */ +///** +// * Annotation to enable injection current a configured property or define the returned data for +// * a configuration template method. Hereby this annotation can be used in multiple ways and combined +// * with other annotations such as {@link ConfigDefault}, {@link WithConfigOperator}, {@link WithPropertyConverter}. +// * +// * Below the most simple variant current a configured class is given: +// * {@code +// * pubic class ConfiguredItem{ +// * +// * @ConfiguredProperty +// * private String aValue; +// * } +// * When this class is configured, e.g. by passing it to {@link org.apache.tamaya.Configuration#configure(Object)}, +// * the following is happening: +// * <ul> +// * <li>The current valid Configuration is evaluated by calling {@code Configuration cfg = ConfigurationProvider.getConfiguration();}</li> +// * <li>The current possible property keys are evaluated by calling {@code cfg.get("aValue");}</li> +// * <li>if not successful, and a @ConfigDefault annotation is present, the default value is used. +// * <li>If no value could be evaluated a ({@link org.apache.tamaya.ConfigException} is thrown.</li> +// * <li>On success, since no type conversion is involved, the value is injected.</li> +// * </ul> +// * +// * In the next example we explicitly define the property keys: +// * {@code +// * @ConfigDefaultSections("section1") +// * pubic class ConfiguredItem{ +// * +// * @ConfiguredProperty({"b", "[a.b.deprecated.keys]", "a"}) +// * @ConfigDefault("myDefaultValue") +// * private String aValue; +// * } +// * +// * Within this example we evaluate multiple possible keys (section1.b, a.b.deprecated.keys, section1.a). Evaluation is +// * aborted if a key could be successfully resolved. Hereby the ordering current the annotations define the ordering +// * current resolution, so in the example above +// * resolution equals to {@code "section1.b", "a.b.deprecated.keys", "section1.a"}. If no value has bee found, +// * the configured default {@code myDefaultValue} is returned. +// */ +@Qualifier +@Retention(RetentionPolicy.RUNTIME) +@Target(value = { ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER }) +public @interface Config { + + /** + * Get the property names to be used. Hereby the first non null keys evaluated is injected as property keys. + * + * @return the property names, not null. If missing the field or method name being injected is used by default. + */ + @Nonbinding + String[] value() default {}; + + /** + * The default value to be injected, if none of the configuration keys could be resolved. If no key has been + * resolved and no default value is defined, it is handled as a deployment error. Depending on the extension loaded + * default values can be fixed Strings or even themselves resolvable. For typed configuration of type T entries + * that are not Strings the default value must be a valid input to the corresponding + * {@link org.apache.tamaya.spi.PropertyConverter}. + * + * @return default value used in case resolution fails. + */ + @Nonbinding + String defaultValue() default ""; + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/ConfigAutoInject.java ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/ConfigAutoInject.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/ConfigAutoInject.java new file mode 100644 index 0000000..1ed659e --- /dev/null +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/ConfigAutoInject.java @@ -0,0 +1,36 @@ +/* + * 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.inject.api; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation to control injection of a configured bean. The configuration keys + * to be resolved are basically determined by the {@link Config} + * annotation(s). When this annotation is added the injection systems tries to inject all + * fields found, also including fields not annotated with {@code @ConfigProperty}. + * Fields not to be injected ccan be annotated with {@code @NoConfig} to exclude them + * being eleceted for injection. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(value = { ElementType.TYPE }) +public @interface ConfigAutoInject {} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/ConfigDefaultSections.java ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/ConfigDefaultSections.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/ConfigDefaultSections.java new file mode 100644 index 0000000..2037de6 --- /dev/null +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/ConfigDefaultSections.java @@ -0,0 +1,44 @@ +/* + * 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.inject.api; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** todo The author of this should fix this invalid Javadoc. Oliver B. Fischer, 2015-12-27 */ +///** +// * Annotation to control injection and resolution current a configured bean. The configuration keys +// * to be resolved are basically determined by the {@link org.apache.tamaya.inject.ConfigProperty} +// * annotation(s). Nevertheless these annotations can also have relative key names. This annotation allows +// * to define a configuration area that is prefixed to all relative configuration keys within the +// * corresponding class/template interface. +// */ +@Retention(RetentionPolicy.RUNTIME) +@Target(value = { ElementType.TYPE }) +public @interface ConfigDefaultSections { + + /** + * Allows to declare an section names that are prepended to resolve relative configuration keys. + * @return the section names to used for key resolution. + */ + String[] value() default {}; + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/ConfiguredItemSupplier.java ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/ConfiguredItemSupplier.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/ConfiguredItemSupplier.java new file mode 100644 index 0000000..5e57121 --- /dev/null +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/ConfiguredItemSupplier.java @@ -0,0 +1,41 @@ +/* + * 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.inject.api; + +/** + * Represents a supplier of results. + * + * There is no requirement that a new or distinct result be returned each + * time the supplier is invoked. + * + * This is a functional interface, + * whose functional method is {@link #get()}. + * + * @param <T> the type of results supplied by this supplier + */ +//@FunctionalInterface +public interface ConfiguredItemSupplier<T> { + + /** + * Gets a result. + * + * @return a result + */ + T get(); +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/DynamicValue.java ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/DynamicValue.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/DynamicValue.java new file mode 100644 index 0000000..4cc29da --- /dev/null +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/DynamicValue.java @@ -0,0 +1,161 @@ +/* + * 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.inject.api; + +import java.beans.PropertyChangeListener; + + +/** + * <p>A accessor for a single configured value. This can be used to support values that may change during runtime, + * reconfigured or final. Hereby external code (could be Tamaya configuration listners or client code), can set a + * new value. Depending on the {@link UpdatePolicy} the new value is immedeately active or it requires an active commit + * by client code. Similarly an instance also can ignore all later changes to the value.</p> + * + * <p>Types of this interface can be used as injection targets in injected beans or as template resiult on configuration + * templates.</p> + * + * <h3>Implementation Specification</h3> + * Implementation of this interface must be + * <ul> + * <li>Serializable, when also the item stored is serializable</li> + * <li>Thread safe</li> + * </ul> + * + * @param <T> The type of the value. + */ +public interface DynamicValue<T> { + + /** + * Performs a commit, if necessary, and returns the current value. + * + * @return the non-null value held by this {@code DynamicValue} + * @throws org.apache.tamaya.ConfigException if there is no value present + * + * @see DynamicValue#isPresent() + */ + T commitAndGet(); + + /** + * Commits a new value that has not been committed yet, make it the new value of the instance. On change any + * registered listeners will be triggered. + */ + void commit(); + + /** + * Access the {@link UpdatePolicy} used for updating this value. + * @return the update policy, never null. + */ + UpdatePolicy getUpdatePolicy(); + + /** + * Add a listener to be called as weak reference, when this value has been changed. + * @param l the listener, not null + */ + void addListener(PropertyChangeListener l); + + /** + * Removes a listener to be called, when this value has been changed. + * @param l the listner to be removed, not null + */ + void removeListener(PropertyChangeListener l); + + /** + * If a value is present in this {@code DynamicValue}, returns the value, + * otherwise throws {@code ConfigException}. + * + * @return the non-null value held by this {@code Optional} + * @throws org.apache.tamaya.ConfigException if there is no value present + * + * @see DynamicValue#isPresent() + */ + T get(); + + /** + * Method to check for and apply a new value. Depending on the {@link UpdatePolicy} + * the value is immediately or deferred visible (or it may even be ignored completely). + * @return true, if a new value has been detected. The value may not be visible depending on the current + * {@link UpdatePolicy} in place. + */ + boolean updateValue(); + + /** + * Evaluates the current value dynamically from the underlying configuration. + * @return the current actual value, or null. + */ + T evaluateValue(); + + /** + * Sets a new {@link UpdatePolicy}. + * @param updatePolicy the new policy, not null. + */ + void setUpdatePolicy(UpdatePolicy updatePolicy); + + /** + * Access a new value that has not yet been committed. + * @return the uncommitted new value, or null. + */ + T getNewValue(); + + /** + * Return {@code true} if there is a value present, otherwise {@code false}. + * + * @return {@code true} if there is a value present, otherwise {@code false} + */ + boolean isPresent(); + + /** + * Return the value if present, otherwise return {@code other}. + * + * @param other the value to be returned if there is no value present, may + * be null + * @return the value, if present, otherwise {@code other} + */ + T orElse(T other); + + /** + * Return the value if present, otherwise invoke {@code other} and return + * the result of that invocation. + * + * @param other a {@code ConfiguredItemSupplier} whose result is returned if no value + * is present + * @return the value if present otherwise the result of {@code other.get()} + * @throws NullPointerException if value is not present and {@code other} is + * null + */ + T orElseGet(ConfiguredItemSupplier<? extends T> other); + + /** + * Return the contained value, if present, otherwise throw an exception + * to be created by the provided supplier. + * + * NOTE A method reference to the exception constructor with an empty + * argument list can be used as the supplier. For example, + * {@code IllegalStateException::new} + * + * @param <X> Type of the exception to be thrown + * @param exceptionSupplier The supplier which will return the exception to + * be thrown + * @return the present value + * @throws X if there is no value present + * @throws NullPointerException if no value is present and + * {@code exceptionSupplier} is null + */ + <X extends Throwable> T orElseThrow(ConfiguredItemSupplier<? extends X> exceptionSupplier) throws X; + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/InjectionUtils.java ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/InjectionUtils.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/InjectionUtils.java new file mode 100644 index 0000000..a010e96 --- /dev/null +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/InjectionUtils.java @@ -0,0 +1,127 @@ +/* + * 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.inject.api; + +import java.lang.reflect.Field; +import java.lang.reflect.Member; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.ListIterator; + +/** + * Utility class with several commonly used functions. + */ +public final class InjectionUtils { + + private InjectionUtils(){} + + + /** + * Collects all keys to be be accessed as defined by any annotations of type + * {@link ConfigDefaultSections}, {@link Config}. + * @param field the (optionally) annotated field instance + * @return the regarding key list to be accessed fomr the {@link org.apache.tamaya.Configuration}. + */ + public static List<String> getKeys(Field field) { + ConfigDefaultSections areasAnnot = field.getDeclaringClass().getAnnotation(ConfigDefaultSections.class); + return InjectionUtils.evaluateKeys(field, areasAnnot, field.getAnnotation(Config.class)); + } + + /** + * Collects all keys to be be accessed as defined by any annotations of type + * {@link ConfigDefaultSections}, {@link Config}. + * @param method the (optionally) annotated method instance + * @return the regarding key list to be accessed fomr the {@link org.apache.tamaya.Configuration}. + */ + public static List<String> getKeys(Method method) { + ConfigDefaultSections areasAnnot = method.getDeclaringClass().getAnnotation(ConfigDefaultSections.class); + return InjectionUtils.evaluateKeys(method, areasAnnot, method.getAnnotation(Config.class)); + } + + /** + * Evaluates all absolute configuration keys based on the member name found. + * + * @param member member to analyze. + * @param areasAnnot the (optional) annotation defining areas to be looked up. + * @return the list of current keys in order how they should be processed/looked up. + */ + public static List<String> evaluateKeys(Member member, ConfigDefaultSections areasAnnot) { + List<String> keys = new ArrayList<>(); + String name = member.getName(); + String mainKey; + if (name.startsWith("get") || name.startsWith("set")) { + mainKey = Character.toLowerCase(name.charAt(3)) + name.substring(4); + } else { + mainKey = Character.toLowerCase(name.charAt(0)) + name.substring(1); + } + keys.add(mainKey); + if (areasAnnot != null) { + // Add prefixed entries, including absolute (root) entry for "" area keys. + for (String area : areasAnnot.value()) { + if (!area.isEmpty()) { + keys.add(area + '.' + mainKey); + } + } + } else { // add package name + keys.add(member.getDeclaringClass().getName() + '.' + mainKey); + keys.add(member.getDeclaringClass().getSimpleName() + '.' + mainKey); + } + return keys; + } + + /** + * Evaluates all absolute configuration keys based on the annotations found in a class. + * + * @param member member to analyze. + * @param areasAnnot the (optional) annotation definining areas to be looked up. + * @param propertyAnnotation the annotation on field/method level that may defined one or + * several keys to be looked up (in absolute or relative form). + * @return the list current keys in order how they should be processed/looked up. + */ + public static List<String> evaluateKeys(Member member, ConfigDefaultSections areasAnnot, Config propertyAnnotation) { + if(propertyAnnotation==null){ + return evaluateKeys(member, areasAnnot); + } + List<String> keys = new ArrayList<>(Arrays.asList(propertyAnnotation.value())); + if (keys.isEmpty()) { + keys.add(member.getName()); + } + ListIterator<String> iterator = keys.listIterator(); + while (iterator.hasNext()) { + String next = iterator.next(); + if (next.startsWith("[") && next.endsWith("]")) { + // absolute key, strip away brackets, take key as is + iterator.set(next.substring(1, next.length() - 1)); + } else { + if (areasAnnot != null && areasAnnot.value().length>0) { + // Remove original entry, since it will be replaced with prefixed entries + iterator.remove(); + // Add prefixed entries, including absolute (root) entry for "" area keys. + for (String area : areasAnnot.value()) { + iterator.add(area.isEmpty() ? next : area + '.' + next); + } + } + } + } + return keys; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/LoadPolicy.java ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/LoadPolicy.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/LoadPolicy.java new file mode 100644 index 0000000..b9540fd --- /dev/null +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/LoadPolicy.java @@ -0,0 +1,42 @@ +/* + * 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.inject.api; + +/** + * Available policies that describe how changes affecting configured values are published/reinjected + * for a {@link DynamicValue}. + * The policy also affects the cases were any configured listeners/listener methods are called for + * propagation current configuration changes. + */ +public enum LoadPolicy { + /** + * The configuration keys is evaluated once, when the owning component is loaded/configured, but never updated later. + */ + INITIAL, + /** + * The configuration keys is evaluated exactly once on its first access/use lazily, but never updated later. + * @see DynamicValue#get() + * @see DynamicValue#commitAndGet() + */ + LAZY, + /** + * The configuration value is evaluated evertime it is accessed. + */ + ALWAYS +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/NoConfig.java ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/NoConfig.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/NoConfig.java new file mode 100644 index 0000000..c5234d3 --- /dev/null +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/NoConfig.java @@ -0,0 +1,36 @@ +/* + * 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.inject.api; + + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * This is a small marker annotations to inform Tamaya that the annotated element should never be injected with + * configured data. This is useful because by default Tamaya tries to lookup and inject configuration also by + * using property or method names without annotations. With that annotation none of these will be happen. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(value = { ElementType.FIELD, ElementType.METHOD }) +public @interface NoConfig { + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/UpdatePolicy.java ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/UpdatePolicy.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/UpdatePolicy.java new file mode 100644 index 0000000..231b9b9 --- /dev/null +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/UpdatePolicy.java @@ -0,0 +1,40 @@ +/* + * 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.inject.api; + +/** + * Policy to control how new values are applied to a {@link DynamicValue}. + */ +public enum UpdatePolicy { + /** New values are applied immedately and registered listeners are informed about the change. */ + IMMEDEATE, + /** New values or not applied, but stored in the newValue property. Explcit call to DynamicValue#commit + of DynamicValue#commitAndGet are required to accept the change and inform the listeners about the change. + * Registered listeners will be informed, when the commit was performed explicitly. + */ + EXPLCIT, + /** + * New values are always immedately discarded, listeners are not triggered. + */ + NEVER, + /** + * All listeners are informed about the change encountered, but the value will not be applied. + */ + LOG_ONLY +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/WithConfigOperator.java ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/WithConfigOperator.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/WithConfigOperator.java new file mode 100644 index 0000000..6630e53 --- /dev/null +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/WithConfigOperator.java @@ -0,0 +1,45 @@ +/* + * 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.inject.api; + +import org.apache.tamaya.ConfigOperator; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation to define an configuration operator to be used before accessing a configured key. + * This allows filtering the current configuration, e.g. to realize views or ensure security + * constraints. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(value = {ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER}) +public @interface WithConfigOperator { + + /** + * Define a custom adapter that should be used to adapt the configuration entry injected. This overrides any + * general org.apache.tamaya.core.internal registered. If no adapter is defined (default) and no corresponding adapter is + * registered, it is handled as a deployment error. + * @return adapter used to transform the configuration entry. + */ + Class<? extends ConfigOperator> value(); + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/WithPropertyConverter.java ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/WithPropertyConverter.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/WithPropertyConverter.java new file mode 100644 index 0000000..499360c --- /dev/null +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/WithPropertyConverter.java @@ -0,0 +1,46 @@ +/* + * 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.inject.api; + + +import org.apache.tamaya.spi.PropertyConverter; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation to define a type adapter to be used before injecting a configured key, or for applying changes. + * This will override any other adapter for performing the type conversion before + * injecting the field keys. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(value = {ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER}) +public @interface WithPropertyConverter { + + /** + * Define a custom adapter or codec that should be used to adapt the configuration entry injected. This overrides any + * general org.apache.tamaya.core.internal registered. If no adapter is defined (default) and no corresponding adapter is + * registered, it is handled as a deployment error. + * @return adapter used to change the configuration entry. + */ + Class<? extends PropertyConverter<?>> value(); + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/package-info.java ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/package-info.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/package-info.java new file mode 100644 index 0000000..b5d8bc3 --- /dev/null +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/package-info.java @@ -0,0 +1,22 @@ +/* + * 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. + */ +/** + * Common njection API. + */ +package org.apache.tamaya.inject.api; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/ConfiguredField.java ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/ConfiguredField.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/ConfiguredField.java new file mode 100644 index 0000000..94c0091 --- /dev/null +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/ConfiguredField.java @@ -0,0 +1,68 @@ +/* + * 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.inject.spi; + +import org.apache.tamaya.Configuration; + +import java.lang.reflect.Field; +import java.util.Collection; + +/** + * Abstract model of an field used to inject configuration. + */ +public interface ConfiguredField { + + /** + * Get the field's type. + * @return the field type, never null. + */ + Class<?> getType(); + + /** + * Get a list of all target keys for the given field. The first resolvable key normally determines the + * configuration value injected. + * @return a list of evaluated keys. + */ + Collection<String> getConfiguredKeys(); + + /** + * Get the field's name. + * @return the name, not null. + */ + String getName(); + + /** + * Get the field's full signature. + * @return the signature, not null. + */ + String getSignature(); + + /** + * Get the annotated field. + * @return the field, not null. + */ + Field getAnnotatedField(); + + /** + * Actually calls the annotated method on the instance, hereby passing the configuration values evaluated. + * @param instance the instance, not null. + * @param config the configuration, not null. + */ + void configure(Object instance, Configuration config); +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/ConfiguredMethod.java ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/ConfiguredMethod.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/ConfiguredMethod.java new file mode 100644 index 0000000..128946e --- /dev/null +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/ConfiguredMethod.java @@ -0,0 +1,70 @@ +/* + * 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.inject.spi; + +import org.apache.tamaya.Configuration; + +import java.lang.reflect.Method; +import java.util.Collection; + +/** + * Abstract model of an method used to inject configuration. + */ +public interface ConfiguredMethod { + + /** + * Get the key required to be evaluated. + * @return the configured keys. + */ + Collection<String> getConfiguredKeys(); + + /** + * Get the methods input parameter types. + * @return the method param types, not null. + */ + Class<?>[] getParameterTypes(); + + /** + * Get the underlying method reflection type. + * @return the method element. + */ + Method getAnnotatedMethod(); + + /** + * Get the method's name, e.g. {@code setName}. + * @return the name, never null. + */ + String getName(); + + /** + * Get the methods signature, e.g. {@code void setName(String)}. + * @return he signature, never null. + */ + String getSignature(); + + /** + * This method actually configures the given method on a instance of its parent type. + * This evaluates the initial key closure and applies changes on the field. + * + * @param instance the target instance, not null. + * @param config the configuration, not null. + * @throws org.apache.tamaya.ConfigException if evaluation or conversion failed. + */ + void configure(Object instance, Configuration config); +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/ConfiguredType.java ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/ConfiguredType.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/ConfiguredType.java new file mode 100644 index 0000000..0f81dc7 --- /dev/null +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/ConfiguredType.java @@ -0,0 +1,63 @@ +/* + * 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.inject.spi; + +import org.apache.tamaya.Configuration; + +import java.util.Collection; + +/** + * Abstract model of an type used to inject configuration. This also includes instances passed programmatically. + */ +public interface ConfiguredType{ + + /** + * Get the type's class. + * @return the base type. + */ + Class getType(); + + /** + * Get the type's name. + * @return the type's name. + */ + String getName(); + + /** + * Get the registered configured fields. + * @return the registered configured fields, never null. + */ + Collection<ConfiguredField> getConfiguredFields(); + + /** + * Get the registered configured methods. + * @return the registered configured methods, never null. + */ + Collection<ConfiguredMethod> getConfiguredMethods(); + + /** + * This method actually configures an instance using the given configuration data. + * + * @param instance The instance to be configured, not null. + * @param config the target config, not null. + */ + void configure(Object instance, Configuration config); + + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/pom.xml ---------------------------------------------------------------------- diff --git a/modules/injection/pom.xml b/modules/injection/pom.xml index 16809b7..88b8f4b 100644 --- a/modules/injection/pom.xml +++ b/modules/injection/pom.xml @@ -1,24 +1,22 @@ -<!-- -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 current 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. ---> -<project xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> +<!-- + ~ 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. + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> @@ -27,78 +25,15 @@ under the License. <version>0.3-incubating-SNAPSHOT</version> <relativePath>..</relativePath> </parent> - <artifactId>tamaya-injection</artifactId> - <name>Apache Tamaya Injection Support</name> - <packaging>bundle</packaging> - - <properties> - <jdkVersion>1.7</jdkVersion> - </properties> - - <dependencies> - <dependency> - <groupId>org.apache.tamaya</groupId> - <artifactId>tamaya-api</artifactId> - <version>${project.version}</version> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>org.apache.tamaya.ext</groupId> - <artifactId>tamaya-injection-api</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.apache.tamaya</groupId> - <artifactId>tamaya-core</artifactId> - <version>${project.version}</version> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>org.apache.tamaya.ext</groupId> - <artifactId>tamaya-resolver</artifactId> - <version>${project.version}</version> - <optional>true</optional> - </dependency> - <dependency> - <groupId>org.apache.tamaya.ext</groupId> - <artifactId>tamaya-builder</artifactId> - <version>${project.version}</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.tamaya.ext</groupId> - <artifactId>tamaya-events</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.hamcrest</groupId> - <artifactId>java-hamcrest</artifactId> - </dependency> - - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - </dependency> - </dependencies> - - <build> - <plugins> - <plugin> - <groupId>org.apache.felix</groupId> - <artifactId>maven-bundle-plugin</artifactId> - <extensions>true</extensions> - <configuration> - <instructions> - <Export-Package> - org.apache.tamaya.inject - </Export-Package> - <Private-Package> - org.apache.tamaya.inject.internal - </Private-Package> - </instructions> - </configuration> - </plugin> - </plugins> - </build> + <artifactId>tamaya-injection-all</artifactId> + <name>Apache Tamaya Modules - Injection Parent</name> + <packaging>pom</packaging> + + <modules> + <module>injection-api</module> + <module>standalone</module> + <module>cdi-se</module> + <module>cdi-ee</module> + </modules> </project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/src/main/java/org/apache/tamaya/inject/ConfigurationInjection.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/ConfigurationInjection.java b/modules/injection/src/main/java/org/apache/tamaya/inject/ConfigurationInjection.java deleted file mode 100644 index 79d6218..0000000 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/ConfigurationInjection.java +++ /dev/null @@ -1,42 +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.inject; - -import org.apache.tamaya.spi.ServiceContextManager; - -/** - * Singleton accessor class for accessing {@link ConfigurationInjector} instances. - */ -public final class ConfigurationInjection { - - /** - * Singleton constructor. - */ - private ConfigurationInjection() { - } - - /** - * Get the current injector instance. - * - * @return the current injector, not null. - */ - public static ConfigurationInjector getConfigurationInjector() { - return ServiceContextManager.getServiceContext().getService(ConfigurationInjector.class); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/src/main/java/org/apache/tamaya/inject/ConfigurationInjector.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/ConfigurationInjector.java b/modules/injection/src/main/java/org/apache/tamaya/inject/ConfigurationInjector.java deleted file mode 100644 index 563ae47..0000000 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/ConfigurationInjector.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.inject; - - -import org.apache.tamaya.Configuration; -import org.apache.tamaya.inject.api.ConfiguredItemSupplier; - -/** - * Accessor interface for injection of configuration and configuration templates. - */ -public interface ConfigurationInjector { - - /** - * Configures the current instance and registers necessary listener to forward config change events as - * defined by the current annotations in place. - * - * Unannotated types are ignored. - * - * @param <T> the type of the instance. - * @param instance the instance to be configured - * @return the configured instance (allows chaining of operations). - */ - <T> T configure(T instance); - - /** - * Configures the current instance and registers necessary listener to forward config change events as - * defined by the current annotations in place. - * - * Unannotated types are ignored. - * - * @param <T> the type of the instance. - * @param instance the instance to be configured - * @param config the configuration to be used for injection. - * @return the configured instance (allows chaining of operations). - */ - <T> T configure(T instance, Configuration config); - - /** - * Creates a template implementing the annotated methods based on current configuration data. - * - * @param <T> the type of the template. - * @param templateType the type of the template to be created. - * @return the configured template. - */ - <T> T createTemplate(Class<T> templateType); - - /** - * Creates a template implementting the annotated methods based on current configuration data. - * - * @param <T> the type of the template. - * @param config the configuration to be used for backing the template. - * @param templateType the type of the template to be created. - * @return the configured template. - */ - <T> T createTemplate(Class<T> templateType, Configuration config); - - - /** - * Creates a supplier for configured instances of the given type {@code T}. - * - * @param supplier the supplier to create new instances. - * @param <T> the target type. - * @return a supplier creating configured instances of {@code T}. - */ - <T> ConfiguredItemSupplier<T> getConfiguredSupplier(ConfiguredItemSupplier<T> supplier); - - /** - * Creates a supplier for configured instances of the given type {@code T}. - * - * @param supplier the supplier to create new instances. - * @param config the configuration to be used for backing the supplier. - * @param <T> the target type. - * @return a supplier creating configured instances of {@code T}. - */ - <T> ConfiguredItemSupplier<T> getConfiguredSupplier(ConfiguredItemSupplier<T> supplier, Configuration config); - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfigTemplateInvocationHandler.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfigTemplateInvocationHandler.java b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfigTemplateInvocationHandler.java deleted file mode 100644 index 5d634e1..0000000 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfigTemplateInvocationHandler.java +++ /dev/null @@ -1,73 +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.inject.internal; - -import org.apache.tamaya.Configuration; -import org.apache.tamaya.ConfigurationProvider; -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.inject.api.DynamicValue; -import org.apache.tamaya.inject.spi.ConfiguredType; - -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Method; -import java.util.Objects; - -/** - * Invocation handler that handles request against a configuration template. - */ -public final class ConfigTemplateInvocationHandler implements InvocationHandler { - - /** - * The configured type. - */ - private final ConfiguredType type; - - /** - * Creates a new handler instance. - * - * @param type the target type, not null. - */ - public ConfigTemplateInvocationHandler(Class<?> type) { - this.type = new ConfiguredTypeImpl(Objects.requireNonNull(type)); - if (!type.isInterface()) { - throw new IllegalArgumentException("Can only proxy interfaces as configuration templates."); - } - InjectionHelper.sendConfigurationEvent(this.type); - } - - @Override - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - Configuration config = ConfigurationProvider.getConfiguration(); - if ("toString".equals(method.getName())) { - return "Configured Proxy -> " + this.type.getType().getName(); - } else if ("hashCode".equals(method.getName())) { - return Objects.hashCode(proxy); - } else if ("equals".equals(method.getName())) { - return Objects.equals(proxy, args[0]); - } else if ("get".equals(method.getName())) { - return config; - } - if (method.getReturnType() == DynamicValue.class) { - return DefaultDynamicValue.of(method, config); - } - String[] retKey = new String[1]; - String configValue = InjectionHelper.getConfigValue(method, retKey, config); - return InjectionHelper.adaptValue(method, TypeLiteral.of(method.getReturnType()), retKey[0], configValue); - } -}