View Javadoc

1   /*
2    * Copyright 2007 scala-tools.org
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing,
11   * software distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions
14   * and limitations under the License.
15   */
16  package org.scala_tools.maven;
17  
18  import java.io.BufferedReader;
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.InputStreamReader;
22  import java.io.PrintWriter;
23  
24  import org.apache.maven.plugin.logging.Log;
25  import org.codehaus.plexus.util.IOUtil;
26  import org.codehaus.plexus.util.StringUtils;
27  
28  public class StreamLogger extends Thread {
29  	private static final String LS = System.getProperty("line.separator");
30  	private static final boolean emacsMode = StringUtils.isNotEmpty(System.getProperty("emacsMode"));
31  
32      private InputStream in_;
33      private Log log_;
34      private boolean isErr_;
35      private PrintWriter out_;
36  
37      public StreamLogger(InputStream in, Log log, boolean isErr) {
38          in_ = in;
39          log_ = log;
40          isErr_ = isErr;
41      }
42  
43      @Override
44      public void run() {
45          BufferedReader reader = null;
46          try {
47              reader = new BufferedReader(new InputStreamReader(in_));
48              String line = null;
49              StringBuilder sb = null;
50              while ((line = reader.readLine()) != null) {
51                  if (isErr_) {
52                  	if (!emacsMode) {
53                          log_.warn(line);
54                  	} else {
55                  		if (sb == null) {
56                  			sb = new StringBuilder("Compilation failure"+ LS);
57                  		}
58                      	sb.append(LS + line);
59                  	}
60                  } else {
61                      log_.info(line);
62                  }
63              }
64              if (sb != null) {
65              	log_.warn(sb.toString());
66              }
67          } catch(IOException exc) {
68              throw new RuntimeException("wrap: " + exc.getMessage(), exc);
69          } finally {
70              IOUtil.close(reader);
71              IOUtil.close(in_);
72              IOUtil.close(out_);
73          }
74      }
75  }