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.shiro.util;
20  
21  /**
22   * Internal helper class used to find the Java/JDK version
23   * that Shiro is operating within, to allow for automatically
24   * adapting to the present platform's capabilities.
25   *
26   * <p>Note that Shiro does not support 1.2 or earlier JVMs - only 1.3 and later.
27   *
28   * <p><em>This class was borrowed and heavily based upon a nearly identical version found in
29   * the <a href="http://www.springframework.org/">Spring Framework</a>, with minor modifications.
30   * The original author names and copyright (Apache 2.0) has been left in place.  A special
31   * thanks to Rod Johnson, Juergen Hoeller, and Rick Evans for making this available.</em>
32   *
33   * @since 0.2
34   */
35  public abstract class JavaEnvironment {
36  
37      /**
38       * Constant identifying the 1.3.x JVM (JDK 1.3).
39       */
40      public static final int JAVA_13 = 0;
41  
42      /**
43       * Constant identifying the 1.4.x JVM (J2SE 1.4).
44       */
45      public static final int JAVA_14 = 1;
46  
47      /**
48       * Constant identifying the 1.5 JVM (Java 5).
49       */
50      public static final int JAVA_15 = 2;
51  
52      /**
53       * Constant identifying the 1.6 JVM (Java 6).
54       */
55      public static final int JAVA_16 = 3;
56  
57      /**
58       * Constant identifying the 1.7 JVM.
59       */
60      public static final int JAVA_17 = 4;
61  
62      /**
63       * Constant identifying the 1.8 JVM.
64       */
65      public static final int JAVA_18 = 5;
66  
67      /** The virtual machine version, i.e. <code>System.getProperty("java.version");</code>. */
68      private static final String version;
69  
70      /**
71       * The virtual machine <em>major</em> version.  For example, with a <code>version</code> of
72       * <code>1.5.6_10</code>, this would be <code>1.5</code>
73       */
74      private static final int majorVersion;
75  
76      /**
77       * Static code initialization block that sets the
78       * <code>version</code> and <code>majorVersion</code> Class constants
79       * upon initialization.
80       */
81      static {
82          version = System.getProperty("java.version");
83          // version String should look like "1.4.2_10"
84  
85  // NOTE:   JDK 1.9 will be versioned differently '9' and/or 9.x.x
86  // https://blogs.oracle.com/java-platform-group/entry/a_new_jdk_9_version
87  
88          if (version.contains("1.8.")) {
89              majorVersion = JAVA_18;
90          } else if (version.contains("1.7.")) {
91              majorVersion = JAVA_17;
92          } else if (version.contains("1.6.")) {
93              majorVersion = JAVA_16;
94          } else if (version.contains("1.5.")) {
95              majorVersion = JAVA_15;
96          } else if (version.contains("1.4.")) {
97              majorVersion = JAVA_14;
98          } else {
99              // else leave 1.3 as default (it's either 1.3 or unknown)
100             majorVersion = JAVA_13;
101         }
102     }
103 
104 
105     /**
106      * Return the full Java version string, as returned by
107      * <code>System.getProperty("java.version")</code>.
108      *
109      * @return the full Java version string
110      * @see System#getProperty(String)
111      */
112     public static String getVersion() {
113         return version;
114     }
115 
116     /**
117      * Get the major version code. This means we can do things like
118      * <code>if (getMajorVersion() < JAVA_14)</code>.
119      *
120      * @return a code comparable to the JAVA_XX codes in this class
121      * @see #JAVA_13
122      * @see #JAVA_14
123      * @see #JAVA_15
124      * @see #JAVA_16
125      * @see #JAVA_17
126      * @see #JAVA_18
127      */
128     public static int getMajorVersion() {
129         return majorVersion;
130     }
131 
132     /**
133      * Convenience method to determine if the current JVM is at least Java 1.4.
134      *
135      * @return <code>true</code> if the current JVM is at least Java 1.4
136      * @see #getMajorVersion()
137      * @see #JAVA_14
138      * @see #JAVA_15
139      * @see #JAVA_16
140      * @see #JAVA_17
141      * @see #JAVA_18
142      */
143     public static boolean isAtLeastVersion14() {
144         return getMajorVersion() >= JAVA_14;
145     }
146 
147     /**
148      * Convenience method to determine if the current JVM is at least
149      * Java 1.5 (Java 5).
150      *
151      * @return <code>true</code> if the current JVM is at least Java 1.5
152      * @see #getMajorVersion()
153      * @see #JAVA_15
154      * @see #JAVA_16
155      * @see #JAVA_17
156      * @see #JAVA_18
157      */
158     public static boolean isAtLeastVersion15() {
159         return getMajorVersion() >= JAVA_15;
160     }
161 
162     /**
163      * Convenience method to determine if the current JVM is at least
164      * Java 1.6 (Java 6).
165      *
166      * @return <code>true</code> if the current JVM is at least Java 1.6
167      * @see #getMajorVersion()
168      * @see #JAVA_15
169      * @see #JAVA_16
170      * @see #JAVA_17
171      * @see #JAVA_18
172      *
173      * @since 1.2
174      */
175     public static boolean isAtLeastVersion16() {
176         return getMajorVersion() >= JAVA_16;
177     }
178 }