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 */
035public abstract class JavaEnvironment {
036
037    /**
038     * Constant identifying the 1.3.x JVM (JDK 1.3).
039     */
040    public static final int JAVA_13 = 0;
041
042    /**
043     * Constant identifying the 1.4.x JVM (J2SE 1.4).
044     */
045    public static final int JAVA_14 = 1;
046
047    /**
048     * Constant identifying the 1.5 JVM (Java 5).
049     */
050    public static final int JAVA_15 = 2;
051
052    /**
053     * Constant identifying the 1.6 JVM (Java 6).
054     */
055    public static final int JAVA_16 = 3;
056
057    /**
058     * Constant identifying the 1.7 JVM.
059     */
060    public static final int JAVA_17 = 4;
061
062    /**
063     * Constant identifying the 1.8 JVM.
064     */
065    public static final int JAVA_18 = 5;
066
067    /** The virtual machine version, i.e. <code>System.getProperty("java.version");</code>. */
068    private static final String version;
069
070    /**
071     * The virtual machine <em>major</em> version.  For example, with a <code>version</code> of
072     * <code>1.5.6_10</code>, this would be <code>1.5</code>
073     */
074    private static final int majorVersion;
075
076    /**
077     * Static code initialization block that sets the
078     * <code>version</code> and <code>majorVersion</code> Class constants
079     * upon initialization.
080     */
081    static {
082        version = System.getProperty("java.version");
083        // version String should look like "1.4.2_10"
084
085// NOTE:   JDK 1.9 will be versioned differently '9' and/or 9.x.x
086// https://blogs.oracle.com/java-platform-group/entry/a_new_jdk_9_version
087
088        if (version.contains("1.8.")) {
089            majorVersion = JAVA_18;
090        } else if (version.contains("1.7.")) {
091            majorVersion = JAVA_17;
092        } else if (version.contains("1.6.")) {
093            majorVersion = JAVA_16;
094        } else if (version.contains("1.5.")) {
095            majorVersion = JAVA_15;
096        } else if (version.contains("1.4.")) {
097            majorVersion = JAVA_14;
098        } else {
099            // 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}