1 /*
   2  * Copyright (c) 2015, 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 package com.oracle.tools.packager;
  26 
  27 import java.io.File;
  28 import java.io.FileInputStream;
  29 import java.io.FileNotFoundException;
  30 import java.io.IOException;
  31 import java.util.ArrayList;
  32 import java.util.Collection;
  33 import java.util.List;
  34 import java.util.zip.ZipEntry;
  35 import java.util.zip.ZipInputStream;
  36 
  37 
  38 public final class Module {
  39     private String FFileName;
  40     private ModuleType FModuleType;
  41 
  42     private enum JarType {Unknown, UnnamedJar, ModularJar}
  43 
  44 
  45     public enum ModuleType {Unknown, UnnamedJar, ModularJar, Jmod, ExplodedModule}
  46 
  47     public Module(File AFile) {
  48         super();
  49         FFileName = AFile.getPath();
  50         FModuleType = getModuleType(AFile);
  51     }
  52 
  53     public String getFileName() {
  54         return FFileName;
  55     }
  56 
  57     public String getModulePath() {
  58         File file = new File(getFileName());
  59         return file.getParent();
  60     }
  61 
  62     public String getModuleName() {
  63         File file = new File(getFileName());
  64         return getFileWithoutExtension(file.getName());
  65     }
  66 
  67     public ModuleType getModuleType() {
  68         return FModuleType;
  69     }
  70 
  71     public List<Module> getRequiredModules() {
  72         List<Module> result = new ArrayList();
  73 
  74         List<String> files = new ArrayList();
  75         files.add(getFileName());
  76         Collection<String> detectedModules = JDepHelper.calculateModules(files, null);
  77 
  78         for (String filename : detectedModules) {
  79             Module module = new Module(new File(filename));
  80             result.add(module);
  81         }
  82 
  83         return result;
  84     }
  85 
  86     private static ModuleType getModuleType(File AFile) {
  87         ModuleType result = ModuleType.Unknown;
  88         String filename = AFile.getAbsolutePath();
  89 
  90         if (AFile.isFile()) {
  91             if (filename.endsWith(".jmod")) {
  92                 result = ModuleType.Jmod;
  93             }
  94             else if (filename.endsWith(".jar")) {
  95                 JarType status = isModularJar(filename);
  96 
  97                 if (status == JarType.ModularJar) {
  98                     result = ModuleType.ModularJar;
  99                 }
 100                 else if (status == JarType.UnnamedJar) {
 101                     result = ModuleType.UnnamedJar;
 102                 }
 103             }
 104         }
 105         else if (AFile.isDirectory()) {
 106             File moduleInfo = new File(filename + File.separator + "module-info.class");
 107 
 108             if (moduleInfo.exists()) {
 109                 result = ModuleType.ExplodedModule;
 110             }
 111         }
 112 
 113         return result;
 114     }
 115 
 116     private static JarType isModularJar(String FileName) {
 117         JarType result = JarType.Unknown;
 118         List<String> classNames = new ArrayList<String>();
 119 
 120         try {
 121             ZipInputStream zip = new ZipInputStream(new FileInputStream(FileName));
 122             result = JarType.UnnamedJar;
 123 
 124             try {
 125                 for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
 126                     if (entry.getName().matches("module-info.class")) {
 127                         result = JarType.ModularJar;
 128                         break;
 129                     }
 130                 }
 131 
 132                 zip.close();
 133             } catch (IOException ex) {
 134             }
 135         } catch (FileNotFoundException e) {
 136         }
 137 
 138         return result;
 139     }
 140 
 141     private static String getFileWithoutExtension(String FileName) {
 142         return FileName.replaceFirst("[.][^.]+$", "");
 143     }
 144 }