You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by se...@apache.org on 2013/08/13 11:37:42 UTC

svn commit: r1513407 - in /jmeter/trunk: src/jorphan/org/apache/jorphan/exec/ src/protocol/native/org/apache/jmeter/protocol/system/ xdocs/

Author: sebb
Date: Tue Aug 13 09:37:42 2013
New Revision: 1513407

URL: http://svn.apache.org/r1513407
Log:
NativeCommand could be useful elsewhere
Moved code to o.a.jorphan.exec
Bugzilla Id: 55411

Added:
    jmeter/trunk/src/jorphan/org/apache/jorphan/exec/
    jmeter/trunk/src/jorphan/org/apache/jorphan/exec/NativeCommand.java   (with props)
    jmeter/trunk/src/jorphan/org/apache/jorphan/exec/StreamCopier.java
      - copied, changed from r1507501, jmeter/trunk/src/protocol/native/org/apache/jmeter/protocol/system/StreamCopier.java
    jmeter/trunk/src/jorphan/org/apache/jorphan/exec/StreamGobbler.java
      - copied, changed from r1507501, jmeter/trunk/src/protocol/native/org/apache/jmeter/protocol/system/StreamGobbler.java
Removed:
    jmeter/trunk/src/protocol/native/org/apache/jmeter/protocol/system/StreamCopier.java
    jmeter/trunk/src/protocol/native/org/apache/jmeter/protocol/system/StreamGobbler.java
Modified:
    jmeter/trunk/src/protocol/native/org/apache/jmeter/protocol/system/NativeCommand.java
    jmeter/trunk/xdocs/changes.xml

Added: jmeter/trunk/src/jorphan/org/apache/jorphan/exec/NativeCommand.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/jorphan/org/apache/jorphan/exec/NativeCommand.java?rev=1513407&view=auto
==============================================================================
--- jmeter/trunk/src/jorphan/org/apache/jorphan/exec/NativeCommand.java (added)
+++ jmeter/trunk/src/jorphan/org/apache/jorphan/exec/NativeCommand.java Tue Aug 13 09:37:42 2013
@@ -0,0 +1,156 @@
+/*
+ * 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.jorphan.exec;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.jorphan.util.JOrphanUtils;
+
+/**
+ * Native Command 
+ */
+public class NativeCommand {
+
+    private StreamGobbler outputGobbler;
+    private final File directory;
+    private final Map<String, String> env;
+    private Map<String, String> executionEnvironment;
+    private final String stdin;
+    private final String stdout;
+    private final String stderr;
+
+    /**
+     * @param env Environment variables appended to environment
+     * @param directory File working directory
+     */
+    public NativeCommand(File directory, Map<String, String> env) {
+        this(directory, env, null, null, null);
+    }
+
+    /**
+     * 
+     * @param env Environment variables appended to environment
+     * @param directory File working directory
+     * @param stdin File name that will contain data to be input to process
+     * @param stdout File name that will contain out stream
+     * @param stderr File name that will contain err stream
+     */
+    public NativeCommand(File directory, Map<String, String> env, String stdin, String stdout, String stderr) {
+        super();
+        this.directory = directory;
+        this.env = env;
+        this.stdin = JOrphanUtils.nullifyIfEmptyTrimmed(stdin);
+        this.stdout = JOrphanUtils.nullifyIfEmptyTrimmed(stdout);
+        this.stderr = JOrphanUtils.nullifyIfEmptyTrimmed(stderr);
+    }
+
+    /**
+     * @param arguments List<String>
+     * @return return code
+     * @throws InterruptedException
+     * @throws IOException
+     */
+    public int run(List<String> arguments) throws InterruptedException, IOException {
+        Process proc = null;
+        try
+        {
+            ProcessBuilder procBuild = new ProcessBuilder(arguments);
+            procBuild.environment().putAll(env);
+            this.executionEnvironment = Collections.unmodifiableMap(procBuild.environment());
+            procBuild.directory(directory);
+            if (stderr == null || stderr.equals(stdout)) { // we're not redirecting stderr separately
+                procBuild.redirectErrorStream(true);
+            }
+            proc = procBuild.start();
+            StreamCopier swerr = null;
+            if (!procBuild.redirectErrorStream()) { // stderr has separate output file
+                swerr = new StreamCopier(proc.getErrorStream(), new FileOutputStream(stderr));
+                swerr.start();
+            }
+            
+            StreamCopier swout = null;
+            if (stdout != null) {
+                swout = new StreamCopier(proc.getInputStream(), new FileOutputStream(stdout));
+                swout.start();
+            } else {
+                outputGobbler = new StreamGobbler(proc.getInputStream());
+                outputGobbler.start();
+            }
+            
+            StreamCopier swin = null;
+            if (stdin != null) {
+                swin = new StreamCopier(new FileInputStream(stdin), proc.getOutputStream());
+                swin.start();
+            } else {
+                proc.getOutputStream().close(); // ensure the application does not hang if it requests input
+            }
+            int exitVal = proc.waitFor();
+
+            if (outputGobbler != null) {
+                outputGobbler.join();
+            }
+            if (swout != null) {
+                swout.join();
+            }
+            if (swerr != null) {
+                swerr.join();
+            }
+            if (swin != null) {
+                swin.interrupt(); // the copying thread won't generally detect EOF
+                swin.join();
+            }
+            return exitVal;
+        }
+        finally
+        {
+            if(proc != null)
+            {
+                try {
+                    proc.destroy();
+                } catch (Exception ignored) {
+                    // Ignored
+                }
+            }
+        }
+    }
+
+    /**
+     * @return Out/Err stream contents
+     */
+    public String getOutResult() {
+        if(outputGobbler != null) {    
+            return outputGobbler.getResult();
+        } else {
+            return "";
+        }
+    }
+
+    /**
+     * @return the executionEnvironment
+     */
+    public Map<String, String> getExecutionEnvironment() {
+        return executionEnvironment;
+    }
+}
\ No newline at end of file

