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