View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.rng.examples.sampling;
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   * {@link IVersionProvider} implementation that returns version information from
29   * the package jar file's {@code /META-INF/MANIFEST.MF} file.
30   *
31   * @see <a
32   * href="https://github.com/remkop/picocli/blob/master/picocli-examples/src/main/java/picocli/examples/VersionProviderDemo2.java">PicoCLI
33   * version provider demo</a>
34   */
35  class ManifestVersionProvider implements IVersionProvider {
36      /** {@inheritDoc} */
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       * Checks if this is the applicable manifest for the package.
59       *
60       * @param manifest The manifest.
61       * @return true if is the applicable manifest
62       */
63      private static boolean isApplicableManifest(Manifest manifest) {
64          final Attributes attributes = manifest.getMainAttributes();
65          return "org.apache.commons.rng.examples.sampling".equals(get(attributes, "Automatic-Module-Name"));
66      }
67  
68      /**
69       * Gets the named object from the attributes using the key.
70       *
71       * @param attributes The attributes.
72       * @param key The key.
73       * @return the object
74       */
75      private static Object get(Attributes attributes, String key) {
76          return attributes.get(new Attributes.Name(key));
77      }
78  }