1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.util;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  
25  /**
26   * <a href="ServerDetector.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   *
30   */
31  public class ServerDetector {
32  
33      public static final String GERONIMO_ID = "geronimo";
34  
35      public static final String GLASSFISH_ID = "glassfish";
36  
37      public static final String JBOSS_ID = "jboss";
38  
39      public static final String JETTY_ID = "jetty";
40  
41      public static final String JONAS_ID = "jonas";
42  
43      public static final String OC4J_ID = "oc4j";
44  
45      public static final String RESIN_ID = "resin";
46  
47      public static final String TOMCAT_ID = "tomcat";
48  
49      public static final String WEBLOGIC_ID = "weblogic";
50  
51      public static final String WEBSPHERE_ID = "websphere";
52  
53      public static String getServerId() {
54          ServerDetector sd = _instance;
55  
56          if (sd._serverId == null) {
57              if (isGeronimo()) {
58                  sd._serverId = GERONIMO_ID;
59              }
60              else if (isGlassfish()) {
61                  sd._serverId = GLASSFISH_ID;
62              }
63              else if (isJBoss()) {
64                  sd._serverId = JBOSS_ID;
65              }
66              else if (isJOnAS()) {
67                  sd._serverId = JONAS_ID;
68              }
69              else if (isOC4J()) {
70                  sd._serverId = OC4J_ID;
71              }
72              else if (isResin()) {
73                  sd._serverId = RESIN_ID;
74              }
75              else if (isWebLogic()) {
76                  sd._serverId = WEBLOGIC_ID;
77              }
78              else if (isWebSphere()) {
79                  sd._serverId = WEBSPHERE_ID;
80              }
81  
82              if (isJetty()) {
83                  if (sd._serverId == null) {
84                      sd._serverId = JETTY_ID;
85                  }
86                  else {
87                      sd._serverId += "-" + JETTY_ID;
88                  }
89              }
90              else if (isTomcat()) {
91                  if (sd._serverId == null) {
92                      sd._serverId = TOMCAT_ID;
93                  }
94                  else {
95                      sd._serverId += "-" + TOMCAT_ID;
96                  }
97              }
98  
99              if (_log.isInfoEnabled()) {
100                 if (sd._serverId != null) {
101                     _log.info("Detected server " + sd._serverId);
102                 }
103                 else {
104                     _log.info("No server detected");
105                 }
106             }
107 
108             if (sd._serverId == null) {
109                 throw new RuntimeException("Server is not supported");
110             }
111         }
112 
113         return sd._serverId;
114     }
115 
116     public static boolean isGeronimo() {
117         ServerDetector sd = _instance;
118 
119         if (sd._geronimo == null) {
120             sd._geronimo = _detect(
121                 "/org/apache/geronimo/system/main/Daemon.class");
122         }
123 
124         return sd._geronimo.booleanValue();
125     }
126 
127     public static boolean isGlassfish() {
128         ServerDetector sd = _instance;
129 
130         if (sd._glassfish == null) {
131             String value = System.getProperty("com.sun.aas.instanceRoot");
132 
133             if (value != null) {
134                 sd._glassfish = Boolean.TRUE;
135             }
136             else {
137                 sd._glassfish = Boolean.FALSE;
138             }
139         }
140 
141         return sd._glassfish.booleanValue();
142     }
143 
144     public static boolean isGlassfish2() {
145         ServerDetector sd = _instance;
146 
147         if (sd._glassfish2 == null) {
148             if (isGlassfish() && !isGlassfish3()) {
149                 sd._glassfish2 = Boolean.TRUE;
150             }
151             else {
152                 sd._glassfish2 = Boolean.FALSE;
153             }
154         }
155 
156         return sd._glassfish2.booleanValue();
157     }
158 
159     public static boolean isGlassfish3() {
160         ServerDetector sd = _instance;
161 
162         if (sd._glassfish3 == null) {
163             String value = StringPool.BLANK;
164 
165             if (isGlassfish()) {
166                 value = GetterUtil.getString(
167                     System.getProperty("product.name"));
168             }
169 
170             if (value.equals("GlassFish/v3")) {
171                 sd._glassfish3 = Boolean.TRUE;
172             }
173             else {
174                 sd._glassfish3 = Boolean.FALSE;
175             }
176         }
177 
178         return sd._glassfish3.booleanValue();
179     }
180 
181     public static boolean isJBoss() {
182         ServerDetector sd = _instance;
183 
184         if (sd._jBoss == null) {
185             sd._jBoss = _detect("/org/jboss/Main.class");
186         }
187 
188         return sd._jBoss.booleanValue();
189     }
190 
191     public static boolean isJetty() {
192         ServerDetector sd = _instance;
193 
194         if (sd._jetty == null) {
195             sd._jetty = _detect("/org/mortbay/jetty/Server.class");
196         }
197 
198         return sd._jetty.booleanValue();
199     }
200 
201     public static boolean isJOnAS() {
202         ServerDetector sd = _instance;
203 
204         if (sd._jonas == null) {
205             sd._jonas = _detect("/org/objectweb/jonas/server/Server.class");
206         }
207 
208         return sd._jonas.booleanValue();
209     }
210 
211     public static boolean isOC4J() {
212         ServerDetector sd = _instance;
213 
214         if (sd._oc4j == null) {
215             sd._oc4j = _detect("oracle.oc4j.util.ClassUtils");
216         }
217 
218         return sd._oc4j.booleanValue();
219     }
220 
221     public static boolean isResin() {
222         ServerDetector sd = _instance;
223 
224         if (sd._resin == null) {
225             sd._resin = _detect("/com/caucho/server/resin/Resin.class");
226         }
227 
228         return sd._resin.booleanValue();
229     }
230 
231     public static boolean isSupportsComet() {
232         return false;
233     }
234 
235     public static boolean isTomcat() {
236         ServerDetector sd = _instance;
237 
238         if (sd._tomcat == null) {
239             sd._tomcat = _detect(
240                 "/org/apache/catalina/startup/Bootstrap.class");
241         }
242 
243         if (sd._tomcat == null) {
244             sd._tomcat = _detect("/org/apache/catalina/startup/Embedded.class");
245         }
246 
247         return sd._tomcat.booleanValue();
248     }
249 
250     public static boolean isWebLogic() {
251         ServerDetector sd = _instance;
252 
253         if (sd._webLogic == null) {
254             sd._webLogic = _detect("/weblogic/Server.class");
255         }
256 
257         return sd._webLogic.booleanValue();
258     }
259 
260     public static boolean isWebSphere() {
261         ServerDetector sd = _instance;
262 
263         if (sd._webSphere == null) {
264             sd._webSphere = _detect(
265                 "/com/ibm/websphere/product/VersionInfo.class");
266         }
267 
268         return sd._webSphere.booleanValue();
269     }
270 
271     private static Boolean _detect(String className) {
272         try {
273             ClassLoader.getSystemClassLoader().loadClass(className);
274 
275             return Boolean.TRUE;
276         }
277         catch (ClassNotFoundException cnfe) {
278             ServerDetector sd = _instance;
279 
280             Class<?> c = sd.getClass();
281 
282             if (c.getResource(className) != null) {
283                 return Boolean.TRUE;
284             }
285             else {
286                 return Boolean.FALSE;
287             }
288         }
289     }
290 
291     private ServerDetector() {
292     }
293 
294     private static Log _log = LogFactoryUtil.getLog(ServerDetector.class);
295 
296     private static ServerDetector _instance = new ServerDetector();
297 
298     private String _serverId;
299     private Boolean _geronimo;
300     private Boolean _glassfish;
301     private Boolean _glassfish2;
302     private Boolean _glassfish3;
303     private Boolean _jBoss;
304     private Boolean _jetty;
305     private Boolean _jonas;
306     private Boolean _oc4j;
307     private Boolean _resin;
308     private Boolean _tomcat;
309     private Boolean _webLogic;
310     private Boolean _webSphere;
311 
312 }