001    /**
002     * Copyright (c) 2000-present 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            public static boolean isOracle() {
115                    return _instance._oracle;
116            }
117    
118            protected JavaDetector() {
119                    _javaClassPath = System.getProperty("java.class.path");
120                    _javaClassVersion = GetterUtil.getDouble(
121                            System.getProperty("java.class.version"));
122                    _javaRuntimeName = System.getProperty("java.runtime.name");
123                    _javaRuntimeVersion = System.getProperty("java.runtime.version");
124                    _javaSpecificationVersion = GetterUtil.getDouble(
125                            System.getProperty("java.specification.version"));
126                    _javaVendor = System.getProperty("java.vendor");
127                    _javaVersion = System.getProperty("java.version");
128                    _javaVmVersion = System.getProperty("java.vm.version");
129    
130                    _64bit = Validator.equals(
131                            "64", System.getProperty("sun.arch.data.model"));
132    
133                    boolean oracle = false;
134    
135                    if (_javaVendor != null) {
136                            _ibm = _javaVendor.startsWith("IBM");
137    
138                            if (_javaVendor.startsWith("Oracle") ||
139                                    _javaVendor.startsWith("Sun")) {
140    
141                                    oracle = true;
142                            }
143                    }
144                    else {
145                            _ibm = false;
146                    }
147    
148                    _oracle = oracle;
149    
150                    if (_javaRuntimeName != null) {
151                            _openJDK = _javaRuntimeName.contains("OpenJDK");
152                    }
153                    else {
154                            _openJDK = false;
155                    }
156    
157                    if (_log.isDebugEnabled()) {
158                            LogUtil.debug(_log, new SortedProperties(System.getProperties()));
159                    }
160            }
161    
162            private static final String _JAVA_VERSION_JDK_4 = "1.4.";
163    
164            private static final String _JAVA_VERSION_JDK_5 = "1.5.";
165    
166            private static final String _JAVA_VERSION_JDK_6 = "1.6.";
167    
168            private static final String _JAVA_VERSION_JDK_7 = "1.7.";
169    
170            private static final Log _log = LogFactoryUtil.getLog(JavaDetector.class);
171    
172            private static final JavaDetector _instance = new JavaDetector();
173    
174            private final boolean _64bit;
175            private final boolean _ibm;
176            private final String _javaClassPath;
177            private final double _javaClassVersion;
178            private final String _javaRuntimeName;
179            private final String _javaRuntimeVersion;
180            private final double _javaSpecificationVersion;
181            private final String _javaVendor;
182            private final String _javaVersion;
183            private final String _javaVmVersion;
184            private final boolean _openJDK;
185            private final boolean _oracle;
186    
187    }