Repository: incubator-tamaya-extensions Updated Branches: refs/heads/master 02cf6e69d -> ab204aa02
TAMAYA-193: Moved spi classes into spi package and ensured build is running. Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/commit/009b7fd6 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/009b7fd6 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/009b7fd6 Branch: refs/heads/master Commit: 009b7fd6efd6eb225f3c6eaf672f947059fabc9d Parents: 02cf6e6 Author: anatole <[email protected]> Authored: Fri Nov 11 16:27:11 2016 +0100 Committer: anatole <[email protected]> Committed: Fri Nov 11 16:27:11 2016 +0100 ---------------------------------------------------------------------- modules/injection/cdi-ee/pom.xml | 1 - .../integration/cdi/DefaultDynamicValue.java | 4 +- modules/injection/cdi-se/pom.xml | 7 - .../tamaya/inject/api/BaseDynamicValue.java | 125 ------------------ .../org/apache/tamaya/inject/api/Config.java | 118 ++++++++++------- .../tamaya/inject/api/ConfigAutoInject.java | 29 ++++- .../inject/api/ConfigDefaultSections.java | 17 +-- .../tamaya/inject/api/InjectionUtils.java | 127 ------------------ .../tamaya/inject/spi/BaseDynamicValue.java | 127 ++++++++++++++++++ .../tamaya/inject/spi/InjectionUtils.java | 129 +++++++++++++++++++ modules/injection/standalone/pom.xml | 43 +++---- .../inject/internal/ConfiguredFieldImpl.java | 2 +- .../inject/internal/ConfiguredSetterMethod.java | 2 +- .../inject/internal/DefaultDynamicValue.java | 4 +- .../tamaya/inject/internal/InjectionHelper.java | 2 +- pom.xml | 8 ++ 16 files changed, 394 insertions(+), 351 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/009b7fd6/modules/injection/cdi-ee/pom.xml ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-ee/pom.xml b/modules/injection/cdi-ee/pom.xml index 4804738..8e3e3d3 100644 --- a/modules/injection/cdi-ee/pom.xml +++ b/modules/injection/cdi-ee/pom.xml @@ -27,7 +27,6 @@ under the License. </parent> <artifactId>tamaya-cdi</artifactId> - <groupId>tamaya-integration</groupId> <name>Apache Tamaya Modules - Injection CDI</name> <packaging>bundle</packaging> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/009b7fd6/modules/injection/cdi-ee/src/main/java/org/apache/tamaya/integration/cdi/DefaultDynamicValue.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-ee/src/main/java/org/apache/tamaya/integration/cdi/DefaultDynamicValue.java b/modules/injection/cdi-ee/src/main/java/org/apache/tamaya/integration/cdi/DefaultDynamicValue.java index 98ee05a..7015598 100644 --- a/modules/injection/cdi-ee/src/main/java/org/apache/tamaya/integration/cdi/DefaultDynamicValue.java +++ b/modules/injection/cdi-ee/src/main/java/org/apache/tamaya/integration/cdi/DefaultDynamicValue.java @@ -21,9 +21,9 @@ package org.apache.tamaya.integration.cdi; import org.apache.tamaya.ConfigException; import org.apache.tamaya.Configuration; import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.inject.api.BaseDynamicValue; +import org.apache.tamaya.inject.spi.BaseDynamicValue; import org.apache.tamaya.inject.api.DynamicValue; -import org.apache.tamaya.inject.api.InjectionUtils; +import org.apache.tamaya.inject.spi.InjectionUtils; import org.apache.tamaya.inject.api.LoadPolicy; import org.apache.tamaya.inject.api.UpdatePolicy; import org.apache.tamaya.inject.api.WithPropertyConverter; http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/009b7fd6/modules/injection/cdi-se/pom.xml ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/pom.xml b/modules/injection/cdi-se/pom.xml index 36b1b77..0f2e93a 100644 --- a/modules/injection/cdi-se/pom.xml +++ b/modules/injection/cdi-se/pom.xml @@ -27,7 +27,6 @@ under the License. </parent> <artifactId>tamaya-cdi-se</artifactId> - <groupId>tamaya-integration</groupId> <name>Apache Tamaya Modules - Injection CDI (Direct)</name> <packaging>bundle</packaging> @@ -83,12 +82,6 @@ under the License. <artifactId>java-hamcrest</artifactId> <scope>test</scope> </dependency> - - <dependency> - <groupId>org.apache.tamaya.ext</groupId> - <artifactId>tamaya-classloader-support</artifactId> - <version>${project.version}</version> - </dependency> <dependency> <groupId>org.apache.tamaya.ext</groupId> <artifactId>tamaya-spisupport</artifactId> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/009b7fd6/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 deleted file mode 100644 index 2f8c559..0000000 --- a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/BaseDynamicValue.java +++ /dev/null @@ -1,125 +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.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/009b7fd6/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 index 2484934..c182cb9 100644 --- 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 @@ -26,63 +26,85 @@ 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. -// */ +/** + * Annotation to define injection of a configured property or define the configuration data + * backing a configuration template method. Hereby this annotation can be used in multiple + * ways and combined with other annotations such as {@link WithConfigOperator}, {@link WithPropertyConverter}. + * + * <h3>Simplest variant</h3> + * Below the most simple variant of a configured class is given: + * {@code + * package a.b; + * + * pubic class ConfiguredItem{ + * + * @Config + * private String aValue; + * } + * } + * Configuration resolution is implemented as follows: + * <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("a.b.ConfigureItem.aValue");}, + * {@code cfg.get("ConfigureItem.aValue");}, {@code cfg.get("aValue");}</li> + * <li>if not successful, and since no @ConfigDefault annotation is present, the configured 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> + * + * <h3>Explicit annotations</h3> + * In the next example we explicitly define the configuration keys to be used: + * {@code + * @ConfigDefaultSections("section1") + * pubic class ConfiguredItem{ + * + * @Config({"b", "[a.b.deprecated.keys]", "a"}) + * @ConfigDefault("myDefaultValue") + * private String aValue; + * } + * + * Within this example we evaluate multiple possible keys: {@code section1.b, a.b.deprecated.keys, section1.a}. + * Evaluation is aborted if a key could be successfully resolved. Hereby the ordering of the annotation values + * define the ordering of resolution. If no value could be found, the configured default {@code myDefaultValue} is + * injected. + * + * <h3>Using explicit field annotation only</h3> + * In the last example we explicitly define the configuration keys but omit the section part, letting the default + * section names to be taken: + * {@code + * package a.b; + * + * pubic class ConfiguredItem{ + * + * @Config({"b", "[a.b.deprecated.keys]", "a"}) + * @ConfigDefault("myDefaultValue") + * private String aValue; + * } + * + * Key resolution is similar to above, but now the default package names are used, resulting in + * {@code a.b.ConfiguredItem.b, ConfiguredItem.b, a.b.deprecated.keys, a.b.ConfiguredItem.a, ConfiguredItem.a} + * being evaluated. + */ @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. + * Defines the configuration property keys to be used. Hereby the first non null value evaluated is injected as + * property value. * - * @return the property names, not null. If missing the field or method name being injected is used by default. + * @return the property keys, not null. If empty, the field or property name (of a setter method) 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 + * resolved and no default value is defined, it is, by default, 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 a corresponding * {@link org.apache.tamaya.spi.PropertyConverter}. * * @return default value used in case resolution fails. @@ -90,4 +112,12 @@ public @interface Config { @Nonbinding String defaultValue() default ""; + /** + * FLag that defines if a configuration property is required. If a required + * property is missing, a {@link org.apache.tamaya.ConfigException} is raised. + * Default is {@code true}. + * @return the flag value. + */ + @Nonbinding + boolean required() default true; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/009b7fd6/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 index 1ed659e..b8e9b0c 100644 --- 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 @@ -24,13 +24,28 @@ 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. + * Adding this annotation tells the Tamaya injection system to inject all + * fields found, also including fields not annotated with {@code @Config}. + * The configuration keys to be used for resolution are basically determined + * by the {@link Config} annotation(s). If missing the key are evaluated in the + * following order: + * <ul> + * <li>packagename.simpleClassname.fieldName</li> + * <li>simpleClassname.fieldName</li> + * <li>fieldName</li> + * </ul> + * Fields not to be injected can be annotated with {@code @NoConfig} to exclude + * them being eleceted for injection. + * @see Config */ @Retention(RetentionPolicy.RUNTIME) @Target(value = { ElementType.TYPE }) -public @interface ConfigAutoInject {} +public @interface ConfigAutoInject { + + /** + * Flag that tells the injection system if {@link org.apache.tamaya.ConfigException} should + * ne thrown when a property could not be resolved. Default is {@code false}. + * @return + */ + boolean failIfMissing() default false; +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/009b7fd6/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 index 2037de6..2ad941a 100644 --- 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 @@ -23,14 +23,15 @@ 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. -// */ +/** + * This annotation allows to define the default section names used for evaluating the candidate + * keys used for evaluating configuration values. The configuration keys + * used are additionally determined by the {@link org.apache.tamaya.inject.api.Config} + * annotation(s). This annotation allows + * to define the configuration section that is prefixed to all <b>relative</b> configuration keys. + * @see Config + * @see ConfigAutoInject + */ @Retention(RetentionPolicy.RUNTIME) @Target(value = { ElementType.TYPE }) public @interface ConfigDefaultSections { http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/009b7fd6/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 deleted file mode 100644 index a010e96..0000000 --- a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/InjectionUtils.java +++ /dev/null @@ -1,127 +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.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/009b7fd6/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/BaseDynamicValue.java ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/BaseDynamicValue.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/BaseDynamicValue.java new file mode 100644 index 0000000..38703b2 --- /dev/null +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/BaseDynamicValue.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.spi; + + +import org.apache.tamaya.inject.api.ConfiguredItemSupplier; +import org.apache.tamaya.inject.api.DynamicValue; + +import java.io.Serializable; + +/** + * Basic abstract implementation skeleton for a {@link DynamicValue}. This can be used to support values that may + * change during runtime. Hereby external code (could be Tamaya configuration listners or client + * code), can apply a new value. Depending on the {@link org.apache.tamaya.inject.api.UpdatePolicy} the new value is applied immedeately, when the + * change has been identified, or it requires an programmatic commit by client code to + * activate the change in the {@link DynamicValue}. Similarly an instance also can ignore all + * later changes to the value.</p> + * + * <h3>Implementation Specification</h3> + * This class is + * <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/009b7fd6/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/InjectionUtils.java ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/InjectionUtils.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/InjectionUtils.java new file mode 100644 index 0000000..a26a209 --- /dev/null +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/InjectionUtils.java @@ -0,0 +1,129 @@ +/* + * 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.inject.api.Config; +import org.apache.tamaya.inject.api.ConfigDefaultSections; + +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; + +/** + * 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 sectionAnnot 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 sectionAnnot) { + List<String> keys = new ArrayList<>(); + List<String> areaKeys = evaluateSectionKeys(member, sectionAnnot); + String key = null; + String name = member.getName(); + if (name.startsWith("get") || name.startsWith("set")) { + key = Character.toLowerCase(name.charAt(3)) + name.substring(4); + } else { + key = Character.toLowerCase(name.charAt(0)) + name.substring(1); + } + for(String areaKey:areaKeys) { + keys.add(areaKey + '.' + key); + } + 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> result = new ArrayList<>(); + List<String> memberKeys = new ArrayList<>(Arrays.asList(propertyAnnotation.value())); + if (memberKeys.isEmpty()) { + memberKeys.add(member.getName()); + } + List<String> areaKeys = evaluateSectionKeys(member, areasAnnot); + for(String memberKey:memberKeys){ + if (memberKey.startsWith("[") && memberKey.endsWith("]")) { + // absolute key, strip away brackets, take key as is + result.add(memberKey); + }else{ + for(String areaKey:areaKeys) { + result.add(areaKey + '.' + memberKey); + } + result.add(memberKey); + } + } + return result; + } + + private static List<String> evaluateSectionKeys(Member member, ConfigDefaultSections sectionAnnot) { + List<String> areaKeys = new ArrayList<>(); + if (sectionAnnot != null && sectionAnnot.value().length>0) { + // Remove original entry, since it will be replaced with prefixed entries + areaKeys.addAll(Arrays.asList(sectionAnnot.value())); + }else{ + areaKeys.add(member.getDeclaringClass().getName()); + areaKeys.add(member.getDeclaringClass().getSimpleName()); + } + return areaKeys; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/009b7fd6/modules/injection/standalone/pom.xml ---------------------------------------------------------------------- diff --git a/modules/injection/standalone/pom.xml b/modules/injection/standalone/pom.xml index 5d5c975..7732b9b 100644 --- a/modules/injection/standalone/pom.xml +++ b/modules/injection/standalone/pom.xml @@ -1,21 +1,21 @@ <!-- - ~ 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. - --> +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"> @@ -28,7 +28,7 @@ <relativePath>..</relativePath> </parent> <artifactId>tamaya-injection</artifactId> - <name>Apache Tamaya odules - Injection Standalone</name> + <name>Apache Tamaya Modules - Injection Standalone</name> <packaging>bundle</packaging> <properties> @@ -61,12 +61,6 @@ </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> @@ -74,7 +68,6 @@ <groupId>org.hamcrest</groupId> <artifactId>java-hamcrest</artifactId> </dependency> - <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/009b7fd6/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/ConfiguredFieldImpl.java ---------------------------------------------------------------------- diff --git a/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/ConfiguredFieldImpl.java b/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/ConfiguredFieldImpl.java index 64b0c95..7389f83 100644 --- a/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/ConfiguredFieldImpl.java +++ b/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/ConfiguredFieldImpl.java @@ -23,7 +23,7 @@ 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.api.InjectionUtils; +import org.apache.tamaya.inject.spi.InjectionUtils; import org.apache.tamaya.inject.spi.ConfiguredField; import java.lang.reflect.Field; http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/009b7fd6/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/ConfiguredSetterMethod.java ---------------------------------------------------------------------- diff --git a/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/ConfiguredSetterMethod.java b/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/ConfiguredSetterMethod.java index b69df20..1d66ca2 100644 --- a/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/ConfiguredSetterMethod.java +++ b/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/ConfiguredSetterMethod.java @@ -21,7 +21,7 @@ package org.apache.tamaya.inject.internal; import org.apache.tamaya.ConfigException; import org.apache.tamaya.Configuration; import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.inject.api.InjectionUtils; +import org.apache.tamaya.inject.spi.InjectionUtils; import org.apache.tamaya.inject.spi.ConfiguredMethod; import java.lang.reflect.Method; http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/009b7fd6/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/DefaultDynamicValue.java ---------------------------------------------------------------------- diff --git a/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/DefaultDynamicValue.java b/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/DefaultDynamicValue.java index 2f051b3..0c7bed4 100644 --- a/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/DefaultDynamicValue.java +++ b/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/DefaultDynamicValue.java @@ -21,12 +21,12 @@ package org.apache.tamaya.inject.internal; import org.apache.tamaya.ConfigException; import org.apache.tamaya.Configuration; import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.inject.api.BaseDynamicValue; import org.apache.tamaya.inject.api.DynamicValue; -import org.apache.tamaya.inject.api.InjectionUtils; +import org.apache.tamaya.inject.spi.InjectionUtils; 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.spi.BaseDynamicValue; import org.apache.tamaya.spi.ConversionContext; import org.apache.tamaya.spi.PropertyConverter; http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/009b7fd6/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/InjectionHelper.java ---------------------------------------------------------------------- diff --git a/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/InjectionHelper.java b/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/InjectionHelper.java index 305a660..f34393d 100644 --- a/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/InjectionHelper.java +++ b/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/InjectionHelper.java @@ -34,7 +34,7 @@ import org.apache.tamaya.events.ConfigEventManager; import org.apache.tamaya.events.spi.BaseConfigEvent; import org.apache.tamaya.inject.api.Config; import org.apache.tamaya.inject.api.ConfigDefaultSections; -import org.apache.tamaya.inject.api.InjectionUtils; +import org.apache.tamaya.inject.spi.InjectionUtils; import org.apache.tamaya.inject.api.WithPropertyConverter; import org.apache.tamaya.inject.spi.ConfiguredType; import org.apache.tamaya.resolver.spi.ExpressionEvaluator; http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/009b7fd6/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 408ddf5..c3e9a69 100644 --- a/pom.xml +++ b/pom.xml @@ -759,6 +759,14 @@ under the License. <preparationGoals>clean install</preparationGoals> </configuration> </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>1.7</source> + <target>1.7</target> + </configuration> + </plugin> </plugins> </build>
