Commits

Jan Börner committed 0876de1

Added detection method for Eclipse Jetty (standalone).

Comments (0)

Files changed (2)

scm-core/src/main/java/sonia/scm/ServletContainer.java

  */
 public enum ServletContainer
 {
-  SCM_SERVER, JETTY, TOMCAT, GLASSFISH, JBOSS, GERONIMO, JONAS, OC4J, RESIN,
-  WEBLOGIC, WEBSPHERE, UNKNOWN;
+	SCM_SERVER,
+	/** Jetty version <= 6 */
+	JETTY,
+	/** Jetty version >= 7 */
+	ECLIPSE_JETTY,
+	TOMCAT,
+	GLASSFISH,
+	JBOSS,
+	GERONIMO,
+	JONAS,
+	OC4J,
+	RESIN,
+	WEBLOGIC,
+	WEBSPHERE,
+	UNKNOWN;
 }

scm-core/src/main/java/sonia/scm/ServletContainerDetector.java

  */
 public class ServletContainerDetector
 {
+	/** Make usage of the logging framework. */
+	private static final Logger LOGGER = LoggerFactory.getLogger(ServletContainerDetector.class);
+	/** Servlet request for alternate detection method. */
+	private HttpServletRequest request = null;
 
   /**
+	 * Constructs a new ServletContainerDetector.
+	 */
+	public ServletContainerDetector() {
+
+	}
+
+	/**
+	 * Constructs a new ServletContainerDetector depending on the ServletRequest.
+	 * @param req The ServletRequest.
+	 */
+	public ServletContainerDetector(final HttpServletRequest req) {
+		request = req;
+	}
+
+	/**
    * Detects the ServletContainer.
    *
    *
   }
 
   /**
+	 * Alternate detection of ServletContainer using DefaultServletDetection.
+	 * @param req The used Servlet instance.
+	 * @return the detected ServletContainer.
+	 */
+	public static ServletContainer detect(final HttpServletRequest req)
+	{
+		return new ServletContainerDetector(req).detectContainer();
+	}
+
+	/**
    * Detects the ServletContainer.
    *
    *
    */
   public ServletContainer detectContainer()
   {
+		LOGGER.trace("Detecting servlet container...");
     ServletContainer container = ServletContainer.UNKNOWN;
 
     if (isScmServer())
     {
       container = ServletContainer.JETTY;
     }
+		else if (isEclipseJetty())
+		{
+			container = ServletContainer.ECLIPSE_JETTY;
+		}
     else if (isTomcat())
     {
       container = ServletContainer.TOMCAT;
     }
 
+		if (ServletContainer.UNKNOWN.equals(container)) {
+			LOGGER.trace("Servlet container is unknown.");
+		}
+
     return container;
   }
 
    */
   public boolean isGlassfish()
   {
-    String value = System.getProperty("com.sun.aas.instanceRoot");
+		final String value = System.getProperty("com.sun.aas.instanceRoot");
 
     if (value != null)
     {
   }
 
   /**
+	 * Returns true if the ServletContainer is a Eclipse Jetty.
+	 *
+	 *
+	 * @return true if the ServletContainer is a Eclipse Jetty
+	 */
+	public boolean isEclipseJetty()
+	{
+		boolean jetty = detect("/org/eclipse/jetty/server/Server.class");
+		if (!jetty && null != request) {
+			jetty = detectDefaultServlet("org.eclipse.jetty");
+		}
+		return jetty;
+	}
+
+	/**
    * Returns true if the ServletContainer is a OC4J.
    *
    *
    *
    * @return true if class exists in system classpath
    */
-  private boolean detect(String clazz)
+	private boolean detect(final String clazz)
   {
     try
     {
-      ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
+			final ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
 
       systemClassLoader.loadClass(clazz);
 
       return true;
     }
-    catch (ClassNotFoundException cnfe)
+		catch (final ClassNotFoundException cnfe)
     {
-      Class<?> classObj = getClass();
+			final Class<?> classObj = getClass();
 
       if (classObj.getResource(clazz) != null)
       {
       }
     }
   }
+
+	/**
+	 * An alternate detection. The default servlet that must be implemented by each application, so we can get it's
+	 * class name and compare against our suggestion.
+	 * @param keyword Part of the class path that is needed at the implementation class.
+	 */
+	private boolean detectDefaultServlet(final String keyword)
+	{
+		// Request the default servlet (its pretty safe to say it will always be there)
+		final RequestDispatcher dispatcher = request.getSession().getServletContext().getNamedDispatcher("default");
+		if (dispatcher == null)
+		{
+			return false;
+		}
+
+		// If the request dispatcher implementation contains the keyword, we can claim a match
+		return dispatcher.getClass().getName().contains(keyword);
+	}
 }