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