< prev index next >

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

Print this page




   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.tools.jlink.internal;
  27 



  28 import java.nio.file.Path;
  29 import java.util.Objects;



  30 import jdk.tools.jlink.internal.Archive.Entry.EntryType;
  31 
  32 /**
  33  * An Archive backed by a jmod file.
  34  */
  35 public class JmodArchive extends JarArchive {
  36 
  37     private static final String JMOD_EXT    = ".jmod";
  38     private static final String MODULE_NAME = "module";
  39     private static final String MODULE_INFO = "module-info.class";
  40     private static final String CLASSES     = "classes";
  41     private static final String NATIVE_LIBS = "native";
  42     private static final String NATIVE_CMDS = "bin";
  43     private static final String CONFIG      = "conf";
























  44 
  45     public JmodArchive(String mn, Path jmod) {
  46         super(mn, jmod);
  47         String filename = Objects.requireNonNull(jmod.getFileName()).toString();

  48         if (!filename.endsWith(JMOD_EXT)) {
  49             throw new UnsupportedOperationException("Unsupported format: " + filename);
  50         }


  51     }
  52 
  53     @Override
  54     EntryType toEntryType(String entryName) {
  55         String section = getSection(entryName.replace('\\', '/'));








































  56         switch (section) {
  57             case CLASSES:
  58                 return EntryType.CLASS_OR_RESOURCE;
  59             case NATIVE_LIBS:
  60                 return EntryType.NATIVE_LIB;
  61             case NATIVE_CMDS:
  62                 return EntryType.NATIVE_CMD;
  63             case CONFIG:
  64                 return EntryType.CONFIG;
  65             case MODULE_NAME:
  66                 return EntryType.MODULE_NAME;
  67             default:
  68                 throw new InternalError("unexpected entry: " + section);
  69         }
  70     }
  71 
  72     private static String getSection(String entryName) {
  73         int i = entryName.indexOf('/');
  74         // Unnamed section.
  75         String section = "";
  76         if (i > 0) {
  77             section = entryName.substring(0, entryName.indexOf('/'));
  78         }
  79         return section;


  80     }
  81 
  82     @Override
  83     String getFileName(String entryName) {
  84         entryName = entryName.replace('\\', '/');
  85         return entryName.substring(entryName.indexOf('/') + 1);
  86     }
  87 }


   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.tools.jlink.internal;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.io.UncheckedIOException;
  31 import java.nio.file.Path;
  32 import java.util.Objects;
  33 import java.util.stream.Stream;
  34 
  35 import jdk.internal.jmod.JmodFile;
  36 import jdk.tools.jlink.internal.Archive.Entry.EntryType;
  37 
  38 /**
  39  * An Archive backed by a jmod file.
  40  */
  41 public class JmodArchive implements Archive {

  42     private static final String JMOD_EXT    = ".jmod";
  43 
  44     /**
  45      * An entry located in a jmod file.
  46      */
  47     public class JmodEntry extends Entry {
  48         private final JmodFile.Entry entry;
  49 
  50         JmodEntry(String path, String name, EntryType type,
  51                   JmodFile.Entry entry) {
  52             super(JmodArchive.this, path, name, type);
  53             this.entry = Objects.requireNonNull(entry);
  54         }
  55 
  56         /**
  57          * Returns the number of uncompressed bytes for this entry.
  58          */
  59         @Override
  60         public long size() {
  61             return entry.size();
  62         }
  63 
  64         @Override
  65         public InputStream stream() throws IOException {
  66             return jmodFile.getInputStream(entry.section(), entry.name());
  67         }
  68     }
  69 
  70     private final Path file;
  71     private final String moduleName;
  72     private JmodFile jmodFile;
  73 
  74     public JmodArchive(String mn, Path jmod) {
  75         Objects.requireNonNull(mn);
  76         Objects.requireNonNull(jmod.getFileName());
  77         String filename = jmod.toString();
  78         if (!filename.endsWith(JMOD_EXT)) {
  79             throw new UnsupportedOperationException("Unsupported format: " + filename);
  80         }
  81         this.moduleName = mn;
  82         this.file = jmod;
  83     }
  84 
  85     @Override
  86     public String moduleName() {
  87         return moduleName;
  88     }
  89 
  90     @Override
  91     public Path getPath() {
  92         return file;
  93     }
  94 
  95     @Override
  96     public Stream<Entry> entries() {
  97         ensureOpen();
  98         return jmodFile.stream()
  99                        .map(this::toEntry);
 100     }
 101 
 102     private void ensureOpen() {
 103         if (jmodFile == null) {
 104             try {
 105                 open();
 106             } catch(IOException ioe){
 107                 throw new UncheckedIOException(ioe);
 108             }
 109         }
 110     }
 111 
 112     @Override
 113     public void open() throws IOException {
 114         if (jmodFile != null) {
 115             jmodFile.close();
 116         }
 117         this.jmodFile = new JmodFile(file);
 118     }
 119 
 120     @Override
 121     public void close() throws IOException {
 122         if (jmodFile != null) {
 123             jmodFile.close();
 124         }
 125     }
 126 
 127     private EntryType toEntryType(JmodFile.Section section) {
 128         switch (section) {
 129             case CLASSES:
 130                 return EntryType.CLASS_OR_RESOURCE;
 131             case NATIVE_LIBS:
 132                 return EntryType.NATIVE_LIB;
 133             case NATIVE_CMDS:
 134                 return EntryType.NATIVE_CMD;
 135             case CONFIG:
 136                 return EntryType.CONFIG;


 137             default:
 138                 throw new InternalError("unexpected entry: " + section);
 139         }
 140     }
 141 
 142     private Entry toEntry(JmodFile.Entry entry) {
 143         EntryType type = toEntryType(entry.section());
 144         String name = entry.name();
 145         String path = entry.section().jmodDir() + "/" + name;
 146 
 147         // Entry.path() contains the kind of file native, conf, bin, ...
 148         // Keep it to avoid naming conflict (eg: native/jvm.cfg and config/jvm.cfg
 149         String resourceName = name;
 150         if (type != EntryType.CLASS_OR_RESOURCE && type != EntryType.OTHER_FILES) {
 151             resourceName = path;
 152         }
 153 
 154         return new JmodEntry(path, resourceName, type, entry);



 155     }
 156 }
< prev index next >