1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  
20  /**
21   * <a href="ServerDetector.java.html"><b><i>View Source</i></b></a>
22   *
23   * @author Brian Wing Shun Chan
24   */
25  public class ServerDetector {
26  
27      public static final String GERONIMO_ID = "geronimo";
28  
29      public static final String GLASSFISH_ID = "glassfish";
30  
31      public static final String JBOSS_ID = "jboss";
32  
33      public static final String JETTY_ID = "jetty";
34  
35      public static final String JONAS_ID = "jonas";
36  
37      public static final String OC4J_ID = "oc4j";
38  
39      public static final String ORION_ID = "orion";
40  
41      public static final String PRAMATI_ID = "pramati";
42  
43      public static final String RESIN_ID = "resin";
44  
45      public static final String TOMCAT_ID = "tomcat";
46  
47      public static final String WEBLOGIC_ID = "weblogic";
48  
49      public static final String WEBSPHERE_ID = "websphere";
50  
51      public static String getServerId() {
52          if (_serverId == null) {
53              if (isGeronimo()) {
54                  _serverId = GERONIMO_ID;
55              }
56              else if (isGlassfish()) {
57                  _serverId = GLASSFISH_ID;
58              }
59              else if (isJBoss()) {
60                  _serverId = JBOSS_ID;
61              }
62              else if (isJOnAS()) {
63                  _serverId = JONAS_ID;
64              }
65              else if (isOC4J()) {
66                  _serverId = OC4J_ID;
67              }
68              else if (isOrion()) {
69                  _serverId = ORION_ID;
70              }
71              else if (isPramati()) {
72                  _serverId = PRAMATI_ID;
73              }
74              else if (isResin()) {
75                  _serverId = RESIN_ID;
76              }
77              else if (isWebLogic()) {
78                  _serverId = WEBLOGIC_ID;
79              }
80              else if (isWebSphere()) {
81                  _serverId = WEBSPHERE_ID;
82              }
83  
84              if (isJetty()) {
85                  if (_serverId == null) {
86                      _serverId = JETTY_ID;
87                  }
88                  else {
89                      _serverId += "-" + JETTY_ID;
90                  }
91              }
92              else if (isTomcat()) {
93                  if (_serverId == null) {
94                      _serverId = TOMCAT_ID;
95                  }
96                  else {
97                      _serverId += "-" + TOMCAT_ID;
98                  }
99              }
100 
101             if (_log.isInfoEnabled()) {
102                 if (_serverId != null) {
103                     _log.info("Detected server " + _serverId);
104                 }
105                 else {
106                     _log.info("No server detected");
107                 }
108             }
109 
110             if (_serverId == null) {
111                 throw new RuntimeException("Server is not supported");
112             }
113         }
114 
115         return _serverId;
116     }
117 
118     public static boolean isGeronimo() {
119         if (_geronimo == null) {
120             _geronimo = _detect(
121                 "/org/apache/geronimo/system/main/Daemon.class");
122         }
123 
124         return _geronimo.booleanValue();
125     }
126 
127     public static boolean isGlassfish() {
128         if (_glassfish == null) {
129             String value = System.getProperty("com.sun.aas.instanceRoot");
130 
131             if (value != null) {
132                 _glassfish = Boolean.TRUE;
133             }
134             else {
135                 _glassfish = Boolean.FALSE;
136             }
137         }
138 
139         return _glassfish.booleanValue();
140     }
141 
142     public static boolean isGlassfish2() {
143         if (_glassfish2 == null) {
144             if (isGlassfish() && !isGlassfish3()) {
145                 _glassfish2 = Boolean.TRUE;
146             }
147             else {
148                 _glassfish2 = Boolean.FALSE;
149             }
150         }
151 
152         return _glassfish2.booleanValue();
153     }
154 
155     public static boolean isGlassfish3() {
156         if (_glassfish3 == null) {
157             String value = StringPool.BLANK;
158 
159             if (isGlassfish()) {
160                 value = GetterUtil.getString(
161                     System.getProperty("product.name"));
162             }
163 
164             if (value.equals("GlassFish/v3")) {
165                 _glassfish3 = Boolean.TRUE;
166             }
167             else {
168                 _glassfish3 = Boolean.FALSE;
169             }
170         }
171 
172         return _glassfish3.booleanValue();
173     }
174 
175     public static boolean isJBoss() {
176         if (_jBoss == null) {
177             _jBoss = _detect("/org/jboss/Main.class");
178         }
179 
180         return _jBoss.booleanValue();
181     }
182 
183     public static boolean isJetty() {
184         if (_jetty == null) {
185             _jetty = _detect("/org/mortbay/jetty/Server.class");
186         }
187 
188         return _jetty.booleanValue();
189     }
190 
191     public static boolean isJOnAS() {
192         if (_jonas == null) {
193             _jonas = _detect("/org/objectweb/jonas/server/Server.class");
194         }
195 
196         return _jonas.booleanValue();
197     }
198 
199     public static boolean isOC4J() {
200         if (_oc4j == null) {
201             _oc4j = _detect("oracle.oc4j.util.ClassUtils");
202         }
203 
204         return _oc4j.booleanValue();
205     }
206 
207     public static boolean isOrion() {
208         if (_orion == null) {
209             _orion = _detect("/com/evermind/server/ApplicationServer.class");
210         }
211 
212         return _orion.booleanValue();
213     }
214 
215     public static boolean isPramati() {
216         if (_pramati == null) {
217             _pramati = _detect("/com/pramati/Server.class");
218         }
219 
220         return _pramati.booleanValue();
221     }
222 
223     public static boolean isResin() {
224         if (_resin == null) {
225             _resin = _detect("/com/caucho/server/resin/Resin.class");
226         }
227 
228         return _resin.booleanValue();
229     }
230 
231     public static boolean isSupportsComet() {
232         return false;
233     }
234 
235     public static boolean isTomcat() {
236         if (_tomcat == null) {
237             _tomcat = _detect("/org/apache/catalina/startup/Bootstrap.class");
238         }
239 
240         if (_tomcat == null) {
241             _tomcat = _detect("/org/apache/catalina/startup/Embedded.class");
242         }
243 
244         return _tomcat.booleanValue();
245     }
246 
247     public static boolean isWebLogic() {
248         if (_webLogic == null) {
249             _webLogic = _detect("/weblogic/Server.class");
250         }
251 
252         return _webLogic.booleanValue();
253     }
254 
255     public static boolean isWebSphere() {
256         if (_webSphere == null) {
257             _webSphere = _detect(
258                 "/com/ibm/websphere/product/VersionInfo.class");
259         }
260 
261         return _webSphere.booleanValue();
262     }
263 
264     private static Boolean _detect(String className) {
265         try {
266             ClassLoader.getSystemClassLoader().loadClass(className);
267 
268             return Boolean.TRUE;
269         }
270         catch (ClassNotFoundException cnfe) {
271             Class<?> classObj = _instance.getClass();
272 
273             if (classObj.getResource(className) != null) {
274                 return Boolean.TRUE;
275             }
276             else {
277                 return Boolean.FALSE;
278             }
279         }
280     }
281 
282     private ServerDetector() {
283     }
284 
285     private static Log _log = LogFactoryUtil.getLog(ServerDetector.class);
286 
287     private static ServerDetector _instance = new ServerDetector();
288 
289     private static String _serverId;
290     private static Boolean _geronimo;
291     private static Boolean _glassfish;
292     private static Boolean _glassfish2;
293     private static Boolean _glassfish3;
294     private static Boolean _jBoss;
295     private static Boolean _jetty;
296     private static Boolean _jonas;
297     private static Boolean _oc4j;
298     private static Boolean _orion;
299     private static Boolean _pramati;
300     private static Boolean _resin;
301     private static Boolean _tomcat;
302     private static Boolean _webLogic;
303     private static Boolean _webSphere;
304 
305 }