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