001
014
015 package com.liferay.portal.kernel.util;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019
020
023 public class ServerDetector {
024
025 public static final String GERONIMO_ID = "geronimo";
026
027 public static final String GLASSFISH_ID = "glassfish";
028
029 public static final String JBOSS_ID = "jboss";
030
031 public static final String JETTY_ID = "jetty";
032
033 public static final String JONAS_ID = "jonas";
034
035 public static final String OC4J_ID = "oc4j";
036
037 public static final String RESIN_ID = "resin";
038
039 public static final String TOMCAT_ID = "tomcat";
040
041 public static final String WEBLOGIC_ID = "weblogic";
042
043 public static final String WEBSPHERE_ID = "websphere";
044
045 public static ServerDetector getInstance() {
046 if (_instance == null) {
047 _instance = new ServerDetector();
048
049 _instance._init();
050 }
051
052 return _instance;
053 }
054
055 public static String getServerId() {
056 return getInstance()._serverId;
057 }
058
059 public static void init(String serverId) {
060 ServerDetector serverDetector = new ServerDetector();
061
062 serverDetector._serverId = serverId;
063
064 if (serverId.equals(GERONIMO_ID)) {
065 serverDetector._geronimo = true;
066 }
067 else if (serverId.equals(GLASSFISH_ID)) {
068 serverDetector._glassfish = true;
069 }
070 else if (serverId.equals(JBOSS_ID)) {
071 serverDetector._jBoss = true;
072 }
073 else if (serverId.equals(JETTY_ID)) {
074 serverDetector._jetty = true;
075 }
076 else if (serverId.equals(JONAS_ID)) {
077 serverDetector._jonas = true;
078 }
079 else if (serverId.equals(OC4J_ID)) {
080 serverDetector._oc4j = true;
081 }
082 else if (serverId.equals(RESIN_ID)) {
083 serverDetector._resin = true;
084 }
085 else if (serverId.equals(TOMCAT_ID)) {
086 serverDetector._tomcat = true;
087 }
088 else if (serverId.equals(WEBLOGIC_ID)) {
089 serverDetector._webLogic = true;
090 }
091 else if (serverId.equals(WEBSPHERE_ID)) {
092 serverDetector._webSphere = true;
093 }
094 else {
095 serverDetector._init();
096 }
097
098 _instance = serverDetector;
099 }
100
101 public static boolean isGeronimo() {
102 return getInstance()._geronimo;
103 }
104
105 public static boolean isGlassfish() {
106 return getInstance()._glassfish;
107 }
108
109 public static boolean isJBoss() {
110 return getInstance()._jBoss;
111 }
112
113 public static boolean isJetty() {
114 return getInstance()._jetty;
115 }
116
117 public static boolean isJOnAS() {
118 return getInstance()._jonas;
119 }
120
121 public static boolean isOC4J() {
122 return getInstance()._oc4j;
123 }
124
125 public static boolean isResin() {
126 return getInstance()._resin;
127 }
128
129 public static boolean isSupportsComet() {
130 return getInstance()._supportsComet;
131 }
132
133 public static boolean isTomcat() {
134 return getInstance()._tomcat;
135 }
136
137 public static boolean isWebLogic() {
138 return getInstance()._webLogic;
139 }
140
141 public static boolean isWebSphere() {
142 return getInstance()._webSphere;
143 }
144
145 private boolean _detect(String className) {
146 try {
147 ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
148
149 systemClassLoader.loadClass(className);
150
151 return true;
152 }
153 catch (ClassNotFoundException cnfe) {
154 Class<?> clazz = getClass();
155
156 if (clazz.getResource(className) != null) {
157 return true;
158 }
159 else {
160 return false;
161 }
162 }
163 }
164
165 private boolean _hasSystemProperty(String key) {
166 String value = System.getProperty(key);
167
168 if (value != null) {
169 return true;
170 }
171 else {
172 return false;
173 }
174 }
175
176 private void _init() {
177 if (_isGeronimo()) {
178 _serverId = GERONIMO_ID;
179 _geronimo = true;
180 }
181 else if (_isGlassfish()) {
182 _serverId = GLASSFISH_ID;
183 _glassfish = true;
184 }
185 else if (_isJBoss()) {
186 _serverId = JBOSS_ID;
187 _jBoss = true;
188 }
189 else if (_isJOnAS()) {
190 _serverId = JONAS_ID;
191 _jonas = true;
192 }
193 else if (_isOC4J()) {
194 _serverId = OC4J_ID;
195 _oc4j = true;
196 }
197 else if (_isResin()) {
198 _serverId = RESIN_ID;
199 _resin = true;
200 }
201 else if (_isWebLogic()) {
202 _serverId = WEBLOGIC_ID;
203 _webLogic = true;
204 }
205 else if (_isWebSphere()) {
206 _serverId = WEBSPHERE_ID;
207 _webSphere = true;
208 }
209
210 if (_serverId == null) {
211 if (_isJetty()) {
212 _serverId = JETTY_ID;
213 _jetty = true;
214 }
215 else if (_isTomcat()) {
216 _serverId = TOMCAT_ID;
217 _tomcat = true;
218 }
219 }
220
221 if (System.getProperty("external-properties") == null) {
222 if (_log.isInfoEnabled()) {
223 if (_serverId != null) {
224 _log.info("Detected server " + _serverId);
225 }
226 else {
227 _log.info("No server detected");
228 }
229 }
230 }
231
232
235 }
236
237 private boolean _isGeronimo() {
238 return _hasSystemProperty("org.apache.geronimo.home.dir");
239 }
240
241 private boolean _isGlassfish() {
242 return _hasSystemProperty("com.sun.aas.instanceRoot");
243 }
244
245 private boolean _isJBoss() {
246 return _hasSystemProperty("jboss.home.dir");
247 }
248
249 private boolean _isJetty() {
250 return _hasSystemProperty("jetty.home");
251 }
252
253 private boolean _isJOnAS() {
254 return _hasSystemProperty("jonas.base");
255 }
256
257 private boolean _isOC4J() {
258 return _detect("oracle.oc4j.util.ClassUtils");
259 }
260
261 private boolean _isResin() {
262 return _hasSystemProperty("resin.home");
263 }
264
265 private boolean _isTomcat() {
266 return _hasSystemProperty("catalina.base");
267 }
268
269 private boolean _isWebLogic() {
270 return _detect("/weblogic/Server.class");
271 }
272
273 private boolean _isWebSphere() {
274 return _detect("/com/ibm/websphere/product/VersionInfo.class");
275 }
276
277 private static Log _log = LogFactoryUtil.getLog(ServerDetector.class);
278
279 private static ServerDetector _instance;
280
281 private boolean _geronimo;
282 private boolean _glassfish;
283 private boolean _jBoss;
284 private boolean _jetty;
285 private boolean _jonas;
286 private boolean _oc4j;
287 private boolean _resin;
288 private String _serverId;
289 private boolean _supportsComet;
290 private boolean _tomcat;
291 private boolean _webLogic;
292 private boolean _webSphere;
293
294 }