001    /**
002     * Copyright (c) 2000-2010 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 GERONIMO_ID = "geronimo";
026    
027            public static final String GLASSFISH_ID = "glassfish";
028    
029            public static final String JBOSS_ID = "jboss";
030    
031            public static final String JETTY_ID = "jetty";
032    
033            public static final String JONAS_ID = "jonas";
034    
035            public static final String OC4J_ID = "oc4j";
036    
037            public static final String RESIN_ID = "resin";
038    
039            public static final String TOMCAT_ID = "tomcat";
040    
041            public static final String WEBLOGIC_ID = "weblogic";
042    
043            public static final String WEBSPHERE_ID = "websphere";
044    
045            public static String getServerId() {
046                    if (_serverId == null) {
047                            if (isGeronimo()) {
048                                    _serverId = GERONIMO_ID;
049                            }
050                            else if (isGlassfish()) {
051                                    _serverId = GLASSFISH_ID;
052                            }
053                            else if (isJBoss()) {
054                                    _serverId = JBOSS_ID;
055                            }
056                            else if (isJOnAS()) {
057                                    _serverId = JONAS_ID;
058                            }
059                            else if (isOC4J()) {
060                                    _serverId = OC4J_ID;
061                            }
062                            else if (isResin()) {
063                                    _serverId = RESIN_ID;
064                            }
065                            else if (isWebLogic()) {
066                                    _serverId = WEBLOGIC_ID;
067                            }
068                            else if (isWebSphere()) {
069                                    _serverId = WEBSPHERE_ID;
070                            }
071    
072                            if (isJetty()) {
073                                    if (_serverId == null) {
074                                            _serverId = JETTY_ID;
075                                    }
076                            }
077                            else if (isTomcat()) {
078                                    if (_serverId == null) {
079                                            _serverId = TOMCAT_ID;
080                                    }
081                            }
082    
083                            if (_log.isInfoEnabled()) {
084                                    if (_serverId != null) {
085                                            _log.info("Detected server " + _serverId);
086                                    }
087                                    else {
088                                            _log.info("No server detected");
089                                    }
090                            }
091    
092                            if (_serverId == null) {
093                                    throw new RuntimeException("Server is not supported");
094                            }
095                    }
096    
097                    return _serverId;
098            }
099    
100            public static boolean isGeronimo() {
101                    if (_geronimo == null) {
102                            _geronimo = _detect(
103                                    "/org/apache/geronimo/system/main/Daemon.class");
104                    }
105    
106                    return _geronimo.booleanValue();
107            }
108    
109            public static boolean isGlassfish() {
110                    if (_glassfish == null) {
111                            String value = System.getProperty("com.sun.aas.instanceRoot");
112    
113                            if (value != null) {
114                                    _glassfish = Boolean.TRUE;
115                            }
116                            else {
117                                    _glassfish = Boolean.FALSE;
118                            }
119                    }
120    
121                    return _glassfish.booleanValue();
122            }
123    
124            public static boolean isJBoss() {
125                    if (_jBoss == null) {
126                            _jBoss = _detect("/org/jboss/Main.class");
127                    }
128    
129                    return _jBoss.booleanValue();
130            }
131    
132            public static boolean isJetty() {
133                    if (_jetty == null) {
134                            _jetty = _detect("/org/mortbay/jetty/Server.class");
135                    }
136    
137                    return _jetty.booleanValue();
138            }
139    
140            public static boolean isJOnAS() {
141                    if (_jonas == null) {
142                            _jonas = _detect("/org/objectweb/jonas/server/Server.class");
143    
144                            if (!_jonas && (System.getProperty("jonas.root") != null)) {
145                                    _jonas = Boolean.TRUE;
146                            }
147                    }
148    
149                    return _jonas.booleanValue();
150            }
151    
152            public static boolean isOC4J() {
153                    if (_oc4j == null) {
154                            _oc4j = _detect("oracle.oc4j.util.ClassUtils");
155                    }
156    
157                    return _oc4j.booleanValue();
158            }
159    
160            public static boolean isResin() {
161                    if (_resin == null) {
162                            _resin = _detect("/com/caucho/server/resin/Resin.class");
163                    }
164    
165                    return _resin.booleanValue();
166            }
167    
168            public static boolean isSupportsComet() {
169                    return false;
170            }
171    
172            public static boolean isTomcat() {
173                    if (_tomcat == null) {
174                            _tomcat = _detect("/org/apache/catalina/startup/Bootstrap.class");
175                    }
176    
177                    if (_tomcat == null) {
178                            _tomcat = _detect("/org/apache/catalina/startup/Embedded.class");
179                    }
180    
181                    return _tomcat.booleanValue();
182            }
183    
184            public static boolean isWebLogic() {
185                    if (_webLogic == null) {
186                            _webLogic = _detect("/weblogic/Server.class");
187                    }
188    
189                    return _webLogic.booleanValue();
190            }
191    
192            public static boolean isWebSphere() {
193                    if (_webSphere == null) {
194                            _webSphere = _detect(
195                                    "/com/ibm/websphere/product/VersionInfo.class");
196                    }
197    
198                    return _webSphere.booleanValue();
199            }
200    
201            private static Boolean _detect(String className) {
202                    try {
203                            ClassLoader.getSystemClassLoader().loadClass(className);
204    
205                            return Boolean.TRUE;
206                    }
207                    catch (ClassNotFoundException cnfe) {
208                            Class<?> classObj = _instance.getClass();
209    
210                            if (classObj.getResource(className) != null) {
211                                    return Boolean.TRUE;
212                            }
213                            else {
214                                    return Boolean.FALSE;
215                            }
216                    }
217            }
218    
219            private ServerDetector() {
220            }
221    
222            private static Log _log = LogFactoryUtil.getLog(ServerDetector.class);
223    
224            private static ServerDetector _instance = new ServerDetector();
225    
226            private static String _serverId;
227            private static Boolean _geronimo;
228            private static Boolean _glassfish;
229            private static Boolean _jBoss;
230            private static Boolean _jetty;
231            private static Boolean _jonas;
232            private static Boolean _oc4j;
233            private static Boolean _resin;
234            private static Boolean _tomcat;
235            private static Boolean _webLogic;
236            private static Boolean _webSphere;
237    
238    }