1 package org.oddjob.webapp.servlets; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.io.PrintWriter ; 6 7 import javax.servlet.ServletConfig ; 8 import javax.servlet.ServletContext ; 9 import javax.servlet.ServletException ; 10 import javax.servlet.http.HttpServlet ; 11 import javax.servlet.http.HttpServletRequest ; 12 import javax.servlet.http.HttpServletResponse ; 13 14 import org.apache.log4j.Logger; 15 import org.oddjob.Oddjob; 16 import org.oddjob.util.OddjobLockedException; 17 import org.oddjob.webapp.WebappConstants; 18 19 20 28 29 public class OddjobServlet extends HttpServlet { 30 private static final long serialVersionUID = 20051103; 31 private static final Logger logger = Logger.getLogger(OddjobServlet.class); 32 33 34 private Oddjob oddjob; 35 36 37 private Thread thread; 38 39 43 public void init(ServletConfig config) throws ServletException { 44 oddjob = new Oddjob(); 45 thread = new Thread (oddjob); 46 47 ServletContext context = config.getServletContext(); 48 49 String servletContextPath = context.getRealPath("/"); 50 String oddjobFile = config.getServletContext().getInitParameter( 51 WebappConstants.FILE_PARAM); 52 if (oddjobFile == null) { 53 oddjobFile = config.getInitParameter( 54 WebappConstants.FILE_PARAM); 55 } 56 if (oddjobFile == null) { 57 oddjobFile = servletContextPath + "/WEB-INF/" + "oddjob.xml"; 58 } else { 59 oddjobFile = servletContextPath + "/" + oddjobFile; 60 } 61 oddjob.setFile(new File (oddjobFile)); 62 63 String name = context.getInitParameter( 64 WebappConstants.NAME_PARAM); 65 if (name == null) { 66 name = config.getInitParameter( 67 WebappConstants.NAME_PARAM); 68 } 69 if (name == null) { 70 name = "Oddjob"; 71 } 72 oddjob.setName(name); 73 74 context.setAttribute(WebappConstants.ODDJOB_INSTANCE, oddjob); 75 thread.start(); 76 } 77 78 82 public void doGet(HttpServletRequest req, HttpServletResponse res) 83 throws ServletException , IOException { 84 85 res.setContentType("text/plain"); 86 PrintWriter out = res.getWriter(); 87 out.println("Oddjob"); 88 } 89 90 94 public void destroy() { 95 96 oddjob.stop(); 97 boolean destroyed = false; 98 while (!destroyed) { 99 try { 100 oddjob.destroy(); 101 destroyed = true; 102 } catch (OddjobLockedException e) { 103 logger.debug("Oddjob still running, waiting 1 second."); 104 synchronized (this) { 105 try { 106 wait(1000); 107 } catch (InterruptedException e2) { 108 break; 109 } 110 } 111 } 112 } 113 try { 114 thread.join(); 115 } catch (InterruptedException e) { 116 } 118 119 } 120 } 121 | Popular Tags |