Repository: incubator-tamaya Updated Branches: refs/heads/master 4ddf46f74 -> 987186e18
Removed currently unused code. Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/987186e1 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/987186e1 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/987186e1 Branch: refs/heads/master Commit: 987186e1887a602d852f78faf92056a861859b4b Parents: 4ddf46f Author: anatole <anat...@apache.org> Authored: Tue Feb 17 00:44:42 2015 +0100 Committer: anatole <anat...@apache.org> Committed: Tue Feb 17 00:44:42 2015 +0100 ---------------------------------------------------------------------- .../ConfigTemplateInvocationHandler.java | 34 +-------- .../tamaya/inject/internal/ConfiguredField.java | 74 +++----------------- .../inject/internal/ConfiguredSetterMethod.java | 46 +----------- .../tamaya/inject/internal/ConfiguredType.java | 10 +-- .../inject/internal/DefaultDynamicValue.java | 70 +++++++++++++++++- .../tamaya/inject/internal/InjectionUtils.java | 2 +- 6 files changed, 86 insertions(+), 150 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/987186e1/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 index 4909207..1e0709b 100644 --- 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 @@ -18,17 +18,12 @@ */ package org.apache.tamaya.inject.internal; -import org.apache.tamaya.ConfigException; import org.apache.tamaya.Configuration; -import org.apache.tamaya.PropertyConverter; import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.inject.DynamicValue; -import org.apache.tamaya.inject.WithPropertyConverter; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; import java.util.Objects; /** @@ -74,34 +69,7 @@ public final class ConfigTemplateInvocationHandler implements InvocationHandler return this.configuration; } if (method.getReturnType() == DynamicValue.class) { - // Check for adapter/filter - Type targetType = method.getGenericReturnType(); - if (targetType == null) { - throw new ConfigException("Failed to evaluate target type for " + method.getDeclaringClass() - .getTypeName() + '.' + method.getName()); - } - if (targetType instanceof ParameterizedType) { - ParameterizedType pt = (ParameterizedType) targetType; - Type[] types = pt.getActualTypeArguments(); - if (types.length != 1) { - throw new ConfigException("Failed to evaluate target type for " + method.getDeclaringClass() - .getTypeName() + '.' + method.getName()); - } - targetType = (Class) types[0]; - } - PropertyConverter<Object> propertyConverter = null; - WithPropertyConverter annot = method.getAnnotation(WithPropertyConverter.class); - if (annot != null) { - try { - propertyConverter = (PropertyConverter<Object>) annot.value().newInstance(); - } catch (Exception e) { - throw new ConfigException("Failed to instantiate annotated PropertyConverter on " + - method.getDeclaringClass().getTypeName() - + '.' + method.getName(), e); - } - } - return new DefaultDynamicValue<Object>(method.getName(), - this.configuration, TypeLiteral.of(targetType), propertyConverter, InjectionUtils.getKeys(method)); + return DefaultDynamicValue.of(method, configuration); } String configValue = InjectionUtils.getConfigValue(method); Object result = InjectionUtils.adaptValue(method, TypeLiteral.of(method.getReturnType()), configValue); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/987186e1/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredField.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredField.java b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredField.java index a2a01ea..c4a4fad 100644 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredField.java +++ b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredField.java @@ -19,21 +19,13 @@ package org.apache.tamaya.inject.internal; import org.apache.tamaya.ConfigException; -import org.apache.tamaya.Configuration; import org.apache.tamaya.ConfigurationProvider; -import org.apache.tamaya.PropertyConverter; import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.inject.ConfigRoot; -import org.apache.tamaya.inject.ConfiguredProperty; import org.apache.tamaya.inject.DynamicValue; -import org.apache.tamaya.inject.WithPropertyConverter; import java.lang.reflect.Field; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; import java.security.AccessController; import java.security.PrivilegedExceptionAction; -import java.util.List; import java.util.Objects; import java.util.logging.Logger; @@ -68,12 +60,11 @@ public class ConfiguredField { * @param target the target instance. * @throws ConfigException if evaluation or conversion failed. */ - public void applyInitialValue(Object target) throws ConfigException { + public void applyValue(Object target) throws ConfigException { if (this.annotatedField.getType() == DynamicValue.class) { - initDynamicValue(target); + applyDynamicValue(target); } else { - String configValue = InjectionUtils.getConfigValue(this.annotatedField); - applyValue(target, configValue, false); + applyValue(target, false); } } @@ -84,44 +75,15 @@ public class ConfiguredField { * @param target the target instance, not null. * @throws ConfigException if the configuration required could not be resolved or converted. */ - public void initDynamicValue(Object target) throws ConfigException { + private void applyDynamicValue(Object target) throws ConfigException { Objects.requireNonNull(target); try { - // Check for adapter/filter - Type targetType = this.annotatedField.getGenericType(); - if (targetType == null) { - throw new ConfigException("Failed to evaluate target type for " + annotatedField.getAnnotatedType().getType().getTypeName() - + '.' + annotatedField.getName()); - } - if (targetType instanceof ParameterizedType) { - ParameterizedType pt = (ParameterizedType) targetType; - Type[] types = pt.getActualTypeArguments(); - if (types.length != 1) { - throw new ConfigException("Failed to evaluate target type for " + annotatedField.getAnnotatedType().getType().getTypeName() - + '.' + annotatedField.getName()); - } - targetType = (Class) types[0]; - } - PropertyConverter<?> propertyConverter = null; - WithPropertyConverter annot = this.annotatedField.getAnnotation(WithPropertyConverter.class); - if (annot != null) { - try { - propertyConverter = annot.value().newInstance(); - } catch (Exception e) { - throw new ConfigException("Failed to instantiate annotated PropertyConverter on " + - annotatedField.getAnnotatedType().getType().getTypeName() - + '.' + annotatedField.getName(), e); - } - } - List<String> keys = InjectionUtils.getKeys(this.annotatedField); - Configuration configuration = ConfigurationProvider.getConfiguration(); - Object value = new DefaultDynamicValue(this.annotatedField.getName(), configuration, - TypeLiteral.of(targetType), propertyConverter, keys); AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> { annotatedField.setAccessible(true); return annotatedField; }); - annotatedField.set(target, value); + annotatedField.set(target, + DefaultDynamicValue.of(annotatedField, ConfigurationProvider.getConfiguration())); } catch (Exception e) { throw new ConfigException("Failed to annotation configured field: " + this.annotatedField.getDeclaringClass() .getName() + '.' + annotatedField.getName(), e); @@ -129,16 +91,16 @@ public class ConfiguredField { } /** - * This method reapplies a changed configuration keys to the field. + * This method applies a configuration to the field. * * @param target the target instance, not null. - * @param configValue the new keys to be applied, null will trigger the evaluation current the configured default keys. * @param resolve set to true, if expression resolution should be applied on the keys passed. * @throws ConfigException if the configuration required could not be resolved or converted. */ - public void applyValue(Object target, String configValue, boolean resolve) throws ConfigException { + private void applyValue(Object target,boolean resolve) throws ConfigException { Objects.requireNonNull(target); try { + String configValue = InjectionUtils.getConfigValue(this.annotatedField); // Next step perform expression resolution, if any String evaluatedValue = resolve && configValue != null ? InjectionUtils.evaluateValue(configValue) @@ -146,12 +108,10 @@ public class ConfiguredField { // Check for adapter/filter Object value = InjectionUtils.adaptValue(this.annotatedField, TypeLiteral.of(this.annotatedField.getType()), evaluatedValue); - AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> { annotatedField.setAccessible(true); return annotatedField; }); - annotatedField.set(target, value); } catch (Exception e) { throw new ConfigException("Failed to annotation configured field: " + this.annotatedField.getDeclaringClass() @@ -159,22 +119,6 @@ public class ConfiguredField { } } - - /** - * This method checks if the given (qualified) configuration key is referenced fromMap this field. - * This is useful to determine, if a key changed in a configuration should trigger any change events - * on the related instances. - * - * @param key the (qualified) configuration key, not null. - * @return true, if the key is referenced. - */ - public boolean matchesKey(String key) { - ConfiguredProperty prop = this.annotatedField.getAnnotation(ConfiguredProperty.class); - ConfigRoot areasAnnot = this.annotatedField.getDeclaringClass().getAnnotation(ConfigRoot.class); - List<String> keys = InjectionUtils.evaluateKeys(this.annotatedField, areasAnnot, prop); - return keys.contains(key); - } - @Override public String toString() { return "ConfiguredField{" + http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/987186e1/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredSetterMethod.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredSetterMethod.java b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredSetterMethod.java index 9c3b9cd..8dca102 100644 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredSetterMethod.java +++ b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredSetterMethod.java @@ -23,13 +23,9 @@ import java.security.AccessController; import java.security.PrivilegedExceptionAction; import java.util.Objects; import java.util.Optional; -import java.util.function.Consumer; import org.apache.tamaya.ConfigException; import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.inject.ConfigRoot; -import org.apache.tamaya.inject.ConfiguredProperty; -import org.apache.tamaya.event.PropertyChangeSet; /** * Small class that contains and manages all information and access to a configured field and a concrete instance current @@ -54,35 +50,14 @@ public class ConfiguredSetterMethod { m.getParameterCount() == 1).get(); } - public Consumer<PropertyChangeSet> createConsumer(Object instance){ - // TODO consider environment as well - return event -> { - String configValue = InjectionUtils.getConfigValue(setterMethod); - applyValue(instance,configValue, false); - }; - } - - /** * Evaluate the initial keys fromMap the configuration and applyChanges it to the field. * * @param target the target instance. * @throws ConfigException if evaluation or conversion failed. */ - public void applyInitialValue(Object target) throws ConfigException { + public void applyValue(Object target, boolean resolve) throws ConfigException { String configValue = InjectionUtils.getConfigValue(this.setterMethod); - applyValue(target, configValue, false); - } - - /** - * This method reapplies a changed configuration keys to the field. - * - * @param target the target instance, not null. - * @param configValue the new keys to be applied, null will trigger the evaluation current the configured default keys. - * @param resolve set to true, if expression resolution should be applied on the keys passed. - * @throws org.apache.tamaya.ConfigException if the configuration required could not be resolved or converted. - */ - public void applyValue(Object target, String configValue, boolean resolve) throws ConfigException { Objects.requireNonNull(target); try { String evaluatedString = resolve && configValue != null @@ -105,23 +80,4 @@ public class ConfiguredSetterMethod { } - - /** - * This method checks if the given (qualified) configuration key is referenced fromMap this field. - * This is useful to determine, if a key changed in a configuration should trigger any change events - * on the related instances. - * - * @param key the (qualified) configuration key, not null. - * @return true, if the key is referenced. - */ - public boolean matchesKey(String key) { - ConfigRoot areasAnnot = this.setterMethod.getDeclaringClass().getAnnotation(ConfigRoot.class); - ConfiguredProperty prop = this.setterMethod.getAnnotation(ConfiguredProperty.class); - if (InjectionUtils.evaluateKeys(this.setterMethod, areasAnnot, prop).contains(key)) { - return true; - } - return InjectionUtils.evaluateKeys(this.setterMethod, areasAnnot).contains(key); - } - - } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/987186e1/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredType.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredType.java b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredType.java index 8828598..2932e04 100644 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredType.java +++ b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredType.java @@ -152,14 +152,14 @@ public class ConfiguredType { */ public void configure(Object instance) { for (ConfiguredField field : configuredFields) { - field.applyInitialValue(instance); + field.applyValue(instance); } for (ConfiguredSetterMethod method : configuredSetterMethods) { - method.applyInitialValue(instance); - // TODO, if method should be recalled on changes, corresponding callbacks could be registered here - WeakConfigListenerManager.of().registerConsumer(instance, method.createConsumer(instance)); + method.applyValue(instance, true); +// // TODO, if method should be recalled on changes, corresponding callbacks could be registered here +// WeakConfigListenerManager.of().registerConsumer(instance, method.createConsumer(instance)); } - // Register callbacks for this intance (weakly) + // Register callbacks for this instance (weakly) for (ConfigChangeCallbackMethod callback : callbackMethods) { WeakConfigListenerManager.of().registerConsumer(instance, callback.createConsumer(instance)); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/987186e1/modules/injection/src/main/java/org/apache/tamaya/inject/internal/DefaultDynamicValue.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/DefaultDynamicValue.java b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/DefaultDynamicValue.java index 9f97ad1..dd5684b 100644 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/DefaultDynamicValue.java +++ b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/DefaultDynamicValue.java @@ -18,10 +18,12 @@ */ package org.apache.tamaya.inject.internal; +import org.apache.tamaya.ConfigException; import org.apache.tamaya.Configuration; import org.apache.tamaya.PropertyConverter; import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.inject.DynamicValue; +import org.apache.tamaya.inject.WithPropertyConverter; import java.beans.PropertyChangeEvent; import java.io.IOException; @@ -29,6 +31,10 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.lang.ref.WeakReference; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; @@ -106,7 +112,7 @@ public final class DefaultDynamicValue<T> implements DynamicValue<T>, Serializab * @param targetType the target type, not null. * @param propertyConverter the optional converter to be used. */ - DefaultDynamicValue(String propertyName, Configuration configuration, TypeLiteral<T> targetType, + private DefaultDynamicValue(String propertyName, Configuration configuration, TypeLiteral<T> targetType, PropertyConverter<T> propertyConverter, List<String> keys) { this.propertyName = Objects.requireNonNull(propertyName); this.keys = keys.toArray(new String[keys.size()]); @@ -116,6 +122,68 @@ public final class DefaultDynamicValue<T> implements DynamicValue<T>, Serializab this.value = Optional.ofNullable(evaluateValue()); } + public static DynamicValue of(Field annotatedField, Configuration configuration){ + // Check for adapter/filter + Type targetType = annotatedField.getGenericType(); + if (targetType == null) { + throw new ConfigException("Failed to evaluate target type for " + annotatedField.getAnnotatedType().getType().getTypeName() + + '.' + annotatedField.getName()); + } + if (targetType instanceof ParameterizedType) { + ParameterizedType pt = (ParameterizedType) targetType; + Type[] types = pt.getActualTypeArguments(); + if (types.length != 1) { + throw new ConfigException("Failed to evaluate target type for " + annotatedField.getAnnotatedType().getType().getTypeName() + + '.' + annotatedField.getName()); + } + targetType = (Class) types[0]; + } + PropertyConverter<?> propertyConverter = null; + WithPropertyConverter annot = annotatedField.getAnnotation(WithPropertyConverter.class); + if (annot != null) { + try { + propertyConverter = annot.value().newInstance(); + } catch (Exception e) { + throw new ConfigException("Failed to instantiate annotated PropertyConverter on " + + annotatedField.getAnnotatedType().getType().getTypeName() + + '.' + annotatedField.getName(), e); + } + } + List<String> keys = InjectionUtils.getKeys(annotatedField); + return new DefaultDynamicValue(annotatedField.getName(), configuration, + TypeLiteral.of(targetType), propertyConverter, keys); + } + + public static DynamicValue of(Method method, Configuration configuration){ + // Check for adapter/filter + Type targetType = method.getGenericReturnType(); + if (targetType == null) { + throw new ConfigException("Failed to evaluate target type for " + method.getDeclaringClass() + .getTypeName() + '.' + method.getName()); + } + if (targetType instanceof ParameterizedType) { + ParameterizedType pt = (ParameterizedType) targetType; + Type[] types = pt.getActualTypeArguments(); + if (types.length != 1) { + throw new ConfigException("Failed to evaluate target type for " + method.getDeclaringClass() + .getTypeName() + '.' + method.getName()); + } + targetType = (Class) types[0]; + } + PropertyConverter<Object> propertyConverter = null; + WithPropertyConverter annot = method.getAnnotation(WithPropertyConverter.class); + if (annot != null) { + try { + propertyConverter = (PropertyConverter<Object>) annot.value().newInstance(); + } catch (Exception e) { + throw new ConfigException("Failed to instantiate annotated PropertyConverter on " + + method.getDeclaringClass().getTypeName() + + '.' + method.getName(), e); + } + } + return new DefaultDynamicValue<Object>(method.getName(), + configuration, TypeLiteral.of(targetType), propertyConverter, InjectionUtils.getKeys(method)); + } /** * Performs a commit, if necessary, and returns the current value. http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/987186e1/modules/injection/src/main/java/org/apache/tamaya/inject/internal/InjectionUtils.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/InjectionUtils.java b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/InjectionUtils.java index fae86b5..80854d8 100644 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/InjectionUtils.java +++ b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/InjectionUtils.java @@ -44,7 +44,7 @@ import org.apache.tamaya.spi.ServiceContext; /** - * Created by Anatole on 19.12.2014. + * Utility class containing several aspects used in this module. */ @SuppressWarnings("unchecked") final class InjectionUtils {