001: /**********************************************************************
002: Copyright (c) 2005 Andy Jefferson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015:
016: Contributors:
017: ...
018: **********************************************************************/package org.jpox.util;
019:
020: import java.util.StringTokenizer;
021:
022: /**
023: * Utilities relating to the version of Java in use at runtime.
024: * @version $Revision: 1.8 $
025: */
026: public class JavaUtils {
027: private static boolean versionInitialised = false;
028: private static int majorVersion = 1;
029: private static int minorVersion = 0;
030: private static int isJRE14 = -1;
031: private static int isJRE15 = -1;
032: private static int isJRE16 = -1;
033:
034: /**
035: * Accessor for whether the JRE is 1.4 (or above).
036: * Checks for the presence of a known 1.4 class.
037: * @return Whether the JRE is 1.4 or above
038: */
039: public static boolean isJRE1_4OrAbove() {
040: if (isJRE14 == -1) {
041: try {
042: Class.forName("java.util.Currency");
043: isJRE14 = 1;
044: } catch (Exception e) {
045: isJRE14 = 0;
046: }
047: }
048: return isJRE14 == 1;
049: }
050:
051: /**
052: * Accessor for whether the JRE is 1.5 (or above).
053: * Checks for the presence of a known 1.5 class.
054: * @return Whether the JRE is 1.5 or above
055: */
056: public static boolean isJRE1_5OrAbove() {
057: if (isJRE15 == -1) {
058: try {
059: Class.forName("java.util.Queue");
060: isJRE15 = 1;
061: } catch (Exception e) {
062: isJRE15 = 0;
063: }
064: }
065: return isJRE15 == 1;
066: }
067:
068: /**
069: * Accessor for whether the JRE is 1.6 (or above).
070: * Checks for the presence of a known 1.6 class.
071: * @return Whether the JRE is 1.6 or above
072: */
073: public static boolean isJRE1_6OrAbove() {
074: if (isJRE16 == -1) {
075: try {
076: Class.forName("java.util.Deque");
077: isJRE16 = 1;
078: } catch (Exception e) {
079: isJRE16 = 0;
080: }
081: }
082: return isJRE16 == 1;
083: }
084:
085: /**
086: * Accessor for the major version number of the JRE.
087: * @return The major version number of the JRE
088: */
089: public static int getJREMajorVersion() {
090: if (!versionInitialised) {
091: initialiseJREVersion();
092: }
093: return majorVersion;
094: }
095:
096: /**
097: * Accessor for the minor version number of the JRE.
098: * @return The minor version number of the JRE
099: */
100: public static int getJREMinorVersion() {
101: if (!versionInitialised) {
102: initialiseJREVersion();
103: }
104: return minorVersion;
105: }
106:
107: /**
108: * Utility to initialise the values of the JRE major/minor version.
109: * Assumes the "java.version" string is in the form "XX.YY.ZZ".
110: * Works for SUN JRE's.
111: */
112: private static void initialiseJREVersion() {
113: String version = System.getProperty("java.version");
114:
115: // Assume that the version string is of the form XX.YY.ZZ (works for SUN JREs)
116: StringTokenizer tokeniser = new StringTokenizer(version, ".");
117: String token = tokeniser.nextToken();
118: try {
119: Integer ver = new Integer(token);
120: majorVersion = ver.intValue();
121:
122: token = tokeniser.nextToken();
123: ver = new Integer(token);
124: minorVersion = ver.intValue();
125: } catch (Exception e) {
126: // Do nothing
127: }
128: versionInitialised = true;
129: }
130:
131: /**
132: * Check if the current version is greater or equals than the argument version.
133: * @param version the version
134: * @return true if the runtime version is greater equals than the argument
135: */
136: public static boolean isGreaterEqualsThan(String version) {
137: boolean greaterEquals = false;
138: StringTokenizer tokeniser = new StringTokenizer(version, ".");
139: String token = tokeniser.nextToken();
140: try {
141: Integer ver = new Integer(token);
142: int majorVersion = ver.intValue();
143:
144: token = tokeniser.nextToken();
145: ver = new Integer(token);
146: int minorVersion = ver.intValue();
147: if (getJREMajorVersion() >= majorVersion
148: && getJREMinorVersion() >= minorVersion) {
149: greaterEquals = true;
150: }
151: } catch (Exception e) {
152: // Do nothing
153: }
154:
155: return greaterEquals;
156: }
157:
158: /**
159: * Check if the current version is equals than the argument version.
160: * @param version the version
161: * @return true if the runtime version is equals than the argument
162: */
163: public static boolean isEqualsThan(String version) {
164: boolean equals = false;
165: StringTokenizer tokeniser = new StringTokenizer(version, ".");
166: String token = tokeniser.nextToken();
167: try {
168: Integer ver = new Integer(token);
169: int majorVersion = ver.intValue();
170:
171: token = tokeniser.nextToken();
172: ver = new Integer(token);
173: int minorVersion = ver.intValue();
174: if (getJREMajorVersion() == majorVersion
175: && getJREMinorVersion() == minorVersion) {
176: equals = true;
177: }
178: } catch (Exception e) {
179: // Do nothing
180: }
181:
182: return equals;
183: }
184: }
|