SimpleHttpHandler.java
001 /**
002  *
003  * All content copyright (c) 2003-2008 Terracotta, Inc.,
004  * except as may otherwise be noted in a separate copyright notice.
005  * All rights reserved.
006  *
007  */
008 package demo.sharedqueue;
009 
010 import java.io.IOException;
011 
012 import javax.servlet.ServletException;
013 import javax.servlet.http.HttpServletRequest;
014 import javax.servlet.http.HttpServletResponse;
015 
016 import org.mortbay.jetty.HttpConnection;
017 import org.mortbay.jetty.Request;
018 import org.mortbay.jetty.handler.AbstractHandler;
019 
020 /**
021  *  Description of the Class
022  *
023  *@author    Terracotta, Inc.
024  */
025 public class SimpleHttpHandler extends AbstractHandler {
026 
027    private final demo.sharedqueue.Queue queue;
028 
029    /**
030     *  Description of the Field
031     */
032    public static final String ACTION = "/webapp";
033 
034    /**
035     *  Description of the Field
036     */
037    public static final String ACTION_ADDWORK = "/addWork";
038 
039    /**
040     *  Description of the Field
041     */
042    public static final String ACTION_GETINFO = "/getInfo";
043 
044    /**
045     *  Description of the Field
046     */
047    public static final String UNITS_OF_WORK = "unitsOfWork";
048 
049    public SimpleHttpHandler(Queue queue) {
050       this.queue = queue;
051    }
052 
053    public final void handle(String target, HttpServletRequest request,
054          HttpServletResponse response, int dispatchthrows IOException,
055          ServletException {
056       Request base_request = (request instanceof Request(Requestrequest
057             : HttpConnection.getCurrentConnection().getRequest();
058       if (target.equals(ACTION_ADDWORK)) {
059          doAddWork(base_request, response);
060       }
061       else if (target.equals(ACTION_GETINFO)) {
062          doGetInfo(base_request, response);
063       }
064    }
065 
066    private final int getIntForParameter(HttpServletRequest request,
067          final String name) {
068       String param = request.getParameter(name);
069       try {
070          return param == null : Integer.parseInt(param);
071       }
072       catch (NumberFormatException nfe) {
073          return 0;
074       }
075    }
076 
077    private final void doAddWork(Request request, HttpServletResponse response)
078           throws IOException {
079       final int unitsOfWork = getIntForParameter(request, UNITS_OF_WORK);
080       final Thread processor = new Thread(
081                new Runnable() {
082                   public void run() {
083                      for (int i = 0; i < unitsOfWork; i++) {
084                         queue.addJob();
085                         // added slight delay to improve visuals
086                         try {
087                            Thread.sleep(50);
088                         }
089                         catch (InterruptedException ie) {
090                            System.err.println(ie.getMessage());
091                         }
092                      }
093                   }
094                });
095       processor.start();
096       response.sendRedirect(ACTION);
097    }
098 
099    private final void doGetInfo(Request request, HttpServletResponse response)
100           throws IOException {
101       response.setContentType("text/xml");
102       response.setStatus(HttpServletResponse.SC_OK);
103 
104       response.getWriter()
105             .println(
106             "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
107       response.getWriter().println("<root>");
108       response.getWriter().println(queue.getXmlData());
109       response.getWriter().println("</root>");
110       request.setHandled(true);
111    }
112 }