001: package example;
002:
003: import java.io.IOException;
004:
005: import java.util.logging.Level;
006: import java.util.logging.Logger;
007:
008: import javax.servlet.*;
009: import javax.servlet.http.*;
010:
011: import javax.webbeans.In;
012:
013: /**
014: * Filter to show a maintenance page if the MaintenanceRunner is active.
015: *
016: * <h3>init parameters</h3>
017: * <dl>
018: * <dt>url
019: * <dd>The url to forward to when active, if not set then a 503
020: * (SERVICE_UNAVAILABLE) error code is returned.
021: * <dt>min-estimated-time
022: * <dd>The minimum amount of time in seconds for an estimate of when the task
023: * will complete, default is 5 seconds.
024: * </dl>
025: */
026: public class PeriodicTaskFilter implements Filter {
027: static protected final Logger log = Logger
028: .getLogger(PeriodicTaskFilter.class.getName());
029:
030: private String _url = null;
031: private int _minEstimatedTime = 5;;
032:
033: @In
034: private PeriodicTask _periodicTask;
035:
036: public PeriodicTaskFilter() {
037: }
038:
039: public void setPeriodicTask(PeriodicTask periodicTask) {
040: _periodicTask = periodicTask;
041: }
042:
043: /**
044: * The url to forward to when active, if not set then a 503
045: * (SERVICE_UNAVAILABLE) error code is returned.
046: */
047: public void setUrl(String url) {
048: _url = url;
049: }
050:
051: /**
052: * The minimum amount of time in seconds for an estimate of when the task
053: * will complete, default is 5 seconds.
054: */
055: public void setMinEstimatedTime(int seconds) {
056: _minEstimatedTime = seconds;
057: }
058:
059: public void init(FilterConfig filterConfig) throws ServletException {
060: String p;
061:
062: p = filterConfig.getInitParameter("url");
063: if (p != null)
064: setUrl(p);
065:
066: p = filterConfig.getInitParameter("min-estimated-time");
067: if (p != null)
068: setMinEstimatedTime(Integer.parseInt(p));
069: }
070:
071: public void doFilter(ServletRequest request,
072: ServletResponse response, FilterChain chain)
073: throws ServletException, IOException {
074: if (_periodicTask.isActive()) {
075: dispatch((HttpServletRequest) request,
076: (HttpServletResponse) response);
077: } else {
078: chain.doFilter(request, response);
079: }
080: }
081:
082: /**
083: * Disptach to a page that shows a "temporarily unavailable" message, or
084: * respond with 503.
085: */
086: protected void dispatch(HttpServletRequest request,
087: HttpServletResponse response) throws ServletException,
088: IOException {
089: long remaining = _periodicTask.getEstimatedTimeRemaining();
090:
091: // convert to seconds
092: remaining = ((1000L + remaining) / 1000L) - 1;
093:
094: if (remaining < _minEstimatedTime)
095: remaining = _minEstimatedTime;
096:
097: response.addHeader("Cache-Control", "max-age=" + remaining);
098: response.addHeader("refresh", String.valueOf(remaining));
099:
100: if (_url != null)
101: request.getRequestDispatcher(_url).forward(request,
102: response);
103: else
104: response.sendError(response.SC_SERVICE_UNAVAILABLE);
105: }
106:
107: public void destroy() {
108: }
109: }
|