Propchange: jmeter/trunk/src/jorphan/org/apache/jorphan/exec/NativeCommand.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jmeter/trunk/src/jorphan/org/apache/jorphan/exec/NativeCommand.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Copied: jmeter/trunk/src/jorphan/org/apache/jorphan/exec/StreamCopier.java (from r1507501, jmeter/trunk/src/protocol/native/org/apache/jmeter/protocol/system/StreamCopier.java)
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/jorphan/org/apache/jorphan/exec/StreamCopier.java?p2=jmeter/trunk/src/jorphan/org/apache/jorphan/exec/StreamCopier.java&p1=jmeter/trunk/src/protocol/native/org/apache/jmeter/protocol/system/StreamCopier.java&r1=1507501&r2=1513407&rev=1513407&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/native/org/apache/jmeter/protocol/system/StreamCopier.java (original)
+++ jmeter/trunk/src/jorphan/org/apache/jorphan/exec/StreamCopier.java Tue Aug 13 09:37:42 2013
@@ -16,7 +16,7 @@
  *
  */
 
-package org.apache.jmeter.protocol.system;
+package org.apache.jorphan.exec;
 
 import java.io.IOException;
 import java.io.InputStream;

Copied: jmeter/trunk/src/jorphan/org/apache/jorphan/exec/StreamGobbler.java (from r1507501, jmeter/trunk/src/protocol/native/org/apache/jmeter/protocol/system/StreamGobbler.java)
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/jorphan/org/apache/jorphan/exec/StreamGobbler.java?p2=jmeter/trunk/src/jorphan/org/apache/jorphan/exec/StreamGobbler.java&p1=jmeter/trunk/src/protocol/native/org/apache/jmeter/protocol/system/StreamGobbler.java&r1=1507501&r2=1513407&rev=1513407&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/native/org/apache/jmeter/protocol/system/StreamGobbler.java (original)
+++ jmeter/trunk/src/jorphan/org/apache/jorphan/exec/StreamGobbler.java Tue Aug 13 09:37:42 2013
@@ -16,7 +16,7 @@
  *
  */
 
-package org.apache.jmeter.protocol.system;
+package org.apache.jorphan.exec;
 
 import java.io.BufferedReader;
 import java.io.IOException;

Modified: jmeter/trunk/src/protocol/native/org/apache/jmeter/protocol/system/NativeCommand.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/native/org/apache/jmeter/protocol/system/NativeCommand.java?rev=1513407&r1=1513406&r2=1513407&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/native/org/apache/jmeter/protocol/system/NativeCommand.java (original)
+++ jmeter/trunk/src/protocol/native/org/apache/jmeter/protocol/system/NativeCommand.java Tue Aug 13 09:37:42 2013
@@ -19,34 +19,19 @@
 package org.apache.jmeter.protocol.system;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.util.Collections;
-import java.util.List;
 import java.util.Map;
 
-import org.apache.jorphan.util.JOrphanUtils;
-
 /**
  * Native Command 
  */
