1 /*
  2  * Copyright (c) 2015, 2020, 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.internal.module;
 27 
 28 import java.lang.module.Configuration;
 29 import java.lang.module.ResolvedModule;
 30 import java.util.HashMap;
 31 import java.util.Map;
 32 import java.util.Set;
 33 import java.util.function.Function;
 34 
 35 import jdk.internal.loader.ClassLoaders;
 36 
 37 /**
 38  * Supports the mapping of modules to class loaders. The set of modules mapped
 39  * to the boot and platform class loaders is generated at build time from
 40  * this source file.
 41  */
 42 public final class ModuleLoaderMap {
 43 
 44     /**
 45      * Maps the system modules to the built-in class loaders.
 46      */
 47     private static final class Mapper implements Function<String, ClassLoader> {
 48 
 49         private static final ClassLoader PLATFORM_CLASSLOADER =
 50                 ClassLoaders.platformClassLoader();
 51         private static final ClassLoader APP_CLASSLOADER =
 52                 ClassLoaders.appClassLoader();
 53 
 54         private static final Integer PLATFORM_LOADER_INDEX = 1;
 55         private static final Integer APP_LOADER_INDEX      = 2;
 56 
 57         /**
 58          * Map from module to a class loader index. The index is resolved to the
 59          * actual class loader in {@code apply}.
 60          */
 61         private final Map<String, Integer> map;
 62 
 63         /**
 64          * Creates a Mapper to map module names in the given Configuration to
 65          * built-in classloaders.
 66          *
 67          * As a proxy for the actual classloader, we store an easily archiveable
 68          * index value in the internal map. The index is stored as a boxed value
 69          * so that we can cheaply do identity comparisons during bootstrap.
 70          */
 71         Mapper(Configuration cf) {
 72             var map = new HashMap<String, Integer>();
 73             for (ResolvedModule resolvedModule : cf.modules()) {
 74                 String mn = resolvedModule.name();
 75                 if (!Modules.bootModules.contains(mn)) {
 76                     if (Modules.platformModules.contains(mn)) {
 77                         map.put(mn, PLATFORM_LOADER_INDEX);
 78                     } else {
 79                         map.put(mn, APP_LOADER_INDEX);
 80                     }
 81                 }
 82             }
 83             this.map = map;
 84         }
 85 
 86         @Override
 87         public ClassLoader apply(String name) {
 88             Integer loader = map.get(name);
 89             if (loader == APP_LOADER_INDEX) {
 90                 return APP_CLASSLOADER;
 91             } else if (loader == PLATFORM_LOADER_INDEX) {
 92                 return PLATFORM_CLASSLOADER;
 93             } else { // BOOT_LOADER_INDEX
 94                 return null;
 95             }
 96         }
 97     }
 98 
 99     /**
100      * Returns the names of the modules defined to the boot loader.
101      */
102     public static Set<String> bootModules() {
103         return Modules.bootModules;
104     }
105 
106     /**
107      * Returns the names of the modules defined to the platform loader.
108      */
109     public static Set<String> platformModules() {
110         return Modules.platformModules;
111     }
112 
113     private static class Modules {
114         // list of boot modules is generated at build time.
115         private static final Set<String> bootModules =
116                 Set.of(new String[] { "@@BOOT_MODULE_NAMES@@" });
117 
118         // list of platform modules is generated at build time.
119         private static final Set<String> platformModules =
120                 Set.of(new String[] { "@@PLATFORM_MODULE_NAMES@@" });
121     }
122 
123     /**
124      * Returns a function to map modules in the given configuration to the
125      * built-in class loaders.
126      */
127     static Function<String, ClassLoader> mappingFunction(Configuration cf) {
128         return new Mapper(cf);
129     }
130 
131     /**
132      * When defining modules for a configuration, we only allow defining modules
133      * to the boot or platform classloader if the ClassLoader mapping function
134      * originate from here.
135      */
136     public static boolean isBuiltinMapper(Function<String, ClassLoader> clf) {
137         return clf instanceof Mapper;
138     }
139 }