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