tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anat...@apache.org
Subject [3/6] incubator-tamaya git commit: TAMAYA-160: Added support for autoijecting configuration on non annotated classes.
Date Wed, 11 May 2016 02:41:41 GMT
TAMAYA-160: Added support for autoijecting configuration on non annotated classes.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/ef221a7e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/ef221a7e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/ef221a7e

Branch: refs/heads/master
Commit: ef221a7e22fc14d0b0b8d14af3ac2d74dfb567e3
Parents: a03615f
Author: anatole <anatole@apache.org>
Authored: Wed May 11 03:47:20 2016 +0200
Committer: anatole <anatole@apache.org>
Committed: Wed May 11 03:47:20 2016 +0200

----------------------------------------------------------------------
 .../tamaya/inject/ConfigurationInjector.java    |  6 +++-
 .../inject/internal/ConfiguredTypeImpl.java     | 23 +++++++++-------
 .../internal/DefaultConfigurationInjector.java  | 29 +++++++++++++++++---
 3 files changed, 43 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ef221a7e/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
index 9a3fe5c..563ae47 100644
--- a/modules/injection/src/main/java/org/apache/tamaya/inject/ConfigurationInjector.java
+++ b/modules/injection/src/main/java/org/apache/tamaya/inject/ConfigurationInjector.java
@@ -30,7 +30,9 @@ 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).
@@ -41,6 +43,8 @@ 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
      * @param config the configuration to be used for injection.

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ef221a7e/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredTypeImpl.java
----------------------------------------------------------------------
diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredTypeImpl.java
b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredTypeImpl.java
index ecb464c..b40f6c9 100644
--- a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredTypeImpl.java
+++ b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredTypeImpl.java
@@ -62,18 +62,21 @@ public class ConfiguredTypeImpl implements ConfiguredType{
      *
      * @param type the instance type.
      */
-
     public ConfiguredTypeImpl(Class type) {
         this.type = Objects.requireNonNull(type);
-        ConfigDefaultSections confType = (ConfigDefaultSections)
-                type.getAnnotation(ConfigDefaultSections.class);
-        ConfigAutoInject autoInject = (ConfigAutoInject)type.getAnnotation(ConfigAutoInject.class);
-        if(confType!=null){
-            initFields(type, autoInject!=null);
-            initMethods(type, autoInject!=null);
-        }else{
-            initFields(type, false);
-            initMethods(type, false);
+        if(!isConfigured(type)){
+            LOG.info("Auto-Configuring type: " + type.getName());
+            initFields(type, true);
+            initMethods(type, true);
+        }else {
+            ConfigAutoInject autoInject = (ConfigAutoInject) type.getAnnotation(ConfigAutoInject.class);
+            if (autoInject != null) {
+                initFields(type, autoInject != null);
+                initMethods(type, autoInject != null);
+            } else {
+                initFields(type, false);
+                initMethods(type, false);
+            }
         }
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ef221a7e/modules/injection/src/main/java/org/apache/tamaya/inject/internal/DefaultConfigurationInjector.java
----------------------------------------------------------------------
diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/DefaultConfigurationInjector.java
b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/DefaultConfigurationInjector.java
index 0795d66..a3a7015 100644
--- a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/DefaultConfigurationInjector.java
+++ b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/DefaultConfigurationInjector.java
@@ -47,6 +47,8 @@ public final class DefaultConfigurationInjector implements ConfigurationInjector
 
     private static final Logger LOG = Logger.getLogger(DefaultConfigurationInjector.class.getName());
 
+    private boolean autoConfigureEnabled = true;
+
     /**
      * Extract the configuration annotation config and registers it per class, for later
reuse.
      *
@@ -55,13 +57,32 @@ public final class DefaultConfigurationInjector implements ConfigurationInjector
      */
     public ConfiguredType registerType(Class<?> type) {
         ConfiguredType confType = configuredTypes.get(type);
-        if (confType == null && isConfigured(type)) {
+        if (confType == null) {
+            if(!isConfigAnnotated(type) && !autoConfigureEnabled){
+                return null;
+            }
             confType = new ConfiguredTypeImpl(type);
             configuredTypes.put(type, confType);
             InjectionHelper.sendConfigurationEvent(confType);
         }
         return confType;
-//        return configuredTypes.computeIfAbsent(type, ConfigDefaultSections::new);
+    }
+
+    /**
+     * If set also non annotated instances can be configured or created as templates.
+     * @return true, if autoConfigureEnabled.
+     */
+    public boolean isAutoConfigureEnabled(){
+        return autoConfigureEnabled;
+    }
+
+    /**
+     * Setting to true enables also configuration/templating of non annotated classes or
+     * interfaces.
+     * @param enabled true enables also configuration/templating of
+     */
+    public void setAutoConfigureEnabled(boolean enabled){
+        this.autoConfigureEnabled = enabled;
     }
 
     /**
@@ -69,7 +90,7 @@ public final class DefaultConfigurationInjector implements ConfigurationInjector
      * @param type the target type, not null.
      * @return true, if the type, a method or field has Tamaya config annotation on it.
      */
-    private boolean isConfigured(Class<?> type) {
+    private boolean isConfigAnnotated(Class<?> type) {
         if(type.getClass().isAnnotationPresent(ConfigDefaultSections.class)){
             return true;
         }
@@ -111,7 +132,7 @@ public final class DefaultConfigurationInjector implements ConfigurationInjector
         if(configuredType!=null){
             configuredType.configure(instance, config);
         }else{
-            LOG.info("Instance passed is not annotated for configuration: " + instance);
+            LOG.info("Instance passed is not configurable: " + instance);
         }
         return instance;
     }


Mime
View raw message