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 import java.nio.file.Path; 25 import java.io.BufferedReader; 26 import java.io.ByteArrayOutputStream; 27 import java.io.Closeable; 28 import java.io.File; 29 import java.io.FileFilter; 30 import java.io.FileOutputStream; 31 import java.io.IOException; 32 import java.io.InputStream; 33 import java.io.InputStreamReader; 34 import java.io.PrintStream; 35 import java.nio.charset.Charset; 36 import java.nio.file.Files; 37 import java.util.ArrayList; 38 import java.util.Arrays; 39 import java.util.Collections; 40 import java.util.List; 41 import java.util.Map; 42 import java.util.jar.JarFile; 43 import java.util.jar.JarOutputStream; 44 import java.util.jar.Pack200; 45 import java.util.zip.ZipEntry; 46 import java.util.zip.ZipFile; 47 48 import static java.nio.file.StandardCopyOption.*; 49 import static java.nio.file.StandardOpenOption.*; 50 51 /** 52 * 53 * @author ksrini 54 */ 55 56 /* 57 * This class contains the commonly used utilities. 58 */ 59 class Utils { 60 static final String JavaHome = System.getProperty("test.java", 61 System.getProperty("java.home")); 62 static final boolean IsWindows = 63 System.getProperty("os.name").startsWith("Windows"); 64 static final boolean Is64Bit = 65 System.getProperty("sun.arch.data.model", "32").equals("64"); 66 static final File JavaSDK = new File(JavaHome); 67 68 static final String PACK_FILE_EXT = ".pack"; 69 static final String JAVA_FILE_EXT = ".java"; 70 static final String CLASS_FILE_EXT = ".class"; 527 static String getUnpack200Cmd() { 528 return getAjavaCmd("unpack200"); 529 } 530 531 static String getPack200Cmd() { 532 return getAjavaCmd("pack200"); 533 } 534 535 static String getJavaCmd() { 536 return getAjavaCmd("java"); 537 } 538 539 static String getJavacCmd() { 540 return getAjavaCmd("javac"); 541 } 542 543 static String getJarCmd() { 544 return getAjavaCmd("jar"); 545 } 546 547 static String getJimageCmd() { 548 return getAjavaCmd("jimage"); 549 } 550 551 static String getAjavaCmd(String cmdStr) { 552 File binDir = new File(JavaHome, "bin"); 553 File unpack200File = IsWindows 554 ? new File(binDir, cmdStr + ".exe") 555 : new File(binDir, cmdStr); 556 557 String cmd = unpack200File.getAbsolutePath(); 558 if (!unpack200File.canExecute()) { 559 throw new RuntimeException("please check" + 560 cmd + " exists and is executable"); 561 } 562 return cmd; 563 } 564 565 static File createRtJar() throws IOException { 566 File libDir = new File(JavaHome, "lib"); 567 File modules = new File(libDir, "modules"); 568 List<String> cmdList = new ArrayList<>(); 569 cmdList.add(getJimageCmd()); 570 cmdList.add("extract"); 571 cmdList.add(modules.getAbsolutePath()); 572 cmdList.add("--dir"); 573 cmdList.add("out"); 574 runExec(cmdList); 575 576 File rtJar = new File("rt.jar"); 577 cmdList.clear(); 578 cmdList.add(getJarCmd()); 579 // cmdList.add("cvf"); too noisy 580 cmdList.add("cf"); 581 cmdList.add(rtJar.getName()); 582 cmdList.add("-C"); 583 cmdList.add("out"); 584 cmdList.add("."); 585 runExec(cmdList); 586 587 recursiveDelete(new File("out")); 588 return rtJar; 589 } 590 } | 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 import java.nio.file.Path; 25 import java.io.BufferedReader; 26 import java.io.ByteArrayOutputStream; 27 import java.io.Closeable; 28 import java.io.File; 29 import java.io.FileFilter; 30 import java.io.FileOutputStream; 31 import java.io.IOException; 32 import java.io.InputStream; 33 import java.io.InputStreamReader; 34 import java.io.PrintStream; 35 import java.net.URI; 36 import java.net.URL; 37 import java.nio.charset.Charset; 38 import java.nio.file.attribute.BasicFileAttributes; 39 import java.nio.file.FileSystem; 40 import java.nio.file.FileSystems; 41 import java.nio.file.FileVisitResult; 42 import java.nio.file.FileVisitor; 43 import java.nio.file.Files; 44 import java.util.ArrayList; 45 import java.util.Arrays; 46 import java.util.Collections; 47 import java.util.List; 48 import java.util.Map; 49 import java.util.HashMap; 50 import java.util.jar.JarFile; 51 import java.util.jar.JarOutputStream; 52 import java.util.jar.Pack200; 53 import java.util.regex.Matcher; 54 import java.util.regex.Pattern; 55 import java.util.zip.ZipEntry; 56 import java.util.zip.ZipFile; 57 58 import static java.nio.file.StandardCopyOption.*; 59 import static java.nio.file.StandardOpenOption.*; 60 61 62 /** 63 * 64 * @author ksrini 65 */ 66 67 /* 68 * This class contains the commonly used utilities. 69 */ 70 class Utils { 71 static final String JavaHome = System.getProperty("test.java", 72 System.getProperty("java.home")); 73 static final boolean IsWindows = 74 System.getProperty("os.name").startsWith("Windows"); 75 static final boolean Is64Bit = 76 System.getProperty("sun.arch.data.model", "32").equals("64"); 77 static final File JavaSDK = new File(JavaHome); 78 79 static final String PACK_FILE_EXT = ".pack"; 80 static final String JAVA_FILE_EXT = ".java"; 81 static final String CLASS_FILE_EXT = ".class"; 538 static String getUnpack200Cmd() { 539 return getAjavaCmd("unpack200"); 540 } 541 542 static String getPack200Cmd() { 543 return getAjavaCmd("pack200"); 544 } 545 546 static String getJavaCmd() { 547 return getAjavaCmd("java"); 548 } 549 550 static String getJavacCmd() { 551 return getAjavaCmd("javac"); 552 } 553 554 static String getJarCmd() { 555 return getAjavaCmd("jar"); 556 } 557 558 static String getAjavaCmd(String cmdStr) { 559 File binDir = new File(JavaHome, "bin"); 560 File unpack200File = IsWindows 561 ? new File(binDir, cmdStr + ".exe") 562 : new File(binDir, cmdStr); 563 564 String cmd = unpack200File.getAbsolutePath(); 565 if (!unpack200File.canExecute()) { 566 throw new RuntimeException("please check" + 567 cmd + " exists and is executable"); 568 } 569 return cmd; 570 } 571 572 // used to get all classes 573 static File createRtJar() throws Exception { 574 File rtJar = new File("rt.jar"); 575 new JrtToZip(".*\\.class", rtJar).run(); 576 return rtJar; 577 } 578 579 // used to select the contents 580 static File createRtJar(String pattern) throws Exception { 581 File rtJar = new File("rt.jar"); 582 new JrtToZip(pattern, rtJar).run(); 583 return rtJar; 584 } 585 586 /* 587 * A helper class to create a pseudo rt.jar. 588 */ 589 static class JrtToZip { 590 591 final File outFile; 592 final Pattern pattern; 593 594 public static void main(String[] args) throws Exception { 595 new JrtToZip(args[0], new File(args[1])).run(); 596 } 597 598 JrtToZip(String pattern, File outFile) throws Exception { 599 this.pattern = Pattern.compile(pattern); 600 this.outFile = outFile; 601 } 602 603 void run() throws Exception { 604 URI uri = URI.create("jar:" + outFile.toURI()); 605 Map<String, String> env = new HashMap<>(); 606 env.put("create", "true"); 607 try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) { 608 toZipfs(zipfs); 609 } 610 } 611 612 void toZipfs(FileSystem zipfs) throws Exception { 613 FileSystem jrtfs = FileSystems.getFileSystem(URI.create("jrt:/")); 614 for (Path root : jrtfs.getRootDirectories()) { 615 Files.walkFileTree(root, new FileVisitor<Path>() { 616 @Override 617 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { 618 // ignore unneeded directory 619 if (dir.startsWith("/packages")) 620 return FileVisitResult.SKIP_SUBTREE; 621 622 // pre-create required directories 623 Path zpath = zipfs.getPath(dir.toString()); 624 Files.createDirectories(zpath); 625 return FileVisitResult.CONTINUE; 626 } 627 628 @Override 629 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { 630 Matcher matcher = pattern.matcher(file.toString()); 631 if (matcher.matches()) { 632 // System.out.println("x: " + file); 633 Path zpath = zipfs.getPath(file.toString()); 634 Files.copy(file, zpath, REPLACE_EXISTING); 635 } 636 return FileVisitResult.CONTINUE; 637 } 638 639 @Override 640 public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { 641 return FileVisitResult.CONTINUE; 642 } 643 644 @Override 645 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { 646 return FileVisitResult.CONTINUE; 647 } 648 }); 649 } 650 } 651 } 652 } |