1 /*
   2  * Copyright (c) 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 package jdk.internal.loader;
  26 
  27 import java.util.Map;
  28 import jdk.internal.misc.VM;
  29 import jdk.internal.module.ServicesCatalog;
  30 
  31 /**
  32  * Used to archive the built-in class loaders, their services catalogs, and the
  33  * package-to-module map used by the built-in class loaders.
  34  */
  35 class ArchivedClassLoaders {
  36     private static ArchivedClassLoaders archivedClassLoaders;
  37 
  38     private final ClassLoader bootLoader;
  39     private final ClassLoader platformLoader;
  40     private final ClassLoader appLoader;
  41     private final ServicesCatalog[] servicesCatalogs;
  42     private final Map<String, ?> packageToModule;
  43 
  44     private ArchivedClassLoaders() {
  45         bootLoader = ClassLoaders.bootLoader();
  46         platformLoader = ClassLoaders.platformClassLoader();
  47         appLoader = ClassLoaders.appClassLoader();
  48 
  49         servicesCatalogs = new ServicesCatalog[3];
  50         servicesCatalogs[0] = BootLoader.getServicesCatalog();
  51         servicesCatalogs[1] = ServicesCatalog.getServicesCatalog(platformLoader);
  52         servicesCatalogs[2] = ServicesCatalog.getServicesCatalog(appLoader);
  53 
  54         packageToModule = BuiltinClassLoader.packageToModule();
  55     }
  56 
  57     ClassLoader bootLoader() {
  58         return bootLoader;
  59     }
  60 
  61     ClassLoader platformLoader() {
  62         return platformLoader;
  63     }
  64 
  65     ClassLoader appLoader() {
  66         return appLoader;
  67     }
  68 
  69     ServicesCatalog servicesCatalog(ClassLoader loader) {
  70         if (loader == null) {
  71             return servicesCatalogs[0];
  72         } else if (loader == platformLoader) {
  73             return servicesCatalogs[1];
  74         } else if (loader == appLoader) {
  75             return servicesCatalogs[2];
  76         } else {
  77             throw new InternalError();
  78         }
  79     }
  80 
  81     Map<String, ?> packageToModule() {
  82         return packageToModule;
  83     }
  84 
  85     static void archive() {
  86         archivedClassLoaders = new ArchivedClassLoaders();
  87     }
  88 
  89     static ArchivedClassLoaders get() {
  90         return archivedClassLoaders;
  91     }
  92 
  93     static {
  94         VM.initializeFromArchive(ArchivedClassLoaders.class);
  95     }
  96 }