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.apps; 25 26 import java.io.BufferedReader; 27 import java.io.ByteArrayOutputStream; 28 import java.io.IOException; 29 import java.io.InputStream; 30 import java.io.InputStreamReader; 31 import java.io.OutputStream; 32 import java.io.StringReader; 33 import java.nio.file.Files; 34 import java.nio.file.NoSuchFileException; 35 import java.nio.file.Path; 36 import java.nio.file.Paths; 37 import java.nio.file.attribute.BasicFileAttributes; 38 import java.nio.file.attribute.FileTime; 39 import java.util.ArrayList; 40 import java.util.Date; 41 import java.util.List; 42 import java.util.Map; 43 import java.util.stream.Collectors; 44 import java.util.UUID; 45 import jdk.test.lib.process.OutputBuffer; 46 import jdk.test.lib.process.ProcessTools; 47 import jdk.test.lib.process.StreamPumper; 48 49 /** 50 * This is a framework to launch an app that could be synchronized with caller 51 * to make further attach actions reliable across supported platforms 52 53 * Caller example: 54 * SmartTestApp a = SmartTestApp.startApp(cmd); 55 * // do something 56 * a.stopApp(); 57 * 58 * or fine grained control 59 * 60 * a = new SmartTestApp("MyLock.lck"); 61 * a.createLock(); 62 * a.runApp(); 63 * a.waitAppReady(); 64 * // do something 65 * a.deleteLock(); 66 * a.waitAppTerminate(); 117 return this.getClass().getName(); 118 } 119 120 /** 121 * 122 * @return pid of java process running testapp 123 */ 124 public long getPid() { 125 if (appProcess == null) { 126 throw new RuntimeException("Process is not alive"); 127 } 128 return appProcess.pid(); 129 } 130 131 /** 132 * 133 * @return process object 134 */ 135 public Process getProcess() { 136 return appProcess; 137 } 138 139 /** 140 * 141 * @return OutputBuffer object for the LingeredApp's output. Can only be called 142 * after LingeredApp has exited. 143 */ 144 public OutputBuffer getOutput() { 145 if (appProcess.isAlive()) { 146 throw new RuntimeException("Process is still alive. Can't get its output."); 147 } 148 if (output == null) { 149 output = OutputBuffer.of(stdoutBuffer.toString(), stderrBuffer.toString()); 150 } 151 return output; 152 } 153 154 /* 155 * Capture all stdout and stderr output from the LingeredApp so it can be returned 156 * to the driver app later. This code is modeled after ProcessTools.getOutput(). | 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.apps; 25 26 import java.io.BufferedReader; 27 import java.io.ByteArrayOutputStream; 28 import java.io.IOException; 29 import java.io.StringReader; 30 import java.nio.file.Files; 31 import java.nio.file.NoSuchFileException; 32 import java.nio.file.Path; 33 import java.nio.file.Paths; 34 import java.nio.file.attribute.BasicFileAttributes; 35 import java.nio.file.attribute.FileTime; 36 import java.util.ArrayList; 37 import java.util.Date; 38 import java.util.List; 39 import java.util.Map; 40 import java.util.stream.Collectors; 41 import java.util.UUID; 42 import jdk.test.lib.process.OutputBuffer; 43 import jdk.test.lib.process.StreamPumper; 44 45 /** 46 * This is a framework to launch an app that could be synchronized with caller 47 * to make further attach actions reliable across supported platforms 48 49 * Caller example: 50 * SmartTestApp a = SmartTestApp.startApp(cmd); 51 * // do something 52 * a.stopApp(); 53 * 54 * or fine grained control 55 * 56 * a = new SmartTestApp("MyLock.lck"); 57 * a.createLock(); 58 * a.runApp(); 59 * a.waitAppReady(); 60 * // do something 61 * a.deleteLock(); 62 * a.waitAppTerminate(); 113 return this.getClass().getName(); 114 } 115 116 /** 117 * 118 * @return pid of java process running testapp 119 */ 120 public long getPid() { 121 if (appProcess == null) { 122 throw new RuntimeException("Process is not alive"); 123 } 124 return appProcess.pid(); 125 } 126 127 /** 128 * 129 * @return process object 130 */ 131 public Process getProcess() { 132 return appProcess; 133 } 134 135 /** 136 * @return the LingeredApp's output. 137 * Can be called after the app is run. 138 */ 139 public String getProcessStdout() { 140 return stdoutBuffer.toString(); 141 } 142 143 /** 144 * 145 * @return OutputBuffer object for the LingeredApp's output. Can only be called 146 * after LingeredApp has exited. 147 */ 148 public OutputBuffer getOutput() { 149 if (appProcess.isAlive()) { 150 throw new RuntimeException("Process is still alive. Can't get its output."); 151 } 152 if (output == null) { 153 output = OutputBuffer.of(stdoutBuffer.toString(), stderrBuffer.toString()); 154 } 155 return output; 156 } 157 158 /* 159 * Capture all stdout and stderr output from the LingeredApp so it can be returned 160 * to the driver app later. This code is modeled after ProcessTools.getOutput(). |