View Javadoc

1   /*
2    * Copyright 2011 SOFTEC sa. All rights reserved.
3    *
4    * This source code is licensed under the Creative Commons
5    * Attribution-NonCommercial-NoDerivs 3.0 Luxembourg
6    * License.
7    *
8    * To view a copy of this license, visit
9    * http://creativecommons.org/licenses/by-nc-nd/3.0/lu/
10   * or send a letter to Creative Commons, 171 Second Street,
11   * Suite 300, San Francisco, California, 94105, USA.
12   */
13  
14  package org.codehaus.mojo.javascript;
15  
16  import org.apache.maven.plugin.logging.Log;
17  
18  import java.io.BufferedReader;
19  import java.io.IOException;
20  import java.io.InputStreamReader;
21  import java.io.PrintStream;
22  
23  /**
24   * Utitlity class to manage the android emulator Thread.
25   */
26  public class AndroidEmulatorThread extends Thread {
27      /**
28       * The process builder containing the command to launch to execute the emulator.
29       */
30      private ProcessBuilder processBuilder;
31  
32      /**
33       * The maven logger.
34       */
35      private Log log;
36  
37      /**
38       * The emulator process.
39       */
40      private Process process;
41  
42      /**
43       * Create a new instance.
44       * @param processBuilder The ProcessBuilder to use to execute the emulator.
45       * @param log The maven logger.
46       */
47      public AndroidEmulatorThread(ProcessBuilder processBuilder, Log log) {
48          this.processBuilder = processBuilder;
49          this.log = log;
50      }
51  
52      @Override
53      public void run() {
54          try {
55              //processBuilder.redirectErrorStream(true);
56              process = processBuilder.start();
57          } catch (IOException e) {
58              log.error("Error while starting thread", e);
59              process = null;
60          }
61  
62          if (process != null) {
63              outputProcessLog();
64          }
65  
66          System.out.println("process execution finished");
67      }
68  
69      /**
70       * Check if the emulator process is running.
71       * @return true if the emulator process is running,
72       * false otherwise.
73       */
74      public boolean isRunning() {
75          if (process == null) {
76              return false;
77          }
78          try {
79              process.exitValue();
80              return false;
81          } catch (IllegalThreadStateException ise) {
82              return true;
83          }
84      }
85  
86      /**
87       * Output the emulator process log.
88       */
89      private void outputProcessLog() {
90          BufferedReader outReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
91          BufferedReader errReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
92  
93          while (isRunning()) {
94              try {
95                  fetchRow(outReader, System.out);
96                  fetchRow(errReader, System.err);
97              } catch (IOException e) {
98                  System.err.println("Error while reading process input");
99              }
100         }
101 
102         try {
103             fetchRow(outReader, System.out);
104             fetchRow(errReader, System.err);
105         } catch (IOException e) {
106             System.err.println("Error while reading process input");
107         }
108     }
109 
110     /**
111      * Fetch a row from a stream and
112      * output it to the specified output.
113      * @param reader The reader from which to read a row.
114      * @param outStream The output stream.
115      * @throws IOException If an I/O exception occurs.
116      */
117     private void fetchRow(BufferedReader reader, PrintStream outStream) throws IOException {
118         if (reader.ready()) {
119             String line = reader.readLine();
120             if (line != null) {
121                 outStream.println(line);
122             }
123         }
124     }
125 }