1 /*
   2  * Copyright (c) 2014, 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 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 }