You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by ff...@apache.org on 2015/05/27 08:36:49 UTC

karaf git commit: [KARAF-3738]Karaf client should be able to read environment variables

Repository: karaf
Updated Branches:
  refs/heads/karaf-3.0.x 7457d66a0 -> e25a498c0


[KARAF-3738]Karaf client should be able to read environment variables


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

Branch: refs/heads/karaf-3.0.x
Commit: e25a498c04184d955bd46f551c6118e8b63756ac
Parents: 7457d66
Author: Freeman Fang <fr...@gmail.com>
Authored: Wed May 27 14:36:13 2015 +0800
Committer: Freeman Fang <fr...@gmail.com>
Committed: Wed May 27 14:36:13 2015 +0800

----------------------------------------------------------------------
 .../org/apache/karaf/client/ClientConfig.java   | 22 ++++++++++++++++++++
 1 file changed, 22 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/e25a498c/client/src/main/java/org/apache/karaf/client/ClientConfig.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/karaf/client/ClientConfig.java b/client/src/main/java/org/apache/karaf/client/ClientConfig.java
index ab08a72..0516ab0 100644
--- a/client/src/main/java/org/apache/karaf/client/ClientConfig.java
+++ b/client/src/main/java/org/apache/karaf/client/ClientConfig.java
@@ -19,7 +19,10 @@ package org.apache.karaf.client;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.util.Map;
 import java.util.Properties;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.slf4j.impl.SimpleLogger;
 
@@ -43,12 +46,14 @@ public class ClientConfig {
         Properties shellCfg = loadProps(new File(System.getProperty("karaf.etc"), "org.apache.karaf.shell.cfg"));
 
         host = shellCfg.getProperty("sshHost", "localhost");
+        host = expandEnvVars(host);
         if (host.contains("${")) {
             // if sshHost property contains a reference to another property (coming from etc/config.properties
             // or etc/custom.properties), we fall back to "localhost" default value
             host = "localhost";
         }
         String portString = shellCfg.getProperty("sshPort", "8101");
+        portString = expandEnvVars(portString);
         if (portString.contains("${")) {
             // if sshPort property contains a reference to another property (coming from etc/config.properties
             // or etc/custom.properties), we fall back to "8101" default value
@@ -201,6 +206,23 @@ public class ClientConfig {
         return props;
     }
 
+    
+    private static String expandEnvVars(String text) {
+        Map<String, String> envMap = System.getenv();
+        String pattern = "\\$\\{([A-Za-z0-9]+)\\}";
+        Pattern expr = Pattern.compile(pattern);
+        Matcher matcher = expr.matcher(text);
+        while (matcher.find()) {
+            String envValue = envMap.get(matcher.group(1).toUpperCase());
+            if (envValue != null) {
+                envValue = envValue.replace("\\", "\\\\");
+                Pattern subexpr = Pattern.compile(Pattern.quote(matcher.group(0)));
+                text = subexpr.matcher(text).replaceAll(envValue);
+            }
+        }
+        return text;
+    }
+    
     public String getHost() {
         return host;
     }