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