src/share/classes/com/sun/tools/jdeps/PlatformClassPath.java

changeset 1472
0c244701188e
child 1638
fd3fdaff0257
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/jdeps/PlatformClassPath.java	Fri Dec 28 22:25:21 2012 -0800
     1.3 @@ -0,0 +1,169 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +package com.sun.tools.jdeps;
    1.29 +
    1.30 +import java.io.File;
    1.31 +import java.io.IOException;
    1.32 +import java.nio.file.FileVisitResult;
    1.33 +import java.nio.file.Files;
    1.34 +import java.nio.file.Path;
    1.35 +import java.nio.file.SimpleFileVisitor;
    1.36 +import java.nio.file.attribute.BasicFileAttributes;
    1.37 +import java.util.*;
    1.38 +
    1.39 +/**
    1.40 + * ClassPath for Java SE and JDK
    1.41 + */
    1.42 +class PlatformClassPath {
    1.43 +    /*
    1.44 +     * Profiles for Java SE
    1.45 +     *
    1.46 +     * This is a temporary workaround until a common API is defined for langtools
    1.47 +     * to determine which profile a given classname belongs to.  The list of
    1.48 +     * packages and profile names are hardcoded in jdk.properties and
    1.49 +     * split packages are not supported.
    1.50 +     */
    1.51 +    static class Profile {
    1.52 +        final String name;
    1.53 +        final Set<String> packages;
    1.54 +
    1.55 +        Profile(String name) {
    1.56 +            this.name = name;
    1.57 +            this.packages = new HashSet<String>();
    1.58 +        }
    1.59 +    }
    1.60 +
    1.61 +    private final static String JAVAFX = "javafx";
    1.62 +    private final static Map<String,Profile> map = getProfilePackages();
    1.63 +    static String getProfileName(String packageName) {
    1.64 +        Profile profile = map.get(packageName);
    1.65 +        if (packageName.startsWith(JAVAFX + ".")) {
    1.66 +            profile = map.get(JAVAFX);
    1.67 +        }
    1.68 +        return profile != null ? profile.name : "";
    1.69 +    }
    1.70 +
    1.71 +    private final static List<Archive> javaHomeArchives = init();
    1.72 +    static List<Archive> getArchives() {
    1.73 +        return javaHomeArchives;
    1.74 +    }
    1.75 +
    1.76 +    static boolean contains(Archive archive) {
    1.77 +        return javaHomeArchives.contains(archive);
    1.78 +    }
    1.79 +
    1.80 +    private static List<Archive> init() {
    1.81 +        List<Archive> result = new ArrayList<Archive>();
    1.82 +        String javaHome = System.getProperty("java.home");
    1.83 +        List<File> files = new ArrayList<File>();
    1.84 +        File jre = new File(javaHome, "jre");
    1.85 +        File lib = new File(javaHome, "lib");
    1.86 +
    1.87 +        try {
    1.88 +            if (jre.exists() && jre.isDirectory()) {
    1.89 +                result.addAll(addJarFiles(new File(jre, "lib")));
    1.90 +                result.addAll(addJarFiles(lib));
    1.91 +            } else if (lib.exists() && lib.isDirectory()) {
    1.92 +                // either a JRE or a jdk build image
    1.93 +                File classes = new File(javaHome, "classes");
    1.94 +                if (classes.exists() && classes.isDirectory()) {
    1.95 +                    // jdk build outputdir
    1.96 +                    result.add(new Archive(classes, ClassFileReader.newInstance(classes)));
    1.97 +                }
    1.98 +                // add other JAR files
    1.99 +                result.addAll(addJarFiles(lib));
   1.100 +            } else {
   1.101 +                throw new RuntimeException("\"" + javaHome + "\" not a JDK home");
   1.102 +            }
   1.103 +        } catch (IOException e) {
   1.104 +            throw new RuntimeException(e);
   1.105 +        }
   1.106 +
   1.107 +        // add a JavaFX profile if there is jfxrt.jar
   1.108 +        for (Archive archive : result) {
   1.109 +            if (archive.getFileName().equals("jfxrt.jar")) {
   1.110 +                map.put(JAVAFX, new Profile("jfxrt.jar"));
   1.111 +            }
   1.112 +        }
   1.113 +        return result;
   1.114 +    }
   1.115 +
   1.116 +    private static List<Archive> addJarFiles(File f) throws IOException {
   1.117 +        final List<Archive> result = new ArrayList<Archive>();
   1.118 +        final Path root = f.toPath();
   1.119 +        final Path ext = root.resolve("ext");
   1.120 +        Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
   1.121 +            @Override
   1.122 +            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
   1.123 +                throws IOException
   1.124 +            {
   1.125 +                if (dir.equals(root) || dir.equals(ext)) {
   1.126 +                    return FileVisitResult.CONTINUE;
   1.127 +                } else {
   1.128 +                    // skip other cobundled JAR files
   1.129 +                    return FileVisitResult.SKIP_SUBTREE;
   1.130 +                }
   1.131 +            }
   1.132 +            @Override
   1.133 +            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
   1.134 +                throws IOException
   1.135 +            {
   1.136 +                File f = file.toFile();
   1.137 +                String fn = f.getName();
   1.138 +                if (fn.endsWith(".jar") && !fn.equals("alt-rt.jar")) {
   1.139 +                    result.add(new Archive(f, ClassFileReader.newInstance(f)));
   1.140 +                }
   1.141 +                return FileVisitResult.CONTINUE;
   1.142 +            }
   1.143 +        });
   1.144 +        return result;
   1.145 +    }
   1.146 +
   1.147 +    private static Map<String,Profile> getProfilePackages() {
   1.148 +        Map<String,Profile> map = new HashMap<String,Profile>();
   1.149 +
   1.150 +        // read the properties as a ResourceBundle as the build compiles
   1.151 +        // the properties file into Java class.  Another alternative is
   1.152 +        // to load it as Properties and fix the build to exclude this file.
   1.153 +        ResourceBundle profileBundle =
   1.154 +            ResourceBundle.getBundle("com.sun.tools.jdeps.resources.jdk");
   1.155 +
   1.156 +        int i=1;
   1.157 +        String key;
   1.158 +        while (profileBundle.containsKey((key = "profile." + i + ".name"))) {
   1.159 +            Profile profile = new Profile(profileBundle.getString(key));
   1.160 +            String n = profileBundle.getString("profile." + i + ".packages");
   1.161 +            String[] pkgs = n.split("\\s+");
   1.162 +            for (String p : pkgs) {
   1.163 +                if (p.isEmpty()) continue;
   1.164 +                assert(map.containsKey(p) == false);
   1.165 +                profile.packages.add(p);
   1.166 +                map.put(p, profile);
   1.167 +            }
   1.168 +            i++;
   1.169 +        }
   1.170 +        return map;
   1.171 +    }
   1.172 +}

mercurial