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/14 11:41:51 UTC

git commit: DELTASPIKE-197 pickup ConfigSource via ServiceLoader

Updated Branches:
  refs/heads/master d69a530d6 -> c05c64d61


DELTASPIKE-197 pickup ConfigSource via ServiceLoader

We once had this functionality. Adding it back after getting
frequent requests about it.


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

Branch: refs/heads/master
Commit: c05c64d61f173921c9a88ab881c384bf46432a98
Parents: d69a530
Author: Mark Struberg <st...@apache.org>
Authored: Thu Jun 14 11:40:01 2012 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Thu Jun 14 11:40:01 2012 +0200

----------------------------------------------------------------------
 .../deltaspike/core/api/config/ConfigResolver.java |    8 ++-
 .../deltaspike/core/spi/config/ConfigSource.java   |    7 ++-
 .../test/api/config/ConfigResolverTest.java        |    7 ++
 .../test/api/config/TestConfigSource.java          |   50 +++++++++++++++
 ....apache.deltaspike.core.spi.config.ConfigSource |   20 ++++++
 5 files changed, 89 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/c05c64d6/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
index 90fe59e..c58cb07 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
@@ -36,7 +36,11 @@ import org.apache.deltaspike.core.util.ServiceUtils;
 import javax.enterprise.inject.Typed;
 
 /**
- * Resolve the configuration via their well defined ordinals.
+ * <p>Resolve the configuration via their well defined ordinals.</p>
+ *
+ * <p>You can provide your own lookup paths by implementing
+ * and registering additional {@link ConfigSource} or
+ * {@link ConfigSourceProvider} implementations.</p>
  */
 @Typed()
 public final class ConfigResolver
@@ -165,7 +169,7 @@ public final class ConfigResolver
 
     private static List<ConfigSource> resolveConfigSources()
     {
-        List<ConfigSource> appConfigSources = new ArrayList<ConfigSource>();
+        List<ConfigSource> appConfigSources = ServiceUtils.loadServiceImplementations(ConfigSource.class);
 
         List<ConfigSourceProvider> configSourceProviderServiceLoader =
             ServiceUtils.loadServiceImplementations(ConfigSourceProvider.class);

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/c05c64d6/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/ConfigSource.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/ConfigSource.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/ConfigSource.java
index c603dbe..692b07a 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/ConfigSource.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/ConfigSource.java
@@ -24,7 +24,12 @@ package org.apache.deltaspike.core.spi.config;
  * JNDI configuration, a properties file, etc</p>
  * 
  * <p>The custom implementation can be 'registered' using a
- * {@link ConfigSourceProvider} </p>
+ * {@link ConfigSourceProvider} or via the
+ * {@link java.util.ServiceLoader} mechanism. In the later case
+ * it must get registered via creating a
+ * <i>META-INF/services/org.apache.deltaspike.core.spi.config.ConfigSource</i>
+ * file and adding the fully qualified class name of your ConfigSource
+ * implementation into it. </p>
  */
 public interface ConfigSource
 {

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/c05c64d6/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/ConfigResolverTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/ConfigResolverTest.java b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/ConfigResolverTest.java
index f44ba47..809ccc1 100644
--- a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/ConfigResolverTest.java
+++ b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/ConfigResolverTest.java
@@ -43,4 +43,11 @@ public class ConfigResolverTest
         Assert.assertEquals("test1", result.get(0));
         Assert.assertEquals("test2", result.get(1));
     }
+
+    @Test
+    public void testStandaloneConfigSource()
+    {
+        Assert.assertNull(ConfigResolver.getPropertyValue("notexisting"));
+        Assert.assertEquals("testvalue", ConfigResolver.getPropertyValue("testkey"));
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/c05c64d6/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java
new file mode 100644
index 0000000..b159acc
--- /dev/null
+++ b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.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.api.config;
+
+import org.apache.deltaspike.core.spi.config.ConfigSource;
+
+/**
+ * Test ConfigSource
+ *
+ * It statically returns 'testvalue' for the key 'testkey'
+ */
+public class TestConfigSource implements ConfigSource
+{
+
+    private int ordinal = 700;
+
+    @Override
+    public String getConfigName()
+    {
+        return "testConfig";
+    }
+
+    @Override
+    public int getOrdinal()
+    {
+        return ordinal;
+    }
+
+    @Override
+    public String getPropertyValue(String key)
+    {
+        return "testkey".equals(key) ? "testvalue" : null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/c05c64d6/deltaspike/core/api/src/test/resources/META-INF/services/org.apache.deltaspike.core.spi.config.ConfigSource
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/test/resources/META-INF/services/org.apache.deltaspike.core.spi.config.ConfigSource b/deltaspike/core/api/src/test/resources/META-INF/services/org.apache.deltaspike.core.spi.config.ConfigSource
new file mode 100644
index 0000000..84d5875
--- /dev/null
+++ b/deltaspike/core/api/src/test/resources/META-INF/services/org.apache.deltaspike.core.spi.config.ConfigSource
@@ -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 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.
+#####################################################################################
+
+org.apache.deltaspike.test.api.config.TestConfigSource