|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 2021, 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 com.oracle.svm.hosted; |
| 27 | + |
| 28 | +import java.util.ArrayDeque; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.Deque; |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.HashSet; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Locale; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.MissingResourceException; |
| 38 | +import java.util.ResourceBundle; |
| 39 | +import java.util.Set; |
| 40 | +import java.util.stream.Collectors; |
| 41 | + |
| 42 | +import com.oracle.svm.core.ClassLoaderSupport; |
| 43 | + |
| 44 | +import jdk.internal.module.Modules; |
| 45 | + |
| 46 | +public final class ClassLoaderSupportImpl extends ClassLoaderSupport { |
| 47 | + |
| 48 | + private final NativeImageClassLoaderSupport classLoaderSupport; |
| 49 | + private final Map<String, Set<Module>> packageToModules; |
| 50 | + |
| 51 | + ClassLoaderSupportImpl(NativeImageClassLoaderSupport classLoaderSupport) { |
| 52 | + this.classLoaderSupport = classLoaderSupport; |
| 53 | + packageToModules = new HashMap<>(); |
| 54 | + buildPackageToModulesMap(classLoaderSupport); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected boolean isNativeImageClassLoaderImpl(ClassLoader loader) { |
| 59 | + return loader == classLoaderSupport.getClassLoader() || loader instanceof NativeImageSystemClassLoader; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public List<ResourceBundle> getResourceBundle(String bundleSpec, Locale locale) { |
| 64 | + String[] specParts = bundleSpec.split(":", 2); |
| 65 | + String moduleName; |
| 66 | + String bundleName; |
| 67 | + if (specParts.length > 1) { |
| 68 | + moduleName = specParts[0]; |
| 69 | + bundleName = specParts[1]; |
| 70 | + } else { |
| 71 | + moduleName = null; |
| 72 | + bundleName = specParts[0]; |
| 73 | + } |
| 74 | + String packageName = packageName(bundleName); |
| 75 | + if (packageName == null) { |
| 76 | + throw new MissingResourceException("ResourceBundle does not seem to be a fully qualified class name.", bundleName, locale.toLanguageTag()); |
| 77 | + } |
| 78 | + Set<Module> modules; |
| 79 | + if (moduleName != null) { |
| 80 | + modules = classLoaderSupport.findModule(moduleName).stream().collect(Collectors.toSet()); |
| 81 | + } else { |
| 82 | + modules = packageToModules.getOrDefault(packageName, Collections.emptySet()); |
| 83 | + } |
| 84 | + if (modules.isEmpty()) { |
| 85 | + throw new MissingResourceException("ResourceBundle cannot be found.", bundleSpec, locale.toLanguageTag()); |
| 86 | + } |
| 87 | + ArrayList<ResourceBundle> resourceBundles = new ArrayList<>(); |
| 88 | + for (Module module : modules) { |
| 89 | + Module exportTargetModule = ClassLoaderSupportImpl.class.getModule(); |
| 90 | + if (!module.isExported(packageName, exportTargetModule)) { |
| 91 | + System.out.printf("!!! Open for ResourceBundle access: %s:%s%n", module.getName(), packageName); |
| 92 | + Modules.addOpens(module, packageName, exportTargetModule); |
| 93 | + } |
| 94 | + resourceBundles.add(ResourceBundle.getBundle(bundleName, locale, module)); |
| 95 | + } |
| 96 | + return resourceBundles; |
| 97 | + } |
| 98 | + |
| 99 | + private static String packageName(String bundleName) { |
| 100 | + int classSep = bundleName.replace('/', '.').lastIndexOf('.'); |
| 101 | + if (classSep == -1) { |
| 102 | + /* The bundle is not specified via a java.class or java.properties format. */ |
| 103 | + return null; |
| 104 | + } |
| 105 | + return bundleName.substring(0, classSep); |
| 106 | + } |
| 107 | + |
| 108 | + private void buildPackageToModulesMap(NativeImageClassLoaderSupport classLoaderSupport) { |
| 109 | + for (ModuleLayer layer : allLayers(classLoaderSupport.moduleLayerForImageBuild)) { |
| 110 | + for (Module module : layer.modules()) { |
| 111 | + for (String packageName : module.getDescriptor().packages()) { |
| 112 | + addToPackageNameModules(module, packageName); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + dumpPackageNameModulesMapping(); |
| 117 | + } |
| 118 | + |
| 119 | + private static List<ModuleLayer> allLayers(ModuleLayer moduleLayer) { |
| 120 | + /** Implementation taken from {@link ModuleLayer#layers()} */ |
| 121 | + List<ModuleLayer> allLayers = new ArrayList<>(); |
| 122 | + Set<ModuleLayer> visited = new HashSet<>(); |
| 123 | + Deque<ModuleLayer> stack = new ArrayDeque<>(); |
| 124 | + visited.add(moduleLayer); |
| 125 | + stack.push(moduleLayer); |
| 126 | + |
| 127 | + while (!stack.isEmpty()) { |
| 128 | + ModuleLayer layer = stack.pop(); |
| 129 | + allLayers.add(layer); |
| 130 | + |
| 131 | + // push in reverse order |
| 132 | + for (int i = layer.parents().size() - 1; i >= 0; i--) { |
| 133 | + ModuleLayer parent = layer.parents().get(i); |
| 134 | + if (!visited.contains(parent)) { |
| 135 | + visited.add(parent); |
| 136 | + stack.push(parent); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + return allLayers; |
| 141 | + } |
| 142 | + |
| 143 | + private void addToPackageNameModules(Module moduleName, String packageName) { |
| 144 | + Set<Module> prevValue = packageToModules.get(packageName); |
| 145 | + if (prevValue == null) { |
| 146 | + /* Mostly packageName is only used in a single module */ |
| 147 | + packageToModules.put(packageName, Collections.singleton(moduleName)); |
| 148 | + } else if (prevValue.size() == 1) { |
| 149 | + /* Transition to HashSet - happens rarely */ |
| 150 | + HashSet<Module> newValue = new HashSet<>(); |
| 151 | + newValue.add(prevValue.iterator().next()); |
| 152 | + newValue.add(moduleName); |
| 153 | + packageToModules.put(packageName, newValue); |
| 154 | + } else if (prevValue.size() > 1) { |
| 155 | + /* Add to exiting HashSet - happens rarely */ |
| 156 | + prevValue.add(moduleName); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + public void dumpPackageNameModulesMapping() { |
| 161 | + packageToModules.entrySet().stream() |
| 162 | + .sorted(Map.Entry.comparingByKey()) |
| 163 | + .map(e -> e.getKey() + " -> " + e.getValue().stream() |
| 164 | + .map(Module::getName) |
| 165 | + .collect(Collectors.joining(", "))) |
| 166 | + .forEach(System.out::println); |
| 167 | + } |
| 168 | +} |
0 commit comments