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    /** The virtual machine version, i.e. <code>System.getProperty("java.version");</code>. */
063    private static final String version;
064
065    /**
066     * The virtual machine <em>major</em> version.  For example, with a <code>version</code> of
067     * <code>1.5.6_10</code>, this would be <code>1.5</code>
068     */
069    private static final int majorVersion;
070
071    /**
072     * Static code initialization block that sets the
073     * <code>version</code> and <code>majorVersion</code> Class constants
074     * upon initialization.
075     */
076    static {
077        version = System.getProperty("java.version");
078        // version String should look like "1.4.2_10"
079        if (version.indexOf("1.7.") != -1) {
080            majorVersion = JAVA_17;
081        } else if (version.indexOf("1.6.") != -1) {
082            majorVersion = JAVA_16;
083        } else if (version.indexOf("1.5.") != -1) {
084            majorVersion = JAVA_15;
085        } else if (version.indexOf("1.4.") != -1) {
086            majorVersion = JAVA_14;
087        } else {
088            // else leave 1.3 as default (it's either 1.3 or unknown)
089            majorVersion = JAVA_13;
090        }
091    }
092
093
094    /**
095     * Return the full Java version string, as returned by
096     * <code>System.getProperty("java.version")</code>.
097     *
098     * @return the full Java version string
099     * @see System#getProperty(String)
100     */
101    public static String getVersion() {
102        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        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        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        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        return getMajorVersion() >= JAVA_16;
162    }
163}