001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.shiro.util;
020
021/**
022 * Internal helper class used to find the Java/JDK version
023 * that Shiro is operating within, to allow for automatically
024 * adapting to the present platform's capabilities.
025 *
026 * <p>Note that Shiro does not support 1.2 or earlier JVMs - only 1.3 and later.
027 *
028 * <p><em>This class was borrowed and heavily based upon a nearly identical version found in
029 * the <a href="http://www.springframework.org/">Spring Framework</a>, with minor modifications.
030 * The original author names and copyright (Apache 2.0) has been left in place.  A special
031 * thanks to Rod Johnson, Juergen Hoeller, and Rick Evans for making this available.</em>
032 *
033 * @since 0.2
034 * @deprecated This class is no longer used in Shiro and will be removed in the next major version.
035 */
036@Deprecated
037public abstract class JavaEnvironment {
038
039    /**
040     * Constant identifying the 1.3.x JVM (JDK 1.3).
041     */
042    public static final int JAVA_13 = 0;
043
044    /**
045     * Constant identifying the 1.4.x JVM (J2SE 1.4).
046     */
047    public static final int JAVA_14 = 1;
048
049    /**
050     * Constant identifying the 1.5 JVM (Java 5).
051     */
052    public static final int JAVA_15 = 2;
053
054    /**
055     * Constant identifying the 1.6 JVM (Java 6).
056     */
057    public static final int JAVA_16 = 3;
058
059    /**
060     * Constant identifying the 1.7 JVM.
061     */
062    public static final int JAVA_17 = 4;
063
064    /**
065     * Constant identifying the 1.8 JVM.
066     */
067    public static final int JAVA_18 = 5;
068
069    /**
070     * The virtual machine version, i.e. <code>System.getProperty("java.version");</code>.
071     */
072    private static final String VERSION;
073
074    /**
075     * The virtual machine <em>major</em> version.  For example, with a <code>version</code> of
076     * <code>1.5.6_10</code>, this would be <code>1.5</code>
077     */
078    private static final int MAJOR_VERSION;
079
080    /**
081     * Static code initialization block that sets the
082     * <code>version</code> and <code>majorVersion</code> Class constants
083     * upon initialization.
084     */
085    static {
086        VERSION = System.getProperty("java.version");
087        // version String should look like "1.4.2_10"
088
089// NOTE:   JDK 1.9 will be versioned differently '9' and/or 9.x.x
090// https://blogs.oracle.com/java-platform-group/entry/a_new_jdk_9_version
091
092        if (VERSION.contains("1.8.")) {
093            MAJOR_VERSION = JAVA_18;
094        } else if (VERSION.contains("1.7.")) {
095            MAJOR_VERSION = JAVA_17;
096        } else if (VERSION.contains("1.6.")) {
097            MAJOR_VERSION = JAVA_16;
098        } else if (VERSION.contains("1.5.")) {
099            MAJOR_VERSION = JAVA_15;
100        } else if (VERSION.contains("1.4.")) {
101            MAJOR_VERSION = JAVA_14;
102        } else {
103            // else leave 1.3 as default (it's either 1.3 or unknown)
104            MAJOR_VERSION = JAVA_13;
105        }
106    }
107
108
109    /**
110     * Return the full Java version string, as returned by
111     * <code>System.getProperty("java.version")</code>.
112     *
113     * @return the full Java version string
114     * @see System#getProperty(String)
115     */
116    public static String getVersion() {
117        return VERSION;
118    }
119
120    /**
121     * Get the major version code. This means we can do things like
122     * <code>if (getMajorVersion() < JAVA_14)</code>.
123     *
124     * @return a code comparable to the JAVA_XX codes in this class
125     * @see #JAVA_13
126     * @see #JAVA_14
127     * @see #JAVA_15
128     * @see #JAVA_16
129     * @see #JAVA_17
130     * @see #JAVA_18
131     */
132    public static int getMajorVersion() {
133        return MAJOR_VERSION;
134    }
135
136    /**
137     * Convenience method to determine if the current JVM is at least Java 1.4.
138     *
139     * @return <code>true</code> if the current JVM is at least Java 1.4
140     * @see #getMajorVersion()
141     * @see #JAVA_14
142     * @see #JAVA_15
143     * @see #JAVA_16
144     * @see #JAVA_17
145     * @see #JAVA_18
146     */
147    public static boolean isAtLeastVersion14() {
148        return getMajorVersion() >= JAVA_14;
149    }
150
151    /**
152     * Convenience method to determine if the current JVM is at least
153     * Java 1.5 (Java 5).
154     *
155     * @return <code>true</code> if the current JVM is at least Java 1.5
156     * @see #getMajorVersion()
157     * @see #JAVA_15
158     * @see #JAVA_16
159     * @see #JAVA_17
160     * @see #JAVA_18
161     */
162    public static boolean isAtLeastVersion15() {
163        return getMajorVersion() >= JAVA_15;
164    }
165
166    /**
167     * Convenience method to determine if the current JVM is at least
168     * Java 1.6 (Java 6).
169     *
170     * @return <code>true</code> if the current JVM is at least Java 1.6
171     * @see #getMajorVersion()
172     * @see #JAVA_15
173     * @see #JAVA_16
174     * @see #JAVA_17
175     * @see #JAVA_18
176     * @since 1.2
177     */
178    public static boolean isAtLeastVersion16() {
179        return getMajorVersion() >= JAVA_16;
180    }
181}