You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by an...@apache.org on 2015/08/21 02:41:48 UTC
[6/6] incubator-tamaya git commit: Updated injection module to
auto-document injected configuration and used templates within the module
extension (optionally, only if module is on the classpath).
Updated injection module to auto-document injected configuration and used templates within the module extension (optionally, only if module is on the classpath).
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/3c5b214d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/3c5b214d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/3c5b214d
Branch: refs/heads/master
Commit: 3c5b214d883f47ccc4702b4e3367b1477fa84d00
Parents: c534341
Author: anatole <an...@apache.org>
Authored: Fri Aug 21 02:41:26 2015 +0200
Committer: anatole <an...@apache.org>
Committed: Fri Aug 21 02:41:26 2015 +0200
----------------------------------------------------------------------
modules/injection/pom.xml | 6 +
.../ConfigTemplateInvocationHandler.java | 1 +
.../tamaya/inject/internal/ConfiguredField.java | 19 +++
.../inject/internal/ConfiguredSetterMethod.java | 34 ++++-
.../tamaya/inject/internal/ConfiguredType.java | 24 ++++
.../internal/DefaultConfigurationInjector.java | 9 +-
.../internal/InjectableValidationProvider.java | 46 ++++++
.../tamaya/inject/internal/InjectionUtils.java | 1 -
.../tamaya/inject/internal/ModelPopulator.java | 143 +++++++++++++++++++
...pache.tamaya.model.spi.ValidationProviderSpi | 20 +++
10 files changed, 292 insertions(+), 11 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3c5b214d/modules/injection/pom.xml
----------------------------------------------------------------------
diff --git a/modules/injection/pom.xml b/modules/injection/pom.xml
index 0d779ae..9857727 100644
--- a/modules/injection/pom.xml
+++ b/modules/injection/pom.xml
@@ -59,6 +59,12 @@ under the License.
<scope>test</scope>
</dependency>
<dependency>
+ <groupId>org.apache.tamaya.ext.model</groupId>
+ <artifactId>tamaya-model</artifactId>
+ <version>0.1-incubating-SNAPSHOT</version>
+ <optional>true</optional>
+ </dependency>
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3c5b214d/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 1e0709b..b2fec95 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
@@ -55,6 +55,7 @@ public final class ConfigTemplateInvocationHandler implements InvocationHandler
throw new IllegalArgumentException("Can only proxy interfaces as configuration templates.");
}
this.configuration = Objects.requireNonNull(configuration);
+ ModelPopulator.registerTemplate(type);
}
@Override
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3c5b214d/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 0b6e108..fd7f6fc 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
@@ -26,6 +26,7 @@ import org.apache.tamaya.inject.DynamicValue;
import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
+import java.util.Collection;
import java.util.Objects;
import java.util.logging.Logger;
@@ -54,6 +55,7 @@ public class ConfiguredField {
this.annotatedField = field;
}
+
/**
* Evaluate the initial keys fromMap the configuration and applyChanges it to the field.
*
@@ -125,10 +127,27 @@ public class ConfiguredField {
}
}
+ /**
+ * Get the field's type.
+ * @return the field's type, not null.
+ */
+ public Class<?> getType(){
+ return this.annotatedField.getType();
+ }
+
+ /**
+ * Access the applyable configuration keys for this field.
+ * @return the configuration keys, never null.
+ */
+ public Collection<String> getConfiguredKeys(){
+ return InjectionUtils.getKeys(this.annotatedField);
+ }
+
@Override
public String toString() {
return "ConfiguredField{" +
annotatedField.getName() + ": " +
" " + annotatedField.getAnnotatedType().getType().getTypeName() + '}';
}
+
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3c5b214d/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 a9cb58e..6eb6e74 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
@@ -18,14 +18,15 @@
*/
package org.apache.tamaya.inject.internal;
+import org.apache.tamaya.ConfigException;
+import org.apache.tamaya.TypeLiteral;
+
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
+import java.util.Collection;
import java.util.Objects;
-import org.apache.tamaya.ConfigException;
-import org.apache.tamaya.TypeLiteral;
-
/**
* Small class that contains and manages all information and access to a configured field and a concrete instance current
* it (referenced by a weak reference). It also implements all aspects current keys filtering, conversions any applying the
@@ -37,6 +38,7 @@ public class ConfiguredSetterMethod {
* The configured field instance.
*/
private Method setterMethod;
+ private Collection<String> configuredKeys;
/**
* Models a configured field and provides mechanisms for injection.
@@ -65,7 +67,7 @@ public class ConfiguredSetterMethod {
: configValue;
// Check for adapter/filter
- Object value = InjectionUtils.adaptValue(this.setterMethod, TypeLiteral.of(this.setterMethod.getParameterTypes()[0]), evaluatedString);
+ Object value = InjectionUtils.adaptValue(this.setterMethod, TypeLiteral.of(this.setterMethod.getParameterTypes()[0]), evaluatedString);
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
@Override
@@ -83,4 +85,28 @@ public class ConfiguredSetterMethod {
}
+ /**
+ * Access the applyable configuration keys for this field.
+ *
+ * @return the configuration keys, never null.
+ */
+ public Collection<String> getConfiguredKeys() {
+ return InjectionUtils.getKeys(this.setterMethod);
+ }
+
+ /**
+ * Get the type to be set on the setter method.
+ * @return
+ */
+ public Class<?> getParameterType() {
+ return this.setterMethod.getParameterTypes()[0];
+ }
+
+ /**
+ * Access the annotated method.
+ * @return the annotated method, not null.
+ */
+ public Method getAnnotatedMethod() {
+ return this.setterMethod;
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3c5b214d/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 020064d..cba90e1 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
@@ -221,6 +221,30 @@ public class ConfiguredType {
return this.type;
}
+ /**
+ * Get the registered configured fields.
+ * @return the registered configured fields, never null.
+ */
+ public Collection<ConfiguredField> getConfiguredFields(){
+ return configuredFields;
+ }
+
+ /**
+ * Get the registered annotated setter methods.
+ * @return the registered annotated setter methods, never null.
+ */
+ public Collection<ConfiguredSetterMethod> getConfiguredSetterMethods(){
+ return configuredSetterMethods;
+ }
+
+ /**
+ * Get the registered annotated callback methods.
+ * @return the registered annotated callback methods, never null.
+ */
+ public Collection<ConfigChangeCallbackMethod> getObserverMethods(){
+ return callbackMethods;
+ }
+
@Override
public String toString() {
return "ConfiguredType{"+ this.getType().getName() + '}';
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3c5b214d/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 cb6448e..06de9f5 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
@@ -43,20 +43,17 @@ public final class DefaultConfigurationInjector implements ConfigurationInjector
* @param type the type to be configured.
* @return the configured type registered.
*/
- public ConfiguredType registerTypeInternal(Class<?> type) {
+ public ConfiguredType registerType(Class<?> type) {
ConfiguredType confType = configuredTypes.get(type);
if (confType == null) {
confType = new ConfiguredType(type);
+ ModelPopulator.register(confType);
configuredTypes.put(type, confType);
}
return confType;
// return configuredTypes.computeIfAbsent(type, ConfiguredType::new);
}
- void registerType(Class<?> type) {
- registerTypeInternal(type);
- }
-
/**
* Configured the current instance and reigsterd necessary listener to forward config change events as
* defined by the current annotations in place.
@@ -66,7 +63,7 @@ public final class DefaultConfigurationInjector implements ConfigurationInjector
@Override
public <T> T configure(T instance) {
Class type = Objects.requireNonNull(instance).getClass();
- ConfiguredType configuredType = registerTypeInternal(type);
+ ConfiguredType configuredType = registerType(type);
Objects.requireNonNull(configuredType).configure(instance);
return instance;
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3c5b214d/modules/injection/src/main/java/org/apache/tamaya/inject/internal/InjectableValidationProvider.java
----------------------------------------------------------------------
diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/InjectableValidationProvider.java b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/InjectableValidationProvider.java
new file mode 100644
index 0000000..0761492
--- /dev/null
+++ b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/InjectableValidationProvider.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.internal;
+
+import org.apache.tamaya.model.Validation;
+import org.apache.tamaya.model.spi.ValidationProviderSpi;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Created by Anatole on 19.08.2015.
+ */
+public class InjectableValidationProvider implements ValidationProviderSpi{
+
+ private static Collection<Validation> validations = new ArrayList<>();
+
+ static void addValidation(Validation validation){
+ List<Validation> newList = new ArrayList<>(validations);
+ newList.add(validation);
+ InjectableValidationProvider.validations = newList;
+ }
+
+ @Override
+ public Collection<Validation> getValidations() {
+ return Collections.unmodifiableCollection(validations);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3c5b214d/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 40957af..c8b52da 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
@@ -268,5 +268,4 @@ final class InjectionUtils {
return expression;
}
-
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3c5b214d/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ModelPopulator.java
----------------------------------------------------------------------
diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ModelPopulator.java b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ModelPopulator.java
new file mode 100644
index 0000000..f0218b4
--- /dev/null
+++ b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ModelPopulator.java
@@ -0,0 +1,143 @@
+/*
+ * 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.model.ConfigValidator;
+import org.apache.tamaya.model.spi.ParameterValidation;
+
+import java.lang.reflect.Method;
+import java.util.Collection;
+import java.util.logging.Logger;
+
+/**
+ * Internal facade that registers all kind of injected fields as {@link org.apache.tamaya.model.Validation} entries,
+ * so all configured injection points are visible as documented configuration hooks.
+ */
+final class ModelPopulator {
+
+ /**
+ * The logger.
+ */
+ private static final Logger LOG = Logger.getLogger(ModelPopulator.class.getName());
+
+ /**
+ * The singleton class of the model module to be tested for availability on the classpath.
+ */
+ private static final String MODEL_SINGLETON_CLASS = "org.apache.tamaya.model.ConfigValidator";
+ /**
+ * Flag initialized to true, if the model module is visible on the same classloader, also this module was loaded.
+ */
+ private static boolean modelModuleLoaded = checkModelModuleLoaded();
+
+ /**
+ * Initializes the flag, controlling the availability of this component.
+ *
+ * @return true, if the tamaya-model module is visible.
+ */
+ private static boolean checkModelModuleLoaded() {
+ try {
+ Class.forName(MODEL_SINGLETON_CLASS, false, ModelPopulator.class.getClassLoader());
+ LOG.info("Tamaya Model extension is available. Validation models will be updated" +
+ " based on injected config.");
+ return true;
+ } catch (Exception e) {
+ LOG.info("Tamaya Model extension not available. Validation models will not be updated" +
+ " based on injected config.");
+ return false;
+ }
+ }
+
+ /**
+ * Utility classes should not be instantiated.
+ */
+ private ModelPopulator(){}
+
+ /**
+ * Registers the given {@link ConfiguredType} into the tamaya-model validation/documentation module.
+ *
+ * @param confType the type, not null.
+ */
+ public static void register(ConfiguredType confType) {
+ if (!modelModuleLoaded) {
+ return;
+ }
+ for (ConfiguredField field : confType.getConfiguredFields()) {
+ Collection<String> keys = field.getConfiguredKeys();
+ for (String key : keys) {
+ ParameterValidation val = ConfigValidator.getValidation(key, ParameterValidation.class);
+ if (val == null) {
+ InjectableValidationProvider.addValidation(new ParameterValidation.Builder(key)
+ .setType(field.getType().getName())
+ .setDescription("Injected field: " +
+ field.annotatedField.getDeclaringClass().getName() + '.' + field.toString() +
+ ", \nconfigured with keys: " + keys)
+ .build());
+ }
+ }
+ }
+ for (ConfiguredSetterMethod method : confType.getConfiguredSetterMethods()) {
+ Collection<String> keys = method.getConfiguredKeys();
+ for (String key : keys) {
+ ParameterValidation val = ConfigValidator.getValidation(key, ParameterValidation.class);
+ if (val == null) {
+ InjectableValidationProvider.addValidation(new ParameterValidation.Builder(key)
+ .setType(method.getParameterType().getName())
+ .setDescription("Injected field: " +
+ method.getAnnotatedMethod().getDeclaringClass().getName() + '.' + method.toString() +
+ ", \nconfigured with keys: " + keys)
+ .build());
+ }
+ }
+ }
+// for (ConfigChangeCallbackMethod callback : confType.getObserverMethods() {
+// Collection<String> keys = callback.getConfiguredKeys();
+// for (String key : keys) {
+// CallbackValidation val = ConfigValidator.getValidation(key, CallbackValidation.class);
+// if (val == null) {
+// InjectableValidationProvider.addValidation(new CallbackValidation(key, callback));
+// }
+// }
+// }
+
+ }
+
+ public static void registerTemplate(Class<?> type) {
+ if (!modelModuleLoaded) {
+ return;
+ }
+ for (Method method : type.getMethods()) {
+ if (method.getDeclaringClass() == Object.class) {
+ // skip methods decalred on object.
+ continue;
+ }
+ Collection<String> keys = InjectionUtils.getKeys(method);
+ for (String key : keys) {
+ ParameterValidation val = ConfigValidator.getValidation(key, ParameterValidation.class);
+ if (val == null) {
+ InjectableValidationProvider.addValidation(new ParameterValidation.Builder(key)
+ .setType(method.getReturnType().getName())
+ .setDescription("Template method: " +
+ type.getName() + '.' + method.toString() + ", \n" +
+ "configured with keys: " + keys)
+ .build());
+ }
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3c5b214d/modules/injection/src/main/resources/META-INF/services/org.apache.tamaya.model.spi.ValidationProviderSpi
----------------------------------------------------------------------
diff --git a/modules/injection/src/main/resources/META-INF/services/org.apache.tamaya.model.spi.ValidationProviderSpi b/modules/injection/src/main/resources/META-INF/services/org.apache.tamaya.model.spi.ValidationProviderSpi
new file mode 100644
index 0000000..ef93b75
--- /dev/null
+++ b/modules/injection/src/main/resources/META-INF/services/org.apache.tamaya.model.spi.ValidationProviderSpi
@@ -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 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.
+#
+# Note: this provider is loaded by the model extension module
+org.apache.tamaya.inject.internal.InjectableValidationProvider
\ No newline at end of file