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    /** The virtual machine version, i.e. <code>System.getProperty("java.version");</code>. */
070    private static final String version;
071
072    /**
073     * The virtual machine <em>major</em> version.  For example, with a <code>version</code> of
074     * <code>1.5.6_10</code>, this would be <code>1.5</code>
075     */
076    private static final int majorVersion;
077
078    /**
079     * Static code initialization block that sets the
080     * <code>version</code> and <code>majorVersion</code> Class constants
081     * upon initialization.
082     */
083    static {
084        version = System.getProperty("java.version");
085        // version String should look like "1.4.2_10"
086
087// NOTE:   JDK 1.9 will be versioned differently '9' and/or 9.x.x
088// https://blogs.oracle.com/java-platform-group/entry/a_new_jdk_9_version
089
090        if (version.contains("1.8.")) {
091            majorVersion = JAVA_18;
092        } else if (version.contains("1.7.")) {
093            majorVersion = JAVA_17;
094        } else if (version.contains("1.6.")) {
095            majorVersion = JAVA_16;
096        } else if (version.contains("1.5.")) {
097            majorVersion = JAVA_15;
098        } else if (version.contains("1.4.")) {
099            majorVersion = JAVA_14;
100        } else {
101            // else leave 1.3 as default (it's either 1.3 or unknown)
102            majorVersion = JAVA_13;
103        }
104    }
105
106
107    /**
108     * Return the full Java version string, as returned by
109     * <code>System.getProperty("java.version")</code>.
110     *
111     * @return the full Java version string
112     * @see System#getProperty(String)
113     */
114    public static String getVersion() {
115        return version;
116    }
117
118    /**
119     * Get the major version code. This means we can do things like
120     * <code>if (getMajorVersion() < JAVA_14)</code>.
121     *
122     * @return a code comparable to the JAVA_XX codes in this class
123     * @see #JAVA_13
124     * @see #JAVA_14
125     * @see #JAVA_15
126     * @see #JAVA_16
127     * @see #JAVA_17
128     * @see #JAVA_18
129     */
130    public static int getMajorVersion() {
131        return majorVersion;
132    }
133
134    /**
135     * Convenience method to determine if the current JVM is at least Java 1.4.
136     *
137     * @return <code>true</code> if the current JVM is at least Java 1.4
138     * @see #getMajorVersion()
139     * @see #JAVA_14
140     * @see #JAVA_15
141     * @see #JAVA_16
142     * @see #JAVA_17
143     * @see #JAVA_18
144     */
145    public static boolean isAtLeastVersion14() {
146        return getMajorVersion() >= JAVA_14;
147    }
148
149    /**
150     * Convenience method to determine if the current JVM is at least
151     * Java 1.5 (Java 5).
152     *
153     * @return <code>true</code> if the current JVM is at least Java 1.5
154     * @see #getMajorVersion()
155     * @see #JAVA_15
156     * @see #JAVA_16
157     * @see #JAVA_17
158     * @see #JAVA_18
159     */
160    public static boolean isAtLeastVersion15() {
161        return getMajorVersion() >= JAVA_15;
162    }
163
164    /**
165     * Convenience method to determine if the current JVM is at least
166     * Java 1.6 (Java 6).
167     *
168     * @return <code>true</code> if the current JVM is at least Java 1.6
169     * @see #getMajorVersion()
170     * @see #JAVA_15
171     * @see #JAVA_16
172     * @see #JAVA_17
173     * @see #JAVA_18
174     *
175     * @since 1.2
176     */
177    public static boolean isAtLeastVersion16() {
178        return getMajorVersion() >= JAVA_16;
179    }
180}