geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kl...@apache.org
Subject [6/8] geode git commit: Minor cleanup
Date Tue, 14 Feb 2017 05:01:52 GMT
Minor cleanup


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

Branch: refs/heads/feature/GEODE-2474
Commit: da250b38fefa412c9841ecbc6ac3c2cf2173e75b
Parents: fb14e9a
Author: Kirk Lund <klund@apache.org>
Authored: Mon Feb 13 15:33:40 2017 -0800
Committer: Kirk Lund <klund@apache.org>
Committed: Mon Feb 13 21:01:33 2017 -0800

----------------------------------------------------------------------
 .../internal/cli/functions/NetstatFunction.java | 113 ++++++++++---------
 1 file changed, 62 insertions(+), 51 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/geode/blob/da250b38/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/NetstatFunction.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/NetstatFunction.java
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/NetstatFunction.java
index c435e04..5fa30f4 100644
--- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/NetstatFunction.java
+++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/NetstatFunction.java
@@ -16,16 +16,20 @@ package org.apache.geode.management.internal.cli.functions;
 
 import java.io.BufferedReader;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.logging.log4j.Logger;
+
 import org.apache.geode.cache.execute.Function;
 import org.apache.geode.cache.execute.FunctionContext;
+import org.apache.geode.distributed.DistributedSystem;
 import org.apache.geode.distributed.internal.InternalDistributedSystem;
-import org.apache.geode.distributed.internal.membership.InternalDistributedMember;
 import org.apache.geode.internal.InternalEntity;
+import org.apache.geode.internal.logging.LogService;
 import org.apache.geode.management.internal.cli.CliUtil;
 import org.apache.geode.management.internal.cli.CliUtil.DeflaterInflaterData;
 import org.apache.geode.management.internal.cli.GfshParser;
@@ -36,7 +40,9 @@ import org.apache.geode.management.internal.cli.i18n.CliStrings;
  * 
  * @since GemFire 7.0
  */
