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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.IOException;
  25 import java.io.InputStream;
  26 import java.lang.module.ModuleReader;
  27 import java.lang.module.ModuleReference;
  28 import java.lang.module.ResolvedModule;
  29 import java.lang.reflect.Layer;
  30 import java.net.URL;
  31 import java.util.Enumeration;
  32 
  33 /**
  34  * Usage: Main $MODULE
  35  *
  36  * Finds $MODULE in the boot layer and then tests that it can locate every
  37  * resource in the module content (JAR file).
  38  */
  39 
  40 public class Main {
  41 
  42     static void testFind(String name) throws Exception {
  43         // getResource
  44         URL url = Main.class.getClassLoader().getResource(name);
  45         if (url == null)
  46             throw new RuntimeException("Unable to locate: " + name);
  47         System.out.println(name + " => " + url);
  48 
  49         // getResources
  50         Enumeration<URL> urls = Main.class.getClassLoader().getResources(name);
  51         if (!urls.hasMoreElements())
  52             throw new RuntimeException("Unable to locate: " + name);
  53         URL first = urls.nextElement();
  54         if (!first.toURI().equals(url.toURI()))
  55             throw new RuntimeException("found " + first + " ???");
  56 
  57         // getResourceAsStream
  58         if (!url.toString().endsWith("/")) {
  59             InputStream in = Main.class.getClassLoader().getResourceAsStream(name);
  60             if (in == null)
  61                 throw new RuntimeException("Unable to locate: " + name);
  62             in.close();
  63         }
  64     }
  65 
  66     static void testFindUnchecked(String name) {
  67         try {
  68             testFind(name);
  69         } catch (Exception e) {
  70             throw new RuntimeException(e);
  71         }
  72     }
  73 
  74     public static void main(String[] args) throws Exception {
  75         String mn = args[0];
  76 
  77         ModuleReference mref = Layer.boot()
  78                 .configuration()
  79                 .findModule(mn)
  80                 .map(ResolvedModule::reference)
  81                 .orElseThrow(() -> new RuntimeException(mn + " not resolved!!"));
  82 
  83         try (ModuleReader reader = mref.open()) {
  84             reader.list().forEach(name -> {
  85                 testFindUnchecked(name);
  86 
  87                 // if the resource is a directory then find without trailing slash
  88                 if (name.endsWith("/")) {
  89                     testFindUnchecked(name.substring(0, name.length() - 1));
  90                 }
  91             });
  92         }
  93     }
  94 }