View Javadoc

1   package org.paneris.jammyjoes.upload;
2   
3   import java.io.IOException;
4   import java.io.PrintWriter;
5   import java.util.Date;
6   
7   import javax.servlet.Servlet;
8   import javax.servlet.ServletConfig;
9   import javax.servlet.ServletException;
10  import javax.servlet.ServletRequest;
11  import javax.servlet.ServletResponse;
12  
13  /**
14   * This servlet launches a Warden thread. This will wait for 24 hours before 
15   * uploading new files to ecentives
16   */
17  public class WardenLauncher implements Servlet {
18  
19    private ServletConfig config;
20    Thread warden = null;
21  
22    /**
23     * Servlet initialisation
24     * <p>
25     * Get parameters from the config file and launch a Warden thread
26     *
27     * @see Warden
28    */
29    public void init(ServletConfig config) throws ServletException {
30      System.err.println(new Date() + " Starting the JammyJoes WardenLauncher");
31      this.config = config;
32  
33      // Launch our warden
34      if (warden==null) {
35        Warden war = new Warden(this);
36        warden = new Thread(war, "warden");
37        warden.start();
38      }
39    }
40  
41    /**
42     * Servlet cleanup
43     * 
44     * Clean up our Warden. Its run method checks that it's own
45     * thread is the one pointed to by warden, so setting it
46     * to null means it exits
47     */
48  
49    public void destroy() {
50  //    warden = null;
51    }
52  
53    public ServletConfig getServletConfig() {
54      return config;
55    }
56  
57    public String getServletInfo() {
58      return "A servlet to launch a warden thread. This warden uploads "+
59             "data to ecentives every 24 hours.";
60    }
61  
62    /**
63     * Service the servlet's request. This should always report that
64     * the thread is launched because the only way of stopping it
65     * is through this servlet's destroy method
66     */
67    public void service(ServletRequest req, ServletResponse resp)
68           throws ServletException, IOException {
69    
70      // Give info about the warden
71  
72      resp.setContentType("text/html");
73      PrintWriter out = resp.getWriter();
74      out.println("The warden thread "+
75                  ((warden!=null)?" is launched":" is not launched")
76                 );
77      out.close();
78    }
79  
80  }