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