geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kl...@apache.org
Subject [1/3] incubator-geode git commit: GEODE-68: GFSH SYS_HOST_NAME variable should report hostname if available
Date Thu, 16 Jun 2016 18:16:08 GMT
Repository: incubator-geode
Updated Branches:
  refs/heads/develop 1c5ba1415 -> ca5b45cc8


GEODE-68: GFSH SYS_HOST_NAME variable should report hostname if available

* This closes #157 [klund@apache.org]


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

Branch: refs/heads/develop
Commit: 3c4221895a9e0d5daa9da8d5c461b8700c2c1dbe
Parents: b3955c1
Author: Kevin J. Duling <kduling@pivotal.io>
Authored: Tue Jun 7 13:06:58 2016 -0700
Committer: Kirk Lund <klund@pivotal.io>
Committed: Thu Jun 16 11:15:59 2016 -0700

----------------------------------------------------------------------
 .../management/internal/cli/shell/Gfsh.java     | 37 +++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/3c422189/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/shell/Gfsh.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/shell/Gfsh.java
b/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/shell/Gfsh.java
index 6f4e351..da13bd9 100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/shell/Gfsh.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/shell/Gfsh.java
@@ -20,6 +20,7 @@ import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.PrintStream;
 import java.net.URL;
 import java.text.MessageFormat;
@@ -279,7 +280,7 @@ public class Gfsh extends JLineShell {
   private void initializeEnvironment() {
     env.put(ENV_SYS_USER,              System.getProperty("user.name"));
     env.put(ENV_SYS_USER_HOME,         System.getProperty("user.home"));
-    env.put(ENV_SYS_HOST_NAME,         System.getProperty("user.name"));
+    env.put(ENV_SYS_HOST_NAME,         determineHostName());
     env.put(ENV_SYS_CLASSPATH,         System.getProperty("java.class.path"));
     env.put(ENV_SYS_JAVA_VERSION,      System.getProperty("java.version"));
     env.put(ENV_SYS_OS,                System.getProperty("os.name"));
@@ -306,6 +307,40 @@ public class Gfsh extends JLineShell {
     env.put(ENV_APP_RESULT_VIEWER,            String.valueOf(DEFAULT_APP_RESULT_VIEWER));
   }
 
+  private static String execReadToString(String execCommand) throws IOException {
+    Process proc = Runtime.getRuntime().exec(execCommand);
+    try (InputStream stream = proc.getInputStream()) {
+      try (Scanner s = new Scanner(stream).useDelimiter("\\A")) {
+        return s.hasNext() ? s.next().trim() : "";
+      }
+    }
+  }
+
+  private String determineHostName()
+  {
+    // Windows
+    String hostname = System.getenv("COMPUTERNAME");
+    if (hostname == null) {
+      // Unix / Mac / Cygwin sometimes has this set
+      hostname = System.getenv("HOSTNAME");
+    }
+    if (hostname == null) {
+      try {
+        // Unix / Mac / Windows / Cygwin
+        hostname = execReadToString("hostname");
+      }
+      catch (IOException io) {
+        // happens if hostname binary is not found.
+        hostname = "unknown";
+      }
+    }
+    if (hostname == null || hostname.isEmpty()) {
+      // if it still isn't set, default it.
+      hostname = "unknown";
+    }
+    return hostname;
+  }
+
   public static Gfsh getInstance(boolean launchShell, String[] args, GfshConfig gfshConfig)
     throws ClassNotFoundException, IOException
   {


Mime
View raw message