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_CLASS =
42          "/com/sun/appserv/ClassLoaderUtil.class";
43  
44      public static final String GLASSFISH_ID = "glassfish";
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 SUN7_CLASS =
83          "/com/iplanet/ias/tools/cli/IasAdminMain.class";
84  
85      public static final String SUN7_ID = "sun7";
86  
87      public static final String SUN8_CLASS =
88          "/com/sun/enterprise/cli/framework/CLIMain.class";
89  
90      public static final String SUN8_ID = "sun8";
91  
92      public static final String TOMCAT_BOOTSTRAP_CLASS =
93          "/org/apache/catalina/startup/Bootstrap.class";
94  
95      public static final String TOMCAT_EMBEDDED_CLASS =
96          "/org/apache/catalina/startup/Embedded.class";
97  
98      public static final String TOMCAT_ID = "tomcat";
99  
100     public static final String WEBLOGIC_CLASS = "/weblogic/Server.class";
101 
102     public static final String WEBLOGIC_ID = "weblogic";
103 
104     public static final String WEBSPHERE_CLASS =
105         "/com/ibm/websphere/product/VersionInfo.class";
106 
107     public static final String WEBSPHERE_ID = "websphere";
108 
109     public static String getServerId() {
110         ServerDetector sd = _instance;
111 
112         if (sd._serverId == null) {
113             if (ServerDetector.isGeronimo()) {
114                 sd._serverId = GERONIMO_ID;
115             }
116             else if (ServerDetector.isGlassfish()) {
117                 sd._serverId = GLASSFISH_ID;
118             }
119             else if (ServerDetector.isJBoss()) {
120                 sd._serverId = JBOSS_ID;
121             }
122             else if (ServerDetector.isJOnAS()) {
123                 sd._serverId = JONAS_ID;
124             }
125             else if (ServerDetector.isOC4J()) {
126                 sd._serverId = OC4J_ID;
127             }
128             else if (ServerDetector.isOrion()) {
129                 sd._serverId = ORION_ID;
130             }
131             else if (ServerDetector.isPramati()) {
132                 sd._serverId = PRAMATI_ID;
133             }
134             else if (ServerDetector.isResin()) {
135                 sd._serverId = RESIN_ID;
136             }
137             else if (ServerDetector.isRexIP()) {
138                 sd._serverId = REXIP_ID;
139             }
140             else if (ServerDetector.isSun7()) {
141                 sd._serverId = SUN7_ID;
142             }
143             else if (ServerDetector.isSun8()) {
144                 sd._serverId = SUN8_ID;
145             }
146             else if (ServerDetector.isWebLogic()) {
147                 sd._serverId = WEBLOGIC_ID;
148             }
149             else if (ServerDetector.isWebSphere()) {
150                 sd._serverId = WEBSPHERE_ID;
151             }
152 
153             if (ServerDetector.isJetty()) {
154                 if (sd._serverId == null) {
155                     sd._serverId = JETTY_ID;
156                 }
157                 else {
158                     sd._serverId += "-" + JETTY_ID;
159                 }
160             }
161             else if (ServerDetector.isTomcat()) {
162                 if (sd._serverId == null) {
163                     sd._serverId = TOMCAT_ID;
164                 }
165                 else {
166                     sd._serverId += "-" + TOMCAT_ID;
167                 }
168             }
169 
170             if (_log.isInfoEnabled()) {
171                 _log.info("Detected server " + sd._serverId);
172             }
173 
174             if (sd._serverId == null) {
175                 throw new RuntimeException("Server is not supported");
176             }
177         }
178 
179         return sd._serverId;
180     }
181 
182     public static boolean isGeronimo() {
183         ServerDetector sd = _instance;
184 
185         if (sd._geronimo == null) {
186             sd._geronimo = _detect(GERONIMO_CLASS);
187         }
188 
189         return sd._geronimo.booleanValue();
190     }
191 
192     public static boolean isGlassfish() {
193         ServerDetector sd = _instance;
194 
195         if (sd._glassfish == null) {
196             sd._glassfish = _detect(GLASSFISH_CLASS);
197         }
198 
199         return sd._glassfish.booleanValue();
200     }
201 
202     public static boolean isJBoss() {
203         ServerDetector sd = _instance;
204 
205         if (sd._jBoss == null) {
206             sd._jBoss = _detect(JBOSS_CLASS);
207         }
208 
209         return sd._jBoss.booleanValue();
210     }
211 
212     public static boolean isJetty() {
213         ServerDetector sd = _instance;
214 
215         if (sd._jetty == null) {
216             sd._jetty = _detect(JETTY_CLASS);
217         }
218 
219         return sd._jetty.booleanValue();
220     }
221 
222     public static boolean isJOnAS() {
223         ServerDetector sd = _instance;
224 
225         if (sd._jonas == null) {
226             sd._jonas = _detect(JONAS_CLASS);
227         }
228 
229         return sd._jonas.booleanValue();
230     }
231 
232     public static boolean isOC4J() {
233         ServerDetector sd = _instance;
234 
235         if (sd._oc4j == null) {
236             sd._oc4j = _detect(OC4J_CLASS);
237         }
238 
239         return sd._oc4j.booleanValue();
240     }
241 
242     public static boolean isOrion() {
243         ServerDetector sd = _instance;
244 
245         if (sd._orion == null) {
246             sd._orion = _detect(ORION_CLASS);
247         }
248 
249         return sd._orion.booleanValue();
250     }
251 
252     public static boolean isPramati() {
253         ServerDetector sd = _instance;
254 
255         if (sd._pramati == null) {
256             sd._pramati = _detect(PRAMATI_CLASS);
257         }
258 
259         return sd._pramati.booleanValue();
260     }
261 
262     public static boolean isResin() {
263         ServerDetector sd = _instance;
264 
265         if (sd._resin == null) {
266             sd._resin = _detect(RESIN_CLASS);
267         }
268 
269         return sd._resin.booleanValue();
270     }
271 
272     public static boolean isRexIP() {
273         ServerDetector sd = _instance;
274 
275         if (sd._rexIP == null) {
276             sd._rexIP = _detect(REXIP_CLASS);
277         }
278 
279         return sd._rexIP.booleanValue();
280     }
281 
282     public static boolean isSun() {
283         if (isSun7() || isSun8()) {
284             return true;
285         }
286         else {
287             return false;
288         }
289     }
290 
291     public static boolean isSun7() {
292         ServerDetector sd = _instance;
293 
294         if (sd._sun7 == null) {
295             sd._sun7 = _detect(SUN7_CLASS);
296         }
297 
298         return sd._sun7.booleanValue();
299     }
300 
301     public static boolean isSun8() {
302         ServerDetector sd = _instance;
303 
304         if (sd._sun8 == null) {
305             sd._sun8 = _detect(SUN8_CLASS);
306         }
307 
308         return sd._sun8.booleanValue();
309     }
310 
311     public static boolean isTomcat() {
312         ServerDetector sd = _instance;
313 
314         if (sd._tomcat == null) {
315             sd._tomcat = _detect(TOMCAT_BOOTSTRAP_CLASS);
316         }
317 
318         if (sd._tomcat == null) {
319             sd._tomcat = _detect(TOMCAT_EMBEDDED_CLASS);
320         }
321 
322         return sd._tomcat.booleanValue();
323     }
324 
325     public static boolean isWebLogic() {
326         ServerDetector sd = _instance;
327 
328         if (sd._webLogic == null) {
329             sd._webLogic = _detect(WEBLOGIC_CLASS);
330         }
331 
332         return sd._webLogic.booleanValue();
333     }
334 
335     public static boolean isWebSphere() {
336         ServerDetector sd = _instance;
337 
338         if (sd._webSphere == null) {
339             sd._webSphere = _detect(WEBSPHERE_CLASS);
340         }
341 
342         return sd._webSphere.booleanValue();
343     }
344 
345     private static Boolean _detect(String className) {
346         try {
347             ClassLoader.getSystemClassLoader().loadClass(className);
348 
349             return Boolean.TRUE;
350         }
351         catch (ClassNotFoundException cnfe) {
352             ServerDetector sd = _instance;
353 
354             Class<?> c = sd.getClass();
355 
356             if (c.getResource(className) != null) {
357                 return Boolean.TRUE;
358             }
359             else {
360                 return Boolean.FALSE;
361             }
362         }
363     }
364 
365     private ServerDetector() {
366     }
367 
368     private static Log _log = LogFactoryUtil.getLog(ServerDetector.class);
369 
370     private static ServerDetector _instance = new ServerDetector();
371 
372     private String _serverId;
373     private Boolean _geronimo;
374     private Boolean _glassfish;
375     private Boolean _jBoss;
376     private Boolean _jetty;
377     private Boolean _jonas;
378     private Boolean _oc4j;
379     private Boolean _orion;
380     private Boolean _pramati;
381     private Boolean _resin;
382     private Boolean _rexIP;
383     private Boolean _sun7;
384     private Boolean _sun8;
385     private Boolean _tomcat;
386     private Boolean _webLogic;
387     private Boolean _webSphere;
388 
389 }