View Javadoc

1   /*
2    *     $File:  $
3    *   $Change:  $
4    * $Revision:  $
5    * $DateTime:  $
6    *   $Author:  $
7    *
8    * Licensed Materials - Exclusive Property of TUI InfoTec GmbH, Hannover
9    * Copyright TUI InfoTec GmbH 2005 All Rights Reserved
10   */
11  package de.matthias_burbach.deputy.core.util;
12  
13  import java.io.BufferedReader;
14  import java.io.InputStream;
15  import java.io.InputStreamReader;
16  
17  /***
18   * @author Matthias Burbach
19   */
20  public class CommandLine {
21      /***
22       * Holds the output written to standard out by {@link #execute(String)}.
23       */
24      private StringBuffer stdout = new StringBuffer();
25  
26      /***
27       * Holds the output written to standard error by {@link #execute(String)}.
28       */
29      private StringBuffer stderr = new StringBuffer();
30  
31      /***
32       * Executes the command on the command line.
33       *
34       * @param command The command to execute.
35       * @return The exit value of the process executed.
36       *         By convention, 0 usually indicates normal execution.
37       */
38      public int execute(final String command) {
39          int exitValue = -1;
40          try {
41              Process process = Runtime.getRuntime().exec(command);
42              captureOutput(process.getInputStream(), stdout);
43              captureOutput(process.getErrorStream(), stderr);
44              exitValue = process.waitFor();
45          } catch (Exception e) {
46              e.printStackTrace();
47          }
48          return exitValue;
49      }
50  
51      /***
52       * @return The standard out output of the last command executed.
53       */
54      public String getStdout() {
55          return stdout.toString();
56      }
57  
58      /***
59       * This variant is helpful if regular expression pattern matching is to be
60       * applied on the string.
61       *
62       * @return The standard out output of the last command executed.
63       *         Each new line character is replaced by a blank.
64       */
65      public String getStdoutWithoutNewLines() {
66          return stdout.toString().replace('\n', ' ');
67      }
68  
69      /***
70       * @return The standard error output of the last command executed.
71       */
72      public String getStdErr() {
73          return stderr.toString();
74      }
75  
76      /***
77       * This variant is helpful if regular expression pattern matching is to be
78       * applied on the string.
79       *
80       * @return The standard error output of the last command executed.
81       *         Each new line character is replaced by a blank.
82       */
83      public String getStdErrWithoutNewLines() {
84          return stderr.toString().replace('\n', ' ');
85      }
86  
87      /***
88       * @param is The input stream to capture the output from.
89       * @param storage The storage to save the captured output in.
90       * @throws Exception if anything goes wrong
91       */
92      private void captureOutput(
93              final InputStream is, final StringBuffer storage) throws Exception {
94          storage.setLength(0);
95          InputStreamReader isr = new InputStreamReader(is);
96          BufferedReader br = new BufferedReader(isr);
97          String line = null;
98          while ((line = br.readLine()) != null) {
99            storage.append(line + "\n");
100         }
101     }
102 }