< prev index next >

src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JmodArchive.java

Print this page

        

@@ -23,65 +23,134 @@
  * questions.
  */
 
 package jdk.tools.jlink.internal;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UncheckedIOException;
 import java.nio.file.Path;
 import java.util.Objects;
+import java.util.stream.Stream;
+
+import jdk.internal.jmod.JmodFile;
 import jdk.tools.jlink.internal.Archive.Entry.EntryType;
 
 /**
  * An Archive backed by a jmod file.
  */
-public class JmodArchive extends JarArchive {
-
+public class JmodArchive implements Archive {
     private static final String JMOD_EXT    = ".jmod";
-    private static final String MODULE_NAME = "module";
-    private static final String MODULE_INFO = "module-info.class";
-    private static final String CLASSES     = "classes";
-    private static final String NATIVE_LIBS = "native";
-    private static final String NATIVE_CMDS = "bin";
-    private static final String CONFIG      = "conf";
+
+    /**
+     * An entry located in a jmod file.
+     */
+    public class JmodEntry extends Entry {
+        private final JmodFile.Entry entry;
+
+        JmodEntry(String path, String name, EntryType type,
+                  JmodFile.Entry entry) {
+            super(JmodArchive.this, path, name, type);
+            this.entry = Objects.requireNonNull(entry);
+        }
+
+        /**
+         * Returns the number of uncompressed bytes for this entry.
+         */
+        @Override
+        public long size() {
+            return entry.size();
+        }
+
+        @Override
+        public InputStream stream() throws IOException {
+            return jmodFile.getInputStream(entry.section(), entry.name());
+        }
+    }
+
+    private final Path file;
+    private final String moduleName;
+    private JmodFile jmodFile;
 
     public JmodArchive(String mn, Path jmod) {
-        super(mn, jmod);
-        String filename = Objects.requireNonNull(jmod.getFileName()).toString();
+        Objects.requireNonNull(mn);
+        Objects.requireNonNull(jmod.getFileName());
+        String filename = jmod.toString();
         if (!filename.endsWith(JMOD_EXT)) {
             throw new UnsupportedOperationException("Unsupported format: " + filename);
         }
+        this.moduleName = mn;
+        this.file = jmod;
     }
 
     @Override
-    EntryType toEntryType(String entryName) {
-        String section = getSection(entryName.replace('\\', '/'));
+    public String moduleName() {
+        return moduleName;
+    }
+
+    @Override
+    public Path getPath() {
+        return file;
+    }
+
+    @Override
+    public Stream<Entry> entries() {
+        ensureOpen();
+        return jmodFile.stream()
+                       .map(this::toEntry);
+    }
+
+    private void ensureOpen() {
+        if (jmodFile == null) {
+            try {
+                open();
+            } catch(IOException ioe){
+                throw new UncheckedIOException(ioe);
+            }
+        }
+    }
+
+    @Override
+    public void open() throws IOException {
+        if (jmodFile != null) {
+            jmodFile.close();
+        }
+        this.jmodFile = new JmodFile(file);
+    }
+
+    @Override
+    public void close() throws IOException {
+        if (jmodFile != null) {
+            jmodFile.close();
+        }
+    }
+
+    private EntryType toEntryType(JmodFile.Section section) {
         switch (section) {
             case CLASSES:
                 return EntryType.CLASS_OR_RESOURCE;
             case NATIVE_LIBS:
                 return EntryType.NATIVE_LIB;
             case NATIVE_CMDS:
                 return EntryType.NATIVE_CMD;
             case CONFIG:
                 return EntryType.CONFIG;
-            case MODULE_NAME:
-                return EntryType.MODULE_NAME;
             default:
                 throw new InternalError("unexpected entry: " + section);
         }
     }
 
-    private static String getSection(String entryName) {
-        int i = entryName.indexOf('/');
-        // Unnamed section.
-        String section = "";
-        if (i > 0) {
-            section = entryName.substring(0, entryName.indexOf('/'));
-        }
-        return section;
+    private Entry toEntry(JmodFile.Entry entry) {
+        EntryType type = toEntryType(entry.section());
+        String name = entry.name();
+        String path = entry.section().jmodDir() + "/" + name;
+
+        // Entry.path() contains the kind of file native, conf, bin, ...
+        // Keep it to avoid naming conflict (eg: native/jvm.cfg and config/jvm.cfg
+        String resourceName = name;
+        if (type != EntryType.CLASS_OR_RESOURCE && type != EntryType.OTHER_FILES) {
+            resourceName = path;
     }
 
-    @Override
-    String getFileName(String entryName) {
-        entryName = entryName.replace('\\', '/');
-        return entryName.substring(entryName.indexOf('/') + 1);
+        return new JmodEntry(path, resourceName, type, entry);
     }
 }
< prev index next >