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    import java.util.Objects;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class JavaDetector {
027    
028            public static String getJavaClassPath() {
029                    return _instance._javaClassPath;
030            }
031    
032            public static double getJavaClassVersion() {
033                    return _instance._javaClassVersion;
034            }
035    
036            public static String getJavaRuntimeName() {
037                    return _instance._javaRuntimeName;
038            }
039    
040            public static String getJavaRuntimeVersion() {
041                    return _instance._javaRuntimeVersion;
042            }
043    
044            public static double getJavaSpecificationVersion() {
045                    return _instance._javaSpecificationVersion;
046            }
047    
048            public static String getJavaVendor() {
049                    return _instance._javaVendor;
050            }
051    
052            public static String getJavaVersion() {
053                    return _instance._javaVersion;
054            }
055    
056            public static String getJavaVmVersion() {
057                    return _instance._javaVmVersion;
058            }
059    
060            public static boolean is64bit() {
061                    return _instance._64bit;
062            }
063    
064            public static boolean isIBM() {
065                    return _instance._ibm;
066            }
067    
068            public static boolean isJDK7() {
069                    String javaVersion = getJavaVersion();
070    
071                    if (javaVersion.startsWith(_JAVA_VERSION_JDK_7)) {
072                            return true;
073                    }
074                    else {
075                            return false;
076                    }
077            }
078    
079            public static boolean isJDK8() {
080                    String javaVersion = getJavaVersion();
081    
082                    if (javaVersion.startsWith(_JAVA_VERSION_JDK_8)) {
083                            return true;
084                    }
085                    else {
086                            return false;
087                    }
088            }
089    
090            public static boolean isOpenJDK() {
091                    return _instance._openJDK;
092            }
093    
094            public static boolean isOracle() {
095                    return _instance._oracle;
096            }
097    
098            protected JavaDetector() {
099                    _javaClassPath = System.getProperty("java.class.path");
100                    _javaClassVersion = GetterUtil.getDouble(
101                            System.getProperty("java.class.version"));
102                    _javaRuntimeName = System.getProperty("java.runtime.name");
103                    _javaRuntimeVersion = System.getProperty("java.runtime.version");
104                    _javaSpecificationVersion = GetterUtil.getDouble(
105                            System.getProperty("java.specification.version"));
106                    _javaVendor = System.getProperty("java.vendor");
107                    _javaVersion = System.getProperty("java.version");
108                    _javaVmVersion = System.getProperty("java.vm.version");
109    
110                    _64bit = Objects.equals(
111                            "64", System.getProperty("sun.arch.data.model"));
112    
113                    boolean oracle = false;
114    
115                    if (_javaVendor != null) {
116                            _ibm = _javaVendor.startsWith("IBM");
117    
118                            if (_javaVendor.startsWith("Oracle") ||
119                                    _javaVendor.startsWith("Sun")) {
120    
121                                    oracle = true;
122                            }
123                    }
124                    else {
125                            _ibm = false;
126                    }
127    
128                    _oracle = oracle;
129    
130                    if (_javaRuntimeName != null) {
131                            _openJDK = _javaRuntimeName.contains("OpenJDK");
132                    }
133                    else {
134                            _openJDK = false;
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_7 = "1.7.";
143    
144            private static final String _JAVA_VERSION_JDK_8 = "1.8.";
145    
146            private static final Log _log = LogFactoryUtil.getLog(JavaDetector.class);
147    
148            private static final JavaDetector _instance = new JavaDetector();
149    
150            private final boolean _64bit;
151            private final boolean _ibm;
152            private final String _javaClassPath;
153            private final double _javaClassVersion;
154            private final String _javaRuntimeName;
155            private final String _javaRuntimeVersion;
156            private final double _javaSpecificationVersion;
157            private final String _javaVendor;
158            private final String _javaVersion;
159            private final String _javaVmVersion;
160            private final boolean _openJDK;
161            private final boolean _oracle;
162    
163    }