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    
020    /**
021     * @author Brian Wing Shun Chan
022     */
023    public class ServerDetector {
024    
025            public static final String GLASSFISH_ID = "glassfish";
026    
027            public static final String JBOSS_ID = "jboss";
028    
029            public static final String JETTY_ID = "jetty";
030    
031            public static final String JONAS_ID = "jonas";
032    
033            public static final String OC4J_ID = "oc4j";
034    
035            public static final String RESIN_ID = "resin";
036    
037            public static final String TOMCAT_ID = "tomcat";
038    
039            public static final String WEBLOGIC_ID = "weblogic";
040    
041            public static final String WEBSPHERE_ID = "websphere";
042    
043            public static final String WILDFLY_ID = "wildfly";
044    
045            public static ServerDetector getInstance() {
046                    if (_instance == null) {
047                            _instance = new ServerDetector();
048    
049                            _instance._init();
050                    }
051    
052                    return _instance;
053            }
054    
055            public static String getServerId() {
056                    return getInstance()._serverId;
057            }
058    
059            public static void init(String serverId) {
060                    ServerDetector serverDetector = new ServerDetector();
061    
062                    serverDetector._serverId = serverId;
063    
064                    if (serverId.equals(GLASSFISH_ID)) {
065                            serverDetector._glassfish = true;
066                    }
067                    else if (serverId.equals(JBOSS_ID)) {
068                            serverDetector._jBoss = true;
069                    }
070                    else if (serverId.equals(JETTY_ID)) {
071                            serverDetector._jetty = true;
072                    }
073                    else if (serverId.equals(JONAS_ID)) {
074                            serverDetector._jonas = true;
075                    }
076                    else if (serverId.equals(OC4J_ID)) {
077                            serverDetector._oc4j = true;
078                    }
079                    else if (serverId.equals(RESIN_ID)) {
080                            serverDetector._resin = true;
081                    }
082                    else if (serverId.equals(TOMCAT_ID)) {
083                            serverDetector._tomcat = true;
084                    }
085                    else if (serverId.equals(WEBLOGIC_ID)) {
086                            serverDetector._webLogic = true;
087                    }
088                    else if (serverId.equals(WEBSPHERE_ID)) {
089                            serverDetector._webSphere = true;
090                    }
091                    else if (serverId.equals(WILDFLY_ID)) {
092                            serverDetector._wildfly = true;
093                    }
094                    else {
095                            serverDetector._init();
096                    }
097    
098                    _instance = serverDetector;
099            }
100    
101            public static boolean isGlassfish() {
102                    return getInstance()._glassfish;
103            }
104    
105            public static boolean isJBoss() {
106                    return getInstance()._jBoss;
107            }
108    
109            public static boolean isJetty() {
110                    return getInstance()._jetty;
111            }
112    
113            public static boolean isJOnAS() {
114                    return getInstance()._jonas;
115            }
116    
117            public static boolean isOC4J() {
118                    return getInstance()._oc4j;
119            }
120    
121            public static boolean isResin() {
122                    return getInstance()._resin;
123            }
124    
125            public static boolean isSupportsComet() {
126                    return _SUPPORTS_COMET;
127            }
128    
129            /**
130             * @deprecated As of 7.0.0, with no direct replacement
131             */
132            @Deprecated
133            public static boolean isSupportsHotDeploy() {
134                    return true;
135            }
136    
137            public static boolean isTomcat() {
138                    return getInstance()._tomcat;
139            }
140    
141            public static boolean isWebLogic() {
142                    return getInstance()._webLogic;
143            }
144    
145            public static boolean isWebSphere() {
146                    return getInstance()._webSphere;
147            }
148    
149            public static boolean isWildfly() {
150                    return getInstance()._wildfly;
151            }
152    
153            /**
154             * @deprecated As of 7.3.0, with no direct replacement
155             */
156            @Deprecated
157            public static void setSupportsHotDeploy(boolean supportsHotDeploy) {
158            }
159    
160            private boolean _detect(String className) {
161                    try {
162                            ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
163    
164                            systemClassLoader.loadClass(className);
165    
166                            return true;
167                    }
168                    catch (ClassNotFoundException cnfe) {
169                            Class<?> clazz = getClass();
170    
171                            if (clazz.getResource(className) != null) {
172                                    return true;
173                            }
174                            else {
175                                    return false;
176                            }
177                    }
178            }
179    
180            private boolean _hasSystemProperty(String key) {
181                    String value = System.getProperty(key);
182    
183                    if (value != null) {
184                            return true;
185                    }
186                    else {
187                            return false;
188                    }
189            }
190    
191            private void _init() {
192                    if (_isGlassfish()) {
193                            _serverId = GLASSFISH_ID;
194                            _glassfish = true;
195                    }
196                    else if (_isJBoss()) {
197                            _serverId = JBOSS_ID;
198                            _jBoss = true;
199                    }
200                    else if (_isJOnAS()) {
201                            _serverId = JONAS_ID;
202                            _jonas = true;
203                    }
204                    else if (_isOC4J()) {
205                            _serverId = OC4J_ID;
206                            _oc4j = true;
207                    }
208                    else if (_isResin()) {
209                            _serverId = RESIN_ID;
210                            _resin = true;
211                    }
212                    else if (_isWebLogic()) {
213                            _serverId = WEBLOGIC_ID;
214                            _webLogic = true;
215                    }
216                    else if (_isWebSphere()) {
217                            _serverId = WEBSPHERE_ID;
218                            _webSphere = true;
219                    }
220                    else if (_isWildfly()) {
221                            _serverId = WILDFLY_ID;
222                            _wildfly = true;
223                    }
224    
225                    if (_serverId == null) {
226                            if (_isJetty()) {
227                                    _serverId = JETTY_ID;
228                                    _jetty = true;
229                            }
230                            else if (_isTomcat()) {
231                                    _serverId = TOMCAT_ID;
232                                    _tomcat = true;
233                            }
234                    }
235    
236                    if (System.getProperty("external-properties") == null) {
237                            if (_log.isInfoEnabled()) {
238                                    if (_serverId != null) {
239                                            _log.info("Detected server " + _serverId);
240                                    }
241                                    else {
242                                            _log.info("No server detected");
243                                    }
244                            }
245                    }
246    
247                    /*if (_serverId == null) {
248                            throw new RuntimeException("Server is not supported");
249                    }*/
250            }
251    
252            private boolean _isGlassfish() {
253                    return _hasSystemProperty("com.sun.aas.instanceRoot");
254            }
255    
256            private boolean _isJBoss() {
257                    return _hasSystemProperty("jboss.home.dir");
258            }
259    
260            private boolean _isJetty() {
261                    return _hasSystemProperty("jetty.home");
262            }
263    
264            private boolean _isJOnAS() {
265                    return _hasSystemProperty("jonas.base");
266            }
267    
268            private boolean _isOC4J() {
269                    return _detect("oracle.oc4j.util.ClassUtils");
270            }
271    
272            private boolean _isResin() {
273                    return _hasSystemProperty("resin.home");
274            }
275    
276            private boolean _isTomcat() {
277                    return _hasSystemProperty("catalina.base");
278            }
279    
280            private boolean _isWebLogic() {
281                    return _detect("/weblogic/Server.class");
282            }
283    
284            private boolean _isWebSphere() {
285                    return _detect("/com/ibm/websphere/product/VersionInfo.class");
286            }
287    
288            private boolean _isWildfly() {
289                    return _hasSystemProperty("jboss.home.dir");
290            }
291    
292            private static final boolean _SUPPORTS_COMET = false;
293    
294            private static final Log _log = LogFactoryUtil.getLog(ServerDetector.class);
295    
296            private static ServerDetector _instance;
297    
298            private boolean _glassfish;
299            private boolean _jBoss;
300            private boolean _jetty;
301            private boolean _jonas;
302            private boolean _oc4j;
303            private boolean _resin;
304            private String _serverId;
305            private boolean _tomcat;
306            private boolean _webLogic;
307            private boolean _webSphere;
308            private boolean _wildfly;
309    
310    }