-public class NativeCommand {
-
-    private StreamGobbler outputGobbler;
-    private final File directory;
-    private final Map<String, String> env;
-    private Map<String, String> executionEnvironment;
-    private final String stdin;
-    private final String stdout;
-    private final String stderr;
+public class NativeCommand extends  org.apache.jorphan.exec.NativeCommand {
 
     /**
      * @param env Environment variables appended to environment
      * @param directory File working directory
      */
     public NativeCommand(File directory, Map<String, String> env) {
-        this(directory, env, null, null, null);
+        super(directory, env);
     }
 
     /**
@@ -58,99 +43,7 @@ public class NativeCommand {
      * @param stderr File name that will contain err stream
      */
     public NativeCommand(File directory, Map<String, String> env, String stdin, String stdout, String stderr) {
-        super();
-        this.directory = directory;
-        this.env = env;
-        this.stdin = JOrphanUtils.nullifyIfEmptyTrimmed(stdin);
-        this.stdout = JOrphanUtils.nullifyIfEmptyTrimmed(stdout);
-        this.stderr = JOrphanUtils.nullifyIfEmptyTrimmed(stderr);
-    }
-
-    /**
-     * @param arguments List<String>
-     * @return return code
-     * @throws InterruptedException
-     * @throws IOException
-     */
-    public int run(List<String> arguments) throws InterruptedException, IOException {
-        Process proc = null;
-        try
-        {
-            ProcessBuilder procBuild = new ProcessBuilder(arguments);
-            procBuild.environment().putAll(env);
-            this.executionEnvironment = Collections.unmodifiableMap(procBuild.environment());
-            procBuild.directory(directory);
-            if (stderr == null || stderr.equals(stdout)) { // we're not redirecting stderr separately
-                procBuild.redirectErrorStream(true);
-            }
-            proc = procBuild.start();
-            StreamCopier swerr = null;
-            if (!procBuild.redirectErrorStream()) { // stderr has separate output file
-                swerr = new StreamCopier(proc.getErrorStream(), new FileOutputStream(stderr));
-                swerr.start();
-            }
-            
-            StreamCopier swout = null;
-            if (stdout != null) {
-                swout = new StreamCopier(proc.getInputStream(), new FileOutputStream(stdout));
-                swout.start();
-            } else {
-                outputGobbler = new StreamGobbler(proc.getInputStream());
-                outputGobbler.start();
-            }
-            
-            StreamCopier swin = null;
-            if (stdin != null) {
-                swin = new StreamCopier(new FileInputStream(stdin), proc.getOutputStream());
-                swin.start();
-            } else {
-                proc.getOutputStream().close(); // ensure the application does not hang if it requests input
-            }
-            int exitVal = proc.waitFor();
-
-            if (outputGobbler != null) {
-                outputGobbler.join();
-            }
-            if (swout != null) {
-                swout.join();
-            }
-            if (swerr != null) {
-                swerr.join();
-            }
-            if (swin != null) {
-                swin.interrupt(); // the copying thread won't generally detect EOF
-                swin.join();
-            }
-            return exitVal;
-        }
-        finally
-        {
-            if(proc != null)
-            {
-                try {
-                    proc.destroy();
-                } catch (Exception ignored) {
-                    // Ignored
-                }
-            }
-        }
+        super(directory, env, stdin, stdout, stderr);
     }
 
-    /**
-     * @return Out/Err stream contents
-     */
-    public String getOutResult() {
-        if(outputGobbler != null) {    
-            return outputGobbler.getResult();
-        } else {
-            return "";
-        }
-    }
-
-    /**
-     * @return the executionEnvironment
-     */
-    public Map<String, String> getExecutionEnvironment() {
-        return executionEnvironment;
-    }
 }
\ No newline at end of file

Modified: jmeter/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jmeter/trunk/xdocs/changes.xml?rev=1513407&r1=1513406&r2=1513407&view=diff
==============================================================================
--- jmeter/trunk/xdocs/changes.xml (original)
+++ jmeter/trunk/xdocs/changes.xml Tue Aug 13 09:37:42 2013
@@ -387,6 +387,7 @@ Previously the default was 1, which coul
 <li><bugzilla>54903</bugzilla> - Remove the dependency on the Activation Framework. Contributed by Emmanuel Bourg (ebourg at apache.org)</li>
 <li>Moved commons-lang (2.6) to lib/doc as it's only needed by Velocity.</li>
 <li>Re-organised and simplified NOTICE and LICENSE files.</li>
+<li><bugzilla>55411</bugzilla> -  NativeCommand could be useful elsewhere. Copied code to o.a.jorphan.exec.</li>
 </ul>
 
 <h2>Thanks</h2>