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_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                 _log.info("Detected server " + sd._serverId);
156             }
157 
158             if (sd._serverId == null) {
159                 throw new RuntimeException("Server is not supported");
160             }
161         }
162 
163         return sd._serverId;
164     }
165 
166     public static boolean isGeronimo() {
167         ServerDetector sd = _instance;
168 
169         if (sd._geronimo == null) {
170             sd._geronimo = _detect(GERONIMO_CLASS);
171         }
172 
173         return sd._geronimo.booleanValue();
174     }
175 
176     public static boolean isGlassfish() {
177         ServerDetector sd = _instance;
178 
179         if (sd._glassfish == null) {
180             String value = System.getProperty(GLASSFISH_SYSTEM_PROPERTY);
181 
182             if (value != null) {
183                 sd._glassfish = Boolean.TRUE;
184             }
185             else {
186                 sd._glassfish = Boolean.FALSE;
187             }
188         }
189 
190         return sd._glassfish.booleanValue();
191     }
192 
193     public static boolean isJBoss() {
194         ServerDetector sd = _instance;
195 
196         if (sd._jBoss == null) {
197             sd._jBoss = _detect(JBOSS_CLASS);
198         }
199 
200         return sd._jBoss.booleanValue();
201     }
202 
203     public static boolean isJetty() {
204         ServerDetector sd = _instance;
205 
206         if (sd._jetty == null) {
207             sd._jetty = _detect(JETTY_CLASS);
208         }
209 
210         return sd._jetty.booleanValue();
211     }
212 
213     public static boolean isJOnAS() {
214         ServerDetector sd = _instance;
215 
216         if (sd._jonas == null) {
217             sd._jonas = _detect(JONAS_CLASS);
218         }
219 
220         return sd._jonas.booleanValue();
221     }
222 
223     public static boolean isOC4J() {
224         ServerDetector sd = _instance;
225 
226         if (sd._oc4j == null) {
227             sd._oc4j = _detect(OC4J_CLASS);
228         }
229 
230         return sd._oc4j.booleanValue();
231     }
232 
233     public static boolean isOrion() {
234         ServerDetector sd = _instance;
235 
236         if (sd._orion == null) {
237             sd._orion = _detect(ORION_CLASS);
238         }
239 
240         return sd._orion.booleanValue();
241     }
242 
243     public static boolean isPramati() {
244         ServerDetector sd = _instance;
245 
246         if (sd._pramati == null) {
247             sd._pramati = _detect(PRAMATI_CLASS);
248         }
249 
250         return sd._pramati.booleanValue();
251     }
252 
253     public static boolean isResin() {
254         ServerDetector sd = _instance;
255 
256         if (sd._resin == null) {
257             sd._resin = _detect(RESIN_CLASS);
258         }
259 
260         return sd._resin.booleanValue();
261     }
262 
263     public static boolean isRexIP() {
264         ServerDetector sd = _instance;
265 
266         if (sd._rexIP == null) {
267             sd._rexIP = _detect(REXIP_CLASS);
268         }
269 
270         return sd._rexIP.booleanValue();
271     }
272 
273     public static boolean isTomcat() {
274         ServerDetector sd = _instance;
275 
276         if (sd._tomcat == null) {
277             sd._tomcat = _detect(TOMCAT_BOOTSTRAP_CLASS);
278         }
279 
280         if (sd._tomcat == null) {
281             sd._tomcat = _detect(TOMCAT_EMBEDDED_CLASS);
282         }
283 
284         return sd._tomcat.booleanValue();
285     }
286 
287     public static boolean isWebLogic() {
288         ServerDetector sd = _instance;
289 
290         if (sd._webLogic == null) {
291             sd._webLogic = _detect(WEBLOGIC_CLASS);
292         }
293 
294         return sd._webLogic.booleanValue();
295     }
296 
297     public static boolean isWebSphere() {
298         ServerDetector sd = _instance;
299 
300         if (sd._webSphere == null) {
301             sd._webSphere = _detect(WEBSPHERE_CLASS);
302         }
303 
304         return sd._webSphere.booleanValue();
305     }
306 
307     private static Boolean _detect(String className) {
308         try {
309             ClassLoader.getSystemClassLoader().loadClass(className);
310 
311             return Boolean.TRUE;
312         }
313         catch (ClassNotFoundException cnfe) {
314             ServerDetector sd = _instance;
315 
316             Class<?> c = sd.getClass();
317 
318             if (c.getResource(className) != null) {
319                 return Boolean.TRUE;
320             }
321             else {
322                 return Boolean.FALSE;
323             }
324         }
325     }
326 
327     private ServerDetector() {
328     }
329 
330     private static Log _log = LogFactoryUtil.getLog(ServerDetector.class);
331 
332     private static ServerDetector _instance = new ServerDetector();
333 
334     private String _serverId;
335     private Boolean _geronimo;
336     private Boolean _glassfish;
337     private Boolean _jBoss;
338     private Boolean _jetty;
339     private Boolean _jonas;
340     private Boolean _oc4j;
341     private Boolean _orion;
342     private Boolean _pramati;
343     private Boolean _resin;
344     private Boolean _rexIP;
345     private Boolean _tomcat;
346     private Boolean _webLogic;
347     private Boolean _webSphere;
348 
349 }