1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  
20  /**
21   * <a href="ServerDetector.java.html"><b><i>View Source</i></b></a>
22   *
23   * @author Brian Wing Shun Chan
24   */
25  public class ServerDetector {
26  
27      public static final String GERONIMO_ID = "geronimo";
28  
29      public static final String GLASSFISH_ID = "glassfish";
30  
31      public static final String JBOSS_ID = "jboss";
32  
33      public static final String JETTY_ID = "jetty";
34  
35      public static final String JONAS_ID = "jonas";
36  
37      public static final String OC4J_ID = "oc4j";
38  
39      public static final String RESIN_ID = "resin";
40  
41      public static final String TOMCAT_ID = "tomcat";
42  
43      public static final String WEBLOGIC_ID = "weblogic";
44  
45      public static final String WEBSPHERE_ID = "websphere";
46  
47      public static String getServerId() {
48          if (_serverId == null) {
49              if (isGeronimo()) {
50                  _serverId = GERONIMO_ID;
51              }
52              else if (isGlassfish()) {
53                  _serverId = GLASSFISH_ID;
54              }
55              else if (isJBoss()) {
56                  _serverId = JBOSS_ID;
57              }
58              else if (isJOnAS()) {
59                  _serverId = JONAS_ID;
60              }
61              else if (isOC4J()) {
62                  _serverId = OC4J_ID;
63              }
64              else if (isResin()) {
65                  _serverId = RESIN_ID;
66              }
67              else if (isWebLogic()) {
68                  _serverId = WEBLOGIC_ID;
69              }
70              else if (isWebSphere()) {
71                  _serverId = WEBSPHERE_ID;
72              }
73  
74              if (isJetty()) {
75                  if (_serverId == null) {
76                      _serverId = JETTY_ID;
77                  }
78              }
79              else if (isTomcat()) {
80                  if (_serverId == null) {
81                      _serverId = TOMCAT_ID;
82                  }
83              }
84  
85              if (_log.isInfoEnabled()) {
86                  if (_serverId != null) {
87                      _log.info("Detected server " + _serverId);
88                  }
89                  else {
90                      _log.info("No server detected");
91                  }
92              }
93  
94              if (_serverId == null) {
95                  throw new RuntimeException("Server is not supported");
96              }
97          }
98  
99          return _serverId;
100     }
101 
102     public static boolean isGeronimo() {
103         if (_geronimo == null) {
104             _geronimo = _detect(
105                 "/org/apache/geronimo/system/main/Daemon.class");
106         }
107 
108         return _geronimo.booleanValue();
109     }
110 
111     public static boolean isGlassfish() {
112         if (_glassfish == null) {
113             String value = System.getProperty("com.sun.aas.instanceRoot");
114 
115             if (value != null) {
116                 _glassfish = Boolean.TRUE;
117             }
118             else {
119                 _glassfish = Boolean.FALSE;
120             }
121         }
122 
123         return _glassfish.booleanValue();
124     }
125 
126     public static boolean isJBoss() {
127         if (_jBoss == null) {
128             _jBoss = _detect("/org/jboss/Main.class");
129         }
130 
131         return _jBoss.booleanValue();
132     }
133 
134     public static boolean isJetty() {
135         if (_jetty == null) {
136             _jetty = _detect("/org/mortbay/jetty/Server.class");
137         }
138 
139         return _jetty.booleanValue();
140     }
141 
142     public static boolean isJOnAS() {
143         if (_jonas == null) {
144             _jonas = _detect("/org/objectweb/jonas/server/Server.class");
145 
146             if (!_jonas && (System.getProperty("jonas.root") != null)) {
147                 _jonas = Boolean.TRUE;
148             }
149         }
150 
151         return _jonas.booleanValue();
152     }
153 
154     public static boolean isOC4J() {
155         if (_oc4j == null) {
156             _oc4j = _detect("oracle.oc4j.util.ClassUtils");
157         }
158 
159         return _oc4j.booleanValue();
160     }
161 
162     public static boolean isResin() {
163         if (_resin == null) {
164             _resin = _detect("/com/caucho/server/resin/Resin.class");
165         }
166 
167         return _resin.booleanValue();
168     }
169 
170     public static boolean isSupportsComet() {
171         return false;
172     }
173 
174     public static boolean isTomcat() {
175         if (_tomcat == null) {
176             _tomcat = _detect("/org/apache/catalina/startup/Bootstrap.class");
177         }
178 
179         if (_tomcat == null) {
180             _tomcat = _detect("/org/apache/catalina/startup/Embedded.class");
181         }
182 
183         return _tomcat.booleanValue();
184     }
185 
186     public static boolean isWebLogic() {
187         if (_webLogic == null) {
188             _webLogic = _detect("/weblogic/Server.class");
189         }
190 
191         return _webLogic.booleanValue();
192     }
193 
194     public static boolean isWebSphere() {
195         if (_webSphere == null) {
196             _webSphere = _detect(
197                 "/com/ibm/websphere/product/VersionInfo.class");
198         }
199 
200         return _webSphere.booleanValue();
201     }
202 
203     private static Boolean _detect(String className) {
204         try {
205             ClassLoader.getSystemClassLoader().loadClass(className);
206 
207             return Boolean.TRUE;
208         }
209         catch (ClassNotFoundException cnfe) {
210             Class<?> classObj = _instance.getClass();
211 
212             if (classObj.getResource(className) != null) {
213                 return Boolean.TRUE;
214             }
215             else {
216                 return Boolean.FALSE;
217             }
218         }
219     }
220 
221     private ServerDetector() {
222     }
223 
224     private static Log _log = LogFactoryUtil.getLog(ServerDetector.class);
225 
226     private static ServerDetector _instance = new ServerDetector();
227 
228     private static String _serverId;
229     private static Boolean _geronimo;
230     private static Boolean _glassfish;
231     private static Boolean _jBoss;
232     private static Boolean _jetty;
233     private static Boolean _jonas;
234     private static Boolean _oc4j;
235     private static Boolean _resin;
236     private static Boolean _tomcat;
237     private static Boolean _webLogic;
238     private static Boolean _webSphere;
239 
240 }