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     */
019    package 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     * @author Rod Johnson
034     * @author Juergen Hoeller
035     * @author Rick Evans
036     * @author Les Hazlewood
037     * @since 0.2
038     */
039    public abstract class JavaEnvironment {
040    
041        /**
042         * Constant identifying the 1.3.x JVM (JDK 1.3).
043         */
044        public static final int JAVA_13 = 0;
045    
046        /**
047         * Constant identifying the 1.4.x JVM (J2SE 1.4).
048         */
049        public static final int JAVA_14 = 1;
050    
051        /**
052         * Constant identifying the 1.5 JVM (Java 5).
053         */
054        public static final int JAVA_15 = 2;
055    
056        /**
057         * Constant identifying the 1.6 JVM (Java 6).
058         */
059        public static final int JAVA_16 = 3;
060    
061        /**
062         * Constant identifying the 1.7 JVM.
063         */
064        public static final int JAVA_17 = 4;
065    
066        /** The virtual machine version, i.e. <code>System.getProperty("java.version");</code>. */
067        private static final String version;
068    
069        /**
070         * The virtual machine <em>major</em> version.  For example, with a <code>version</code> of
071         * <code>1.5.6_10</code>, this would be <code>1.5</code>
072         */
073        private static final int majorVersion;
074    
075        /**
076         * Static code initialization block that sets the
077         * <code>version</code> and <code>majorVersion</code> Class constants
078         * upon initialization.
079         */
080        static {
081            version = System.getProperty("java.version");
082            // version String should look like "1.4.2_10"
083            if (version.indexOf("1.7.") != -1) {
084                majorVersion = JAVA_17;
085            } else if (version.indexOf("1.6.") != -1) {
086                majorVersion = JAVA_16;
087            } else if (version.indexOf("1.5.") != -1) {
088                majorVersion = JAVA_15;
089            } else if (version.indexOf("1.4.") != -1) {
090                majorVersion = JAVA_14;
091            } else {
092                // else leave 1.3 as default (it's either 1.3 or unknown)
093                majorVersion = JAVA_13;
094            }
095        }
096    
097    
098        /**
099         * Return the full Java version string, as returned by
100         * <code>System.getProperty("java.version")</code>.
101         *
102         * @return the full Java version string
103         * @see System#getProperty(String)
104         */
105        public static String getVersion() {
106            return version;
107        }
108    
109        /**
110         * Get the major version code. This means we can do things like
111         * <code>if (getMajorVersion() < JAVA_14)</code>.
112         *
113         * @return a code comparable to the JAVA_XX codes in this class
114         * @see #JAVA_13
115         * @see #JAVA_14
116         * @see #JAVA_15
117         * @see #JAVA_16
118         * @see #JAVA_17
119         */
120        public static int getMajorVersion() {
121            return majorVersion;
122        }
123    
124        /**
125         * Convenience method to determine if the current JVM is at least Java 1.4.
126         *
127         * @return <code>true</code> if the current JVM is at least Java 1.4
128         * @see #getMajorVersion()
129         * @see #JAVA_14
130         * @see #JAVA_15
131         * @see #JAVA_16
132         * @see #JAVA_17
133         */
134        public static boolean isAtLeastVersion14() {
135            return getMajorVersion() >= JAVA_14;
136        }
137    
138        /**
139         * Convenience method to determine if the current JVM is at least
140         * Java 1.5 (Java 5).
141         *
142         * @return <code>true</code> if the current JVM is at least Java 1.5
143         * @see #getMajorVersion()
144         * @see #JAVA_15
145         * @see #JAVA_16
146         * @see #JAVA_17
147         */
148        public static boolean isAtLeastVersion15() {
149            return getMajorVersion() >= JAVA_15;
150        }
151    }