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