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 import java.lang.reflect.Field;
021
022 import javax.management.MBeanServer;
023 import javax.management.MBeanServerFactory;
024 import javax.management.ObjectName;
025
026
029 public class ServerDetector {
030
031 public static final String GERONIMO_ID = "geronimo";
032
033 public static final String GLASSFISH_ID = "glassfish";
034
035 public static final String JBOSS_ID = "jboss";
036
037 public static final String JETTY_ID = "jetty";
038
039 public static final String JONAS_ID = "jonas";
040
041 public static final String OC4J_ID = "oc4j";
042
043 public static final String RESIN_ID = "resin";
044
045 public static final String SYSTEM_PROPERTY_KEY_SERVER_DETECTOR_SERVER_ID =
046 "server.detector.server.id";
047
048 public static final String TOMCAT_ID = "tomcat";
049
050 public static final String WEBLOGIC_ID = "weblogic";
051
052 public static final String WEBSPHERE_ID = "websphere";
053
054 public static final String WILDFLY_ID = "wildfly";
055
056
059 @Deprecated
060 public static ServerDetector getInstance() {
061 return new ServerDetector();
062 }
063
064 public static String getServerId() {
065 return StringUtil.toLowerCase(_serverType.toString());
066 }
067
068
071 @Deprecated
072 public static void init(String serverId) {
073 ServerType serverType = null;
074
075 try {
076 serverType = ServerType.valueOf(StringUtil.toUpperCase(serverId));
077 }
078 catch (IllegalArgumentException iae) {
079 serverType = _detectServerType();
080 }
081
082 try {
083 Field field = ReflectionUtil.getDeclaredField(
084 ServerDetector.class, "_serverType");
085
086 field.set(null, serverType);
087 }
088 catch (Exception e) {
089 ReflectionUtil.throwException(e);
090 }
091 }
092
093 public static boolean isGeronimo() {
094 if (_serverType == ServerType.GERONIMO) {
095 return true;
096 }
097
098 return false;
099 }
100
101 public static boolean isGlassfish() {
102 if (_serverType == ServerType.GLASSFISH) {
103 return true;
104 }
105
106 return false;
107 }
108
109 public static boolean isJBoss() {
110 if ((_serverType == ServerType.JBOSS) ||
111 (_serverType == ServerType.JBOSS5) ||
112 (_serverType == ServerType.JBOSS7)) {
113
114 return true;
115 }
116
117 return false;
118 }
119
120 public static boolean isJBoss5() {
121 if (_serverType == ServerType.JBOSS5) {
122 return true;
123 }
124
125 return false;
126 }
127
128 public static boolean isJBoss7() {
129 if (_serverType == ServerType.JBOSS7) {
130 return true;
131 }
132
133 return false;
134 }
135
136 public static boolean isJetty() {
137 if (_serverType == ServerType.JETTY) {
138 return true;
139 }
140
141 return false;
142 }
143
144 public static boolean isJOnAS() {
145 if (_serverType == ServerType.JONAS) {
146 return true;
147 }
148
149 return false;
150 }
151
152 public static boolean isOC4J() {
153 if (_serverType == ServerType.OC4J) {
154 return true;
155 }
156
157 return false;
158 }
159
160 public static boolean isResin() {
161 if (_serverType == ServerType.RESIN) {
162 return true;
163 }
164
165 return false;
166 }
167
168 public static boolean isSupported(String serverType) {
169 if (serverType.equals(ServerDetector.GERONIMO_ID) ||
170 serverType.equals(ServerDetector.GLASSFISH_ID) ||
171 serverType.equals(ServerDetector.JBOSS_ID) ||
172 serverType.equals(ServerDetector.JONAS_ID) ||
173 serverType.equals(ServerDetector.JETTY_ID) ||
174 serverType.equals(ServerDetector.OC4J_ID) ||
175 serverType.equals(ServerDetector.RESIN_ID) ||
176 serverType.equals(ServerDetector.TOMCAT_ID) ||
177 serverType.equals(ServerDetector.WEBLOGIC_ID) ||
178 serverType.equals(ServerDetector.WEBSPHERE_ID) ||
179 serverType.equals(ServerDetector.WILDFLY_ID) ||
180 serverType.equals(
181 StringUtil.toLowerCase(ServerType.JBOSS5.toString())) ||
182 serverType.equals(
183 StringUtil.toLowerCase(ServerType.JBOSS7.toString()))) {
184
185 return true;
186 }
187
188 return false;
189 }
190
191 public static boolean isSupportsComet() {
192 return _SUPPORTS_COMET;
193 }
194
195 public static boolean isSupportsHotDeploy() {
196 return _supportsHotDeploy;
197 }
198
199 public static boolean isTomcat() {
200 if (_serverType == ServerType.TOMCAT) {
201 return true;
202 }
203
204 return false;
205 }
206
207 public static boolean isWebLogic() {
208 if (_serverType == ServerType.WEBLOGIC) {
209 return true;
210 }
211
212 return false;
213 }
214
215 public static boolean isWebSphere() {
216 if (_serverType == ServerType.WEBSPHERE) {
217 return true;
218 }
219
220 return false;
221 }
222
223 public static boolean isWildfly() {
224 if (_serverType == ServerType.WILDFLY) {
225 return true;
226 }
227
228 return false;
229 }
230
231 public static void setSupportsHotDeploy(boolean supportsHotDeploy) {
232 _supportsHotDeploy = supportsHotDeploy;
233
234 if (_log.isInfoEnabled()) {
235 if (supportsHotDeploy) {
236 _log.info("Server supports hot deploy");
237 }
238 else {
239 _log.info("Server does not support hot deploy");
240 }
241 }
242 }
243
244 private static boolean _detect(String className) {
245 try {
246 ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
247
248 systemClassLoader.loadClass(className);
249
250 return true;
251 }
252 catch (ClassNotFoundException cnfe) {
253 if (ServerDetector.class.getResource(className) != null) {
254 return true;
255 }
256 else {
257 return false;
258 }
259 }
260 }
261
262 private static ServerType _detectServerType() {
263 String serverId = System.getProperty(
264 SYSTEM_PROPERTY_KEY_SERVER_DETECTOR_SERVER_ID);
265
266 if (serverId != null) {
267 return ServerType.valueOf(StringUtil.toUpperCase(serverId));
268 }
269
270 if (_hasSystemProperty("org.apache.geronimo.home.dir")) {
271 return ServerType.GERONIMO;
272 }
273
274 if (_hasSystemProperty("com.sun.aas.instanceRoot")) {
275 return ServerType.GLASSFISH;
276 }
277
278 if (_hasSystemProperty("jboss.home.dir")) {
279 if (_isJBossVersion("5")) {
280 return ServerType.JBOSS5;
281 }
282
283 if (_isJBossVersion("7")) {
284 return ServerType.JBOSS7;
285 }
286
287 return ServerType.JBOSS;
288 }
289
290 if (_hasSystemProperty("jonas.base")) {
291 return ServerType.JONAS;
292 }
293
294 if (_detect("oracle.oc4j.util.ClassUtils")) {
295 return ServerType.OC4J;
296 }
297
298 if (_hasSystemProperty("resin.home")) {
299 return ServerType.RESIN;
300 }
301
302 if (_detect("/weblogic/Server.class")) {
303 return ServerType.WEBLOGIC;
304 }
305
306 if (_detect("/com/ibm/websphere/product/VersionInfo.class")) {
307 return ServerType.WEBSPHERE;
308 }
309
310 if (_hasSystemProperty("jboss.home.dir")) {
311 return ServerType.WILDFLY;
312 }
313
314 if (_hasSystemProperty("jetty.home")) {
315 return ServerType.JETTY;
316 }
317
318 if (_hasSystemProperty("catalina.base")) {
319 return ServerType.TOMCAT;
320 }
321
322 return ServerType.UNKNOWN;
323
324
327 }
328
329 private static boolean _hasSystemProperty(String key) {
330 String value = System.getProperty(key);
331
332 if (value != null) {
333 return true;
334 }
335 else {
336 return false;
337 }
338 }
339
340 private static boolean _isJBossVersion(String versionPrefix) {
341 try {
342 for (MBeanServer mBeanServer :
343 MBeanServerFactory.findMBeanServer(null)) {
344
345 String defaultDomain = GetterUtil.getString(
346 mBeanServer.getDefaultDomain(), "jboss");
347
348 if (defaultDomain.equals("jboss")) {
349 ObjectName objectName = new ObjectName(
350 "jboss.system:type=Server");
351
352 String version = (String)mBeanServer.getAttribute(
353 objectName, "VersionNumber");
354
355 if (version.startsWith(versionPrefix)) {
356 return true;
357 }
358
359 return false;
360 }
361 }
362 }
363 catch (Exception e) {
364 _log.error(e, e);
365 }
366
367 return false;
368 }
369
370 private static final boolean _SUPPORTS_COMET = false;
371
372 private static Log _log = LogFactoryUtil.getLog(ServerDetector.class);
373
374 private static final ServerType _serverType;
375
376 static {
377 _serverType = _detectServerType();
378
379 if (System.getProperty("external-properties") == null) {
380 if (_log.isInfoEnabled()) {
381 _log.info(
382 "Detected server " +
383 StringUtil.toLowerCase(_serverType.toString()));
384 }
385 }
386 }
387
388 private static boolean _supportsHotDeploy;
389
390 private enum ServerType {
391
392 GERONIMO, GLASSFISH, JBOSS, JBOSS5, JBOSS7, JETTY, JONAS, OC4J, RESIN,
393 TOMCAT, UNKNOWN, WEBLOGIC, WEBSPHERE, WILDFLY;
394
395 }
396
397 }