You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by st...@apache.org on 2012/06/15 00:30:19 UTC
git commit: DELTASPIKE-191 add more documentation and samples
Updated Branches:
refs/heads/master 1d9671cde -> 2fa81d4b8
DELTASPIKE-191 add more documentation and samples
This also improves the BaseConfigPropertyProducer
Project: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/commit/2fa81d4b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/tree/2fa81d4b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/diff/2fa81d4b
Branch: refs/heads/master
Commit: 2fa81d4b8b0bbf304e4dd039f6cb5c29fa59ee16
Parents: 1d9671c
Author: Mark Struberg <st...@apache.org>
Authored: Fri Jun 15 00:28:48 2012 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Fri Jun 15 00:28:48 2012 +0200
----------------------------------------------------------------------
.../core/api/config/annotation/ConfigProperty.java | 48 +--------
.../spi/config/BaseConfigPropertyProducer.java | 83 +++++++++++++-
.../injectable/InjectableConfigPropertyTest.java | 17 +++-
.../injectable/numberconfig/NumberConfig.java | 50 +++++++++
.../numberconfig/NumberConfigPropertyProducer.java | 55 ++++++++++
.../numberconfig/NumberConfiguredBean.java | 59 ++++++++++
.../META-INF/apache-deltaspike.properties | 3 +
7 files changed, 261 insertions(+), 54 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/2fa81d4b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/annotation/ConfigProperty.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/annotation/ConfigProperty.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/annotation/ConfigProperty.java
index 126b37f..50ca442 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/annotation/ConfigProperty.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/annotation/ConfigProperty.java
@@ -42,52 +42,8 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
* </pre>
* </p>
*
- * <h2>Providing own Converters and Type injection</h2>
- * <p>DeltaSpikes own configuration system only natively only supports Strings.
- * If you like to apply own Converters or extract other types from those Strings,
- * you can simply do this by providing an own Qualifier and a simple
- * CDI producer method for it.</p>
- *
- * <p>First we write a simple Qualifier:
- * <pre>
- * @Target({ PARAMETER, FIELD, METHOD, CONSTRUCTOR, ANNOTATION_TYPE })
- * @Retention(RUNTIME)
- * @Qualifier
- * public @interface NumberConfig
- * {
- * @Nonbinding
- * boolean name(); // the name of the configuration-key to lookup the value
- *
- * @Nonbinding
- * boolean pattern(); // the pattern for NumberFormatter
- * }
- * </pre>
- * </p>
- *
- * <p>The producer method implementation is pretty easy as well:
- * <pre>
- * @ApplicationScoped
- * public class NumberConfigProducer extends BaseConfigPropertyProducer
- * {
- * @Produces
- * @Dependent
- * @NumberConfig
- * public Float produceNumberConfig(InjectionPoint injectionPoint)
- * {
- * String configuredValue = getStringPropertyValue(injectionPoint);
- *
- * if (configuredValue == null || configuredValue.length() == 0)
- * {
- * return null; // or 0.f depending on what you need
- * }
- *
- * NumberConfig metaData = getAnnotation(injectionPoint, NumberConfig.class);
- * DecimalFormat df = new DecimalFormat(metaData.pattern());
- * return df.parse(configuredValue).floatValue();
- * }
- * }
- * </pre>
- * </p>
+ * <p>See the sample in {@link org.apache.deltaspike.core.spi.config.BaseConfigPropertyProducer}
+ * for how to implement own configuration injection.</p>
*
* @see org.apache.deltaspike.core.api.config.ConfigResolver
* @see org.apache.deltaspike.core.spi.config.BaseConfigPropertyProducer
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/2fa81d4b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/BaseConfigPropertyProducer.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/BaseConfigPropertyProducer.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/BaseConfigPropertyProducer.java
index c13b6bf..40e0b1b 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/BaseConfigPropertyProducer.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/BaseConfigPropertyProducer.java
@@ -27,14 +27,72 @@ import org.apache.deltaspike.core.api.config.annotation.ConfigProperty;
import org.apache.deltaspike.core.util.BeanUtils;
/**
- * This contains the fundamental parts for implementing own
- * ConfigProperty producers.
+ * <p>This contains the fundamental parts for implementing own
+ * ConfigProperty producers.</p>
*
- * TODO: add documentation
+ * <h2>Providing own Converters and Type injection</h2>
+ * <p>DeltaSpikes own configuration system only natively only supports Strings.
+ * If you like to apply own Converters or extract other types from those Strings,
+ * you can simply do this by providing an own Qualifier and a simple
+ * CDI producer method for it.</p>
+ *
+ * <p>First we write a simple Qualifier:
+ * <pre>
+ * @Target({ PARAMETER, FIELD, METHOD, CONSTRUCTOR, ANNOTATION_TYPE })
+ * @Retention(RUNTIME)
+ * @ConfigProperty(named="unused") // the name
+ * @Qualifier
+ * public @interface NumberConfig
+ * {
+ * @Nonbinding
+ * boolean name(); // the name of the configuration-key to lookup the value
+ *
+ * @Nonbinding
+ * String defaultValue() default ConfigProperty.NULL;
+ *
+ * @Nonbinding
+ * boolean pattern(); // the pattern for NumberFormatter
+ * }
+ * </pre>
+ * </p>
+ *
+ * <p>The producer method implementation is pretty easy as well:
+ * <pre>
+ * @ApplicationScoped
+ * public class NumberConfigProducer extends BaseConfigPropertyProducer
+ * {
+ * @Produces
+ * @Dependent
+ * @NumberConfig
+ * public Float produceNumberConfig(InjectionPoint injectionPoint)
+ * {
+ * // resolve the annotation
+ * NumberConfig metaData = getAnnotation(injectionPoint, NumberConfig.class);
+
+ * // get the configured value from the underlying configuration system
+ * String configuredValue = getPropertyValue(metaData.name(), metaData.defaultValue());
+ * if (configuredValue == null)
+ * {
+ * return null;
+ * }
+ *
+ * // format according to the given pattern
+ * DecimalFormat df = new DecimalFormat(metaData.pattern(), new DecimalFormatSymbols(Locale.US));
+ * return df.parse(configuredValue).floatValue(); *
+ * }
+ * }
+ * </pre>
+ * </p>
+
*/
public abstract class BaseConfigPropertyProducer
{
/**
+ * <p>Inspects the given InjectionPoint and search for a {@link ConfigProperty}
+ * annotation or an Annotation with a {@link ConfigProperty} meta-Annotation.
+ * The name and defaultValue information will be used to resolve the
+ * configured value.</p>
+ *
* @param injectionPoint current injection point
* @return the configured value for the given InjectionPoint
*/
@@ -50,16 +108,29 @@ public abstract class BaseConfigPropertyProducer
String configuredValue;
String defaultValue = configProperty.defaultValue();
+ configuredValue = getPropertyValue(configProperty.name(), defaultValue);
+
+ return configuredValue;
+ }
+
+ /**
+ * @param propertyName the name of the property key
+ * @param defaultValue the default value to return if no configured property is found or
+ * {@link ConfigProperty#NULL} if no default value should be returned.
+ * @return the configured value or the defaultValue according to the NULL logic.
+ */
+ protected String getPropertyValue(String propertyName, String defaultValue)
+ {
+ String configuredValue;
if (ConfigProperty.NULL.equals(defaultValue))
{
// no special defaultValue has been configured
- configuredValue = ConfigResolver.getPropertyValue(configProperty.name());
+ configuredValue = ConfigResolver.getPropertyValue(propertyName);
}
else
{
- configuredValue = ConfigResolver.getPropertyValue(configProperty.name(), defaultValue);
+ configuredValue = ConfigResolver.getPropertyValue(propertyName, defaultValue);
}
-
return configuredValue;
}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/2fa81d4b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/InjectableConfigPropertyTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/InjectableConfigPropertyTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/InjectableConfigPropertyTest.java
index e0a45b6..49e7d6f 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/InjectableConfigPropertyTest.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/InjectableConfigPropertyTest.java
@@ -18,6 +18,8 @@
*/
package org.apache.deltaspike.test.core.api.config.injectable;
+import java.net.URL;
+
import org.apache.deltaspike.core.api.provider.BeanProvider;
import org.apache.deltaspike.test.category.SeCategory;
import org.apache.deltaspike.test.util.ArchiveUtils;
@@ -33,7 +35,8 @@ import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
-import java.net.URL;
+import org.apache.deltaspike.test.core.api.config.injectable.numberconfig.NumberConfiguredBean;
+
@RunWith(Arquillian.class)
@Category(SeCategory.class) //X TODO this is only SeCategory as there is currently an Arq problem with properties!
@@ -49,7 +52,8 @@ public class InjectableConfigPropertyTest
.getResource("META-INF/apache-deltaspike.properties");
JavaArchive testJar = ShrinkWrap.create(JavaArchive.class, "injectableConfigPropertyTest.jar")
- .addPackage("org.apache.deltaspike.test.core.api.config.injectable")
+ .addPackage(SettingsBean.class.getPackage())
+ .addPackage(NumberConfiguredBean.class.getPackage())
.addPackage("org.apache.deltaspike.test.category")
.addAsManifestResource(FileUtils.getFileForURL(fileUrl.toString()))
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
@@ -90,4 +94,13 @@ public class InjectableConfigPropertyTest
Assert.assertEquals(Boolean.TRUE, settingsBean.getBooleanPropertyTrue7());
Assert.assertEquals(Boolean.TRUE, settingsBean.getBooleanPropertyTrue8());
}
+
+ @Test
+ public void testNmberConfigInjection()
+ {
+ NumberConfiguredBean numberBean = BeanProvider.getContextualReference(NumberConfiguredBean.class, false);
+ Assert.assertNull(numberBean.getPropertyNonexisting());
+ Assert.assertEquals(Float.valueOf(123.45f), numberBean.getPropertyFromConfig());
+ Assert.assertEquals(Float.valueOf(42.42f), numberBean.getPropertyNonexistingDefaulted());
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/2fa81d4b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/numberconfig/NumberConfig.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/numberconfig/NumberConfig.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/numberconfig/NumberConfig.java
new file mode 100644
index 0000000..c8f58f8
--- /dev/null
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/numberconfig/NumberConfig.java
@@ -0,0 +1,50 @@
+/*
+ * 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.deltaspike.test.core.api.config.injectable.numberconfig;
+
+import javax.enterprise.util.Nonbinding;
+import javax.inject.Qualifier;
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import org.apache.deltaspike.core.api.config.annotation.ConfigProperty;
+
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.CONSTRUCTOR;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+@Target({ PARAMETER, FIELD, METHOD, CONSTRUCTOR, ANNOTATION_TYPE })
+@Retention(RUNTIME)
+@Documented
+@Qualifier
+public @interface NumberConfig
+{
+ @Nonbinding
+ String name();
+
+ @Nonbinding
+ String defaultValue() default ConfigProperty.NULL;
+
+ @Nonbinding
+ String pattern() default "#0.00";
+}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/2fa81d4b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/numberconfig/NumberConfigPropertyProducer.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/numberconfig/NumberConfigPropertyProducer.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/numberconfig/NumberConfigPropertyProducer.java
new file mode 100644
index 0000000..6a4712d
--- /dev/null
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/numberconfig/NumberConfigPropertyProducer.java
@@ -0,0 +1,55 @@
+/*
+ * 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.deltaspike.test.core.api.config.injectable.numberconfig;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.Dependent;
+import javax.enterprise.inject.Produces;
+import javax.enterprise.inject.spi.InjectionPoint;
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.text.ParseException;
+import java.util.Locale;
+
+import org.apache.deltaspike.core.spi.config.BaseConfigPropertyProducer;
+
+@ApplicationScoped
+@SuppressWarnings("UnusedDeclaration")
+public class NumberConfigPropertyProducer extends BaseConfigPropertyProducer
+{
+ @Produces
+ @Dependent
+ @NumberConfig(name = "unused")
+ public Float produceNumberProperty(InjectionPoint injectionPoint) throws ParseException
+ {
+ // resolve the annotation
+ NumberConfig metaData = getAnnotation(injectionPoint, NumberConfig.class);
+
+ // get the configured value from the underlying configuration system
+ String configuredValue = getPropertyValue(metaData.name(), metaData.defaultValue());
+ if (configuredValue == null)
+ {
+ return null;
+ }
+
+ // format according to the given pattern
+ DecimalFormat df = new DecimalFormat(metaData.pattern(), new DecimalFormatSymbols(Locale.US));
+ return df.parse(configuredValue).floatValue();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/2fa81d4b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/numberconfig/NumberConfiguredBean.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/numberconfig/NumberConfiguredBean.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/numberconfig/NumberConfiguredBean.java
new file mode 100644
index 0000000..b187465
--- /dev/null
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/injectable/numberconfig/NumberConfiguredBean.java
@@ -0,0 +1,59 @@
+/*
+ * 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.deltaspike.test.core.api.config.injectable.numberconfig;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+
+
+@ApplicationScoped
+public class NumberConfiguredBean
+{
+ @Inject
+ @NumberConfig(name = "propertyFloat")
+ private Float propertyFromConfig;
+
+ @Inject
+ @NumberConfig(name = "propertyNonexisting")
+ private Float propertyNonexisting;
+
+ @Inject
+ @NumberConfig(name = "propertyNonexisting", defaultValue = "42.42")
+ private Float propertyNonexistingDefaulted;
+
+
+ protected NumberConfiguredBean()
+ {
+ }
+
+ public Float getPropertyFromConfig()
+ {
+ return propertyFromConfig;
+ }
+
+ public Float getPropertyNonexisting()
+ {
+ return propertyNonexisting;
+ }
+
+ public Float getPropertyNonexistingDefaulted()
+ {
+ return propertyNonexistingDefaulted;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/2fa81d4b/deltaspike/core/impl/src/test/resources/META-INF/apache-deltaspike.properties
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/resources/META-INF/apache-deltaspike.properties b/deltaspike/core/impl/src/test/resources/META-INF/apache-deltaspike.properties
index 66bafc1..12fad78 100644
--- a/deltaspike/core/impl/src/test/resources/META-INF/apache-deltaspike.properties
+++ b/deltaspike/core/impl/src/test/resources/META-INF/apache-deltaspike.properties
@@ -36,3 +36,6 @@ configPropertyTrue5=JA
configPropertyTrue6=OUI
configPropertyTrue7=True
configPropertyTrue8=1
+
+# NumberConfig
+propertyFloat=123.45