|
1 /* |
|
2 * Copyright (c) 2016, 2018, 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; |
|
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 |
|
38 public final class Module { |
|
39 private final String filename; |
|
40 private final ModuleType moduleType; |
|
41 |
|
42 public enum JarType {All, UnnamedJar, ModularJar} |
|
43 public enum ModuleType {Unknown, UnnamedJar, ModularJar, Jmod, ExplodedModule} |
|
44 |
|
45 public Module(File AFile) { |
|
46 super(); |
|
47 filename = AFile.getPath(); |
|
48 moduleType = getModuleType(AFile); |
|
49 } |
|
50 |
|
51 public String getModuleName() { |
|
52 File file = new File(getFileName()); |
|
53 // do not try to remove extension for directories |
|
54 return moduleType == ModuleType.ExplodedModule ? |
|
55 file.getName() : getFileWithoutExtension(file.getName()); |
|
56 } |
|
57 |
|
58 public String getFileName() { |
|
59 return filename; |
|
60 } |
|
61 |
|
62 public ModuleType getModuleType() { |
|
63 return moduleType; |
|
64 } |
|
65 |
|
66 private static ModuleType getModuleType(File AFile) { |
|
67 ModuleType result = ModuleType.Unknown; |
|
68 String filename = AFile.getAbsolutePath(); |
|
69 |
|
70 if (AFile.isFile()) { |
|
71 if (filename.endsWith(".jmod")) { |
|
72 result = ModuleType.Jmod; |
|
73 } |
|
74 else if (filename.endsWith(".jar")) { |
|
75 JarType status = isModularJar(filename); |
|
76 |
|
77 if (status == JarType.ModularJar) { |
|
78 result = ModuleType.ModularJar; |
|
79 } |
|
80 else if (status == JarType.UnnamedJar) { |
|
81 result = ModuleType.UnnamedJar; |
|
82 } |
|
83 } |
|
84 } |
|
85 else if (AFile.isDirectory()) { |
|
86 File moduleInfo = new File(filename + File.separator + "module-info.class"); |
|
87 |
|
88 if (moduleInfo.exists()) { |
|
89 result = ModuleType.ExplodedModule; |
|
90 } |
|
91 } |
|
92 |
|
93 return result; |
|
94 } |
|
95 |
|
96 private static JarType isModularJar(String FileName) { |
|
97 JarType result = JarType.All; |
|
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 } |