001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.log.LogUtil;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     */
024    public class JavaDetector {
025    
026            public static String getJavaClassPath() {
027                    return _instance._javaClassPath;
028            }
029    
030            public static double getJavaClassVersion() {
031                    return _instance._javaClassVersion;
032            }
033    
034            public static String getJavaRuntimeName() {
035                    return _instance._javaRuntimeName;
036            }
037    
038            public static String getJavaRuntimeVersion() {
039                    return _instance._javaRuntimeVersion;
040            }
041    
042            public static double getJavaSpecificationVersion() {
043                    return _instance._javaSpecificationVersion;
044            }
045    
046            public static String getJavaVendor() {
047                    return _instance._javaVendor;
048            }
049    
050            public static String getJavaVersion() {
051                    return _instance._javaVersion;
052            }
053    
054            public static String getJavaVmVersion() {
055                    return _instance._javaVmVersion;
056            }
057    
058            public static boolean is64bit() {
059                    return _instance._64bit;
060            }
061    
062            public static boolean isIBM() {
063                    return _instance._ibm;
064            }
065    
066            public static boolean isJDK4() {
067                    String javaVersion = getJavaVersion();
068    
069                    if (javaVersion.startsWith(_JAVA_VERSION_JDK_4)) {
070                            return true;
071                    }
072                    else {
073                            return false;
074                    }
075            }
076    
077            public static boolean isJDK5() {
078                    String javaVersion = getJavaVersion();
079    
080                    if (javaVersion.startsWith(_JAVA_VERSION_JDK_5)) {
081                            return true;
082                    }
083                    else {
084                            return false;
085                    }
086            }
087    
088            public static boolean isJDK6() {
089                    String javaVersion = getJavaVersion();
090    
091                    if (javaVersion.startsWith(_JAVA_VERSION_JDK_6)) {
092                            return true;
093                    }
094                    else {
095                            return false;
096                    }
097            }
098    
099            public static boolean isJDK7() {
100                    String javaVersion = getJavaVersion();
101    
102                    if (javaVersion.startsWith(_JAVA_VERSION_JDK_7)) {
103                            return true;
104                    }
105                    else {
106                            return false;
107                    }
108            }
109    
110            public static boolean isOpenJDK() {
111                    return _instance._openJDK;
112            }
113    
114            protected JavaDetector() {
115                    _javaClassPath = System.getProperty("java.class.path");
116                    _javaClassVersion = GetterUtil.getDouble(
117                            System.getProperty("java.class.version"));
118                    _javaRuntimeName = System.getProperty("java.runtime.name");
119                    _javaRuntimeVersion = System.getProperty("java.runtime.version");
120                    _javaSpecificationVersion = GetterUtil.getDouble(
121                            System.getProperty("java.specification.version"));
122                    _javaVendor = System.getProperty("java.vendor");
123                    _javaVersion = System.getProperty("java.version");
124                    _javaVmVersion = System.getProperty("java.vm.version");
125    
126                    _64bit = Validator.equals(
127                            "64", System.getProperty("sun.arch.data.model"));
128    
129                    if (_javaVendor != null) {
130                            _ibm = _javaVendor.startsWith("IBM");
131                    }
132    
133                    if (_javaRuntimeName != null) {
134                            _openJDK = _javaRuntimeName.contains("OpenJDK");
135                    }
136    
137                    if (_log.isDebugEnabled()) {
138                            LogUtil.debug(_log, new SortedProperties(System.getProperties()));
139                    }
140            }
141    
142            private static final String _JAVA_VERSION_JDK_4 = "1.4.";
143    
144            private static final String _JAVA_VERSION_JDK_5 = "1.5.";
145    
146            private static final String _JAVA_VERSION_JDK_6 = "1.6.";
147    
148            private static final String _JAVA_VERSION_JDK_7 = "1.7.";
149    
150            private static Log _log = LogFactoryUtil.getLog(JavaDetector.class);
151    
152            private static JavaDetector _instance = new JavaDetector();
153    
154            private boolean _64bit;
155            private boolean _ibm;
156            private String _javaClassPath;
157            private double _javaClassVersion;
158            private String _javaRuntimeName;
159            private String _javaRuntimeVersion;
160            private double _javaSpecificationVersion;
161            private String _javaVendor;
162            private String _javaVersion;
163            private String _javaVmVersion;
164            private boolean _openJDK;
165    
166    }