001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                    return _instance._serverId;
047            }
048    
049            public static boolean isGeronimo() {
050                    return _instance._geronimo;
051            }
052    
053            public static boolean isGlassfish() {
054                    return _instance._glassfish;
055            }
056    
057            public static boolean isJBoss() {
058                    return _instance._jBoss;
059            }
060    
061            public static boolean isJetty() {
062                    return _instance._jetty;
063            }
064    
065            public static boolean isJOnAS() {
066                    return _instance._jonas;
067            }
068    
069            public static boolean isOC4J() {
070                    return _instance._oc4j;
071            }
072    
073            public static boolean isResin() {
074                    return _instance._resin;
075            }
076    
077            public static boolean isSupportsComet() {
078                    return _instance._supportsComet;
079            }
080    
081            public static boolean isTomcat() {
082                    return _instance._tomcat;
083            }
084    
085            public static boolean isWebLogic() {
086                    return _instance._webLogic;
087            }
088    
089            public static boolean isWebSphere() {
090                    return _instance._webSphere;
091            }
092    
093            private ServerDetector() {
094                    if (_isGeronimo()) {
095                            _serverId = GERONIMO_ID;
096                            _geronimo = true;
097                    }
098                    else if (_isGlassfish()) {
099                            _serverId = GLASSFISH_ID;
100                            _glassfish = true;
101                    }
102                    else if (_isJBoss()) {
103                            _serverId = JBOSS_ID;
104                            _jBoss = true;
105                    }
106                    else if (_isJOnAS()) {
107                            _serverId = JONAS_ID;
108                            _jonas = true;
109                    }
110                    else if (_isOC4J()) {
111                            _serverId = OC4J_ID;
112                            _oc4j = true;
113                    }
114                    else if (_isResin()) {
115                            _serverId = RESIN_ID;
116                            _resin = true;
117                    }
118                    else if (_isWebLogic()) {
119                            _serverId = WEBLOGIC_ID;
120                            _webLogic = true;
121                    }
122                    else if (_isWebSphere()) {
123                            _serverId = WEBSPHERE_ID;
124                            _webSphere = true;
125                    }
126    
127                    if (_isJetty()) {
128                            if (_serverId == null) {
129                                    _serverId = JETTY_ID;
130                                    _jetty = true;
131                            }
132                    }
133                    else if (_isTomcat()) {
134                            if (_serverId == null) {
135                                    _serverId = TOMCAT_ID;
136                                    _tomcat = true;
137                            }
138                    }
139    
140                    if (System.getProperty("external-properties") == null) {
141                            if (_log.isInfoEnabled()) {
142                                    if (_serverId != null) {
143                                            _log.info("Detected server " + _serverId);
144                                    }
145                                    else {
146                                            _log.info("No server detected");
147                                    }
148                            }
149                    }
150    
151                    /*if (_serverId == null) {
152                            throw new RuntimeException("Server is not supported");
153                    }*/
154            }
155    
156            private boolean _detect(String className) {
157                    try {
158                            ClassLoader systemClassLoader =
159                                    ClassLoader.getSystemClassLoader();
160    
161                            systemClassLoader.loadClass(className);
162    
163                            return true;
164                    }
165                    catch (ClassNotFoundException cnfe) {
166                            Class<?> classObj = getClass();
167    
168                            if (classObj.getResource(className) != null) {
169                                    return true;
170                            }
171                            else {
172                                    return false;
173                            }
174                    }
175            }
176    
177            private boolean _isGeronimo() {
178                    return _detect(
179                            "/org/apache/geronimo/system/main/Daemon.class");
180            }
181    
182            private boolean _isGlassfish() {
183                    String value = System.getProperty("com.sun.aas.instanceRoot");
184    
185                    if (value != null) {
186                            return true;
187                    }
188                    else {
189                            return false;
190                    }
191            }
192    
193            private boolean _isJBoss() {
194                    return _detect("/org/jboss/Main.class");
195            }
196    
197            private boolean _isJetty() {
198                    return _detect("/org/mortbay/jetty/Server.class");
199            }
200    
201            private boolean _isJOnAS() {
202                    boolean jonas = _detect("/org/objectweb/jonas/server/Server.class");
203    
204                    if (!_jonas && (System.getProperty("jonas.root") != null)) {
205                            jonas = true;
206                    }
207    
208                    return jonas;
209            }
210    
211            private boolean _isOC4J() {
212                    return _detect("oracle.oc4j.util.ClassUtils");
213            }
214    
215            private boolean _isResin() {
216                    return _detect("/com/caucho/server/resin/Resin.class");
217            }
218    
219            private boolean _isTomcat() {
220                    boolean tomcat = _detect(
221                            "/org/apache/catalina/startup/Bootstrap.class");
222    
223                    if (!tomcat) {
224                            tomcat = _detect("/org/apache/catalina/startup/Embedded.class");
225                    }
226    
227                    return tomcat;
228            }
229    
230            private boolean _isWebLogic() {
231                    return _detect("/weblogic/Server.class");
232            }
233    
234            private boolean _isWebSphere() {
235                    return _detect(
236                            "/com/ibm/websphere/product/VersionInfo.class");
237            }
238    
239            private static Log _log = LogFactoryUtil.getLog(ServerDetector.class);
240    
241            private static ServerDetector _instance = new ServerDetector();
242    
243            private String _serverId;
244            private boolean _geronimo;
245            private boolean _glassfish;
246            private boolean _jBoss;
247            private boolean _jetty;
248            private boolean _jonas;
249            private boolean _oc4j;
250            private boolean _resin;
251            private boolean _supportsComet;
252            private boolean _tomcat;
253            private boolean _webLogic;
254            private boolean _webSphere;
255    
256    }