开发者

Servlet 3.0 support in embedded Jetty 8.0

For my unit-tests I use a simple test-server based on Jetty:

package eu.kostia.textanalysis.webservices.jetty;

import java.awt.Desktop;
import java.net.URI;

impor开发者_如何学Ct org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class TestServer {
 static private final String CONTEXT_PATH = "/webservice";
 static private final String PROJECT_HOME = System.getenv("MY_WORKSPACE_HOME") + "/WebServices";
 static public final int PORT = 8080;

 private Server server;
 private Exception startException;

 private static class SingletonHolder {
  private static final TestServer INSTANCE = new TestServer();
 }

 /**
  * Returns the singleton instance of the test server.
  * 
  * @return the singleton instance of the test server.
  */
 public static TestServer getInstance() {
  return SingletonHolder.INSTANCE;
 }

 private TestServer() {
  server = new Server(PORT);

  WebAppContext context = new WebAppContext();

  context.setDescriptor(PROJECT_HOME + "/web/WEB-INF/web.xml");
  context.setResourceBase(PROJECT_HOME + "/web");
  context.setContextPath(CONTEXT_PATH);
  context.setParentLoaderPriority(true);


  server.setHandler(context); 
 }

 /**
  * Start the test server. This method returns only when the server is
  * complete started. There is no effect when you invoke this method and the
  * server is already running.
  */
 public void start() {  
  if (!server.isRunning()) {
   startException = null;
   new Thread("TestServer") {
    public void run() {
     try {
      server.start();
      server.join();
     } catch (Exception exc) {
      startException = exc;
     }
    }
   }.start();

   while (true) {
    if (startException != null) {
     throw new Error(startException);
    }

    // Block this method call until the server is started
    if (server.isStarted()) {
     return;
    }
   }
  }
 }

 /**
  * Stop the test server.
  */
 public void stop() {
  try {
   if (server.isRunning()) {
    server.stop();
   }
  } catch (Exception e) {
   throw new Error(e);
  }
 }

 /**
  * Returns {@code true} is the server is running.
  * 
  * @return {@code true} is the server is running.
  */
 public boolean isRunning() {
  return server.isRunning();
 }

 public static void main(String[] args) throws Exception {
  TestServer.getInstance().start();  
  Desktop.getDesktop().browse(new URI("http://localhost:8080/webservice/"));
 }

}

It works very well for servlet configured in web.xml but I'd now like to use the new annotation syntax introduced by the Servlet Specification 3.0, for example:

@WebServlet(urlPatterns = {"/hello"})
public class HelloServlet extends HttpServlet {
         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                PrintWriter writer = response.getWriter();
                writer.print("<h1>HttpServlet using Servlet 3.0</h1>");
        }
}

How shoud I configure Jetty in my TestServer class to process also annotation-based servlets?


Add to your code

context.setConfigurations(new Configuration[] {
                new AnnotationConfiguration(), new WebXmlConfiguration(),
                new WebInfConfiguration(), new TagLibConfiguration(),
                new PlusConfiguration(), new MetaInfConfiguration(),
                new FragmentConfiguration(), new EnvConfiguration() });

You only need to set the AnnotationConfiguration to get the auto-discovery of annotated classes to work. The rest of the configurations are so you can enable other aspects of the container. Supposedly you should be able to do this from the commandline, using OPTIONS=annotations,jsp,(etc...), but I never got that working. At least this way it should pick up your annotated classes properly in the embedded environment.

Also as a side note it appears the Eclipse jetty project has annotation turned off by default, whereas riptide claims to have them turned on by default. I'm guessing this is a difference in the configuration files.


Answering yet another year later.

In the current version of Jetty (8.1) you can accomplish exactly what you want with the command line:

java -jar start.jar OPTIONS=annotations,plus etc/jetty-plus.xml

invoked from the jetty home directory.


Jetty 8 is implementing the servlet 3.0 specification but it's still experimental.

You could also use the embedded glassfish 3 plugin to run your tests. See the below links for some info: http://wikis.sun.com/display/GlassFish/3.1EmbeddedOnePager http://ocpsoft.com/java/using-embedded-glassfish-with-maven/ http://embedded-glassfish.java.net/

I realise as I write this that there are no authoritative resource for using the Glassfish plugin in the manner Jetty is often used. However it does work in a similar way.

I hope this helps at least a bit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