1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.rng.examples.stress;
18
19 import picocli.CommandLine.IVersionProvider;
20
21 import java.io.IOException;
22 import java.net.URL;
23 import java.util.Enumeration;
24 import java.util.jar.Attributes;
25 import java.util.jar.Manifest;
26
27
28
29
30
31
32
33
34
35 class ManifestVersionProvider implements IVersionProvider {
36
37 @Override
38 public String[] getVersion() throws Exception {
39 final Enumeration<URL> resources = Thread.currentThread().getContextClassLoader()
40 .getResources("META-INF/MANIFEST.MF");
41 while (resources.hasMoreElements()) {
42 final URL url = resources.nextElement();
43 try {
44 final Manifest manifest = new Manifest(url.openStream());
45 if (isApplicableManifest(manifest)) {
46 final Attributes attr = manifest.getMainAttributes();
47 return new String[] {get(attr, "Implementation-Title") + " version \"" +
48 get(attr, "Implementation-Version") + "\""};
49 }
50 } catch (final IOException ex) {
51 return new String[] {"Unable to read from " + url + ". " + ex};
52 }
53 }
54 return new String[0];
55 }
56
57
58
59
60
61
62
63 private static boolean isApplicableManifest(Manifest manifest) {
64 final Attributes attributes = manifest.getMainAttributes();
65 return "org.apache.commons.rng.examples.stress".equals(get(attributes, "Automatic-Module-Name"));
66 }
67
68
69
70
71
72
73
74
75 private static Object get(Attributes attributes, String key) {
76 return attributes.get(new Attributes.Name(key));
77 }
78 }