1 /*
   2  * Copyright (c) 2012, 2013, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package com.sun.tools.jdeps;
  26 
  27 import java.io.IOException;
  28 import java.nio.file.FileVisitResult;
  29 import java.nio.file.Files;
  30 import java.nio.file.Path;
  31 import java.nio.file.Paths;
  32 import java.nio.file.SimpleFileVisitor;
  33 import java.nio.file.attribute.BasicFileAttributes;
  34 import java.util.*;
  35 
  36 /**
  37  * ClassPath for Java SE and JDK
  38  */
  39 class PlatformClassPath {
  40     private final static List<Archive> javaHomeArchives = init();
  41 
  42     static List<Archive> getArchives() {
  43         return javaHomeArchives;
  44     }
  45 
  46     static boolean contains(Archive archive) {
  47         // jfxrt.jar is in jre/lib/ext directory but it does not belong
  48         // to any profile
  49         return javaHomeArchives.contains(archive) && !archive.getFileName().equals("jfxrt.jar");
  50     }
  51 
  52     private static List<Archive> init() {
  53         List<Archive> result = new ArrayList<>();
  54         Path home = Paths.get(System.getProperty("java.home"));
  55         try {
  56             if (home.endsWith("jre")) {
  57                 // jar files in <javahome>/jre/lib
  58                 result.addAll(addJarFiles(home.resolve("lib")));
  59             } else if (Files.exists(home.resolve("lib"))) {
  60                 // either a JRE or a jdk build image
  61                 Path classes = home.resolve("classes");
  62                 if (Files.isDirectory(classes)) {
  63                     // jdk build outputdir
  64                     result.add(new Archive(classes, ClassFileReader.newInstance(classes)));
  65                 }
  66                 // add other JAR files
  67                 result.addAll(addJarFiles(home.resolve("lib")));
  68             } else {
  69                 throw new RuntimeException("\"" + home + "\" not a JDK home");
  70             }
  71             return result;
  72         } catch (IOException e) {
  73             throw new Error(e);
  74         }
  75     }
  76 
  77     private static List<Archive> addJarFiles(final Path root) throws IOException {
  78         final List<Archive> result = new ArrayList<>();
  79         final Path ext = root.resolve("ext");
  80         Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
  81             @Override
  82             public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
  83                 throws IOException
  84             {
  85                 if (dir.equals(root) || dir.equals(ext)) {
  86                     return FileVisitResult.CONTINUE;
  87                 } else {
  88                     // skip other cobundled JAR files
  89                     return FileVisitResult.SKIP_SUBTREE;
  90                 }
  91             }
  92             @Override
  93             public FileVisitResult visitFile(Path p, BasicFileAttributes attrs)
  94                 throws IOException
  95             {
  96                 String fn = p.getFileName().toString();
  97                 if (fn.endsWith(".jar") && !fn.equals("alt-rt.jar")) {
  98                     result.add(new Archive(p, ClassFileReader.newInstance(p)));
  99                 }
 100                 return FileVisitResult.CONTINUE;
 101             }
 102         });
 103         return result;
 104     }
 105 }