1   /**
2    * Copyright (c) 2000-2009 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_ID = "glassfish";
42  
43      public static final String GLASSFISH_SYSTEM_PROPERTY =
44          "com.sun.aas.instanceRoot";
45  
46      public static final String JBOSS_CLASS = "/org/jboss/Main.class";
47  
48      public static final String JBOSS_ID = "jboss";
49  
50      public static final String JETTY_CLASS = "/org/mortbay/jetty/Server.class";
51  
52      public static final String JETTY_ID = "jetty";
53  
54      public static final String JONAS_CLASS =
55          "/org/objectweb/jonas/server/Server.class";
56  
57      public static final String JONAS_ID = "jonas";
58  
59      public static final String OC4J_CLASS =
60          "oracle.oc4j.util.ClassUtils";
61  
62      public static final String OC4J_ID = "oc4j";
63  
64      public static final String ORION_CLASS =
65          "/com/evermind/server/ApplicationServer.class";
66  
67      public static final String ORION_ID = "orion";
68  
69      public static final String PRAMATI_CLASS = "/com/pramati/Server.class";
70  
71      public static final String PRAMATI_ID = "pramati";
72  
73      public static final String RESIN_CLASS =
74          "/com/caucho/server/resin/Resin.class";
75  
76      public static final String RESIN_ID = "resin";
77  
78      public static final String REXIP_CLASS = "/com/tcc/Main.class";
79  
80      public static final String REXIP_ID = "rexip";
81  
82      public static final String TOMCAT_BOOTSTRAP_CLASS =
83          "/org/apache/catalina/startup/Bootstrap.class";
84  
85      public static final String TOMCAT_EMBEDDED_CLASS =
86          "/org/apache/catalina/startup/Embedded.class";
87  
88      public static final String TOMCAT_ID = "tomcat";
89  
90      public static final String WEBLOGIC_CLASS = "/weblogic/Server.class";
91  
92      public static final String WEBLOGIC_ID = "weblogic";
93  
94      public static final String WEBSPHERE_CLASS =
95          "/com/ibm/websphere/product/VersionInfo.class";
96  
97      public static final String WEBSPHERE_ID = "websphere";
98  
99      public static String getServerId() {
100         ServerDetector sd = _instance;
101 
102         if (sd._serverId == null) {
103             if (isGeronimo()) {
104                 sd._serverId = GERONIMO_ID;
105             }
106             else if (isGlassfish()) {
107                 sd._serverId = GLASSFISH_ID;
108             }
109             else if (isJBoss()) {
110                 sd._serverId = JBOSS_ID;
111             }
112             else if (isJOnAS()) {
113                 sd._serverId = JONAS_ID;
114             }
115             else if (isOC4J()) {
116                 sd._serverId = OC4J_ID;
117             }
118             else if (isOrion()) {
119                 sd._serverId = ORION_ID;
120             }
121             else if (isPramati()) {
122                 sd._serverId = PRAMATI_ID;
123             }
124             else if (isResin()) {
125                 sd._serverId = RESIN_ID;
126             }
127             else if (isRexIP()) {
128                 sd._serverId = REXIP_ID;
129             }
130             else if (isWebLogic()) {
131                 sd._serverId = WEBLOGIC_ID;
132             }
133             else if (isWebSphere()) {
134                 sd._serverId = WEBSPHERE_ID;
135             }
136 
137             if (isJetty()) {
138                 if (sd._serverId == null) {
139                     sd._serverId = JETTY_ID;
140                 }
141                 else {
142                     sd._serverId += "-" + JETTY_ID;
143                 }
144             }
145             else if (isTomcat()) {
146                 if (sd._serverId == null) {
147                     sd._serverId = TOMCAT_ID;
148                 }
149                 else {
150                     sd._serverId += "-" + TOMCAT_ID;
151                 }
152             }
153 
154             if (_log.isInfoEnabled()) {
155                 if (sd._serverId != null) {
156                     _log.info("Detected server " + sd._serverId);
157                 }
158                 else {
159                     _log.info("No server detected");
160                 }
161             }
162 
163             if (sd._serverId == null) {
164                 throw new RuntimeException("Server is not supported");
165             }
166         }
167 
168         return sd._serverId;
169     }
170 
171     public static boolean isGeronimo() {
172         ServerDetector sd = _instance;
173 
174         if (sd._geronimo == null) {
175             sd._geronimo = _detect(GERONIMO_CLASS);
176         }
177 
178         return sd._geronimo.booleanValue();
179     }
180 
181     public static boolean isGlassfish() {
182         ServerDetector sd = _instance;
183 
184         if (sd._glassfish == null) {
185             String value = System.getProperty(GLASSFISH_SYSTEM_PROPERTY);
186 
187             if (value != null) {
188                 sd._glassfish = Boolean.TRUE;
189             }
190             else {
191                 sd._glassfish = Boolean.FALSE;
192             }
193         }
194 
195         return sd._glassfish.booleanValue();
196     }
197 
198     public static boolean isJBoss() {
199         ServerDetector sd = _instance;
200 
201         if (sd._jBoss == null) {
202             sd._jBoss = _detect(JBOSS_CLASS);
203         }
204 
205         return sd._jBoss.booleanValue();
206     }
207 
208     public static boolean isJetty() {
209         ServerDetector sd = _instance;
210 
211         if (sd._jetty == null) {
212             sd._jetty = _detect(JETTY_CLASS);
213         }
214 
215         return sd._jetty.booleanValue();
216     }
217 
218     public static boolean isJOnAS() {
219         ServerDetector sd = _instance;
220 
221         if (sd._jonas == null) {
222             sd._jonas = _detect(JONAS_CLASS);
223         }
224 
225         return sd._jonas.booleanValue();
226     }
227 
228     public static boolean isOC4J() {
229         ServerDetector sd = _instance;
230 
231         if (sd._oc4j == null) {
232             sd._oc4j = _detect(OC4J_CLASS);
233         }
234 
235         return sd._oc4j.booleanValue();
236     }
237 
238     public static boolean isOrion() {
239         ServerDetector sd = _instance;
240 
241         if (sd._orion == null) {
242             sd._orion = _detect(ORION_CLASS);
243         }
244 
245         return sd._orion.booleanValue();
246     }
247 
248     public static boolean isPramati() {
249         ServerDetector sd = _instance;
250 
251         if (sd._pramati == null) {
252             sd._pramati = _detect(PRAMATI_CLASS);
253         }
254 
255         return sd._pramati.booleanValue();
256     }
257 
258     public static boolean isResin() {
259         ServerDetector sd = _instance;
260 
261         if (sd._resin == null) {
262             sd._resin = _detect(RESIN_CLASS);
263         }
264 
265         return sd._resin.booleanValue();
266     }
267 
268     public static boolean isRexIP() {
269         ServerDetector sd = _instance;
270 
271         if (sd._rexIP == null) {
272             sd._rexIP = _detect(REXIP_CLASS);
273         }
274 
275         return sd._rexIP.booleanValue();
276     }
277 
278     public static boolean isSupportsComet() {
279         return false;
280     }
281 
282     public static boolean isTomcat() {
283         ServerDetector sd = _instance;
284 
285         if (sd._tomcat == null) {
286             sd._tomcat = _detect(TOMCAT_BOOTSTRAP_CLASS);
287         }
288 
289         if (sd._tomcat == null) {
290             sd._tomcat = _detect(TOMCAT_EMBEDDED_CLASS);
291         }
292 
293         return sd._tomcat.booleanValue();
294     }
295 
296     public static boolean isWebLogic() {
297         ServerDetector sd = _instance;
298 
299         if (sd._webLogic == null) {
300             sd._webLogic = _detect(WEBLOGIC_CLASS);
301         }
302 
303         return sd._webLogic.booleanValue();
304     }
305 
306     public static boolean isWebSphere() {
307         ServerDetector sd = _instance;
308 
309         if (sd._webSphere == null) {
310             sd._webSphere = _detect(WEBSPHERE_CLASS);
311         }
312 
313         return sd._webSphere.booleanValue();
314     }
315 
316     private static Boolean _detect(String className) {
317         try {
318             ClassLoader.getSystemClassLoader().loadClass(className);
319 
320             return Boolean.TRUE;
321         }
322         catch (ClassNotFoundException cnfe) {
323             ServerDetector sd = _instance;
324 
325             Class<?> c = sd.getClass();
326 
327             if (c.getResource(className) != null) {
328                 return Boolean.TRUE;
329             }
330             else {
331                 return Boolean.FALSE;
332             }
333         }
334     }
335 
336     private ServerDetector() {
337     }
338 
339     private static Log _log = LogFactoryUtil.getLog(ServerDetector.class);
340 
341     private static ServerDetector _instance = new ServerDetector();
342 
343     private String _serverId;
344     private Boolean _geronimo;
345     private Boolean _glassfish;
346     private Boolean _jBoss;
347     private Boolean _jetty;
348     private Boolean _jonas;
349     private Boolean _oc4j;
350     private Boolean _orion;
351     private Boolean _pramati;
352     private Boolean _resin;
353     private Boolean _rexIP;
354     private Boolean _tomcat;
355     private Boolean _webLogic;
356     private Boolean _webSphere;
357 
358 }