1 package org.codehaus.plexus.languages.java.jpms;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.nio.file.Path;
27 import java.util.jar.JarFile;
28 import java.util.jar.Manifest;
29
30
31
32
33
34
35
36 class ManifestModuleNameExtractor implements ModuleNameExtractor {
37 @Override
38 public String extract(Path file) throws IOException {
39 Manifest manifest = extractManifest(file.toFile());
40
41 String automaticModuleName;
42 if (manifest != null) {
43 automaticModuleName = manifest.getMainAttributes().getValue("Automatic-Module-Name");
44 } else {
45 automaticModuleName = null;
46 }
47
48 return automaticModuleName;
49 }
50
51 private Manifest extractManifest(File file) throws IOException {
52 Manifest manifest;
53 if (file.isFile()) {
54 try (JarFile jarFile = new JarFile(file)) {
55 manifest = jarFile.getManifest();
56 }
57 } else if (new File(file, "META-INF/MANIFEST.MF").exists()) {
58 try (InputStream is = new FileInputStream(new File(file, "META-INF/MANIFEST.MF"))) {
59 manifest = new Manifest(is);
60 }
61 } else {
62 manifest = null;
63 }
64
65 return manifest;
66 }
67 }