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.IOException;
23 import java.io.InputStream;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.util.jar.JarFile;
27 import java.util.jar.Manifest;
28
29
30
31
32
33
34
35 class ManifestModuleNameExtractor implements ModuleNameExtractor {
36 @Override
37 public String extract(Path file) throws IOException {
38 Manifest manifest = extractManifest(file);
39
40 String automaticModuleName;
41 if (manifest != null) {
42 automaticModuleName = manifest.getMainAttributes().getValue("Automatic-Module-Name");
43 } else {
44 automaticModuleName = null;
45 }
46
47 return automaticModuleName;
48 }
49
50 private Manifest extractManifest(Path file) throws IOException {
51 Manifest manifest;
52 if (Files.isRegularFile(file)) {
53 try (JarFile jarFile = new JarFile(file.toFile())) {
54 manifest = jarFile.getManifest();
55 }
56 } else if (Files.exists(file.resolve("META-INF/MANIFEST.MF"))) {
57 try (InputStream is = Files.newInputStream(file.resolve("META-INF/MANIFEST.MF"))) {
58 manifest = new Manifest(is);
59 }
60 } else {
61 manifest = null;
62 }
63
64 return manifest;
65 }
66 }