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