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 2017/05/16 08:56:46 UTC

deltaspike git commit: DELTASPIKE-1249 pick up configuration from usrer home folder

Repository: deltaspike
Updated Branches:
  refs/heads/master f767c80c8 -> 842c84bd1


DELTASPIKE-1249 pick up configuration from usrer home folder

The location which will be looked for is
~/.deltaspike/apache-deltaspike.properties

A ConfigSource for it will only be added if this file exists.


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

Branch: refs/heads/master
Commit: 842c84bd1767905a19e2af01bdfe88d416d1b2da
Parents: f767c80
Author: Mark Struberg <st...@apache.org>
Authored: Tue May 16 10:55:15 2017 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Tue May 16 10:55:15 2017 +0200

----------------------------------------------------------------------
 .../config/DefaultConfigSourceProvider.java     | 42 +++++++++++++++++++-
 .../test/core/api/config/ConfigSourceTest.java  | 38 ++++++++++++++++++
 2 files changed, 78 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/842c84bd/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DefaultConfigSourceProvider.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DefaultConfigSourceProvider.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DefaultConfigSourceProvider.java
index 500839d..fa80e54 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DefaultConfigSourceProvider.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DefaultConfigSourceProvider.java
@@ -23,8 +23,12 @@ import org.apache.deltaspike.core.spi.config.ConfigSource;
 import org.apache.deltaspike.core.spi.config.ConfigSourceProvider;
 import org.apache.deltaspike.core.util.ServiceUtils;
 
+import java.io.File;
+import java.net.MalformedURLException;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 /**
  * Default implementation which uses:
@@ -37,7 +41,11 @@ import java.util.List;
  */
 public class DefaultConfigSourceProvider implements ConfigSourceProvider
 {
-    private static final String PROPERTY_FILE_NAME = "META-INF/apache-deltaspike.properties";
+    private static final Logger LOG = Logger.getLogger(DefaultConfigSourceProvider.class.getName());
+
+    private static final String PROPERTY_FILE_NAME = "apache-deltaspike.properties";
+    private static final String PROPERTY_FILE_RESOURCE = "META-INF/" + PROPERTY_FILE_NAME;
+    private static final String PROPERTY_FILE_HOME_NAME = "/.deltaspike/" + PROPERTY_FILE_NAME;
 
     private List<ConfigSource> configSources = new ArrayList<ConfigSource>();
 
@@ -50,8 +58,10 @@ public class DefaultConfigSourceProvider implements ConfigSourceProvider
         configSources.add(new EnvironmentPropertyConfigSource());
         configSources.add(new LocalJndiConfigSource());
 
+        addUserHomeConfigSource();
+
         EnvironmentPropertyConfigSourceProvider epcsp =
-            new EnvironmentPropertyConfigSourceProvider(PROPERTY_FILE_NAME, true);
+            new EnvironmentPropertyConfigSourceProvider(PROPERTY_FILE_RESOURCE, true);
         configSources.addAll(epcsp.getConfigSources());
 
         registerPropertyFileConfigs();
@@ -59,6 +69,34 @@ public class DefaultConfigSourceProvider implements ConfigSourceProvider
 
 
     /**
+     * Add a ConfigSource for files in the user home folder IF it exists!
+     * The location is ~/.deltaspike/apache-deltaspike.properties
+     */
+    private void addUserHomeConfigSource()
+    {
+        String userHome = System.getProperty("user.home");
+        if (userHome != null && !userHome.isEmpty())
+        {
+            File dsHome = new File(userHome, PROPERTY_FILE_HOME_NAME);
+            if (dsHome.exists())
+            {
+                try
+                {
+                    ConfigSource dsHomeConfigSource = new PropertyFileConfigSource(dsHome.toURI().toURL());
+                    configSources.add(dsHomeConfigSource);
+                    LOG.log(Level.INFO, "Reading configuration from {}", dsHome.getAbsolutePath());
+                }
+                catch (MalformedURLException e)
+                {
+                    LOG.log(Level.WARNING, "Could not read configuration from " + dsHome.getAbsolutePath(), e);
+                }
+
+            }
+        }
+    }
+
+
+    /**
      * Load all {@link PropertyFileConfig}s which are registered via
      * {@code java.util.ServiceLoader}.
      */

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/842c84bd/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigSourceTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigSourceTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigSourceTest.java
index 89407de..ee045d2 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigSourceTest.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/ConfigSourceTest.java
@@ -18,15 +18,23 @@
  */
 package org.apache.deltaspike.test.core.api.config;
 
+import java.io.File;
+import java.io.FileWriter;
+
 import org.apache.deltaspike.core.api.config.ConfigResolver;
 import org.junit.Assert;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
 
 /**
  * Unit tests for {@link org.apache.deltaspike.core.spi.config.ConfigSource}
  */
 public class ConfigSourceTest
 {
+    @Rule
+    public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
     @Test
     public void testConfigViaSystemProperty()
     {
@@ -110,4 +118,34 @@ public class ConfigSourceTest
     }
 
 
+    @Test
+    public void testUserHomeConfigProperties() throws Exception {
+        String userHomeKey = "user.home";
+        String oldUserHome = System.getProperty(userHomeKey);
+        try
+        {
+            File newUserHomeFolder = temporaryFolder.newFolder();
+            System.setProperty(userHomeKey, newUserHomeFolder.getAbsolutePath());
+
+            File dsHomeConfig = new File(newUserHomeFolder, ".deltaspike/apache-deltaspike.properties");
+            dsHomeConfig.getParentFile().mkdirs();
+
+            FileWriter fw = new FileWriter(dsHomeConfig);
+            fw.write("ds.test.fromHome=withLove\ndeltaspike_ordinal=123");
+
+            fw.close();
+
+            // force freshly picking up all ConfigSources for this test
+            ConfigResolver.freeConfigSources();
+
+            Assert.assertEquals("withLove", ConfigResolver.getPropertyValue("ds.test.fromHome"));
+        }
+        finally
+        {
+            System.setProperty(userHomeKey, oldUserHome);
+            ConfigResolver.freeConfigSources();
+        }
+    }
+
+
 }