src/java.base/share/classes/jdk/internal/module/ModuleReferences.java

Print this page




  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.util.List;
  40 import java.util.Objects;
  41 import java.util.Optional;
  42 import java.util.concurrent.locks.Lock;
  43 import java.util.concurrent.locks.ReadWriteLock;
  44 import java.util.concurrent.locks.ReentrantReadWriteLock;
  45 import java.util.function.Supplier;
  46 import java.util.jar.JarEntry;
  47 import java.util.jar.JarFile;
  48 import java.util.stream.Collectors;
  49 import java.util.stream.Stream;
  50 import java.util.zip.ZipFile;
  51 
  52 import jdk.internal.jmod.JmodFile;
  53 import jdk.internal.misc.SharedSecrets;
  54 import jdk.internal.module.ModuleHashes.HashSupplier;
  55 import jdk.internal.util.jar.VersionedStream;
  56 import sun.net.www.ParseUtil;
  57 
  58 
  59 /**
  60  * A factory for creating ModuleReference implementations where the modules are
  61  * packaged as modular JAR file, JMOD files or where the modules are exploded
  62  * on the file system.
  63  */
  64 
  65 class ModuleReferences {
  66     private ModuleReferences() { }
  67 
  68     /**
  69      * Creates a ModuleReference to a possibly-patched module
  70      */
  71     private static ModuleReference newModule(ModuleInfo.Attributes attrs,
  72                                              URI uri,
  73                                              Supplier<ModuleReader> supplier,
  74                                              ModulePatcher patcher,
  75                                              HashSupplier hasher) {


 233                                    JarFile.runtimeVersion());
 234             } catch (IOException ioe) {
 235                 throw new UncheckedIOException(ioe);
 236             }
 237         }
 238 
 239         JarModuleReader(Path path, URI uri) {
 240             this.jf = newJarFile(path);
 241             this.uri = uri;
 242         }
 243 
 244         private JarEntry getEntry(String name) {
 245             return jf.getJarEntry(Objects.requireNonNull(name));
 246         }
 247 
 248         @Override
 249         Optional<URI> implFind(String name) throws IOException {
 250             JarEntry je = getEntry(name);
 251             if (je != null) {
 252                 if (jf.isMultiRelease())
 253                     name = SharedSecrets.javaUtilJarAccess().getRealName(jf, je);
 254                 if (je.isDirectory() && !name.endsWith("/"))
 255                     name += "/";
 256                 String encodedPath = ParseUtil.encodePath(name, false);
 257                 String uris = "jar:" + uri + "!/" + encodedPath;
 258                 return Optional.of(URI.create(uris));
 259             } else {
 260                 return Optional.empty();
 261             }
 262         }
 263 
 264         @Override
 265         Optional<InputStream> implOpen(String name) throws IOException {
 266             JarEntry je = getEntry(name);
 267             if (je != null) {
 268                 return Optional.of(jf.getInputStream(je));
 269             } else {
 270                 return Optional.empty();
 271             }
 272         }
 273 
 274         @Override
 275         Stream<String> implList() throws IOException {
 276             // take snapshot to avoid async close
 277             List<String> names = VersionedStream.stream(jf)
 278                     .map(JarEntry::getName)
 279                     .collect(Collectors.toList());
 280             return names.stream();
 281         }
 282 
 283         @Override
 284         void implClose() throws IOException {
 285             jf.close();
 286         }
 287     }
 288 
 289 
 290     /**
 291      * A ModuleReader for a JMOD file.
 292      */
 293     static class JModModuleReader extends SafeCloseModuleReader {
 294         private final JmodFile jf;
 295         private final URI uri;
 296 
 297         static JmodFile newJmodFile(Path path) {




  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.util.List;
  40 import java.util.Objects;
  41 import java.util.Optional;
  42 import java.util.concurrent.locks.Lock;
  43 import java.util.concurrent.locks.ReadWriteLock;
  44 import java.util.concurrent.locks.ReentrantReadWriteLock;
  45 import java.util.function.Supplier;
  46 import java.util.jar.JarEntry;
  47 import java.util.jar.JarFile;
  48 import java.util.stream.Collectors;
  49 import java.util.stream.Stream;
  50 import java.util.zip.ZipFile;
  51 
  52 import jdk.internal.jmod.JmodFile;

  53 import jdk.internal.module.ModuleHashes.HashSupplier;

  54 import sun.net.www.ParseUtil;
  55 
  56 
  57 /**
  58  * A factory for creating ModuleReference implementations where the modules are
  59  * packaged as modular JAR file, JMOD files or where the modules are exploded
  60  * on the file system.
  61  */
  62 
  63 class ModuleReferences {
  64     private ModuleReferences() { }
  65 
  66     /**
  67      * Creates a ModuleReference to a possibly-patched module
  68      */
  69     private static ModuleReference newModule(ModuleInfo.Attributes attrs,
  70                                              URI uri,
  71                                              Supplier<ModuleReader> supplier,
  72                                              ModulePatcher patcher,
  73                                              HashSupplier hasher) {


 231                                    JarFile.runtimeVersion());
 232             } catch (IOException ioe) {
 233                 throw new UncheckedIOException(ioe);
 234             }
 235         }
 236 
 237         JarModuleReader(Path path, URI uri) {
 238             this.jf = newJarFile(path);
 239             this.uri = uri;
 240         }
 241 
 242         private JarEntry getEntry(String name) {
 243             return jf.getJarEntry(Objects.requireNonNull(name));
 244         }
 245 
 246         @Override
 247         Optional<URI> implFind(String name) throws IOException {
 248             JarEntry je = getEntry(name);
 249             if (je != null) {
 250                 if (jf.isMultiRelease())
 251                     name = je.getRealName();
 252                 if (je.isDirectory() && !name.endsWith("/"))
 253                     name += "/";
 254                 String encodedPath = ParseUtil.encodePath(name, false);
 255                 String uris = "jar:" + uri + "!/" + encodedPath;
 256                 return Optional.of(URI.create(uris));
 257             } else {
 258                 return Optional.empty();
 259             }
 260         }
 261 
 262         @Override
 263         Optional<InputStream> implOpen(String name) throws IOException {
 264             JarEntry je = getEntry(name);
 265             if (je != null) {
 266                 return Optional.of(jf.getInputStream(je));
 267             } else {
 268                 return Optional.empty();
 269             }
 270         }
 271 
 272         @Override
 273         Stream<String> implList() throws IOException {
 274             // take snapshot to avoid async close
 275             List<String> names = jf.versionedStream()
 276                     .map(JarEntry::getName)
 277                     .collect(Collectors.toList());
 278             return names.stream();
 279         }
 280 
 281         @Override
 282         void implClose() throws IOException {
 283             jf.close();
 284         }
 285     }
 286 
 287 
 288     /**
 289      * A ModuleReader for a JMOD file.
 290      */
 291     static class JModModuleReader extends SafeCloseModuleReader {
 292         private final JmodFile jf;
 293         private final URI uri;
 294 
 295         static JmodFile newJmodFile(Path path) {