1 /*
   2  * Copyright (c) 2016, 2019, 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.incubator.jpackage.internal;
  27 
  28 import java.io.File;
  29 import java.io.FileInputStream;
  30 import java.io.FileNotFoundException;
  31 import java.io.IOException;
  32 import java.util.ArrayList;
  33 import java.util.List;
  34 import java.util.zip.ZipEntry;
  35 import java.util.zip.ZipInputStream;
  36 
  37 final class ModFile {
  38     private final String filename;
  39     private final ModType moduleType;
  40 
  41     enum JarType {All, UnnamedJar, ModularJar}
  42     enum ModType {
  43             Unknown, UnnamedJar, ModularJar, Jmod, ExplodedModule}
  44 
  45     ModFile(File aFile) {
  46         super();
  47         filename = aFile.getPath();
  48         moduleType = getModType(aFile);
  49     }
  50 
  51     String getModName() {
  52         File file = new File(getFileName());
  53         // do not try to remove extension for directories
  54         return moduleType == ModType.ExplodedModule ?
  55                 file.getName() : getFileWithoutExtension(file.getName());
  56     }
  57 
  58     String getFileName() {
  59         return filename;
  60     }
  61 
  62     ModType getModType() {
  63         return moduleType;
  64     }
  65 
  66     private static ModType getModType(File aFile) {
  67         ModType result = ModType.Unknown;
  68         String filename = aFile.getAbsolutePath();
  69 
  70         if (aFile.isFile()) {
  71             if (filename.endsWith(".jmod")) {
  72                 result = ModType.Jmod;
  73             }
  74             else if (filename.endsWith(".jar")) {
  75                 JarType status = isModularJar(filename);
  76 
  77                 if (status == JarType.ModularJar) {
  78                     result = ModType.ModularJar;
  79                 }
  80                 else if (status == JarType.UnnamedJar) {
  81                     result = ModType.UnnamedJar;
  82                 }
  83             }
  84         }
  85         else if (aFile.isDirectory()) {
  86             File moduleInfo = new File(
  87                     filename + File.separator + "module-info.class");
  88 
  89             if (moduleInfo.exists()) {
  90                 result = ModType.ExplodedModule;
  91             }
  92         }
  93 
  94         return result;
  95     }
  96 
  97     private static JarType isModularJar(String FileName) {
  98         JarType result = JarType.All;
  99 
 100         try (ZipInputStream zip =
 101                     new ZipInputStream(new FileInputStream(FileName))) {
 102             result = JarType.UnnamedJar;
 103 
 104             for (ZipEntry entry = zip.getNextEntry(); entry != null;
 105                     entry = zip.getNextEntry()) {
 106                 if (entry.getName().matches("module-info.class")) {
 107                     result = JarType.ModularJar;
 108                     break;
 109                 }
 110             }
 111         } catch (IOException ex) {
 112         }
 113 
 114         return result;
 115     }
 116 
 117     private static String getFileWithoutExtension(String FileName) {
 118         return FileName.replaceFirst("[.][^.]+$", "");
 119     }
 120 }