1 /*
   2  * Copyright (c) 2017, 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 
  26 package jdk.nashorn.tools.jjs;
  27 
  28 import java.io.IOException;
  29 import java.net.URI;
  30 import java.nio.file.DirectoryStream;
  31 import java.nio.file.Files;
  32 import java.nio.file.FileSystem;
  33 import java.nio.file.FileSystems;
  34 import java.nio.file.Path;
  35 import java.util.HashSet;
  36 import java.util.Set;
  37 import jdk.nashorn.internal.runtime.Context;
  38 
  39 /**
  40  * A java packages helper that uses jrt file system.
  41  */
  42 final class JrtPackagesHelper extends PackagesHelper {
  43     private final FileSystem jrtfs;
  44 
  45     /**
  46      * Construct a new JrtPackagesHelper.
  47      *
  48      * @param context the current Nashorn Context
  49      */
  50     JrtPackagesHelper(final Context context) throws IOException {
  51         super(context);
  52         jrtfs = FileSystems.getFileSystem(URI.create("jrt:/"));
  53     }
  54 
  55     @Override
  56     void close() throws IOException {
  57     }
  58 
  59     @Override
  60     Set<String> listPackage(final String pkg) throws IOException {
  61         final Set<String> props = new HashSet<>();
  62         // look for the /packages/<package_name> directory
  63         Path pkgDir = jrtfs.getPath("/packages/" + pkg);
  64         if (Files.exists(pkgDir)) {
  65             String pkgSlashName = pkg.replace('.', '/');
  66             try (DirectoryStream<Path> ds = Files.newDirectoryStream(pkgDir)) {
  67                 // it has module links under which this package occurs
  68                 for (Path mod : ds) {
  69                     // get the package directory under /modules
  70                     Path pkgUnderMod = jrtfs.getPath(mod.toString() + "/" + pkgSlashName);
  71                     try (DirectoryStream<Path> ds2 = Files.newDirectoryStream(pkgUnderMod)) {
  72                         for (Path p : ds2) {
  73                             String str = p.getFileName().toString();
  74                             // get rid of ".class", if any
  75                             if (str.endsWith(".class")) {
  76                                 final String clsName = str.substring(0, str.length() - ".class".length());
  77                                 if (clsName.indexOf('$') == -1 && isClassAccessible(pkg + "." + clsName)) {
  78                                     props.add(str);
  79                                 }
  80                             } else if (isPackageAccessible(pkg + "." + str)) {
  81                                 props.add(str);
  82                             }
  83                         }
  84                     }
  85                 }
  86             }
  87         }
  88         return props;
  89     }
  90 }