From b7dd7fd78bae8478ee3274fdfda0887febc64f55 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Wed, 8 Jun 2011 18:18:49 +0200 Subject: [PATCH] added getServletContainer to SystemUtil --- .../main/java/sonia/scm/ServletContainer.java | 44 +++ .../sonia/scm/ServletContainerDetector.java | 295 ++++++++++++++++++ .../main/java/sonia/scm/util/SystemUtil.java | 17 + 3 files changed, 356 insertions(+) create mode 100644 scm-core/src/main/java/sonia/scm/ServletContainer.java create mode 100644 scm-core/src/main/java/sonia/scm/ServletContainerDetector.java diff --git a/scm-core/src/main/java/sonia/scm/ServletContainer.java b/scm-core/src/main/java/sonia/scm/ServletContainer.java new file mode 100644 index 0000000000..bfd4d51230 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/ServletContainer.java @@ -0,0 +1,44 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm; + +/** + * + * @author Sebastian Sdorra + */ +public enum ServletContainer +{ + SCM_SERVER, JETTY, TOMCAT, GLASSFISH, JBOSS, GERONIMO, JONAS, OC4J, RESIN, + WEBLOGIC, WEBSPHERE, UNKNOWN; +} diff --git a/scm-core/src/main/java/sonia/scm/ServletContainerDetector.java b/scm-core/src/main/java/sonia/scm/ServletContainerDetector.java new file mode 100644 index 0000000000..4135c99170 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/ServletContainerDetector.java @@ -0,0 +1,295 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm; + +/** + * Detects the servletcontainer + * + * inspired by com.liferay.portal.kernel.util.ServerDetector of liferay + * + * @author Sebastian Sdorra + */ +public class ServletContainerDetector +{ + + /** + * Method description + * + * + * @return + */ + public static ServletContainer detect() + { + return new ServletContainerDetector().detectContainer(); + } + + /** + * Method description + * + * + * @return + */ + public ServletContainer detectContainer() + { + ServletContainer container = ServletContainer.UNKNOWN; + + if (isScmServer()) + { + container = ServletContainer.SCM_SERVER; + } + else if (isGeronimo()) + { + container = ServletContainer.GERONIMO; + } + else if (isGlassfish()) + { + container = ServletContainer.GLASSFISH; + } + else if (isJBoss()) + { + container = ServletContainer.JBOSS; + } + else if (isJOnAS()) + { + container = ServletContainer.JONAS; + } + else if (isOC4J()) + { + container = ServletContainer.OC4J; + } + else if (isResin()) + { + container = ServletContainer.RESIN; + } + else if (isWebLogic()) + { + container = ServletContainer.WEBLOGIC; + } + else if (isWebSphere()) + { + container = ServletContainer.WEBSPHERE; + } + else if (isJetty()) + { + container = ServletContainer.JETTY; + } + else if (isTomcat()) + { + container = ServletContainer.TOMCAT; + } + + return container; + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + public boolean isGeronimo() + { + return detect("/org/apache/geronimo/system/main/Daemon.class"); + } + + /** + * Method description + * + * + * @return + */ + public boolean isGlassfish() + { + String value = System.getProperty("com.sun.aas.instanceRoot"); + + if (value != null) + { + return true; + } + else + { + return false; + } + } + + /** + * Method description + * + * + * @return + */ + public boolean isJBoss() + { + return detect("/org/jboss/Main.class"); + } + + /** + * Method description + * + * + * @return + */ + public boolean isJOnAS() + { + boolean jonas = detect("/org/objectweb/jonas/server/Server.class"); + + if (!jonas && (System.getProperty("jonas.root") != null)) + { + jonas = true; + } + + return jonas; + } + + /** + * Method description + * + * + * @return + */ + public boolean isJetty() + { + return detect("/org/mortbay/jetty/Server.class"); + } + + /** + * Method description + * + * + * @return + */ + public boolean isOC4J() + { + return detect("oracle.oc4j.util.ClassUtils"); + } + + /** + * Method description + * + * + * @return + */ + public boolean isResin() + { + return detect("/com/caucho/server/resin/Resin.class"); + } + + /** + * Method description + * + * + * @return + */ + public boolean isScmServer() + { + return "scm-server".equals(System.getProperty("app-name")); + } + + /** + * Method description + * + * + * @return + */ + public boolean isTomcat() + { + boolean tomcat = detect("/org/apache/catalina/startup/Bootstrap.class"); + + if (!tomcat) + { + tomcat = detect("/org/apache/catalina/startup/Embedded.class"); + } + + return tomcat; + } + + /** + * Method description + * + * + * @return + */ + public boolean isWebLogic() + { + return detect("/weblogic/Server.class"); + } + + /** + * Method description + * + * + * @return + */ + public boolean isWebSphere() + { + return detect("/com/ibm/websphere/product/VersionInfo.class"); + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param clazz + * + * @return + */ + private boolean detect(String clazz) + { + try + { + ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); + + systemClassLoader.loadClass(clazz); + + return true; + } + catch (ClassNotFoundException cnfe) + { + Class classObj = getClass(); + + if (classObj.getResource(clazz) != null) + { + return true; + } + else + { + return false; + } + } + } +} diff --git a/scm-core/src/main/java/sonia/scm/util/SystemUtil.java b/scm-core/src/main/java/sonia/scm/util/SystemUtil.java index 2dad460a8c..f41479c80e 100644 --- a/scm-core/src/main/java/sonia/scm/util/SystemUtil.java +++ b/scm-core/src/main/java/sonia/scm/util/SystemUtil.java @@ -36,6 +36,8 @@ package sonia.scm.util; //~--- non-JDK imports -------------------------------------------------------- import sonia.scm.Platform; +import sonia.scm.ServletContainer; +import sonia.scm.ServletContainerDetector; /** * @@ -59,6 +61,10 @@ public class SystemUtil System.getProperty(PROPERTY_ARCH), System.getProperty(PROPERTY_OSARCH)); + /** Field description */ + private static ServletContainer servletContainer = + ServletContainerDetector.detect(); + //~--- methods -------------------------------------------------------------- /** @@ -107,6 +113,17 @@ public class SystemUtil return platform; } + /** + * Method description + * + * + * @return + */ + public static ServletContainer getServletContainer() + { + return servletContainer; + } + /** * Method description *