1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  /**
29   * <a href="ServerDetector.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   *
33   */
34  public class ServerDetector {
35  
36      public static final String GERONIMO_CLASS =
37          "/org/apache/geronimo/system/main/Daemon.class";
38  
39      public static final String GERONIMO_ID = "geronimo";
40  
41      public static final String GLASSFISH_2_CLASS =
42          "/com/sun/appserv/ClassLoaderUtil.class";
43  
44      public static final String GLASSFISH_3_CLASS =
45          "/com/sun/enterprise/glassfish/bootstrap/ASMainHK2.class";
46  
47      public static final String GLASSFISH_ID = "glassfish";
48  
49      public static final String JBOSS_CLASS = "/org/jboss/Main.class";
50  
51      public static final String JBOSS_ID = "jboss";
52  
53      public static final String JETTY_CLASS = "/org/mortbay/jetty/Server.class";
54  
55      public static final String JETTY_ID = "jetty";
56  
57      public static final String JONAS_CLASS =
58          "/org/objectweb/jonas/server/Server.class";
59  
60      public static final String JONAS_ID = "jonas";
61  
62      public static final String OC4J_CLASS =
63          "oracle.oc4j.util.ClassUtils";
64  
65      public static final String OC4J_ID = "oc4j";
66  
67      public static final String ORION_CLASS =
68          "/com/evermind/server/ApplicationServer.class";
69  
70      public static final String ORION_ID = "orion";
71  
72      public static final String PRAMATI_CLASS = "/com/pramati/Server.class";
73  
74      public static final String PRAMATI_ID = "pramati";
75  
76      public static final String RESIN_CLASS =
77          "/com/caucho/server/resin/Resin.class";
78  
79      public static final String RESIN_ID = "resin";
80  
81      public static final String REXIP_CLASS = "/com/tcc/Main.class";
82  
83      public static final String REXIP_ID = "rexip";
84  
85      public static final String TOMCAT_BOOTSTRAP_CLASS =
86          "/org/apache/catalina/startup/Bootstrap.class";
87  
88      public static final String TOMCAT_EMBEDDED_CLASS =
89          "/org/apache/catalina/startup/Embedded.class";
90  
91      public static final String TOMCAT_ID = "tomcat";
92  
93      public static final String WEBLOGIC_CLASS = "/weblogic/Server.class";
94  
95      public static final String WEBLOGIC_ID = "weblogic";
96  
97      public static final String WEBSPHERE_CLASS =
98          "/com/ibm/websphere/product/VersionInfo.class";
99  
100     public static final String WEBSPHERE_ID = "websphere";
101 
102     public static String getServerId() {
103         ServerDetector sd = _instance;
104 
105         if (sd._serverId == null) {
106             if (isGeronimo()) {
107                 sd._serverId = GERONIMO_ID;
108             }
109             else if (isGlassfish()) {
110                 sd._serverId = GLASSFISH_ID;
111             }
112             else if (isJBoss()) {
113                 sd._serverId = JBOSS_ID;
114             }
115             else if (isJOnAS()) {
116                 sd._serverId = JONAS_ID;
117             }
118             else if (isOC4J()) {
119                 sd._serverId = OC4J_ID;
120             }
121             else if (isOrion()) {
122                 sd._serverId = ORION_ID;
123             }
124             else if (isPramati()) {
125                 sd._serverId = PRAMATI_ID;
126             }
127             else if (isResin()) {
128                 sd._serverId = RESIN_ID;
129             }
130             else if (isRexIP()) {
131                 sd._serverId = REXIP_ID;
132             }
133             else if (isWebLogic()) {
134                 sd._serverId = WEBLOGIC_ID;
135             }
136             else if (isWebSphere()) {
137                 sd._serverId = WEBSPHERE_ID;
138             }
139 
140             if (isJetty()) {
141                 if (sd._serverId == null) {
142                     sd._serverId = JETTY_ID;
143                 }
144                 else {
145                     sd._serverId += "-" + JETTY_ID;
146                 }
147             }
148             else if (isTomcat()) {
149                 if (sd._serverId == null) {
150                     sd._serverId = TOMCAT_ID;
151                 }
152                 else {
153                     sd._serverId += "-" + TOMCAT_ID;
154                 }
155             }
156 
157             if (_log.isInfoEnabled()) {
158                 _log.info("Detected server " + sd._serverId);
159             }
160 
161             if (sd._serverId == null) {
162                 throw new RuntimeException("Server is not supported");
163             }
164         }
165 
166         return sd._serverId;
167     }
168 
169     public static boolean isGeronimo() {
170         ServerDetector sd = _instance;
171 
172         if (sd._geronimo == null) {
173             sd._geronimo = _detect(GERONIMO_CLASS);
174         }
175 
176         return sd._geronimo.booleanValue();
177     }
178 
179     public static boolean isGlassfish() {
180         if (isGlassfish2() || isGlassfish3()) {
181             return true;
182         }
183         else {
184             return false;
185         }
186     }
187 
188     public static boolean isGlassfish2() {
189         ServerDetector sd = _instance;
190 
191         if (sd._glassfish2 == null) {
192             sd._glassfish2 = _detect(GLASSFISH_2_CLASS);
193         }
194 
195         return sd._glassfish2.booleanValue();
196     }
197 
198     public static boolean isGlassfish3() {
199         ServerDetector sd = _instance;
200 
201         if (sd._glassfish3 == null) {
202             sd._glassfish3 = _detect(GLASSFISH_3_CLASS);
203         }
204 
205         return sd._glassfish3.booleanValue();
206     }
207 
208     public static boolean isJBoss() {
209         ServerDetector sd = _instance;
210 
211         if (sd._jBoss == null) {
212             sd._jBoss = _detect(JBOSS_CLASS);
213         }
214 
215         return sd._jBoss.booleanValue();
216     }
217 
218     public static boolean isJetty() {
219         ServerDetector sd = _instance;
220 
221         if (sd._jetty == null) {
222             sd._jetty = _detect(JETTY_CLASS);
223         }
224 
225         return sd._jetty.booleanValue();
226     }
227 
228     public static boolean isJOnAS() {
229         ServerDetector sd = _instance;
230 
231         if (sd._jonas == null) {
232             sd._jonas = _detect(JONAS_CLASS);
233         }
234 
235         return sd._jonas.booleanValue();
236     }
237 
238     public static boolean isOC4J() {
239         ServerDetector sd = _instance;
240 
241         if (sd._oc4j == null) {
242             sd._oc4j = _detect(OC4J_CLASS);
243         }
244 
245         return sd._oc4j.booleanValue();
246     }
247 
248     public static boolean isOrion() {
249         ServerDetector sd = _instance;
250 
251         if (sd._orion == null) {
252             sd._orion = _detect(ORION_CLASS);
253         }
254 
255         return sd._orion.booleanValue();
256     }
257 
258     public static boolean isPramati() {
259         ServerDetector sd = _instance;
260 
261         if (sd._pramati == null) {
262             sd._pramati = _detect(PRAMATI_CLASS);
263         }
264 
265         return sd._pramati.booleanValue();
266     }
267 
268     public static boolean isResin() {
269         ServerDetector sd = _instance;
270 
271         if (sd._resin == null) {
272             sd._resin = _detect(RESIN_CLASS);
273         }
274 
275         return sd._resin.booleanValue();
276     }
277 
278     public static boolean isRexIP() {
279         ServerDetector sd = _instance;
280 
281         if (sd._rexIP == null) {
282             sd._rexIP = _detect(REXIP_CLASS);
283         }
284 
285         return sd._rexIP.booleanValue();
286     }
287 
288     public static boolean isTomcat() {
289         ServerDetector sd = _instance;
290 
291         if (sd._tomcat == null) {
292             sd._tomcat = _detect(TOMCAT_BOOTSTRAP_CLASS);
293         }
294 
295         if (sd._tomcat == null) {
296             sd._tomcat = _detect(TOMCAT_EMBEDDED_CLASS);
297         }
298 
299         return sd._tomcat.booleanValue();
300     }
301 
302     public static boolean isWebLogic() {
303         ServerDetector sd = _instance;
304 
305         if (sd._webLogic == null) {
306             sd._webLogic = _detect(WEBLOGIC_CLASS);
307         }
308 
309         return sd._webLogic.booleanValue();
310     }
311 
312     public static boolean isWebSphere() {
313         ServerDetector sd = _instance;
314 
315         if (sd._webSphere == null) {
316             sd._webSphere = _detect(WEBSPHERE_CLASS);
317         }
318 
319         return sd._webSphere.booleanValue();
320     }
321 
322     private static Boolean _detect(String className) {
323         try {
324             ClassLoader.getSystemClassLoader().loadClass(className);
325 
326             return Boolean.TRUE;
327         }
328         catch (ClassNotFoundException cnfe) {
329             ServerDetector sd = _instance;
330 
331             Class<?> c = sd.getClass();
332 
333             if (c.getResource(className) != null) {
334                 return Boolean.TRUE;
335             }
336             else {
337                 return Boolean.FALSE;
338             }
339         }
340     }
341 
342     private ServerDetector() {
343     }
344 
345     private static Log _log = LogFactoryUtil.getLog(ServerDetector.class);
346 
347     private static ServerDetector _instance = new ServerDetector();
348 
349     private String _serverId;
350     private Boolean _geronimo;
351     private Boolean _glassfish2;
352     private Boolean _glassfish3;
353     private Boolean _jBoss;
354     private Boolean _jetty;
355     private Boolean _jonas;
356     private Boolean _oc4j;
357     private Boolean _orion;
358     private Boolean _pramati;
359     private Boolean _resin;
360     private Boolean _rexIP;
361     private Boolean _tomcat;
362     private Boolean _webLogic;
363     private Boolean _webSphere;
364 
365 }