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/07/15 22:21:11 UTC
git commit: DELTASPIKt-242 PropertyConfigSource and @ConfigProperty
example
Updated Branches:
refs/heads/master ed4c3759a -> 82dd3db3e
DELTASPIKt-242 PropertyConfigSource and @ConfigProperty example
Project: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/commit/82dd3db3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/tree/82dd3db3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/diff/82dd3db3
Branch: refs/heads/master
Commit: 82dd3db3e39ab8fa6345f1c39e744794b8e8a5e0
Parents: ed4c375
Author: gpetracek <gp...@apache.org>
Authored: Sun Jul 15 21:57:10 2012 +0200
Committer: gpetracek <gp...@apache.org>
Committed: Sun Jul 15 22:19:05 2012 +0200
----------------------------------------------------------------------
.../deltaspike/example/config/ConfigExample.java | 54 +++++++++++++++
.../example/config/ConverterExample.java | 54 ---------------
.../config/CustomConfigPropertyProducer.java | 17 +++++
.../example/config/CustomPropertyConfigSource.java | 36 ++++++++++
.../apache/deltaspike/example/config/Location.java | 41 +++++++++++
.../deltaspike/example/config/LocationId.java | 28 ++++++++
.../deltaspike/example/config/SettingsBean.java | 9 +++
.../main/resources/META-INF/location.properties | 18 +++++
8 files changed, 203 insertions(+), 54 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/82dd3db3/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/ConfigExample.java
----------------------------------------------------------------------
diff --git a/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/ConfigExample.java b/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/ConfigExample.java
new file mode 100644
index 0000000..3d9627f
--- /dev/null
+++ b/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/ConfigExample.java
@@ -0,0 +1,54 @@
+/*
+ * 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.cdise.api.CdiContainer;
+import org.apache.deltaspike.cdise.api.CdiContainerLoader;
+import org.apache.deltaspike.cdise.api.ContextControl;
+import org.apache.deltaspike.core.api.provider.BeanProvider;
+
+import javax.enterprise.context.ApplicationScoped;
+import java.util.logging.Logger;
+
+public class ConfigExample
+{
+ private static final Logger LOG = Logger.getLogger(ConfigExample.class.getName());
+
+ private ConfigExample()
+ {
+ }
+
+ public static void main(String[] args)
+ {
+ CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
+ cdiContainer.boot();
+
+ ContextControl contextControl = cdiContainer.getContextControl();
+ contextControl.startContext(ApplicationScoped.class);
+
+ SettingsBean settingsBean = BeanProvider.getContextualReference(SettingsBean.class, false);
+
+ LOG.info("configured int-value #1: " + settingsBean.getIntProperty1());
+ LOG.info("configured long-value #2: " + settingsBean.getProperty2());
+ LOG.info("configured inverse-value #2: " + settingsBean.getInverseProperty());
+ LOG.info("configured location (custom config): " + settingsBean.getLocationId().name());
+
+ cdiContainer.shutdown();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/82dd3db3/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
deleted file mode 100644
index 2f440ab..0000000
--- a/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/ConverterExample.java
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * 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.cdise.api.CdiContainer;
-import org.apache.deltaspike.cdise.api.CdiContainerLoader;
-import org.apache.deltaspike.cdise.api.ContextControl;
-import org.apache.deltaspike.core.api.provider.BeanProvider;
-
-import javax.enterprise.context.ApplicationScoped;
-import java.util.logging.Logger;
-
-public class ConverterExample
-{
- private static final Logger LOG = Logger.getLogger(ConverterExample.class.getName());
-
- private ConverterExample()
- {
- }
-
- public static void main(String[] args)
- {
- CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
- cdiContainer.boot();
-
- ContextControl contextControl = cdiContainer.getContextControl();
- contextControl.startContext(ApplicationScoped.class);
-
- SettingsBean settingsBean = BeanProvider.getContextualReference(SettingsBean.class, false);
-
- 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/82dd3db3/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
index c212569..f9acd94 100644
--- 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
@@ -82,4 +82,21 @@ public class CustomConfigPropertyProducer extends BaseConfigPropertyProducer
return result;
}
+
+ @Produces
+ @Dependent
+ @Location
+ public LocationId produceLocationId(InjectionPoint injectionPoint)
+ {
+ String configuredValue = getStringPropertyValue(injectionPoint);
+
+ /*
+ //alternative to @ConfigProperty#defaultValue
+ if (configuredValue == null)
+ {
+ return LocationId.LOCATION_X;
+ }
+ */
+ return LocationId.valueOf(configuredValue.trim().toUpperCase());
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/82dd3db3/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/CustomPropertyConfigSource.java
----------------------------------------------------------------------
diff --git a/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/CustomPropertyConfigSource.java b/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/CustomPropertyConfigSource.java
new file mode 100644
index 0000000..f7991b0
--- /dev/null
+++ b/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/CustomPropertyConfigSource.java
@@ -0,0 +1,36 @@
+/*
+ * 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.PropertyConfigSource;
+
+import javax.enterprise.context.ApplicationScoped;
+
+/**
+ * Allows to use a different file than apache-deltaspike.properties
+ */
+@ApplicationScoped
+public class CustomPropertyConfigSource implements PropertyConfigSource
+{
+ @Override
+ public String getPropertyFileName()
+ {
+ return "META-INF/location.properties";
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/82dd3db3/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/Location.java
----------------------------------------------------------------------
diff --git a/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/Location.java b/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/Location.java
new file mode 100644
index 0000000..d9a056b
--- /dev/null
+++ b/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/Location.java
@@ -0,0 +1,41 @@
+/*
+ * 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.inject.Qualifier;
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+@Target({ FIELD, METHOD })
+@Retention(RUNTIME)
+@Documented
+
+@ConfigProperty(name = "locationId", defaultValue = "LOCATION_X" /*as an alternative to a null check in the producer*/)
+
+@Qualifier
+public @interface Location
+{
+}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/82dd3db3/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/LocationId.java
----------------------------------------------------------------------
diff --git a/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/LocationId.java b/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/LocationId.java
new file mode 100644
index 0000000..d73d587
--- /dev/null
+++ b/deltaspike/examples/jse-examples/src/main/java/org/apache/deltaspike/example/config/LocationId.java
@@ -0,0 +1,28 @@
+/*
+ * 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;
+
+/**
+ * Enum as an example for a type-safe config with
+ * {@link org.apache.deltaspike.core.api.config.annotation.ConfigProperty}
+ */
+public enum LocationId
+{
+ LOCATION_X, LOCATION_Y, LOCATION_Z
+}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/82dd3db3/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 698db9a..fce7001 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
@@ -30,6 +30,10 @@ public class SettingsBean
@ConfigProperty(name = "property1")
private Integer intProperty1;
+ @Inject
+ @Location
+ private LocationId locationId;
+
private Long property2;
private Long inverseProperty;
@@ -64,4 +68,9 @@ public class SettingsBean
{
return inverseProperty;
}
+
+ public LocationId getLocationId()
+ {
+ return locationId;
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/82dd3db3/deltaspike/examples/jse-examples/src/main/resources/META-INF/location.properties
----------------------------------------------------------------------
diff --git a/deltaspike/examples/jse-examples/src/main/resources/META-INF/location.properties b/deltaspike/examples/jse-examples/src/main/resources/META-INF/location.properties
new file mode 100644
index 0000000..6cc4d23
--- /dev/null
+++ b/deltaspike/examples/jse-examples/src/main/resources/META-INF/location.properties
@@ -0,0 +1,18 @@
+#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.
+
+locationId=location_z