001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.util;
022:
023: import com.liferay.portal.kernel.log.Log;
024: import com.liferay.portal.kernel.log.LogFactoryUtil;
025:
026: /**
027: * <a href="ServerDetector.java.html"><b><i>View Source</i></b></a>
028: *
029: * @author Brian Wing Shun Chan
030: *
031: */
032: public class ServerDetector {
033:
034: public static final String GERONIMO_CLASS = "/org/apache/geronimo/system/main/Daemon.class";
035:
036: public static final String GERONIMO_ID = "geronimo";
037:
038: public static final String GLASSFISH_CLASS = "/com/sun/appserv/ClassLoaderUtil.class";
039:
040: public static final String GLASSFISH_ID = "glassfish";
041:
042: public static final String JBOSS_CLASS = "/org/jboss/Main.class";
043:
044: public static final String JBOSS_ID = "jboss";
045:
046: public static final String JETTY_CLASS = "/org/mortbay/jetty/Server.class";
047:
048: public static final String JETTY_ID = "jetty";
049:
050: public static final String JONAS_CLASS = "/org/objectweb/jonas/server/Server.class";
051:
052: public static final String JONAS_ID = "jonas";
053:
054: public static final String OC4J_CLASS = "oracle.oc4j.util.ClassUtils";
055:
056: public static final String OC4J_ID = "oc4j";
057:
058: public static final String ORION_CLASS = "/com/evermind/server/ApplicationServer.class";
059:
060: public static final String ORION_ID = "orion";
061:
062: public static final String PRAMATI_CLASS = "/com/pramati/Server.class";
063:
064: public static final String PRAMATI_ID = "pramati";
065:
066: public static final String RESIN_CLASS = "/com/caucho/server/resin/Resin.class";
067:
068: public static final String RESIN_ID = "resin";
069:
070: public static final String REXIP_CLASS = "/com/tcc/Main.class";
071:
072: public static final String REXIP_ID = "rexip";
073:
074: public static final String SUN7_CLASS = "/com/iplanet/ias/tools/cli/IasAdminMain.class";
075:
076: public static final String SUN7_ID = "sun7";
077:
078: public static final String SUN8_CLASS = "/com/sun/enterprise/cli/framework/CLIMain.class";
079:
080: public static final String SUN8_ID = "sun8";
081:
082: public static final String TOMCAT_BOOTSTRAP_CLASS = "/org/apache/catalina/startup/Bootstrap.class";
083:
084: public static final String TOMCAT_EMBEDDED_CLASS = "/org/apache/catalina/startup/Embedded.class";
085:
086: public static final String TOMCAT_ID = "tomcat";
087:
088: public static final String WEBLOGIC_CLASS = "/weblogic/Server.class";
089:
090: public static final String WEBLOGIC_ID = "weblogic";
091:
092: public static final String WEBSPHERE_CLASS = "/com/ibm/websphere/product/VersionInfo.class";
093:
094: public static final String WEBSPHERE_ID = "websphere";
095:
096: public static String getServerId() {
097: ServerDetector sd = _instance;
098:
099: if (sd._serverId == null) {
100: if (ServerDetector.isGeronimo()) {
101: sd._serverId = GERONIMO_ID;
102: } else if (ServerDetector.isGlassfish()) {
103: sd._serverId = GLASSFISH_ID;
104: } else if (ServerDetector.isJBoss()) {
105: sd._serverId = JBOSS_ID;
106: } else if (ServerDetector.isJOnAS()) {
107: sd._serverId = JONAS_ID;
108: } else if (ServerDetector.isOC4J()) {
109: sd._serverId = OC4J_ID;
110: } else if (ServerDetector.isOrion()) {
111: sd._serverId = ORION_ID;
112: } else if (ServerDetector.isPramati()) {
113: sd._serverId = PRAMATI_ID;
114: } else if (ServerDetector.isResin()) {
115: sd._serverId = RESIN_ID;
116: } else if (ServerDetector.isRexIP()) {
117: sd._serverId = REXIP_ID;
118: } else if (ServerDetector.isSun7()) {
119: sd._serverId = SUN7_ID;
120: } else if (ServerDetector.isSun8()) {
121: sd._serverId = SUN8_ID;
122: } else if (ServerDetector.isWebLogic()) {
123: sd._serverId = WEBLOGIC_ID;
124: } else if (ServerDetector.isWebSphere()) {
125: sd._serverId = WEBSPHERE_ID;
126: }
127:
128: if (ServerDetector.isJetty()) {
129: if (sd._serverId == null) {
130: sd._serverId = JETTY_ID;
131: } else {
132: sd._serverId += "-" + JETTY_ID;
133: }
134: } else if (ServerDetector.isTomcat()) {
135: if (sd._serverId == null) {
136: sd._serverId = TOMCAT_ID;
137: } else {
138: sd._serverId += "-" + TOMCAT_ID;
139: }
140: }
141:
142: if (_log.isInfoEnabled()) {
143: _log.info("Detected server " + sd._serverId);
144: }
145:
146: if (sd._serverId == null) {
147: throw new RuntimeException("Server is not supported");
148: }
149: }
150:
151: return sd._serverId;
152: }
153:
154: public static boolean isGeronimo() {
155: ServerDetector sd = _instance;
156:
157: if (sd._geronimo == null) {
158: sd._geronimo = _detect(GERONIMO_CLASS);
159: }
160:
161: return sd._geronimo.booleanValue();
162: }
163:
164: public static boolean isGlassfish() {
165: ServerDetector sd = _instance;
166:
167: if (sd._glassfish == null) {
168: sd._glassfish = _detect(GLASSFISH_CLASS);
169: }
170:
171: return sd._glassfish.booleanValue();
172: }
173:
174: public static boolean isJBoss() {
175: ServerDetector sd = _instance;
176:
177: if (sd._jBoss == null) {
178: sd._jBoss = _detect(JBOSS_CLASS);
179: }
180:
181: return sd._jBoss.booleanValue();
182: }
183:
184: public static boolean isJetty() {
185: ServerDetector sd = _instance;
186:
187: if (sd._jetty == null) {
188: sd._jetty = _detect(JETTY_CLASS);
189: }
190:
191: return sd._jetty.booleanValue();
192: }
193:
194: public static boolean isJOnAS() {
195: ServerDetector sd = _instance;
196:
197: if (sd._jonas == null) {
198: sd._jonas = _detect(JONAS_CLASS);
199: }
200:
201: return sd._jonas.booleanValue();
202: }
203:
204: public static boolean isOC4J() {
205: ServerDetector sd = _instance;
206:
207: if (sd._oc4j == null) {
208: sd._oc4j = _detect(OC4J_CLASS);
209: }
210:
211: return sd._oc4j.booleanValue();
212: }
213:
214: public static boolean isOrion() {
215: ServerDetector sd = _instance;
216:
217: if (sd._orion == null) {
218: sd._orion = _detect(ORION_CLASS);
219: }
220:
221: return sd._orion.booleanValue();
222: }
223:
224: public static boolean isPramati() {
225: ServerDetector sd = _instance;
226:
227: if (sd._pramati == null) {
228: sd._pramati = _detect(PRAMATI_CLASS);
229: }
230:
231: return sd._pramati.booleanValue();
232: }
233:
234: public static boolean isResin() {
235: ServerDetector sd = _instance;
236:
237: if (sd._resin == null) {
238: sd._resin = _detect(RESIN_CLASS);
239: }
240:
241: return sd._resin.booleanValue();
242: }
243:
244: public static boolean isRexIP() {
245: ServerDetector sd = _instance;
246:
247: if (sd._rexIP == null) {
248: sd._rexIP = _detect(REXIP_CLASS);
249: }
250:
251: return sd._rexIP.booleanValue();
252: }
253:
254: public static boolean isSun() {
255: if (isSun7() || isSun8()) {
256: return true;
257: } else {
258: return false;
259: }
260: }
261:
262: public static boolean isSun7() {
263: ServerDetector sd = _instance;
264:
265: if (sd._sun7 == null) {
266: sd._sun7 = _detect(SUN7_CLASS);
267: }
268:
269: return sd._sun7.booleanValue();
270: }
271:
272: public static boolean isSun8() {
273: ServerDetector sd = _instance;
274:
275: if (sd._sun8 == null) {
276: sd._sun8 = _detect(SUN8_CLASS);
277: }
278:
279: return sd._sun8.booleanValue();
280: }
281:
282: public static boolean isTomcat() {
283: ServerDetector sd = _instance;
284:
285: if (sd._tomcat == null) {
286: sd._tomcat = _detect(TOMCAT_BOOTSTRAP_CLASS);
287: }
288:
289: if (sd._tomcat == null) {
290: sd._tomcat = _detect(TOMCAT_EMBEDDED_CLASS);
291: }
292:
293: return sd._tomcat.booleanValue();
294: }
295:
296: public static boolean isWebLogic() {
297: ServerDetector sd = _instance;
298:
299: if (sd._webLogic == null) {
300: sd._webLogic = _detect(WEBLOGIC_CLASS);
301: }
302:
303: return sd._webLogic.booleanValue();
304: }
305:
306: public static boolean isWebSphere() {
307: ServerDetector sd = _instance;
308:
309: if (sd._webSphere == null) {
310: sd._webSphere = _detect(WEBSPHERE_CLASS);
311: }
312:
313: return sd._webSphere.booleanValue();
314: }
315:
316: private static Boolean _detect(String className) {
317: try {
318: ClassLoader.getSystemClassLoader().loadClass(className);
319:
320: return Boolean.TRUE;
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: } else {
329: return Boolean.FALSE;
330: }
331: }
332: }
333:
334: private ServerDetector() {
335: }
336:
337: private static Log _log = LogFactoryUtil
338: .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 _sun7;
354: private Boolean _sun8;
355: private Boolean _tomcat;
356: private Boolean _webLogic;
357: private Boolean _webSphere;
358:
359: }
|