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 return getFileWithoutExtension(file.getName()); 55 } 56 57 public String getFileName() { 58 return filename; 59 } 60 61 public ModuleType getModuleType() { 62 return moduleType; 63 } 64 65 private static ModuleType getModuleType(File AFile) { 66 ModuleType result = ModuleType.Unknown; 67 String filename = AFile.getAbsolutePath(); 68 69 if (AFile.isFile()) { 70 if (filename.endsWith(".jmod")) { 71 result = ModuleType.Jmod; 72 } 73 else if (filename.endsWith(".jar")) { 74 JarType status = isModularJar(filename); 75 76 if (status == JarType.ModularJar) { 77 result = ModuleType.ModularJar; 78 } 79 else if (status == JarType.UnnamedJar) { 80 result = ModuleType.UnnamedJar; 81 } 82 } 83 } 84 else if (AFile.isDirectory()) { 85 File moduleInfo = new File(filename + File.separator + "module-info.class"); 86 87 if (moduleInfo.exists()) { 88 result = ModuleType.ExplodedModule; 89 } 90 } 91 92 return result; 93 } 94 95 private static JarType isModularJar(String FileName) { 96 JarType result = JarType.All; 97 List<String> classNames = new ArrayList<String>(); 98 99 try { 100 ZipInputStream zip = new ZipInputStream(new FileInputStream(FileName)); 101 result = JarType.UnnamedJar; 102 103 try { 104 for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) { 105 if (entry.getName().matches("module-info.class")) { 106 result = JarType.ModularJar; 107 break; 108 } 109 } 110 111 zip.close(); 112 } catch (IOException ex) { 113 } 114 } catch (FileNotFoundException e) { 115 } 116 117 return result; 118 } 119 120 private static String getFileWithoutExtension(String FileName) { 121 return FileName.replaceFirst("[.][^.]+$", ""); 122 } 123 }