1/* 2 * Copyright (c) 2016, 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 24import java.io.BufferedWriter; 25import java.io.File; 26import java.io.FileWriter; 27import java.io.IOException; 28import java.util.ArrayList; 29import java.util.List; 30import java.net.URL; 31 32public class LingeredAppForJps extends LingeredApp { 33 34 // Copy runApp logic here to be able to run an app from JarFile 35 public void runAppWithName(List<String> vmArguments, String runName) 36 throws IOException { 37 38 List<String> cmd = runAppPrepare(vmArguments); 39 if (runName.endsWith(".jar")) { 40 cmd.add("-Xdiag"); 41 cmd.add("-jar"); 42 } 43 cmd.add(runName); 44 cmd.add(lockFileName); 45 46 printCommandLine(cmd); 47 48 ProcessBuilder pb = new ProcessBuilder(cmd); 49 // we don't expect any error output but make sure we are not stuck on pipe 50 pb.redirectError(ProcessBuilder.Redirect.INHERIT); 51 appProcess = pb.start(); 52 startGobblerPipe(); 53 } 54 55 public static LingeredApp startAppJar(List<String> cmd, LingeredAppForJps app, File jar) throws IOException { 56 app.createLock(); 57 try { 58 app.runAppWithName(cmd, jar.getAbsolutePath()); 59 app.waitAppReady(appWaitTime); 60 } catch (Exception ex) { 61 app.deleteLock(); 62 throw ex; 63 } 64 65 return app; 66 } 67 68 /** 69 * The jps output should contain processes' names 70 * (except when jps is started in quite mode). 71 * The expected name of the test process is prepared here. 72 */ 73 public static String getProcessName() { 74 return LingeredAppForJps.class.getSimpleName(); 75 } 76 77 public static String getProcessName(File jar) { 78 return jar.getName(); 79 } 80 81 // full package name for the application's main class or the full path 82 // name to the application's JAR file: 83 84 public static String getFullProcessName() { 85 return LingeredAppForJps.class.getCanonicalName(); 86 } 87 88 public static String getFullProcessName(File jar) { 89 return jar.getAbsolutePath(); 90 } 91 92 public static File buildJar() throws IOException { 93 String className = LingeredAppForJps.class.getName(); 94 File jar = new File(className + ".jar"); 95 String testClassPath = System.getProperty("test.class.path", "?"); 96 97 File manifestFile = new File(className + ".mf"); 98 String nl = System.getProperty("line.separator"); 99 try (BufferedWriter output = new BufferedWriter(new FileWriter(manifestFile))) { 100 output.write("Main-Class: " + className + nl); 101 } 102 103 List<String> jarArgs = new ArrayList<>(); 104 jarArgs.add("-cfm"); 105 jarArgs.add(jar.getAbsolutePath()); 106 jarArgs.add(manifestFile.getAbsolutePath()); 107 108 for (String path : testClassPath.split(File.pathSeparator)) { 109 String classFullName = path + File.separator + className + ".class"; 110 File f = new File(classFullName); 111 if (f.exists()) { 112 jarArgs.add("-C"); 113 jarArgs.add(path); 114 jarArgs.add("."); 115 System.out.println("INFO: scheduled to jar " + path); 116 break; 117 } 118 } 119 120 System.out.println("Running jar " + jarArgs.toString()); 121 sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar"); 122 if (!jarTool.run(jarArgs.toArray(new String[jarArgs.size()]))) { 123 throw new IOException("jar failed: args=" + jarArgs.toString()); 124 } 125 126 manifestFile.delete(); 127 jar.deleteOnExit(); 128 129 // Print content of jar file 130 System.out.println("Content of jar file" + jar.getAbsolutePath()); 131 132 jarArgs = new ArrayList<>(); 133 jarArgs.add("-tvf"); 134 jarArgs.add(jar.getAbsolutePath()); 135 136 jarTool = new sun.tools.jar.Main(System.out, System.err, "jar"); 137 if (!jarTool.run(jarArgs.toArray(new String[jarArgs.size()]))) { 138 throw new IOException("jar failed: args=" + jarArgs.toString()); 139 } 140 141 return jar; 142 } 143 144 public static void main(String args[]) { 145 LingeredApp.main(args); 146 } 147 } 148