检测运行的servlet容器是否为Eclipse Jetty

时间:2013-06-17 15:43:08

标签: eclipse jetty detection

我尝试检测应用程序是否在Eclipse Jetty上作为servlet容器运行。我找到了solution for Mortbay Jetty,但没有找到Eclipse的实际版本。我修改了现有的检查并将Classpath从“/org/mortbay/jetty/Server.class”替换为“/org/eclipse/jetty/server/Server.class”,但是在Jetty 9上检查失败。运行相同的应用程序tomcat很成功。我错了什么?

代码如下:

private boolean detect(final String clazz) {
  try {
    final ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
    systemClassLoader.loadClass(clazz);
    return true;
  } catch (final ClassNotFoundException cnfe) {
    final Class<?> classObj = getClass();
      if (classObj.getResource(clazz) != null) {
        return true;
  } else {
        return false;
      }
  }
}

调用函数:

detect("/org/eclipse/jetty/server/Server.class");

1 个答案:

答案 0 :(得分:3)

访问服务器类可以从嵌入模式完成,但不能是独立的Jetty。 这是因为WebAppClassloader将对org.eclipse.jetty.server.Server类的访问隔离为webapps。

但是,您可以使用以下事实:所有servlet容器必须具有default servlet的实现,只需使用ServletContext访问默认servlet的RequestDispatcher并注意到实现类名。在嵌入式使用期间似乎在Jetty 8.1.10和9.0.3上都能正常工作。 (不久将再试一次独立码头)

全班作为例子:

package com.company.foo;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = { "/detect" })
@SuppressWarnings("serial")
public class DetectServlet extends HttpServlet
{
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        resp.setContentType("text/plain");
        PrintWriter out = resp.getWriter();
        detect(out,"org.eclipse.jetty.server.Server");
        detect(out,"/org/eclipse/jetty/server/Server.class");
        detect(out,"org.eclipse.jetty.server.Request");
        detect(out,"/org/eclipse/jetty/server/Request.class");
        detect(out,"org.mortbay.jetty.Server");
        detect(out,"/org/mortbay/jetty/Server.class");

        detectDefaultServlet(out, req, "org.eclipse.jetty");
        detectDefaultServlet(out, req, "org.apache.catalina");

        String version = getReflectedMethodValue("org.eclipse.jetty.server.Server", "getVersion");
        out.printf("Server.getVersion() = %s%n", version);
    }

    private void detectDefaultServlet(PrintWriter out, HttpServletRequest req, String keyword)
    {
        // Request the default servlet (its pretty safe to say it will always be there)
        RequestDispatcher dispatcher = req.getServletContext().getNamedDispatcher("default");
        if(dispatcher == null) {
            out.printf("detectDefaultServlet(out, req, \"%s\") = <no default servlet>%n", keyword);
            return;
        }

        // If the request dispatcher implementation contains the keyword, we can claim a match
        boolean detected = dispatcher.getClass().getName().contains(keyword);
        out.printf("detectDefaultServlet(out, req, \"%s\") = %b (%s)%n", keyword, detected, dispatcher.getClass().getName());
    }

    private String getReflectedMethodValue(String clazzName, String methodName)
    {
        ClassLoader loader = ClassLoader.getSystemClassLoader();
        try
        {
            Class<?> clazz = Class.forName(clazzName,false,loader);
            Class<?> parameterTypes[] = new Class<?>[0];
            Method method = clazz.getDeclaredMethod(methodName,parameterTypes);
            Object args[] = new Object[0];
            return (String)method.invoke(clazz,args);
        }
        catch (ClassNotFoundException e)
        {
            return "<class-not-found>";
        }
        catch (Throwable t)
        {
            return "<" + t.getClass().getName() + ": " + t.getMessage() + ">";
        }
    }

    private void detect(PrintWriter out, String resource)
    {
        out.printf("detect(\"%s\") = %b%n",resource,detected(resource));
    }

    private boolean detected(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;
            }
        }
    }
}

这将在Jetty 8.1.10 嵌入式

上产生以下输出
detect("org.eclipse.jetty.server.Server") = true
detect("/org/eclipse/jetty/server/Server.class") = true
detect("org.eclipse.jetty.server.Request") = true
detect("/org/eclipse/jetty/server/Request.class") = true
detect("org.mortbay.jetty.Server") = false
detect("/org/mortbay/jetty/Server.class") = false
detectDefaultServlet(out, req, "org.eclipse.jetty") = true (org.eclipse.jetty.server.Dispatcher)
detectDefaultServlet(out, req, "org.apache.catalina") = false (org.eclipse.jetty.server.Dispatcher)
Server.getVersion() = 8.1.10.v20130312

Jetty 9.0.3上的类似输出嵌入式

detect("org.eclipse.jetty.server.Server") = true
detect("/org/eclipse/jetty/server/Server.class") = true
detect("org.eclipse.jetty.server.Request") = true
detect("/org/eclipse/jetty/server/Request.class") = true
detect("org.mortbay.jetty.Server") = false
detect("/org/mortbay/jetty/Server.class") = false
detectDefaultServlet(out, req, "org.eclipse.jetty") = true (org.eclipse.jetty.server.Dispatcher)
detectDefaultServlet(out, req, "org.apache.catalina") = false (org.eclipse.jetty.server.Dispatcher)
Server.getVersion() = 9.0.3.v20130506

但Jetty 9.0.3 Standalone

不同
detect("org.eclipse.jetty.server.Server") = false
detect("/org/eclipse/jetty/server/Server.class") = false
detect("org.eclipse.jetty.server.Request") = false
detect("/org/eclipse/jetty/server/Request.class") = false
detect("org.mortbay.jetty.Server") = false
detect("/org/mortbay/jetty/Server.class") = false
detectDefaultServlet(out, req, "org.eclipse.jetty") = true (org.eclipse.jetty.server.Dispatcher)
detectDefaultServlet(out, req, "org.apache.catalina") = false (org.eclipse.jetty.server.Dispatcher)
Server.getVersion() = <class-not-found>