View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.os;
33  
34  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
35  import java.io.BufferedReader;
36  import java.io.IOException;
37  import java.io.InputStream;
38  import java.io.InputStreamReader;
39  import java.nio.charset.Charset;
40  import java.util.logging.Level;
41  import java.util.logging.Logger;
42  import org.spf4j.base.Strings;
43  
44  /**
45   * @author Zoltan Farkas
46   */
47  @SuppressFBWarnings("FCCD_FIND_CLASS_CIRCULAR_DEPENDENCY")
48  public final class StdOutToStringProcessHandler implements ProcessHandler<String, String> {
49  
50    private Logger log;
51  
52    public StdOutToStringProcessHandler() {
53      this.log = Logger.getLogger(StdOutToStringProcessHandler.class.getName());
54    }
55  
56    public void started(final Process p) {
57      int pid = ProcessUtil.getPid(p);
58      log.log(Level.FINE, "Started {0} with pid={1} ", new Object[]{p, pid});
59      this.log = Logger.getLogger(log.getName() + '.' + pid);
60    }
61  
62    @Override
63    public String handleStdOut(final InputStream stdout) throws IOException {
64      StringBuilder result = new StringBuilder(128);
65      BufferedReader reader = new BufferedReader(new InputStreamReader(stdout, Charset.defaultCharset()));
66      String line;
67      while ((line = reader.readLine()) != null) {
68        log.fine(line);
69        result.append(line).append(Strings.EOL);
70      }
71      log.fine("done with stdout");
72      return result.toString();
73    }
74  
75    @Override
76    public String handleStdErr(final InputStream stderr) throws IOException {
77      StringBuilder result = new StringBuilder(128);
78      BufferedReader reader = new BufferedReader(new InputStreamReader(stderr, Charset.defaultCharset()));
79      String line;
80      while ((line = reader.readLine()) != null) {
81        if (line.startsWith("INFO")) {
82          log.info(line);
83        } else if (line.startsWith("WARN")) {
84          log.warning(line);
85        } else {
86          log.severe(line);
87        }
88        result.append(line).append(Strings.EOL);
89      }
90      log.fine("done with stderr");
91      return result.toString();
92    }
93  
94    @Override
95    public String toString() {
96      return "StdOutToStringProcessHandler{" + "log=" + log + '}';
97    }
98  
99  }