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