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.File;
  28 import java.io.IOException;
  29 import java.nio.file.FileVisitResult;
  30 import java.nio.file.Files;
  31 import java.nio.file.Path;
  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     static List<Archive> getArchives() {
  42         return javaHomeArchives;
  43     }
  44 
  45     static boolean contains(Archive archive) {
  46         return javaHomeArchives.contains(archive);
  47     }
  48 
  49     private static List<Archive> init() {
  50         List<Archive> result = new ArrayList<Archive>();
  51         String javaHome = System.getProperty("java.home");
  52         File jre = new File(javaHome, "jre");
  53         File lib = new File(javaHome, "lib");
  54 
  55         try {
  56             if (jre.exists() && jre.isDirectory()) {
  57                 result.addAll(addJarFiles(new File(jre, "lib")));
  58                 result.addAll(addJarFiles(lib));
  59             } else if (lib.exists() && lib.isDirectory()) {
  60                 // either a JRE or a jdk build image
  61                 File classes = new File(javaHome, "classes");
  62                 if (classes.exists() && classes.isDirectory()) {
  63                     // jdk build outputdir
  64                     result.add(new Archive(classes, ClassFileReader.newInstance(classes)));
  65                 }
  66                 // add other JAR files
  67                 result.addAll(addJarFiles(lib));
  68             } else {
  69                 throw new RuntimeException("\"" + javaHome + "\" not a JDK home");
  70             }
  71         } catch (IOException e) {
  72             throw new RuntimeException(e);
  73         }
  74         return result;
  75     }
  76 
  77     private static List<Archive> addJarFiles(File f) throws IOException {
  78         final List<Archive> result = new ArrayList<Archive>();
  79         final Path root = f.toPath();
  80         final Path ext = root.resolve("ext");
  81         Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
  82             @Override
  83             public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
  84                 throws IOException
  85             {
  86                 if (dir.equals(root) || dir.equals(ext)) {
  87                     return FileVisitResult.CONTINUE;
  88                 } else {
  89                     // skip other cobundled JAR files
  90                     return FileVisitResult.SKIP_SUBTREE;
  91                 }
  92             }
  93             @Override
  94             public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
  95                 throws IOException
  96             {
  97                 File f = file.toFile();
  98                 String fn = f.getName();
  99                 if (fn.endsWith(".jar") && !fn.equals("alt-rt.jar")) {
 100                     result.add(new Archive(f, ClassFileReader.newInstance(f)));
 101                 }
 102                 return FileVisitResult.CONTINUE;
 103             }
 104         });
 105         return result;
 106     }
 107 }