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