+@SuppressWarnings({"serial"})
 public class NetstatFunction implements Function, InternalEntity {
+  private static final Logger logger = LogService.getLogger();
   private static final long serialVersionUID = 1L;
 
   private static final String OS_NAME_LINUX = "Linux";
@@ -60,29 +66,37 @@ public class NetstatFunction implements Function, InternalEntity {
 
   @Override
   public void execute(final FunctionContext context) {
-    InternalDistributedSystem ds = InternalDistributedSystem.getConnectedInstance();
-    if (ds.isConnected()) {
-      InternalDistributedMember distributedMember = ds.getDistributedMember();
-      String host = distributedMember.getHost();
-      NetstatFunctionArgument args = (NetstatFunctionArgument) context.getArguments();
-      boolean withlsof = args.isWithlsof();
-      String lineSeparator = args.getLineSeparator();
-      String netstatOutput = executeCommand(lineSeparator, withlsof);
-
-      StringBuilder netstatInfo = new StringBuilder();
-      addMemberHostHeader(netstatInfo, "{0}", host, lineSeparator); // {0} will be replaced
on
-                                                                    // Manager
-
-      context.getResultSender().lastResult(new NetstatFunctionResult(host, netstatInfo.toString(),
-          CliUtil.compressBytes(netstatOutput.getBytes())));
+    DistributedSystem ds = InternalDistributedSystem.getConnectedInstance();
+    if (ds == null || !ds.isConnected()) {
+      return;
     }
+
+    String host = ds.getDistributedMember().getHost();
+    NetstatFunctionArgument args = (NetstatFunctionArgument) context.getArguments();
+    boolean withlsof = args.isWithlsof();
+    String lineSeparator = args.getLineSeparator();
+
+    String netstatOutput = executeCommand(lineSeparator, withlsof);
+
+    StringBuilder netstatInfo = new StringBuilder();
+
+    // {0} will be replaced on Manager
+    addMemberHostHeader(netstatInfo, "{0}", host, lineSeparator);
+
+    NetstatFunctionResult result = new NetstatFunctionResult(host, netstatInfo.toString(),
+        CliUtil.compressBytes(netstatOutput.getBytes()));
+
+    context.getResultSender().lastResult(result);
   }
 
   private static void addMemberHostHeader(final StringBuilder netstatInfo, final String id,
       final String host, final String lineSeparator) {
-    StringBuilder memberPlatFormInfo = new StringBuilder();
+
+    // TODO:GEODE-2474: replace with SystemUtils
     String osInfo = System.getProperty(OS_NAME_PROP) + " " + System.getProperty(OS_VERSION_PROP)
         + " " + System.getProperty(OS_ARCH_PROP);
+
+    StringBuilder memberPlatFormInfo = new StringBuilder();
     memberPlatFormInfo.append(CliStrings.format(CliStrings.NETSTAT__MSG__FOR_HOST_1_OS_2_MEMBER_0,
         new Object[] {id, host, osInfo, lineSeparator}));
 
@@ -99,6 +113,7 @@ public class NetstatFunction implements Function, InternalEntity {
   }
 
   private static void addNetstatDefaultOptions(final List<String> cmdOptionsList) {
+    // TODO:GEODE-2474: replace with SystemUtils
     String osName = System.getProperty(OS_NAME_PROP);
     if (OS_NAME_LINUX.equalsIgnoreCase(osName)) {
       cmdOptionsList.add("-v");
@@ -118,23 +133,30 @@ public class NetstatFunction implements Function, InternalEntity {
   }
 
   private static void executeNetstat(final StringBuilder netstatInfo, final String lineSeparator)
{
-    List<String> cmdOptionsList = new ArrayList<String>();
+    List<String> cmdOptionsList = new ArrayList<>();
     cmdOptionsList.add(NETSTAT_COMMAND);
     addNetstatDefaultOptions(cmdOptionsList);
 
-    ProcessBuilder procBuilder = new ProcessBuilder(cmdOptionsList);
+    if (logger.isDebugEnabled()) {
+      logger.debug("NetstatFunction executing {}", cmdOptionsList);
+    }
+
+    ProcessBuilder processBuilder = new ProcessBuilder(cmdOptionsList);
     try {
-      Process netstat = procBuilder.start();
-      InputStreamReader reader = new InputStreamReader(netstat.getInputStream());
-      BufferedReader breader = new BufferedReader(reader);
-      String line = "";
+      Process netstat = processBuilder.start();
+
+      InputStream is = netstat.getInputStream();
+      BufferedReader breader = new BufferedReader(new InputStreamReader(is));
+      String line;
 
       while ((line = breader.readLine()) != null) {
         netstatInfo.append(line).append(lineSeparator);
       }
+
+      // TODO: move to finally-block
       netstat.destroy();
     } catch (IOException e) {
-      // Send error also, if any
+      // TODO: change this to keep the full stack trace
       netstatInfo.append(CliStrings.format(CliStrings.NETSTAT__MSG__COULD_NOT_EXECUTE_0_REASON_1,
           new Object[] {NETSTAT_COMMAND, e.getMessage()}));
     } finally {
@@ -144,11 +166,14 @@ public class NetstatFunction implements Function, InternalEntity {
 
   private static void executeLsof(final StringBuilder existingNetstatInfo,
       final String lineSeparator) {
+    existingNetstatInfo.append("################ ").append(LSOF_COMMAND)
+        .append(" output ###################").append(lineSeparator);
+
+    // TODO:GEODE-2474: replace with SystemUtils
     String osName = System.getProperty(OS_NAME_PROP);
-    existingNetstatInfo.append("################ " + LSOF_COMMAND + " output ###################")
-        .append(lineSeparator);
     if (OS_NAME_LINUX.equalsIgnoreCase(osName) || OS_NAME_MACOS.equalsIgnoreCase(osName)
         || OS_NAME_SOLARIS.equalsIgnoreCase(osName)) {
+
       ProcessBuilder procBuilder = new ProcessBuilder(LSOF_COMMAND);
       try {
         Process lsof = procBuilder.start();
@@ -159,9 +184,10 @@ public class NetstatFunction implements Function, InternalEntity {
         while ((line = breader.readLine()) != null) {
           existingNetstatInfo.append(line).append(lineSeparator);
         }
+        // TODO: move this to finally-block
         lsof.destroy();
       } catch (IOException e) {
-        // Send error also, if any
+        // TODO: change this to keep the full stack trace
         String message = e.getMessage();
         if (message.contains("error=2, No such file or directory")) {
           existingNetstatInfo
@@ -181,7 +207,7 @@ public class NetstatFunction implements Function, InternalEntity {
     }
   }
 
-  public static String executeCommand(final String lineSeparator, final boolean withlsof)
{
+  private static String executeCommand(final String lineSeparator, final boolean withlsof)
{
     StringBuilder netstatInfo = new StringBuilder();
 
     executeNetstat(netstatInfo, lineSeparator);
@@ -208,27 +234,26 @@ public class NetstatFunction implements Function, InternalEntity {
     return false;
   }
 
+  public static void main(final String[] args) {
+    String netstat = executeCommand(GfshParser.LINE_SEPARATOR, true);
+    System.out.println(netstat);
+  }
+
   public static class NetstatFunctionArgument implements Serializable {
     private static final long serialVersionUID = 1L;
 
     private final String lineSeparator;
     private final boolean withlsof;
 
-    public NetstatFunctionArgument(String lineSeparator, boolean withlsof) {
+    public NetstatFunctionArgument(final String lineSeparator, final boolean withlsof) {
       this.lineSeparator = lineSeparator;
       this.withlsof = withlsof;
     }
 
-    /**
-     * @return the lineSeparator
-     */
     public String getLineSeparator() {
       return lineSeparator;
     }
 
-    /**
-     * @return the withlsof
-     */
     public boolean isWithlsof() {
       return withlsof;
     }
@@ -241,37 +266,23 @@ public class NetstatFunction implements Function, InternalEntity {
     private final String headerInfo;
     private final DeflaterInflaterData compressedBytes;
 
-    public NetstatFunctionResult(String host, String headerInfo,
-        DeflaterInflaterData compressedBytes) {
+    protected NetstatFunctionResult(final String host, final String headerInfo,
+        final DeflaterInflaterData compressedBytes) {
       this.host = host;
       this.headerInfo = headerInfo;
       this.compressedBytes = compressedBytes;
     }
 
-    /**
-     * @return the host
-     */
     public String getHost() {
       return host;
     }
 
-    /**
-     * @return the headerInfo
-     */
     public String getHeaderInfo() {
       return headerInfo;
     }
 
-    /**
-     * @return the compressedBytes
-     */
     public DeflaterInflaterData getCompressedBytes() {
       return compressedBytes;
     }
   }
-
-  public static void main(String[] args) {
-    String netstat = executeCommand(GfshParser.LINE_SEPARATOR, true);
-    System.out.println(netstat);
-  }
 }


Mime
View raw message