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_ID = "geronimo";
37  
38      public static final String GLASSFISH_ID = "glassfish";
39  
40      public static final String JBOSS_ID = "jboss";
41  
42      public static final String JETTY_ID = "jetty";
43  
44      public static final String JONAS_ID = "jonas";
45  
46      public static final String OC4J_ID = "oc4j";
47  
48      public static final String RESIN_ID = "resin";
49  
50      public static final String TOMCAT_ID = "tomcat";
51  
52      public static final String WEBLOGIC_ID = "weblogic";
53  
54      public static final String WEBSPHERE_ID = "websphere";
55  
56      public static String getServerId() {
57          ServerDetector sd = _instance;
58  
59          if (sd._serverId == null) {
60              if (isGeronimo()) {
61                  sd._serverId = GERONIMO_ID;
62              }
63              else if (isGlassfish()) {
64                  sd._serverId = GLASSFISH_ID;
65              }
66              else if (isJBoss()) {
67                  sd._serverId = JBOSS_ID;
68              }
69              else if (isJOnAS()) {
70                  sd._serverId = JONAS_ID;
71              }
72              else if (isOC4J()) {
73                  sd._serverId = OC4J_ID;
74              }
75              else if (isResin()) {
76                  sd._serverId = RESIN_ID;
77              }
78              else if (isWebLogic()) {
79                  sd._serverId = WEBLOGIC_ID;
80              }
81              else if (isWebSphere()) {
82                  sd._serverId = WEBSPHERE_ID;
83              }
84  
85              if (isJetty()) {
86                  if (sd._serverId == null) {
87                      sd._serverId = JETTY_ID;
88                  }
89                  else {
90                      sd._serverId += "-" + JETTY_ID;
91                  }
92              }
93              else if (isTomcat()) {
94                  if (sd._serverId == null) {
95                      sd._serverId = TOMCAT_ID;
96                  }
97                  else {
98                      sd._serverId += "-" + TOMCAT_ID;
99                  }
100             }
101 
102             if (_log.isInfoEnabled()) {
103                 _log.info("Detected server " + sd._serverId);
104             }
105 
106             if (sd._serverId == null) {
107                 throw new RuntimeException("Server is not supported");
108             }
109         }
110 
111         return sd._serverId;
112     }
113 
114     public static boolean isGeronimo() {
115         ServerDetector sd = _instance;
116 
117         if (sd._geronimo == null) {
118             sd._geronimo = _detect(
119                 "/org/apache/geronimo/system/main/Daemon.class");
120         }
121 
122         return sd._geronimo.booleanValue();
123     }
124 
125     public static boolean isGlassfish() {
126         ServerDetector sd = _instance;
127 
128         if (sd._glassfish == null) {
129             String value = System.getProperty("com.sun.aas.instanceRoot");
130 
131             if (value != null) {
132                 sd._glassfish = Boolean.TRUE;
133             }
134             else {
135                 sd._glassfish = Boolean.FALSE;
136             }
137         }
138 
139         return sd._glassfish.booleanValue();
140     }
141 
142     public static boolean isGlassfish2() {
143         ServerDetector sd = _instance;
144 
145         if (sd._glassfish2 == null) {
146             if (isGlassfish() && !isGlassfish3()) {
147                 sd._glassfish2 = Boolean.TRUE;
148             }
149             else {
150                 sd._glassfish2 = Boolean.FALSE;
151             }
152         }
153 
154         return sd._glassfish2.booleanValue();
155     }
156 
157     public static boolean isGlassfish3() {
158         ServerDetector sd = _instance;
159 
160         if (sd._glassfish3 == null) {
161             String value = StringPool.BLANK;
162 
163             if (isGlassfish()) {
164                 value = GetterUtil.getString(
165                     System.getProperty("product.name"));
166             }
167 
168             if (value.equals("GlassFish/v3")) {
169                 sd._glassfish3 = Boolean.TRUE;
170             }
171             else {
172                 sd._glassfish3 = Boolean.FALSE;
173             }
174         }
175 
176         return sd._glassfish3.booleanValue();
177     }
178 
179     public static boolean isJBoss() {
180         ServerDetector sd = _instance;
181 
182         if (sd._jBoss == null) {
183             sd._jBoss = _detect("/org/jboss/Main.class");
184         }
185 
186         return sd._jBoss.booleanValue();
187     }
188 
189     public static boolean isJetty() {
190         ServerDetector sd = _instance;
191 
192         if (sd._jetty == null) {
193             sd._jetty = _detect("/org/mortbay/jetty/Server.class");
194         }
195 
196         return sd._jetty.booleanValue();
197     }
198 
199     public static boolean isJOnAS() {
200         ServerDetector sd = _instance;
201 
202         if (sd._jonas == null) {
203             sd._jonas = _detect("/org/objectweb/jonas/server/Server.class");
204         }
205 
206         return sd._jonas.booleanValue();
207     }
208 
209     public static boolean isOC4J() {
210         ServerDetector sd = _instance;
211 
212         if (sd._oc4j == null) {
213             sd._oc4j = _detect("oracle.oc4j.util.ClassUtils");
214         }
215 
216         return sd._oc4j.booleanValue();
217     }
218 
219     public static boolean isResin() {
220         ServerDetector sd = _instance;
221 
222         if (sd._resin == null) {
223             sd._resin = _detect("/com/caucho/server/resin/Resin.class");
224         }
225 
226         return sd._resin.booleanValue();
227     }
228 
229     public static boolean isTomcat() {
230         ServerDetector sd = _instance;
231 
232         if (sd._tomcat == null) {
233             sd._tomcat = _detect(
234                 "/org/apache/catalina/startup/Bootstrap.class");
235         }
236 
237         if (sd._tomcat == null) {
238             sd._tomcat = _detect("/org/apache/catalina/startup/Embedded.class");
239         }
240 
241         return sd._tomcat.booleanValue();
242     }
243 
244     public static boolean isWebLogic() {
245         ServerDetector sd = _instance;
246 
247         if (sd._webLogic == null) {
248             sd._webLogic = _detect("/weblogic/Server.class");
249         }
250 
251         return sd._webLogic.booleanValue();
252     }
253 
254     public static boolean isWebSphere() {
255         ServerDetector sd = _instance;
256 
257         if (sd._webSphere == null) {
258             sd._webSphere = _detect(
259                 "/com/ibm/websphere/product/VersionInfo.class");
260         }
261 
262         return sd._webSphere.booleanValue();
263     }
264 
265     private static Boolean _detect(String className) {
266         try {
267             ClassLoader.getSystemClassLoader().loadClass(className);
268 
269             return Boolean.TRUE;
270         }
271         catch (ClassNotFoundException cnfe) {
272             ServerDetector sd = _instance;
273 
274             Class<?> c = sd.getClass();
275 
276             if (c.getResource(className) != null) {
277                 return Boolean.TRUE;
278             }
279             else {
280                 return Boolean.FALSE;
281             }
282         }
283     }
284 
285     private ServerDetector() {
286     }
287 
288     private static Log _log = LogFactoryUtil.getLog(ServerDetector.class);
289 
290     private static ServerDetector _instance = new ServerDetector();
291 
292     private String _serverId;
293     private Boolean _geronimo;
294     private Boolean _glassfish;
295     private Boolean _glassfish2;
296     private Boolean _glassfish3;
297     private Boolean _jBoss;
298     private Boolean _jetty;
299     private Boolean _jonas;
300     private Boolean _oc4j;
301     private Boolean _resin;
302     private Boolean _tomcat;
303     private Boolean _webLogic;
304     private Boolean _webSphere;
305 
306 }