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                 if (sd._serverId != null) {
104                     _log.info("Detected server " + sd._serverId);
105                 }
106                 else {
107                     _log.info("No server detected");
108                 }
109             }
110 
111             if (sd._serverId == null) {
112                 throw new RuntimeException("Server is not supported");
113             }
114         }
115 
116         return sd._serverId;
117     }
118 
119     public static boolean isGeronimo() {
120         ServerDetector sd = _instance;
121 
122         if (sd._geronimo == null) {
123             sd._geronimo = _detect(
124                 "/org/apache/geronimo/system/main/Daemon.class");
125         }
126 
127         return sd._geronimo.booleanValue();
128     }
129 
130     public static boolean isGlassfish() {
131         ServerDetector sd = _instance;
132 
133         if (sd._glassfish == null) {
134             String value = System.getProperty("com.sun.aas.instanceRoot");
135 
136             if (value != null) {
137                 sd._glassfish = Boolean.TRUE;
138             }
139             else {
140                 sd._glassfish = Boolean.FALSE;
141             }
142         }
143 
144         return sd._glassfish.booleanValue();
145     }
146 
147     public static boolean isGlassfish2() {
148         ServerDetector sd = _instance;
149 
150         if (sd._glassfish2 == null) {
151             if (isGlassfish() && !isGlassfish3()) {
152                 sd._glassfish2 = Boolean.TRUE;
153             }
154             else {
155                 sd._glassfish2 = Boolean.FALSE;
156             }
157         }
158 
159         return sd._glassfish2.booleanValue();
160     }
161 
162     public static boolean isGlassfish3() {
163         ServerDetector sd = _instance;
164 
165         if (sd._glassfish3 == null) {
166             String value = StringPool.BLANK;
167 
168             if (isGlassfish()) {
169                 value = GetterUtil.getString(
170                     System.getProperty("product.name"));
171             }
172 
173             if (value.equals("GlassFish/v3")) {
174                 sd._glassfish3 = Boolean.TRUE;
175             }
176             else {
177                 sd._glassfish3 = Boolean.FALSE;
178             }
179         }
180 
181         return sd._glassfish3.booleanValue();
182     }
183 
184     public static boolean isJBoss() {
185         ServerDetector sd = _instance;
186 
187         if (sd._jBoss == null) {
188             sd._jBoss = _detect("/org/jboss/Main.class");
189         }
190 
191         return sd._jBoss.booleanValue();
192     }
193 
194     public static boolean isJetty() {
195         ServerDetector sd = _instance;
196 
197         if (sd._jetty == null) {
198             sd._jetty = _detect("/org/mortbay/jetty/Server.class");
199         }
200 
201         return sd._jetty.booleanValue();
202     }
203 
204     public static boolean isJOnAS() {
205         ServerDetector sd = _instance;
206 
207         if (sd._jonas == null) {
208             sd._jonas = _detect("/org/objectweb/jonas/server/Server.class");
209         }
210 
211         return sd._jonas.booleanValue();
212     }
213 
214     public static boolean isOC4J() {
215         ServerDetector sd = _instance;
216 
217         if (sd._oc4j == null) {
218             sd._oc4j = _detect("oracle.oc4j.util.ClassUtils");
219         }
220 
221         return sd._oc4j.booleanValue();
222     }
223 
224     public static boolean isResin() {
225         ServerDetector sd = _instance;
226 
227         if (sd._resin == null) {
228             sd._resin = _detect("/com/caucho/server/resin/Resin.class");
229         }
230 
231         return sd._resin.booleanValue();
232     }
233 
234     public static boolean isTomcat() {
235         ServerDetector sd = _instance;
236 
237         if (sd._tomcat == null) {
238             sd._tomcat = _detect(
239                 "/org/apache/catalina/startup/Bootstrap.class");
240         }
241 
242         if (sd._tomcat == null) {
243             sd._tomcat = _detect("/org/apache/catalina/startup/Embedded.class");
244         }
245 
246         return sd._tomcat.booleanValue();
247     }
248 
249     public static boolean isWebLogic() {
250         ServerDetector sd = _instance;
251 
252         if (sd._webLogic == null) {
253             sd._webLogic = _detect("/weblogic/Server.class");
254         }
255 
256         return sd._webLogic.booleanValue();
257     }
258 
259     public static boolean isWebSphere() {
260         ServerDetector sd = _instance;
261 
262         if (sd._webSphere == null) {
263             sd._webSphere = _detect(
264                 "/com/ibm/websphere/product/VersionInfo.class");
265         }
266 
267         return sd._webSphere.booleanValue();
268     }
269 
270     private static Boolean _detect(String className) {
271         try {
272             ClassLoader.getSystemClassLoader().loadClass(className);
273 
274             return Boolean.TRUE;
275         }
276         catch (ClassNotFoundException cnfe) {
277             ServerDetector sd = _instance;
278 
279             Class<?> c = sd.getClass();
280 
281             if (c.getResource(className) != null) {
282                 return Boolean.TRUE;
283             }
284             else {
285                 return Boolean.FALSE;
286             }
287         }
288     }
289 
290     private ServerDetector() {
291     }
292 
293     private static Log _log = LogFactoryUtil.getLog(ServerDetector.class);
294 
295     private static ServerDetector _instance = new ServerDetector();
296 
297     private String _serverId;
298     private Boolean _geronimo;
299     private Boolean _glassfish;
300     private Boolean _glassfish2;
301     private Boolean _glassfish3;
302     private Boolean _jBoss;
303     private Boolean _jetty;
304     private Boolean _jonas;
305     private Boolean _oc4j;
306     private Boolean _resin;
307     private Boolean _tomcat;
308     private Boolean _webLogic;
309     private Boolean _webSphere;
310 
311 }