summaryrefslogtreecommitdiffstats
path: root/jack-tests/src/com/android/jack/test/util/ExecuteFile.java
blob: 1f59b634058a31ea1a393ce9c65c92f3712a2b7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed 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 com.android.jack.test.util;

import com.android.sched.util.log.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
 * Class to handle the execution of an external process
 */
public class ExecuteFile {
  @Nonnull
  private final String[] cmdLine;

  @Nonnull
  List<String> env = new ArrayList<String>(0);

  @CheckForNull
  private File workDir;

  @CheckForNull
  private InputStream inStream;
  private boolean inToBeClose;

  @CheckForNull
  private OutputStream outStream;
  private boolean outToBeClose;

  @CheckForNull
  private OutputStream errStream;
  private boolean errToBeClose;
  private boolean verbose;

  @Nonnull
  private final Logger logger;

  public void setErr(@Nonnull File file) throws FileNotFoundException {
    errStream = new FileOutputStream(file);
    errToBeClose = true;
  }

  public void setOut(@Nonnull File file) throws FileNotFoundException {
    outStream = new FileOutputStream(file);
    outToBeClose = true;
  }

  public void setIn(@Nonnull File file) throws FileNotFoundException {
    inStream = new FileInputStream(file);
    inToBeClose = true;
  }

  public void setErr(@Nonnull OutputStream stream) {
    errStream = stream;
  }

  public void setOut(@Nonnull OutputStream stream) {
    outStream = stream;
  }

  public void setIn(@Nonnull InputStream stream) {
    inStream = stream;
  }

  public void setWorkingDir(@Nonnull File dir, boolean create) throws IOException {
    if (!dir.isDirectory()) {
      if (create && !dir.exists()) {
        if (!dir.mkdirs()) {
          throw new IOException("Directory creation failed");
        }
      } else {
        throw new FileNotFoundException(dir.getPath() + " is not a directory");
      }
    }

    workDir = dir;
  }

  public void setVerbose(boolean verbose) {
    this.verbose = verbose;
  }

  public void addEnvVar(@Nonnull String key, @Nonnull String value) {
    env.add(key + "=" + value);
  }

  public ExecuteFile(@Nonnull File exec, @Nonnull String[] args) {
    cmdLine = new String[args.length + 1];
    System.arraycopy(args, 0, cmdLine, 1, args.length);

    cmdLine[0] = exec.getAbsolutePath();
    logger = LoggerFactory.getLogger();
  }

  public ExecuteFile(@Nonnull String exec, @Nonnull String[] args) {
    cmdLine = new String[args.length + 1];
    System.arraycopy(args, 0, cmdLine, 1, args.length);

    cmdLine[0] = exec;
    logger = LoggerFactory.getLogger();
  }

  public ExecuteFile(@Nonnull File exec) {
    cmdLine = new String[1];
    cmdLine[0] = exec.getAbsolutePath();
    logger = LoggerFactory.getLogger();
  }

  public ExecuteFile(@Nonnull String[] cmdLine) {
    this.cmdLine = cmdLine.clone();
    logger = LoggerFactory.getLogger();
  }

  public ExecuteFile(@Nonnull String cmdLine) throws IOException {
    StringReader reader = new StringReader(cmdLine);
    StreamTokenizer tokenizer = new StreamTokenizer(reader);
    tokenizer.resetSyntax();
    // Only standard spaces are recognized as whitespace chars
    tokenizer.whitespaceChars(' ', ' ');
    // Matches alphanumerical and common special symbols like '(' and ')'
    tokenizer.wordChars('!', 'z');
    // Quote chars will be ignored when parsing strings
    tokenizer.quoteChar('\'');
    tokenizer.quoteChar('\"');
    ArrayList<String> tokens = new ArrayList<String>();
    while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
      String token = tokenizer.sval;
      if (token != null) {
        tokens.add(token);
      }
    }
    this.cmdLine = tokens.toArray(new String[0]);
    logger = LoggerFactory.getLogger();
  }

  public int run() throws ExecFileException {
    int ret;
    Process proc = null;
    Thread suckOut = null;
    Thread suckErr = null;
    Thread suckIn = null;

    try {
      StringBuilder cmdLineBuilder = new StringBuilder();
      for (String envElt : env) {
        cmdLineBuilder.append(envElt).append(' ');
      }
      for (String arg : cmdLine) {
        cmdLineBuilder.append(arg).append(' ');
      }
      if (verbose) {
        PrintStream printStream;
        if (outStream instanceof PrintStream) {
          printStream = (PrintStream) outStream;
        } else {
          printStream = System.out;
        }

        if (printStream != null) {
          printStream.println(cmdLineBuilder);
        }
      } else {
        logger.log(Level.INFO, "Execute: {0}", cmdLineBuilder);
      }

      proc = Runtime.getRuntime().exec(cmdLine, env.toArray(new String[env.size()]), workDir);

      InputStream localInStream = inStream;
      if (localInStream != null) {
        suckIn = new Thread(
            new ThreadBytesStreamSucker(localInStream, proc.getOutputStream(), inToBeClose));
      } else {
        proc.getOutputStream().close();
      }

      OutputStream localOutStream = outStream;
      if (localOutStream != null) {
        if (localOutStream instanceof PrintStream) {
          suckOut = new Thread(new ThreadCharactersStreamSucker(proc.getInputStream(),
              (PrintStream) localOutStream, outToBeClose));
        } else {
          suckOut = new Thread(
              new ThreadBytesStreamSucker(proc.getInputStream(), localOutStream, outToBeClose));
        }
      }

      OutputStream localErrStream = errStream;
      if (localErrStream != null) {
        if (localErrStream instanceof PrintStream) {
          suckErr = new Thread(new ThreadCharactersStreamSucker(proc.getErrorStream(),
              (PrintStream) localErrStream, errToBeClose));
        } else {
          suckErr = new Thread(
              new ThreadBytesStreamSucker(proc.getErrorStream(), localErrStream, errToBeClose));
        }
      }

      if (suckIn != null) {
        suckIn.start();
      }
      if (suckOut != null) {
        suckOut.start();
      }
      if (suckErr != null) {
        suckErr.start();
      }

      proc.waitFor();
      if (suckIn != null) {
        suckIn.join();
      }
      if (suckOut != null) {
        suckOut.join();
      }
      if (suckErr != null) {
        suckErr.join();
      }

      ret = proc.exitValue();
      proc.destroy();

      return ret;
    } catch (Exception e) {
      throw new ExecFileException(cmdLine, e);
    }
  }

  private static class ThreadBytesStreamSucker extends BytesStreamSucker implements Runnable {

    public ThreadBytesStreamSucker(@Nonnull InputStream is, @Nonnull OutputStream os,
        boolean toBeClose) {
      super(is, os, toBeClose);
    }

    @Override
    public void run() {
      try {
        suck();
      } catch (IOException e) {
        // Best effort
      }
    }
  }

  private static class ThreadCharactersStreamSucker extends CharactersStreamSucker implements
      Runnable {

    public ThreadCharactersStreamSucker(@Nonnull InputStream is, @Nonnull PrintStream ps,
        boolean toBeClose) {
      super(is, ps, toBeClose);
    }

    @Override
    public void run() {
      try {
        suck();
      } catch (IOException e) {
        // Best effort
      }
    }
  }
}