001    /**
002     * Copyright (c) 2000-2011 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 (_serverId == null) {
128                            if (_isJetty()) {
129                                    _serverId = JETTY_ID;
130                                    _jetty = true;
131                            }
132                            else if (_isTomcat()) {
133                                    _serverId = TOMCAT_ID;
134                                    _tomcat = true;
135                            }
136                    }
137    
138                    if (System.getProperty("external-properties") == null) {
139                            if (_log.isInfoEnabled()) {
140                                    if (_serverId != null) {
141                                            _log.info("Detected server " + _serverId);
142                                    }
143                                    else {
144                                            _log.info("No server detected");
145                                    }
146                            }
147                    }
148    
149                    /*if (_serverId == null) {
150                            throw new RuntimeException("Server is not supported");
151                    }*/
152            }
153    
154            private boolean _detect(String className) {
155                    try {
156                            ClassLoader systemClassLoader =
157                                    ClassLoader.getSystemClassLoader();
158    
159                            systemClassLoader.loadClass(className);
160    
161                            return true;
162                    }
163                    catch (ClassNotFoundException cnfe) {
164                            Class<?> clazz = getClass();
165    
166                            if (clazz.getResource(className) != null) {
167                                    return true;
168                            }
169                            else {
170                                    return false;
171                            }
172                    }
173            }
174    
175            private boolean _hasSystemProperty(String key) {
176                    String value = System.getProperty(key);
177    
178                    if (value != null) {
179                            return true;
180                    }
181                    else {
182                            return false;
183                    }
184            }
185    
186            private boolean _isGeronimo() {
187                    return _hasSystemProperty("org.apache.geronimo.home.dir");
188            }
189    
190            private boolean _isGlassfish() {
191                    return _hasSystemProperty("com.sun.aas.instanceRoot");
192            }
193    
194            private boolean _isJBoss() {
195                    return _hasSystemProperty("jboss.home.dir");
196            }
197    
198            private boolean _isJetty() {
199                    return _hasSystemProperty("jetty.home");
200            }
201    
202            private boolean _isJOnAS() {
203                    return _hasSystemProperty("jonas.base");
204            }
205    
206            private boolean _isOC4J() {
207                    return _detect("oracle.oc4j.util.ClassUtils");
208            }
209    
210            private boolean _isResin() {
211                    return _hasSystemProperty("resin.home");
212            }
213    
214            private boolean _isTomcat() {
215                    return _hasSystemProperty("catalina.base");
216            }
217    
218            private boolean _isWebLogic() {
219                    return _detect("/weblogic/Server.class");
220            }
221    
222            private boolean _isWebSphere() {
223                    return _detect(
224                            "/com/ibm/websphere/product/VersionInfo.class");
225            }
226    
227            private static Log _log = LogFactoryUtil.getLog(ServerDetector.class);
228    
229            private static ServerDetector _instance = new ServerDetector();
230    
231            private String _serverId;
232            private boolean _geronimo;
233            private boolean _glassfish;
234            private boolean _jBoss;
235            private boolean _jetty;
236            private boolean _jonas;
237            private boolean _oc4j;
238            private boolean _resin;
239            private boolean _supportsComet;
240            private boolean _tomcat;
241            private boolean _webLogic;
242            private boolean _webSphere;
243    
244    }