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            public static boolean isSupportsHotDeploy() {
130                    return getInstance()._supportsHotDeploy;
131            }
132    
133            public static boolean isTomcat() {
134                    return getInstance()._tomcat;
135            }
136    
137            public static boolean isWebLogic() {
138                    return getInstance()._webLogic;
139            }
140    
141            public static boolean isWebSphere() {
142                    return getInstance()._webSphere;
143            }
144    
145            public static boolean isWildfly() {
146                    return getInstance()._wildfly;
147            }
148    
149            public static void setSupportsHotDeploy(boolean supportsHotDeploy) {
150                    getInstance()._supportsHotDeploy = supportsHotDeploy;
151    
152                    if (_log.isInfoEnabled()) {
153                            if (supportsHotDeploy) {
154                                    _log.info("Server supports hot deploy");
155                            }
156                            else {
157                                    _log.info("Server does not support hot deploy");
158                            }
159                    }
160            }
161    
162            private boolean _detect(String className) {
163                    try {
164                            ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
165    
166                            systemClassLoader.loadClass(className);
167    
168                            return true;
169                    }
170                    catch (ClassNotFoundException cnfe) {
171                            Class<?> clazz = getClass();
172    
173                            if (clazz.getResource(className) != null) {
174                                    return true;
175                            }
176                            else {
177                                    return false;
178                            }
179                    }
180            }
181    
182            private boolean _hasSystemProperty(String key) {
183                    String value = System.getProperty(key);
184    
185                    if (value != null) {
186                            return true;
187                    }
188                    else {
189                            return false;
190                    }
191            }
192    
193            private void _init() {
194                    if (_isGlassfish()) {
195                            _serverId = GLASSFISH_ID;
196                            _glassfish = true;
197                    }
198                    else if (_isJBoss()) {
199                            _serverId = JBOSS_ID;
200                            _jBoss = true;
201                    }
202                    else if (_isJOnAS()) {
203                            _serverId = JONAS_ID;
204                            _jonas = true;
205                    }
206                    else if (_isOC4J()) {
207                            _serverId = OC4J_ID;
208                            _oc4j = true;
209                    }
210                    else if (_isResin()) {
211                            _serverId = RESIN_ID;
212                            _resin = true;
213                    }
214                    else if (_isWebLogic()) {
215                            _serverId = WEBLOGIC_ID;
216                            _webLogic = true;
217                    }
218                    else if (_isWebSphere()) {
219                            _serverId = WEBSPHERE_ID;
220                            _webSphere = true;
221                    }
222                    else if (_isWildfly()) {
223                            _serverId = WILDFLY_ID;
224                            _wildfly = true;
225                    }
226    
227                    if (_serverId == null) {
228                            if (_isJetty()) {
229                                    _serverId = JETTY_ID;
230                                    _jetty = true;
231                            }
232                            else if (_isTomcat()) {
233                                    _serverId = TOMCAT_ID;
234                                    _tomcat = true;
235                            }
236                    }
237    
238                    if (System.getProperty("external-properties") == null) {
239                            if (_log.isInfoEnabled()) {
240                                    if (_serverId != null) {
241                                            _log.info("Detected server " + _serverId);
242                                    }
243                                    else {
244                                            _log.info("No server detected");
245                                    }
246                            }
247                    }
248    
249                    /*if (_serverId == null) {
250                            throw new RuntimeException("Server is not supported");
251                    }*/
252            }
253    
254            private boolean _isGlassfish() {
255                    return _hasSystemProperty("com.sun.aas.instanceRoot");
256            }
257    
258            private boolean _isJBoss() {
259                    return _hasSystemProperty("jboss.home.dir");
260            }
261    
262            private boolean _isJetty() {
263                    return _hasSystemProperty("jetty.home");
264            }
265    
266            private boolean _isJOnAS() {
267                    return _hasSystemProperty("jonas.base");
268            }
269    
270            private boolean _isOC4J() {
271                    return _detect("oracle.oc4j.util.ClassUtils");
272            }
273    
274            private boolean _isResin() {
275                    return _hasSystemProperty("resin.home");
276            }
277    
278            private boolean _isTomcat() {
279                    return _hasSystemProperty("catalina.base");
280            }
281    
282            private boolean _isWebLogic() {
283                    return _detect("/weblogic/Server.class");
284            }
285    
286            private boolean _isWebSphere() {
287                    return _detect("/com/ibm/websphere/product/VersionInfo.class");
288            }
289    
290            private boolean _isWildfly() {
291                    return _hasSystemProperty("jboss.home.dir");
292            }
293    
294            private static final boolean _SUPPORTS_COMET = false;
295    
296            private static final Log _log = LogFactoryUtil.getLog(ServerDetector.class);
297    
298            private static ServerDetector _instance;
299    
300            private boolean _glassfish;
301            private boolean _jBoss;
302            private boolean _jetty;
303            private boolean _jonas;
304            private boolean _oc4j;
305            private boolean _resin;
306            private String _serverId;
307            private boolean _supportsHotDeploy;
308            private boolean _tomcat;
309            private boolean _webLogic;
310            private boolean _webSphere;
311            private boolean _wildfly;
312    
313    }