01: package vqwiki.servlets;
02:
03: import java.io.IOException;
04: import java.util.ArrayList;
05: import java.util.Calendar;
06: import java.util.Collection;
07: import java.util.ResourceBundle;
08:
09: import javax.servlet.ServletException;
10: import javax.servlet.http.HttpServletRequest;
11: import javax.servlet.http.HttpServletResponse;
12:
13: import org.apache.log4j.Logger;
14:
15: import vqwiki.ChangeLog;
16: import vqwiki.Environment;
17: import vqwiki.WikiBase;
18:
19: /**
20: * @author garethc
21: * Date: 7/03/2003
22: */
23: public class RecentChangesServlet extends VQWikiServlet {
24:
25: Collection all = null;
26: private Object lock = new Object();
27: private long lastReloadTime = 0;
28: private static final Logger logger = Logger
29: .getLogger(RecentChangesServlet.class);
30:
31: /**
32: *
33: */
34: protected void doGet(HttpServletRequest request,
35: HttpServletResponse response) throws ServletException,
36: IOException {
37: if (mustReload()) {
38: try {
39: String virtualWiki = (String) request
40: .getAttribute("virtual-wiki");
41: reload(virtualWiki);
42: } catch (Exception e) {
43: error(request, response, e);
44: return;
45: }
46: }
47: ResourceBundle messages = ResourceBundle.getBundle(
48: "ApplicationResources", request.getLocale());
49: request.setAttribute("title", messages
50: .getString("recentchanges.title"));
51: request.setAttribute("changes", all);
52: dispatch("/jsp/recentChanges.jsp", request, response);
53: }
54:
55: /**
56: *
57: */
58: private void reload(String virtualWiki) throws Exception {
59: synchronized (lock) {
60: logger.debug("Reloading recent changes");
61: Calendar cal = Calendar.getInstance();
62: ChangeLog cl = WikiBase.getInstance()
63: .getChangeLogInstance();
64: int n = Environment.getInstance().getIntSetting(
65: Environment.PROPERTY_RECENT_CHANGES_DAYS);
66: if (n == 0) {
67: n = 5;
68: }
69: all = new ArrayList();
70: for (int i = 0; i < n; i++) {
71: Collection col = cl.getChanges(virtualWiki, cal
72: .getTime());
73: if (col != null)
74: all.addAll(col);
75: cal.add(Calendar.DATE, -1);
76: }
77: lastReloadTime = System.currentTimeMillis();
78: }
79: }
80:
81: /**
82: *
83: */
84: private long getDeltaMillisReload() {
85: return Environment.getInstance()
86: .getRecentChangesRefreshInterval() * 1000 * 60;
87: }
88:
89: /**
90: *
91: */
92: private boolean mustReload() {
93: long delta = getDeltaMillisReload();
94: logger.debug("delta = " + delta);
95: return ((System.currentTimeMillis() - lastReloadTime) > delta);
96: }
97: }
|