View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.jar.classes;
20  
21  import java.util.Map.Entry;
22  import java.util.NavigableMap;
23  import java.util.Objects;
24  import java.util.Set;
25  
26  /**
27   * Gathered facts about the runtime versions contained within a Multi-Release JAR file.
28   *
29   * @see org.apache.maven.shared.jar.classes.JarClassesAnalysis#analyze(org.apache.maven.shared.jar.JarAnalyzer)
30   */
31  public class JarVersionedRuntimes {
32  
33      /**
34       * Information about the JAR's Multi-Release entries
35       */
36      private NavigableMap<Integer, JarVersionedRuntime> versionedRuntimeMap;
37  
38      public JarVersionedRuntimes(NavigableMap<Integer, JarVersionedRuntime> versionedRuntimeMap) {
39          this.versionedRuntimeMap = versionedRuntimeMap;
40      }
41  
42      /**
43       * @return the versionedRuntimeMap
44       */
45      public NavigableMap<Integer, JarVersionedRuntime> getVersionedRuntimeMap() {
46          return versionedRuntimeMap;
47      }
48  
49      public JarVersionedRuntime getJarVersionedRuntime(Integer version) {
50          return versionedRuntimeMap.get(version);
51      }
52  
53      /**
54       * Return the JarClasses associated to the release.
55       * @param version the release version.
56       * @return the JarClasses.
57       */
58      public JarClasses getJarClasses(Integer version) {
59          return versionedRuntimeMap.get(version).getJarClasses();
60      }
61  
62      /**
63       * Get a set of release versions included in the JAR file.
64       * @return a set with the Java versions as Strings.
65       */
66      public Set<Integer> getRuntimeVersionsAsSet() {
67          return versionedRuntimeMap.keySet();
68      }
69  
70      /**
71       * Return the highest the JarClasses of the Jdk version that would be executed if they would be executed on a JVM given by the release parameter.
72       * @param version the Jdk version number of the executing JVM.
73       * @return The fittest JarClasses object matching if found one, or null otherwise.
74       * @throws NullPointerException if release is null.
75       */
76      public JarVersionedRuntime getBestFitJarVersionedRuntime(Integer version) {
77          Objects.requireNonNull(version, "version cannot be null");
78          Entry<Integer, JarVersionedRuntime> entry = versionedRuntimeMap.floorEntry(version);
79          if (entry == null) {
80              return null;
81          }
82          return entry.getValue();
83      }
84  
85      /**
86       * Return the highest the JarClasses of the Jdk version that would be executed if they would be executed given a System property.
87       * Example values: "java.version.specification" or "java.vm.specification.version".
88       * @param key the System property.
89       * @return The best fitting JarClasses object matching if found one, or null otherwise.
90       * @throws NullPointerException if key is null
91       * @throws IllegalStateException if system property value of key is null
92       * @throws IllegalStateException if system property cannot be converted to Integer
93       */
94      public JarVersionedRuntime getBestFitJarVersionedRuntimeBySystemProperty(String key) {
95          Objects.requireNonNull(key, "key cannot null");
96          String property = System.getProperty(key);
97          if (property == null) {
98              throw new IllegalStateException("The value of the system property '" + key + "' is null");
99          }
100         try {
101             Integer release = Integer.parseInt(property);
102             return getBestFitJarVersionedRuntime(release);
103         } catch (NumberFormatException e) {
104             throw new IllegalStateException(
105                     "The value of the system property '" + key + "' [" + property
106                             + "] cannot be converted to an Integer",
107                     e);
108         }
109     }
110 }