1 /*
   2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.test.lib.process;
  25 
  26 import java.io.ByteArrayOutputStream;
  27 import java.io.InputStream;
  28 import java.time.Instant;
  29 import java.util.concurrent.CancellationException;
  30 import java.util.concurrent.ExecutionException;
  31 import java.util.concurrent.Future;
  32 
  33 public interface OutputBuffer {
  34   public static class OutputBufferException extends RuntimeException {
  35     private static final long serialVersionUID = 8528687792643129571L;
  36 
  37     public OutputBufferException(Throwable cause) {
  38       super(cause);
  39     }
  40   }
  41 
  42   /**
  43    * Returns the stdout result
  44    *
  45    * @return stdout result
  46    */
  47   public String getStdout();
  48   /**
  49    * Returns the stderr result
  50    *
  51    * @return stderr result
  52    */
  53   public String getStderr();
  54   public int getExitValue();
  55 
  56   public static OutputBuffer of(Process p) {
  57     return new LazyOutputBuffer(p);
  58   }
  59 
  60   public static OutputBuffer of(String stdout, String stderr, int exitValue) {
  61     return new EagerOutputBuffer(stdout, stderr, exitValue);
  62   }
  63 
  64   public static OutputBuffer of(String stdout, String stderr) {
  65     return of(stdout, stderr, -1);
  66   }
  67 
  68   class LazyOutputBuffer implements OutputBuffer {
  69     private static class StreamTask {
  70       private final ByteArrayOutputStream buffer;
  71       private final Future<Void> future;
  72 
  73       private StreamTask(InputStream stream) {
  74         this.buffer = new ByteArrayOutputStream();
  75         this.future = new StreamPumper(stream, buffer).process();
  76       }
  77 
  78       public String get() {
  79         try {
  80           future.get();
  81           return buffer.toString();
  82         } catch (InterruptedException e) {
  83           Thread.currentThread().interrupt();
  84           throw new OutputBufferException(e);
  85         } catch (ExecutionException | CancellationException e) {
  86           throw new OutputBufferException(e);
  87         }
  88       }
  89     }
  90 
  91     private final StreamTask outTask;
  92     private final StreamTask errTask;
  93     private final Process p;
  94 
  95     private final void logProgress(String state) {
  96         System.out.println("[" + Instant.now().toString() + "] " + state
  97                            + " for process " + p.pid());
  98     }
  99 
 100     private LazyOutputBuffer(Process p) {
 101       this.p = p;
 102       logProgress("Gathering output");
 103       outTask = new StreamTask(p.getInputStream());
 104       errTask = new StreamTask(p.getErrorStream());
 105     }
 106 
 107     @Override
 108     public String getStdout() {
 109       return outTask.get();
 110     }
 111 
 112     @Override
 113     public String getStderr() {
 114       return errTask.get();
 115     }
 116 
 117     @Override
 118     public int getExitValue() {
 119       try {
 120           logProgress("Waiting for completion");
 121           boolean aborted = true;
 122           try {
 123               int result = p.waitFor();
 124               logProgress("Waiting for completion finished");
 125               aborted = false;
 126               return result;
 127           } finally {
 128               if (aborted) {
 129                   logProgress("Waiting for completion FAILED");
 130               }
 131           }
 132       } catch (InterruptedException e) {
 133         Thread.currentThread().interrupt();
 134         throw new OutputBufferException(e);
 135       }
 136     }
 137   }
 138 
 139   class EagerOutputBuffer implements OutputBuffer {
 140     private final String stdout;
 141     private final String stderr;
 142     private final int exitValue;
 143 
 144     private EagerOutputBuffer(String stdout, String stderr, int exitValue) {
 145       this.stdout = stdout;
 146       this.stderr = stderr;
 147       this.exitValue = exitValue;
 148     }
 149 
 150     @Override
 151     public String getStdout() {
 152       return stdout;
 153     }
 154 
 155     @Override
 156     public String getStderr() {
 157       return stderr;
 158     }
 159 
 160     @Override
 161     public int getExitValue() {
 162       return exitValue;
 163     }
 164   }
 165 }