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 java.lang.module; 27 28 import java.io.File; 29 import java.io.IOError; 30 import java.io.IOException; 31 import java.io.InputStream; 32 import java.io.UncheckedIOException; 33 import java.net.URI; 34 import java.nio.ByteBuffer; 35 import java.nio.file.Files; 36 import java.nio.file.Path; 37 import java.nio.file.Paths; 38 import java.util.List; 39 import java.util.Objects; 40 import java.util.Optional; 41 import java.util.concurrent.locks.Lock; 42 import java.util.concurrent.locks.ReadWriteLock; 43 import java.util.concurrent.locks.ReentrantReadWriteLock; 44 import java.util.function.Supplier; 45 import java.util.jar.JarEntry; 46 import java.util.jar.JarFile; 47 import java.util.stream.Collectors; 48 import java.util.stream.Stream; 49 import java.util.zip.ZipFile; 50 51 import jdk.internal.jmod.JmodFile; 52 import jdk.internal.misc.JavaLangAccess; 53 import jdk.internal.misc.SharedSecrets; 54 import jdk.internal.module.ModuleBootstrap; 55 import jdk.internal.module.ModuleHashes; 56 import jdk.internal.module.ModuleHashes.HashSupplier; 57 import jdk.internal.module.ModulePatcher; 58 import jdk.internal.util.jar.VersionedStream; 59 import sun.net.www.ParseUtil; 60 61 62 /** 63 * A factory for creating ModuleReference implementations where the modules are 64 * packaged as modular JAR file, JMOD files or where the modules are exploded 65 * on the file system. 66 */ 67 68 class ModuleReferences { 69 70 private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess(); 71 72 private ModuleReferences() { } 73 74 /** 75 * Creates a ModuleReference to a module or to patched module when 76 * creating modules for the boot Layer and --patch-module is specified. 77 */ 78 private static ModuleReference newModule(ModuleDescriptor md, 79 URI uri, 80 Supplier<ModuleReader> supplier, 81 HashSupplier hasher) { 82 83 ModuleReference mref = new ModuleReference(md, uri, supplier, hasher); 84 if (JLA.getBootLayer() == null) 85 mref = ModuleBootstrap.patcher().patchIfNeeded(mref); 86 87 return mref; 88 } 89 90 /** 91 * Creates a ModuleReference to a module packaged as a modular JAR. 92 */ 93 static ModuleReference newJarModule(ModuleDescriptor md, Path file) { 94 URI uri = file.toUri(); 95 Supplier<ModuleReader> supplier = () -> new JarModuleReader(file, uri); 96 HashSupplier hasher = (a) -> ModuleHashes.computeHash(file, a); 97 return newModule(md, uri, supplier, hasher); 98 } 99 100 /** 101 * Creates a ModuleReference to a module packaged as a JMOD. 102 */ 103 static ModuleReference newJModModule(ModuleDescriptor md, Path file) { 104 URI uri = file.toUri(); 105 Supplier<ModuleReader> supplier = () -> new JModModuleReader(file, uri); 106 HashSupplier hasher = (a) -> ModuleHashes.computeHash(file, a); 107 return newModule(md, file.toUri(), supplier, hasher); 108 } 109 110 /** 111 * Creates a ModuleReference to an exploded module. 112 */ 113 static ModuleReference newExplodedModule(ModuleDescriptor md, Path dir) { 114 Supplier<ModuleReader> supplier = () -> new ExplodedModuleReader(dir); 115 return newModule(md, dir.toUri(), supplier, null); 116 } 117 118 119 /** 120 * A base module reader that encapsulates machinery required to close the 121 * module reader safely. 122 */ 123 static abstract class SafeCloseModuleReader implements ModuleReader { 124 125 // RW lock to support safe close 126 private final ReadWriteLock lock = new ReentrantReadWriteLock(); 127 private final Lock readLock = lock.readLock(); 128 private final Lock writeLock = lock.writeLock(); 129 private boolean closed; 130 131 SafeCloseModuleReader() { } 132 133 /** 134 * Returns a URL to resource. This method is invoked by the find 135 * method to do the actual work of finding the resource. | 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.io.File; 29 import java.io.IOError; 30 import java.io.IOException; 31 import java.io.InputStream; 32 import java.io.UncheckedIOException; 33 import java.lang.module.ModuleReader; 34 import java.lang.module.ModuleReference; 35 import java.net.URI; 36 import java.nio.ByteBuffer; 37 import java.nio.file.Files; 38 import java.nio.file.Path; 39 import java.nio.file.Paths; 40 import java.util.List; 41 import java.util.Objects; 42 import java.util.Optional; 43 import java.util.concurrent.locks.Lock; 44 import java.util.concurrent.locks.ReadWriteLock; 45 import java.util.concurrent.locks.ReentrantReadWriteLock; 46 import java.util.function.Supplier; 47 import java.util.jar.JarEntry; 48 import java.util.jar.JarFile; 49 import java.util.stream.Collectors; 50 import java.util.stream.Stream; 51 import java.util.zip.ZipFile; 52 53 import jdk.internal.jmod.JmodFile; 54 import jdk.internal.misc.JavaLangAccess; 55 import jdk.internal.misc.SharedSecrets; 56 import jdk.internal.module.ModuleHashes.HashSupplier; 57 import jdk.internal.util.jar.VersionedStream; 58 import sun.net.www.ParseUtil; 59 60 61 /** 62 * A factory for creating ModuleReference implementations where the modules are 63 * packaged as modular JAR file, JMOD files or where the modules are exploded 64 * on the file system. 65 */ 66 67 class ModuleReferences { 68 69 private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess(); 70 71 private ModuleReferences() { } 72 73 /** 74 * Creates a ModuleReference to a module or to patched module when 75 * creating modules for the boot Layer and --patch-module is specified. 76 */ 77 private static ModuleReference newModule(ModuleInfo.Attributes attrs, 78 URI uri, 79 Supplier<ModuleReader> supplier, 80 HashSupplier hasher) { 81 82 ModuleReference mref = new ModuleReferenceImpl(attrs.descriptor(), 83 uri, 84 supplier, 85 null, 86 attrs.recordedHashes(), 87 hasher, 88 attrs.moduleResolution()); 89 if (JLA.getBootLayer() == null) 90 mref = ModuleBootstrap.patcher().patchIfNeeded(mref); 91 92 return mref; 93 } 94 95 /** 96 * Creates a ModuleReference to a module packaged as a modular JAR. 97 */ 98 static ModuleReference newJarModule(ModuleInfo.Attributes attrs, Path file) { 99 URI uri = file.toUri(); 100 Supplier<ModuleReader> supplier = () -> new JarModuleReader(file, uri); 101 HashSupplier hasher = (a) -> ModuleHashes.computeHash(file, a); 102 return newModule(attrs, uri, supplier, hasher); 103 } 104 105 /** 106 * Creates a ModuleReference to a module packaged as a JMOD. 107 */ 108 static ModuleReference newJModModule(ModuleInfo.Attributes attrs, Path file) { 109 URI uri = file.toUri(); 110 Supplier<ModuleReader> supplier = () -> new JModModuleReader(file, uri); 111 HashSupplier hasher = (a) -> ModuleHashes.computeHash(file, a); 112 return newModule(attrs, uri, supplier, hasher); 113 } 114 115 /** 116 * Creates a ModuleReference to an exploded module. 117 */ 118 static ModuleReference newExplodedModule(ModuleInfo.Attributes attrs, Path dir) { 119 Supplier<ModuleReader> supplier = () -> new ExplodedModuleReader(dir); 120 return newModule(attrs, dir.toUri(), supplier, null); 121 } 122 123 124 /** 125 * A base module reader that encapsulates machinery required to close the 126 * module reader safely. 127 */ 128 static abstract class SafeCloseModuleReader implements ModuleReader { 129 130 // RW lock to support safe close 131 private final ReadWriteLock lock = new ReentrantReadWriteLock(); 132 private final Lock readLock = lock.readLock(); 133 private final Lock writeLock = lock.writeLock(); 134 private boolean closed; 135 136 SafeCloseModuleReader() { } 137 138 /** 139 * Returns a URL to resource. This method is invoked by the find 140 * method to do the actual work of finding the resource. |