You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by gp...@apache.org on 2012/06/14 02:19:40 UTC
git commit: DELTASPIKE-196 revert dropped example;
helper for custom annotations
Updated Branches:
refs/heads/master 15dde98d6 -> 8a3fe7196
DELTASPIKE-196 revert dropped example; helper for custom annotations
Project: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/commit/8a3fe719
Tree: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/tree/8a3fe719
Diff: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/diff/8a3fe719
Branch: refs/heads/master
Commit: 8a3fe7196ce43c586f45b097c8fbae739ad63f86
Parents: 15dde98
Author: gpetracek <gp...@apache.org>
Authored: Thu Jun 14 02:09:16 2012 +0200
Committer: gpetracek <gp...@apache.org>
Committed: Thu Jun 14 02:15:33 2012 +0200
----------------------------------------------------------------------
.../spi/config/BaseConfigPropertyProducer.java | 26 +++--
.../example/config/ConverterExample.java | 1 +
.../config/CustomConfigPropertyProducer.java | 85 +++++++++++++++
.../deltaspike/example/config/Property2.java | 47 ++++++++
.../config/Property2WithInverseSupport.java | 47 ++++++++
.../deltaspike/example/config/SettingsBean.java | 13 ++-
6 files changed, 207 insertions(+), 12 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/8a3fe719/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 e3c588b..f0bdfa2 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
@@ -33,13 +33,13 @@ import org.apache.deltaspike.core.api.config.annotation.ConfigProperty;
*/
public abstract class BaseConfigPropertyProducer
{
-
/**
+ * @param injectionPoint current injection point
* @return the configured value for the given InjectionPoint
*/
protected String getStringPropertyValue(InjectionPoint injectionPoint)
{
- ConfigProperty configProperty = extractConfigProperty(injectionPoint.getAnnotated());
+ ConfigProperty configProperty = getAnnotation(injectionPoint, ConfigProperty.class);
if (configProperty == null)
{
@@ -48,6 +48,7 @@ public abstract class BaseConfigPropertyProducer
String configuredValue;
String defaultValue = configProperty.defaultValue();
+
if (ConfigProperty.NULL.equals(defaultValue))
{
// no special defaultValue has been configured
@@ -61,28 +62,31 @@ public abstract class BaseConfigPropertyProducer
return configuredValue;
}
-
/**
- * Extract the ConfigProperty annotation from the given Annotated member.
+ * @param injectionPoint current injection point
+ * @param targetType target type
+ * @param <T> type
+ * @return annotation instance extracted from the injection point which matches the given type
*/
- protected ConfigProperty extractConfigProperty(Annotated annotated)
+ protected <T extends Annotation> T getAnnotation(InjectionPoint injectionPoint, Class<T> targetType)
{
- ConfigProperty configProperty = annotated.getAnnotation(ConfigProperty.class);
+ Annotated annotated = injectionPoint.getAnnotated();
- if (configProperty == null)
+ T result = annotated.getAnnotation(targetType);
+
+ if (result == null)
{
- // scan for meta-annotation
for (Annotation annotation : annotated.getAnnotations())
{
- configProperty = annotation.annotationType().getAnnotation(ConfigProperty.class);
+ result = annotation.annotationType().getAnnotation(targetType);
- if (configProperty != null)
+ if (result != null)
{
break;
}
}
}
- return configProperty;
+ return result;
}
}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/8a3fe719/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/ConverterExample.java
----------------------------------------------------------------------
diff --git a/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/ConverterExample.java b/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/ConverterExample.java
index 9574be7..2f440ab 100644
--- a/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/ConverterExample.java
+++ b/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/ConverterExample.java
@@ -46,6 +46,7 @@ public class ConverterExample
LOG.info("configured int-value #1: " + settingsBean.getIntProperty1());
LOG.info("configured long-value #2: " + settingsBean.getProperty2());
+ LOG.info("configured inverse-value #2: " + settingsBean.getInverseProperty());
contextControl.stopContext(ApplicationScoped.class);
cdiContainer.shutdown();
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/8a3fe719/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/CustomConfigPropertyProducer.java
----------------------------------------------------------------------
diff --git a/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/CustomConfigPropertyProducer.java b/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/CustomConfigPropertyProducer.java
new file mode 100644
index 0000000..c212569
--- /dev/null
+++ b/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/CustomConfigPropertyProducer.java
@@ -0,0 +1,85 @@
+/*
+ * 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.example.config;
+
+import org.apache.deltaspike.core.spi.config.BaseConfigPropertyProducer;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.Dependent;
+import javax.enterprise.inject.Produces;
+import javax.enterprise.inject.spi.InjectionPoint;
+import java.util.logging.Logger;
+
+/**
+ * Equivalent to a custom converter
+ */
+@ApplicationScoped
+@SuppressWarnings("UnusedDeclaration")
+public class CustomConfigPropertyProducer extends BaseConfigPropertyProducer
+{
+ private static final Logger LOG = Logger.getLogger(CustomConfigPropertyProducer.class.getName());
+
+ @Produces
+ @Dependent
+ @Property2
+ public Long produceProperty2(InjectionPoint injectionPoint)
+ {
+ String configuredValue = getStringPropertyValue(injectionPoint);
+
+ if (configuredValue == null)
+ {
+ return null;
+ }
+
+ Property2 metaData = getAnnotation(injectionPoint, Property2.class);
+
+ if (metaData.logValue())
+ {
+ LOG.info("value of property 2: " + configuredValue);
+ }
+
+ //X TODO integrate with the HandledHandler of DeltaSpike
+ return Long.parseLong(configuredValue);
+ }
+
+ @Produces
+ @Dependent
+ @Property2WithInverseSupport
+ public Long produceInverseProperty2(InjectionPoint injectionPoint)
+ {
+ String configuredValue = getStringPropertyValue(injectionPoint);
+
+ if (configuredValue == null)
+ {
+ return null;
+ }
+
+ //X TODO integrate with the HandledHandler of DeltaSpike
+ Long result = Long.parseLong(configuredValue);
+
+ Property2WithInverseSupport metaData = getAnnotation(injectionPoint, Property2WithInverseSupport.class);
+
+ if (metaData.inverseConvert())
+ {
+ return result * -1;
+ }
+
+ return result;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/8a3fe719/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/Property2.java
----------------------------------------------------------------------
diff --git a/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/Property2.java b/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/Property2.java
new file mode 100644
index 0000000..ef012da
--- /dev/null
+++ b/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/Property2.java
@@ -0,0 +1,47 @@
+/*
+ * 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.example.config;
+
+import org.apache.deltaspike.core.api.config.annotation.ConfigProperty;
+
+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 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
+
+@ConfigProperty(name = "property2")
+
+@Qualifier
+public @interface Property2
+{
+ @Nonbinding
+ boolean logValue() default true;
+}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/8a3fe719/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/Property2WithInverseSupport.java
----------------------------------------------------------------------
diff --git a/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/Property2WithInverseSupport.java b/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/Property2WithInverseSupport.java
new file mode 100644
index 0000000..c411916
--- /dev/null
+++ b/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/Property2WithInverseSupport.java
@@ -0,0 +1,47 @@
+/*
+ * 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.example.config;
+
+import org.apache.deltaspike.core.api.config.annotation.ConfigProperty;
+
+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 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
+
+@ConfigProperty(name = "property2")
+
+@Qualifier
+public @interface Property2WithInverseSupport
+{
+ @Nonbinding
+ boolean inverseConvert() default false;
+}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/8a3fe719/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/SettingsBean.java
----------------------------------------------------------------------
diff --git a/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/SettingsBean.java b/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/SettingsBean.java
index 01e07fe..698db9a 100644
--- a/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/SettingsBean.java
+++ b/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/SettingsBean.java
@@ -32,16 +32,23 @@ public class SettingsBean
private Long property2;
+ private Long inverseProperty;
+
protected SettingsBean()
{
}
@Inject
- public SettingsBean(@ConfigProperty(name = "property2") Long property2)
+ public SettingsBean(@Property2 Long property2)
{
this.property2 = property2;
}
+ @Inject
+ protected void init(@Property2WithInverseSupport(inverseConvert = true) Long inverseProperty)
+ {
+ this.inverseProperty = inverseProperty;
+ }
public Integer getIntProperty1()
{
@@ -53,4 +60,8 @@ public class SettingsBean
return property2;
}
+ public Long getInverseProperty()
+ {
+ return inverseProperty;
+ }
}