001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.core;
018:
019: /**
020: * Internal helper class used to find the Java/JDK version
021: * that Spring is operating on, to allow for automatically
022: * adapting to the present platform's capabilities.
023: *
024: * <p>Note that Spring requires JVM 1.4 or higher, as of Spring 2.5.
025: *
026: * @author Rod Johnson
027: * @author Juergen Hoeller
028: * @author Rick Evans
029: */
030: public abstract class JdkVersion {
031:
032: /**
033: * Constant identifying the 1.3.x JVM (JDK 1.3).
034: */
035: public static final int JAVA_13 = 0;
036:
037: /**
038: * Constant identifying the 1.4.x JVM (J2SE 1.4).
039: */
040: public static final int JAVA_14 = 1;
041:
042: /**
043: * Constant identifying the 1.5 JVM (Java 5).
044: */
045: public static final int JAVA_15 = 2;
046:
047: /**
048: * Constant identifying the 1.6 JVM (Java 6).
049: */
050: public static final int JAVA_16 = 3;
051:
052: /**
053: * Constant identifying the 1.7 JVM (Java 7).
054: */
055: public static final int JAVA_17 = 4;
056:
057: private static final String javaVersion;
058:
059: private static final int majorJavaVersion;
060:
061: static {
062: javaVersion = System.getProperty("java.version");
063: // version String should look like "1.4.2_10"
064: if (javaVersion.indexOf("1.7.") != -1) {
065: majorJavaVersion = JAVA_17;
066: } else if (javaVersion.indexOf("1.6.") != -1) {
067: majorJavaVersion = JAVA_16;
068: } else if (javaVersion.indexOf("1.5.") != -1) {
069: majorJavaVersion = JAVA_15;
070: } else {
071: // else leave 1.4 as default (it's either 1.4 or unknown)
072: majorJavaVersion = JAVA_14;
073: }
074: }
075:
076: /**
077: * Return the full Java version string, as returned by
078: * <code>System.getProperty("java.version")</code>.
079: * @return the full Java version string
080: * @see System#getProperty(String)
081: */
082: public static String getJavaVersion() {
083: return javaVersion;
084: }
085:
086: /**
087: * Get the major version code. This means we can do things like
088: * <code>if (getMajorJavaVersion() < JAVA_14)</code>.
089: * @return a code comparable to the JAVA_XX codes in this class
090: * @see #JAVA_13
091: * @see #JAVA_14
092: * @see #JAVA_15
093: * @see #JAVA_16
094: * @see #JAVA_17
095: */
096: public static int getMajorJavaVersion() {
097: return majorJavaVersion;
098: }
099:
100: /**
101: * Convenience method to determine if the current JVM is at least Java 1.4.
102: * @return <code>true</code> if the current JVM is at least Java 1.4
103: * @see #getMajorJavaVersion()
104: * @see #JAVA_14
105: * @see #JAVA_15
106: * @see #JAVA_16
107: * @see #JAVA_17
108: */
109: public static boolean isAtLeastJava14() {
110: return true;
111: }
112:
113: /**
114: * Convenience method to determine if the current JVM is at least
115: * Java 1.5 (Java 5).
116: * @return <code>true</code> if the current JVM is at least Java 1.5
117: * @see #getMajorJavaVersion()
118: * @see #JAVA_15
119: * @see #JAVA_16
120: * @see #JAVA_17
121: */
122: public static boolean isAtLeastJava15() {
123: return getMajorJavaVersion() >= JAVA_15;
124: }
125:
126: /**
127: * Convenience method to determine if the current JVM is at least
128: * Java 1.6 (Java 6).
129: * @return <code>true</code> if the current JVM is at least Java 1.6
130: * @see #getMajorJavaVersion()
131: * @see #JAVA_16
132: * @see #JAVA_17
133: */
134: public static boolean isAtLeastJava16() {
135: return getMajorJavaVersion() >= JAVA_16;
136: }
137:
138: }
|