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