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_CLASS =
36          "/org/apache/geronimo/system/main/Daemon.class";
37  
38      public static final String GERONIMO_ID = "geronimo";
39  
40      public static final String GLASSFISH_ID = "glassfish";
41  
42      public static final String GLASSFISH_SYSTEM_PROPERTY =
43          "com.sun.aas.instanceRoot";
44  
45      public static final String JBOSS_CLASS = "/org/jboss/Main.class";
46  
47      public static final String JBOSS_ID = "jboss";
48  
49      public static final String JETTY_CLASS = "/org/mortbay/jetty/Server.class";
50  
51      public static final String JETTY_ID = "jetty";
52  
53      public static final String JONAS_CLASS =
54          "/org/objectweb/jonas/server/Server.class";
55  
56      public static final String JONAS_ID = "jonas";
57  
58      public static final String OC4J_CLASS =
59          "oracle.oc4j.util.ClassUtils";
60  
61      public static final String OC4J_ID = "oc4j";
62  
63      public static final String ORION_CLASS =
64          "/com/evermind/server/ApplicationServer.class";
65  
66      public static final String ORION_ID = "orion";
67  
68      public static final String PRAMATI_CLASS = "/com/pramati/Server.class";
69  
70      public static final String PRAMATI_ID = "pramati";
71  
72      public static final String RESIN_CLASS =
73          "/com/caucho/server/resin/Resin.class";
74  
75      public static final String RESIN_ID = "resin";
76  
77      public static final String REXIP_CLASS = "/com/tcc/Main.class";
78  
79      public static final String REXIP_ID = "rexip";
80  
81      public static final String TOMCAT_BOOTSTRAP_CLASS =
82          "/org/apache/catalina/startup/Bootstrap.class";
83  
84      public static final String TOMCAT_EMBEDDED_CLASS =
85          "/org/apache/catalina/startup/Embedded.class";
86  
87      public static final String TOMCAT_ID = "tomcat";
88  
89      public static final String WEBLOGIC_CLASS = "/weblogic/Server.class";
90  
91      public static final String WEBLOGIC_ID = "weblogic";
92  
93      public static final String WEBSPHERE_CLASS =
94          "/com/ibm/websphere/product/VersionInfo.class";
95  
96      public static final String WEBSPHERE_ID = "websphere";
97  
98      public static String getServerId() {
99          ServerDetector sd = _instance;
100 
101         if (sd._serverId == null) {
102             if (isGeronimo()) {
103                 sd._serverId = GERONIMO_ID;
104             }
105             else if (isGlassfish()) {
106                 sd._serverId = GLASSFISH_ID;
107             }
108             else if (isJBoss()) {
109                 sd._serverId = JBOSS_ID;
110             }
111             else if (isJOnAS()) {
112                 sd._serverId = JONAS_ID;
113             }
114             else if (isOC4J()) {
115                 sd._serverId = OC4J_ID;
116             }
117             else if (isOrion()) {
118                 sd._serverId = ORION_ID;
119             }
120             else if (isPramati()) {
121                 sd._serverId = PRAMATI_ID;
122             }
123             else if (isResin()) {
124                 sd._serverId = RESIN_ID;
125             }
126             else if (isRexIP()) {
127                 sd._serverId = REXIP_ID;
128             }
129             else if (isWebLogic()) {
130                 sd._serverId = WEBLOGIC_ID;
131             }
132             else if (isWebSphere()) {
133                 sd._serverId = WEBSPHERE_ID;
134             }
135 
136             if (isJetty()) {
137                 if (sd._serverId == null) {
138                     sd._serverId = JETTY_ID;
139                 }
140                 else {
141                     sd._serverId += "-" + JETTY_ID;
142                 }
143             }
144             else if (isTomcat()) {
145                 if (sd._serverId == null) {
146                     sd._serverId = TOMCAT_ID;
147                 }
148                 else {
149                     sd._serverId += "-" + TOMCAT_ID;
150                 }
151             }
152 
153             if (_log.isInfoEnabled()) {
154                 if (sd._serverId != null) {
155                     _log.info("Detected server " + sd._serverId);
156                 }
157                 else {
158                     _log.info("No server detected");
159                 }
160             }
161 
162             if (sd._serverId == null) {
163                 throw new RuntimeException("Server is not supported");
164             }
165         }
166 
167         return sd._serverId;
168     }
169 
170     public static boolean isGeronimo() {
171         ServerDetector sd = _instance;
172 
173         if (sd._geronimo == null) {
174             sd._geronimo = _detect(GERONIMO_CLASS);
175         }
176 
177         return sd._geronimo.booleanValue();
178     }
179 
180     public static boolean isGlassfish() {
181         ServerDetector sd = _instance;
182 
183         if (sd._glassfish == null) {
184             String value = System.getProperty(GLASSFISH_SYSTEM_PROPERTY);
185 
186             if (value != null) {
187                 sd._glassfish = Boolean.TRUE;
188             }
189             else {
190                 sd._glassfish = Boolean.FALSE;
191             }
192         }
193 
194         return sd._glassfish.booleanValue();
195     }
196 
197     public static boolean isJBoss() {
198         ServerDetector sd = _instance;
199 
200         if (sd._jBoss == null) {
201             sd._jBoss = _detect(JBOSS_CLASS);
202         }
203 
204         return sd._jBoss.booleanValue();
205     }
206 
207     public static boolean isJetty() {
208         ServerDetector sd = _instance;
209 
210         if (sd._jetty == null) {
211             sd._jetty = _detect(JETTY_CLASS);
212         }
213 
214         return sd._jetty.booleanValue();
215     }
216 
217     public static boolean isJOnAS() {
218         ServerDetector sd = _instance;
219 
220         if (sd._jonas == null) {
221             sd._jonas = _detect(JONAS_CLASS);
222         }
223 
224         return sd._jonas.booleanValue();
225     }
226 
227     public static boolean isOC4J() {
228         ServerDetector sd = _instance;
229 
230         if (sd._oc4j == null) {
231             sd._oc4j = _detect(OC4J_CLASS);
232         }
233 
234         return sd._oc4j.booleanValue();
235     }
236 
237     public static boolean isOrion() {
238         ServerDetector sd = _instance;
239 
240         if (sd._orion == null) {
241             sd._orion = _detect(ORION_CLASS);
242         }
243 
244         return sd._orion.booleanValue();
245     }
246 
247     public static boolean isPramati() {
248         ServerDetector sd = _instance;
249 
250         if (sd._pramati == null) {
251             sd._pramati = _detect(PRAMATI_CLASS);
252         }
253 
254         return sd._pramati.booleanValue();
255     }
256 
257     public static boolean isResin() {
258         ServerDetector sd = _instance;
259 
260         if (sd._resin == null) {
261             sd._resin = _detect(RESIN_CLASS);
262         }
263 
264         return sd._resin.booleanValue();
265     }
266 
267     public static boolean isRexIP() {
268         ServerDetector sd = _instance;
269 
270         if (sd._rexIP == null) {
271             sd._rexIP = _detect(REXIP_CLASS);
272         }
273 
274         return sd._rexIP.booleanValue();
275     }
276 
277     public static boolean isSupportsComet() {
278         return false;
279     }
280 
281     public static boolean isTomcat() {
282         ServerDetector sd = _instance;
283 
284         if (sd._tomcat == null) {
285             sd._tomcat = _detect(TOMCAT_BOOTSTRAP_CLASS);
286         }
287 
288         if (sd._tomcat == null) {
289             sd._tomcat = _detect(TOMCAT_EMBEDDED_CLASS);
290         }
291 
292         return sd._tomcat.booleanValue();
293     }
294 
295     public static boolean isWebLogic() {
296         ServerDetector sd = _instance;
297 
298         if (sd._webLogic == null) {
299             sd._webLogic = _detect(WEBLOGIC_CLASS);
300         }
301 
302         return sd._webLogic.booleanValue();
303     }
304 
305     public static boolean isWebSphere() {
306         ServerDetector sd = _instance;
307 
308         if (sd._webSphere == null) {
309             sd._webSphere = _detect(WEBSPHERE_CLASS);
310         }
311 
312         return sd._webSphere.booleanValue();
313     }
314 
315     private static Boolean _detect(String className) {
316         try {
317             ClassLoader.getSystemClassLoader().loadClass(className);
318 
319             return Boolean.TRUE;
320         }
321         catch (ClassNotFoundException cnfe) {
322             ServerDetector sd = _instance;
323 
324             Class<?> c = sd.getClass();
325 
326             if (c.getResource(className) != null) {
327                 return Boolean.TRUE;
328             }
329             else {
330                 return Boolean.FALSE;
331             }
332         }
333     }
334 
335     private ServerDetector() {
336     }
337 
338     private static Log _log = LogFactoryUtil.getLog(ServerDetector.class);
339 
340     private static ServerDetector _instance = new ServerDetector();
341 
342     private String _serverId;
343     private Boolean _geronimo;
344     private Boolean _glassfish;
345     private Boolean _jBoss;
346     private Boolean _jetty;
347     private Boolean _jonas;
348     private Boolean _oc4j;
349     private Boolean _orion;
350     private Boolean _pramati;
351     private Boolean _resin;
352     private Boolean _rexIP;
353     private Boolean _tomcat;
354     private Boolean _webLogic;
355     private Boolean _webSphere;
356 
357 }