Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
JavaEnvironment |
|
| 1.0;1 |
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 | 0 | 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 | /** The virtual machine version, i.e. <code>System.getProperty("java.version");</code>. */ | |
63 | private static final String version; | |
64 | ||
65 | /** | |
66 | * The virtual machine <em>major</em> version. For example, with a <code>version</code> of | |
67 | * <code>1.5.6_10</code>, this would be <code>1.5</code> | |
68 | */ | |
69 | private static final int majorVersion; | |
70 | ||
71 | /** | |
72 | * Static code initialization block that sets the | |
73 | * <code>version</code> and <code>majorVersion</code> Class constants | |
74 | * upon initialization. | |
75 | */ | |
76 | static { | |
77 | 0 | version = System.getProperty("java.version"); |
78 | // version String should look like "1.4.2_10" | |
79 | 0 | if (version.indexOf("1.7.") != -1) { |
80 | 0 | majorVersion = JAVA_17; |
81 | 0 | } else if (version.indexOf("1.6.") != -1) { |
82 | 0 | majorVersion = JAVA_16; |
83 | 0 | } else if (version.indexOf("1.5.") != -1) { |
84 | 0 | majorVersion = JAVA_15; |
85 | 0 | } else if (version.indexOf("1.4.") != -1) { |
86 | 0 | majorVersion = JAVA_14; |
87 | } else { | |
88 | // else leave 1.3 as default (it's either 1.3 or unknown) | |
89 | 0 | majorVersion = JAVA_13; |
90 | } | |
91 | 0 | } |
92 | ||
93 | ||
94 | /** | |
95 | * Return the full Java version string, as returned by | |
96 | * <code>System.getProperty("java.version")</code>. | |
97 | * | |
98 | * @return the full Java version string | |
99 | * @see System#getProperty(String) | |
100 | */ | |
101 | public static String getVersion() { | |
102 | 0 | return version; |
103 | } | |
104 | ||
105 | /** | |
106 | * Get the major version code. This means we can do things like | |
107 | * <code>if (getMajorVersion() < JAVA_14)</code>. | |
108 | * | |
109 | * @return a code comparable to the JAVA_XX codes in this class | |
110 | * @see #JAVA_13 | |
111 | * @see #JAVA_14 | |
112 | * @see #JAVA_15 | |
113 | * @see #JAVA_16 | |
114 | * @see #JAVA_17 | |
115 | */ | |
116 | public static int getMajorVersion() { | |
117 | 0 | return majorVersion; |
118 | } | |
119 | ||
120 | /** | |
121 | * Convenience method to determine if the current JVM is at least Java 1.4. | |
122 | * | |
123 | * @return <code>true</code> if the current JVM is at least Java 1.4 | |
124 | * @see #getMajorVersion() | |
125 | * @see #JAVA_14 | |
126 | * @see #JAVA_15 | |
127 | * @see #JAVA_16 | |
128 | * @see #JAVA_17 | |
129 | */ | |
130 | public static boolean isAtLeastVersion14() { | |
131 | 0 | return getMajorVersion() >= JAVA_14; |
132 | } | |
133 | ||
134 | /** | |
135 | * Convenience method to determine if the current JVM is at least | |
136 | * Java 1.5 (Java 5). | |
137 | * | |
138 | * @return <code>true</code> if the current JVM is at least Java 1.5 | |
139 | * @see #getMajorVersion() | |
140 | * @see #JAVA_15 | |
141 | * @see #JAVA_16 | |
142 | * @see #JAVA_17 | |
143 | */ | |
144 | public static boolean isAtLeastVersion15() { | |
145 | 0 | return getMajorVersion() >= JAVA_15; |
146 | } | |
147 | ||
148 | /** | |
149 | * Convenience method to determine if the current JVM is at least | |
150 | * Java 1.6 (Java 6). | |
151 | * | |
152 | * @return <code>true</code> if the current JVM is at least Java 1.6 | |
153 | * @see #getMajorVersion() | |
154 | * @see #JAVA_15 | |
155 | * @see #JAVA_16 | |
156 | * @see #JAVA_17 | |
157 | * | |
158 | * @since 1.2 | |
159 | */ | |
160 | public static boolean isAtLeastVersion16() { | |
161 | 0 | return getMajorVersion() >= JAVA_16; |
162 | } | |
163 | } |