Utils.java revision 4248
0N/A/*
0N/A * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
0N/A
0N/Aimport java.nio.file.Path;
0N/Aimport java.io.BufferedReader;
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.Closeable;
0N/Aimport java.io.File;
0N/Aimport java.io.FileFilter;
0N/Aimport java.io.FileOutputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.InputStreamReader;
0N/Aimport java.io.PrintStream;
0N/Aimport java.nio.file.Files;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Arrays;
0N/Aimport java.util.Collections;
0N/Aimport java.util.List;
0N/Aimport java.util.Map;
0N/Aimport java.util.jar.JarFile;
0N/Aimport java.util.jar.JarOutputStream;
0N/Aimport java.util.jar.Pack200;
0N/Aimport java.util.zip.ZipEntry;
0N/Aimport java.util.zip.ZipFile;
0N/A
0N/Aimport static java.nio.file.StandardCopyOption.*;
0N/A
0N/A/**
0N/A *
0N/A * @author ksrini
0N/A */
0N/A
0N/A/*
0N/A * This class contains all the commonly used utilities used by various tests
0N/A * in this directory.
0N/A */
0N/Aclass Utils {
0N/A static final String JavaHome = System.getProperty("test.java",
0N/A System.getProperty("java.home"));
0N/A static final boolean IsWindows =
0N/A System.getProperty("os.name").startsWith("Windows");
0N/A static final boolean Is64Bit =
0N/A System.getProperty("sun.arch.data.model", "32").equals("64");
0N/A static final File JavaSDK = new File(JavaHome).getParentFile();
0N/A
0N/A static final String PACK_FILE_EXT = ".pack";
0N/A static final String JAVA_FILE_EXT = ".java";
0N/A static final String CLASS_FILE_EXT = ".class";
0N/A static final String JAR_FILE_EXT = ".jar";
0N/A
0N/A static final File TEST_SRC_DIR = new File(System.getProperty("test.src"));
0N/A static final String VERIFIER_DIR_NAME = "pack200-verifier";
0N/A static final File VerifierJar = new File(VERIFIER_DIR_NAME + JAR_FILE_EXT);
0N/A
0N/A private Utils() {} // all static
0N/A
0N/A static {
0N/A if (!JavaHome.endsWith("jre")) {
0N/A throw new RuntimeException("Error: requires an SDK to run");
0N/A }
0N/A }
0N/A
0N/A private static void init() throws IOException {
0N/A if (VerifierJar.exists()) {
0N/A return;
0N/A }
0N/A File srcDir = new File(TEST_SRC_DIR, VERIFIER_DIR_NAME);
0N/A List<File> javaFileList = findFiles(srcDir, createFilter(JAVA_FILE_EXT));
0N/A File tmpFile = File.createTempFile("javac", ".tmp");
0N/A File classesDir = new File("xclasses");
0N/A classesDir.mkdirs();
0N/A FileOutputStream fos = null;
0N/A PrintStream ps = null;
0N/A try {
0N/A fos = new FileOutputStream(tmpFile);
0N/A ps = new PrintStream(fos);
0N/A for (File f : javaFileList) {
0N/A ps.println(f.getAbsolutePath());
0N/A }
0N/A } finally {
0N/A close(ps);
0N/A close(fos);
0N/A }
0N/A
0N/A compiler("-d",
0N/A "xclasses",
0N/A "@" + tmpFile.getAbsolutePath());
0N/A
0N/A jar("cvfe",
0N/A VerifierJar.getName(),
0N/A "sun.tools.pack.verify.Main",
0N/A "-C",
0N/A "xclasses",
0N/A ".");
0N/A }
0N/A
0N/A static void dirlist(File dir) {
0N/A File[] files = dir.listFiles();
0N/A System.out.println("--listing " + dir.getAbsolutePath() + "---");
0N/A for (File f : files) {
0N/A StringBuffer sb = new StringBuffer();
0N/A sb.append(f.isDirectory() ? "d " : "- ");
0N/A sb.append(f.getName());
0N/A System.out.println(sb);
0N/A }
0N/A }
0N/A static void doCompareVerify(File reference, File specimen) throws IOException {
0N/A init();
0N/A List<String> cmds = new ArrayList<String>();
0N/A cmds.add(getJavaCmd());
0N/A cmds.add("-jar");
0N/A cmds.add(VerifierJar.getName());
0N/A cmds.add(reference.getAbsolutePath());
0N/A cmds.add(specimen.getAbsolutePath());
0N/A cmds.add("-O");
0N/A runExec(cmds);
0N/A }
0N/A
0N/A static void doCompareBitWise(File reference, File specimen)
0N/A throws IOException {
0N/A init();
0N/A List<String> cmds = new ArrayList<String>();
0N/A cmds.add(getJavaCmd());
0N/A cmds.add("-jar");
0N/A cmds.add(VerifierJar.getName());
0N/A cmds.add(reference.getName());
0N/A cmds.add(specimen.getName());
0N/A cmds.add("-O");
0N/A cmds.add("-b");
0N/A runExec(cmds);
0N/A }
0N/A
0N/A static FileFilter createFilter(final String extension) {
0N/A return new FileFilter() {
0N/A @Override
0N/A public boolean accept(File pathname) {
0N/A String name = pathname.getName();
0N/A if (name.endsWith(extension)) {
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A };
0N/A }
0N/A
0N/A static final FileFilter DIR_FILTER = new FileFilter() {
0N/A public boolean accept(File pathname) {
0N/A if (pathname.isDirectory()) {
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A };
0N/A
0N/A static final FileFilter FILE_FILTER = new FileFilter() {
0N/A public boolean accept(File pathname) {
0N/A if (pathname.isFile()) {
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A };
0N/A
0N/A static void copyFile(File src, File dst) throws IOException {
0N/A Path parent = dst.toPath().getParent();
0N/A if (parent != null) {
0N/A Files.createDirectories(parent);
0N/A }
0N/A Files.copy(src.toPath(), dst.toPath(), COPY_ATTRIBUTES, REPLACE_EXISTING);
0N/A }
0N/A
0N/A static String baseName(File file, String extension) {
0N/A return baseName(file.getAbsolutePath(), extension);
0N/A }
0N/A
0N/A static String baseName(String name, String extension) {
0N/A int cut = name.length() - extension.length();
0N/A return name.lastIndexOf(extension) == cut
0N/A ? name.substring(0, cut)
0N/A : name;
0N/A
0N/A }
0N/A
0N/A /*
0N/A * Suppose a path is provided which consists of a full path
0N/A * this method returns the sub path for a full path ex: /foo/bar/baz/foobar.z
0N/A * and the base path is /foo/bar it will will return baz/foobar.z.
0N/A */
0N/A private static String getEntryPath(String basePath, String fullPath) {
0N/A if (!fullPath.startsWith(basePath)) {
0N/A return null;
0N/A }
0N/A return fullPath.substring(basePath.length());
0N/A }
0N/A
0N/A static String getEntryPath(File basePathFile, File fullPathFile) {
0N/A return getEntryPath(basePathFile.toString(), fullPathFile.toString());
0N/A }
0N/A
0N/A public static void recursiveCopy(File src, File dest) throws IOException {
0N/A if (!src.exists() || !src.canRead()) {
0N/A throw new IOException("file not found or readable: " + src);
0N/A }
0N/A if (dest.exists() && !dest.isDirectory() && !dest.canWrite()) {
0N/A throw new IOException("file not found or writeable: " + dest);
0N/A }
0N/A if (!dest.exists()) {
0N/A dest.mkdirs();
0N/A }
0N/A List<File> a = directoryList(src);
0N/A for (File f : a) {
0N/A copyFile(f, new File(dest, getEntryPath(src, f)));
0N/A }
0N/A }
0N/A
0N/A static List<File> directoryList(File dirname) {
0N/A List<File> dirList = new ArrayList<File>();
0N/A return directoryList(dirname, dirList, null);
0N/A }
0N/A
0N/A private static List<File> directoryList(File dirname, List<File> dirList,
0N/A File[] dirs) {
0N/A dirList.addAll(Arrays.asList(dirname.listFiles(FILE_FILTER)));
0N/A dirs = dirname.listFiles(DIR_FILTER);
0N/A for (File f : dirs) {
0N/A if (f.isDirectory() && !f.equals(dirname)) {
0N/A dirList.add(f);
0N/A directoryList(f, dirList, dirs);
0N/A }
0N/A }
0N/A return dirList;
0N/A }
0N/A
0N/A static void recursiveDelete(File dir) throws IOException {
0N/A if (dir.isFile()) {
0N/A dir.delete();
0N/A } else if (dir.isDirectory()) {
0N/A File[] entries = dir.listFiles();
0N/A for (int i = 0; i < entries.length; i++) {
0N/A if (entries[i].isDirectory()) {
0N/A recursiveDelete(entries[i]);
0N/A }
0N/A entries[i].delete();
0N/A }
0N/A dir.delete();
0N/A }
0N/A }
0N/A
0N/A static List<File> findFiles(File startDir, FileFilter filter)
0N/A throws IOException {
0N/A List<File> list = new ArrayList<File>();
0N/A findFiles0(startDir, list, filter);
0N/A return list;
0N/A }
0N/A /*
0N/A * finds files in the start directory using the the filter, appends
0N/A * the files to the dirList.
0N/A */
0N/A private static void findFiles0(File startDir, List<File> list,
0N/A FileFilter filter) throws IOException {
0N/A File[] foundFiles = startDir.listFiles(filter);
0N/A list.addAll(Arrays.asList(foundFiles));
0N/A File[] dirs = startDir.listFiles(DIR_FILTER);
0N/A for (File dir : dirs) {
0N/A findFiles0(dir, list, filter);
0N/A }
0N/A }
0N/A
0N/A static void close(Closeable c) {
0N/A if (c == null) {
0N/A return;
0N/A }
0N/A try {
0N/A c.close();
0N/A } catch (IOException ignore) {
0N/A }
0N/A }
0N/A
0N/A static void compiler(String... javacCmds) {
0N/A if (com.sun.tools.javac.Main.compile(javacCmds) != 0) {
0N/A throw new RuntimeException("compilation failed");
0N/A }
0N/A }
0N/A
0N/A static void jar(String... jargs) {
0N/A sun.tools.jar.Main jarTool =
0N/A new sun.tools.jar.Main(System.out, System.err, "jartool");
0N/A if (!jarTool.run(jargs)) {
0N/A throw new RuntimeException("jar command failed");
0N/A }
0N/A }
0N/A
0N/A // given a jar file foo.jar will write to foo.pack
0N/A static void pack(JarFile jarFile, File packFile) throws IOException {
0N/A Pack200.Packer packer = Pack200.newPacker();
0N/A Map<String, String> p = packer.properties();
0N/A // Take the time optimization vs. space
0N/A p.put(packer.EFFORT, "1"); // CAUTION: do not use 0.
0N/A // Make the memory consumption as effective as possible
0N/A p.put(packer.SEGMENT_LIMIT, "10000");
0N/A // ignore all JAR deflation requests to save time
0N/A p.put(packer.DEFLATE_HINT, packer.FALSE);
0N/A // save the file ordering of the original JAR
0N/A p.put(packer.KEEP_FILE_ORDER, packer.TRUE);
0N/A FileOutputStream fos = null;
0N/A try {
0N/A // Write out to a jtreg scratch area
0N/A fos = new FileOutputStream(packFile);
0N/A // Call the packer
0N/A packer.pack(jarFile, fos);
0N/A } finally {
0N/A close(fos);
0N/A }
0N/A }
0N/A
0N/A // uses java unpacker, slow but useful to discover issues with the packer
0N/A static void unpackj(File inFile, JarOutputStream jarStream)
0N/A throws IOException {
0N/A unpack0(inFile, jarStream, true);
0N/A
0N/A }
0N/A
0N/A // uses native unpacker using the java APIs
0N/A static void unpackn(File inFile, JarOutputStream jarStream)
0N/A throws IOException {
0N/A unpack0(inFile, jarStream, false);
0N/A }
0N/A
0N/A // given a packed file, create the jar file in the current directory.
0N/A private static void unpack0(File inFile, JarOutputStream jarStream,
0N/A boolean useJavaUnpack) throws IOException {
0N/A // Unpack the files
0N/A Pack200.Unpacker unpacker = Pack200.newUnpacker();
0N/A Map<String, String> props = unpacker.properties();
0N/A if (useJavaUnpack) {
0N/A props.put("com.sun.java.util.jar.pack.disable.native", "true");
0N/A }
0N/A // Call the unpacker
0N/A unpacker.unpack(inFile, jarStream);
0N/A }
0N/A
0N/A static byte[] getBuffer(ZipFile zf, ZipEntry ze) throws IOException {
0N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
0N/A byte buf[] = new byte[8192];
0N/A InputStream is = null;
0N/A try {
0N/A is = zf.getInputStream(ze);
0N/A int n = is.read(buf);
0N/A while (n > 0) {
0N/A baos.write(buf, 0, n);
0N/A n = is.read(buf);
0N/A }
0N/A return baos.toByteArray();
0N/A } finally {
0N/A close(is);
0N/A }
0N/A }
0N/A
0N/A static ArrayList<String> getZipFileEntryNames(ZipFile z) {
0N/A ArrayList<String> out = new ArrayList<String>();
0N/A for (ZipEntry ze : Collections.list(z.entries())) {
0N/A out.add(ze.getName());
0N/A }
0N/A return out;
0N/A }
0N/A static List<String> runExec(List<String> cmdsList) {
0N/A return runExec(cmdsList, null);
0N/A }
0N/A static List<String> runExec(List<String> cmdsList, Map<String, String> penv) {
0N/A ArrayList<String> alist = new ArrayList<String>();
0N/A ProcessBuilder pb =
0N/A new ProcessBuilder(cmdsList);
0N/A Map<String, String> env = pb.environment();
0N/A if (penv != null && !penv.isEmpty()) {
0N/A env.putAll(penv);
0N/A }
0N/A pb.directory(new File("."));
0N/A dirlist(new File("."));
0N/A for (String x : cmdsList) {
0N/A System.out.print(x + " ");
0N/A }
0N/A System.out.println("");
0N/A int retval = 0;
0N/A Process p = null;
0N/A InputStreamReader ir = null;
0N/A BufferedReader rd = null;
0N/A InputStream is = null;
0N/A try {
0N/A pb.redirectErrorStream(true);
0N/A p = pb.start();
0N/A is = p.getInputStream();
0N/A ir = new InputStreamReader(is);
0N/A rd = new BufferedReader(ir, 8192);
0N/A
0N/A String in = rd.readLine();
0N/A while (in != null) {
0N/A alist.add(in);
0N/A System.out.println(in);
0N/A in = rd.readLine();
0N/A }
0N/A retval = p.waitFor();
0N/A if (retval != 0) {
0N/A throw new RuntimeException("process failed with non-zero exit");
0N/A }
0N/A } catch (Exception ex) {
0N/A throw new RuntimeException(ex.getMessage());
0N/A } finally {
0N/A close(rd);
0N/A close(ir);
0N/A close(is);
0N/A if (p != null) {
0N/A p.destroy();
0N/A }
0N/A }
0N/A return alist;
0N/A }
0N/A
0N/A static String getUnpack200Cmd() {
0N/A return getAjavaCmd("unpack200");
0N/A }
0N/A
0N/A static String getPack200Cmd() {
0N/A return getAjavaCmd("pack200");
0N/A }
0N/A
0N/A static String getJavaCmd() {
0N/A return getAjavaCmd("java");
0N/A }
0N/A
0N/A static String getAjavaCmd(String cmdStr) {
0N/A File binDir = new File(JavaHome, "bin");
0N/A File unpack200File = IsWindows
0N/A ? new File(binDir, cmdStr + ".exe")
0N/A : new File(binDir, cmdStr);
0N/A
0N/A String cmd = unpack200File.getAbsolutePath();
0N/A if (!unpack200File.canExecute()) {
0N/A throw new RuntimeException("please check" +
0N/A cmd + " exists and is executable");
0N/A }
0N/A return cmd;
0N/A }
0N/A
0N/A private static List<File> locaterCache = null;
0N/A // search the source dir and jdk dir for requested file and returns
0N/A // the first location it finds.
0N/A static File locateJar(String name) {
0N/A try {
0N/A if (locaterCache == null) {
0N/A locaterCache = new ArrayList<File>();
0N/A locaterCache.addAll(findFiles(TEST_SRC_DIR, createFilter(JAR_FILE_EXT)));
0N/A locaterCache.addAll(findFiles(JavaSDK, createFilter(JAR_FILE_EXT)));
0N/A }
0N/A for (File f : locaterCache) {
0N/A if (f.getName().equals(name)) {
0N/A return f;
0N/A }
0N/A }
0N/A throw new IOException("file not found: " + name);
0N/A } catch (IOException e) {
0N/A throw new RuntimeException(e);
0N/A }
0N/A }
0N/A}
0N/A