From cde90f22cc34b898c684c9de78c5e93022dbd42a Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Thu, 9 Oct 2014 20:40:26 +0200 Subject: Add a "dynamic context" global state, and make WeblogSummary stale when too old. --- org/madore/damlengine/Context.java | 6 ++++++ org/madore/damlengine/DamlEngine.java | 24 +++++++++++++++++----- org/madore/damlengine/TodoWXrefAttr.java | 2 +- .../damlengine/TodoWeblogIndexSelectElement.java | 2 +- .../damlengine/TodoWeblogSelectionElement.java | 2 +- org/madore/damlengine/TodoWrefAttr.java | 2 +- org/madore/damlengine/WeblogSelect.java | 9 +++++++- org/madore/damlengine/WeblogServlet.java | 4 +++- org/madore/damlengine/WeblogSummary.java | 7 +++++-- 9 files changed, 45 insertions(+), 13 deletions(-) (limited to 'org/madore') diff --git a/org/madore/damlengine/Context.java b/org/madore/damlengine/Context.java index 9249fe5..c1f0dce 100644 --- a/org/madore/damlengine/Context.java +++ b/org/madore/damlengine/Context.java @@ -68,6 +68,12 @@ public class Context implements Cloneable { public WeblogSelectionContext wsc; + public static class DynamicContext { + public long modTime; + } + + public DynamicContext dc; + public static class EntryContext { public String year; public String month; diff --git a/org/madore/damlengine/DamlEngine.java b/org/madore/damlengine/DamlEngine.java index cffd589..4826cf1 100644 --- a/org/madore/damlengine/DamlEngine.java +++ b/org/madore/damlengine/DamlEngine.java @@ -115,20 +115,28 @@ public final class DamlEngine { } public static void processDocument(Document doc, - Context.WeblogSelectionContext wsc) { + Context.WeblogSelectionContext wsc, + Context.DynamicContext dc) { TodoDeque todoDeque = new TodoDeque(); Context ctx = new Context(doc); ctx.wsc = wsc; + ctx.dc = dc; todoDeque.registerAtEnd(new RootTodo(ctx)); todoDeque.dispatchLoop(); } + public static void processDocument(Document doc, + Context.WeblogSelectionContext wsc) { + processDocument(doc, wsc, null); + } + public static void processDocument(Document doc) { - processDocument(doc, null); + processDocument(doc, null, null); } public static void fullProcess(InputStream in, OutputStream out, - Context.WeblogSelectionContext wsc) + Context.WeblogSelectionContext wsc, + Context.DynamicContext dc) throws Exception { final DOMImplementationLS domils @@ -142,7 +150,7 @@ public final class DamlEngine { LSInput input = domils.createLSInput(); input.setByteStream(in); Document doc = par.parse(input); - processDocument(doc, wsc); + processDocument(doc, wsc, dc); doc.normalizeDocument(); Unparser unparser = new Unparser(doc, new OutputStreamWriter(out, "UTF-8"), @@ -151,9 +159,15 @@ public final class DamlEngine { } + public static void fullProcess(InputStream in, OutputStream out, + Context.WeblogSelectionContext wsc) + throws Exception { + fullProcess(in, out, wsc, null); + } + public static void fullProcess(InputStream in, OutputStream out) throws Exception { - fullProcess(in, out, null); + fullProcess(in, out, null, null); } public static boolean runAsServlet = false; diff --git a/org/madore/damlengine/TodoWXrefAttr.java b/org/madore/damlengine/TodoWXrefAttr.java index dbc5511..5065465 100644 --- a/org/madore/damlengine/TodoWXrefAttr.java +++ b/org/madore/damlengine/TodoWXrefAttr.java @@ -26,7 +26,7 @@ public class TodoWXrefAttr extends TodoAttr { if ( ctx.wsc == null ) throw new IllegalStateException("wxref attribute encountered with no weblog selection state"); int prev = ctx.wsc.sel.first() - 1; - WeblogSummary sum = WeblogSummary.getSummary(); + WeblogSummary sum = WeblogSummary.getSummary(ctx.dc); if ( sum != null && sum.entries != null && sum.entries.containsKey(new Integer(prev)) ) { WeblogSummary.EntrySummary ent diff --git a/org/madore/damlengine/TodoWeblogIndexSelectElement.java b/org/madore/damlengine/TodoWeblogIndexSelectElement.java index 004e330..21ed367 100644 --- a/org/madore/damlengine/TodoWeblogIndexSelectElement.java +++ b/org/madore/damlengine/TodoWeblogIndexSelectElement.java @@ -29,7 +29,7 @@ public final class TodoWeblogIndexSelectElement extends TodoDefaultElement { @Override public void handleNodeOnly() { - WeblogSummary wsum = WeblogSummary.getSummary(); + WeblogSummary wsum = WeblogSummary.getSummary(ctx.dc); if ( wsum == null || wsum.entries == null ) { throw new IllegalStateException("weblog-index-select element encountered with no weblog summary available"); } diff --git a/org/madore/damlengine/TodoWeblogSelectionElement.java b/org/madore/damlengine/TodoWeblogSelectionElement.java index 637bc85..9d557d4 100644 --- a/org/madore/damlengine/TodoWeblogSelectionElement.java +++ b/org/madore/damlengine/TodoWeblogSelectionElement.java @@ -159,7 +159,7 @@ public final class TodoWeblogSelectionElement extends TodoDefaultElement { case SINGLE_TITLE: if ( ! ( ctx.wsc instanceof Context.WeblogSingleSelectionContext ) ) throw new IllegalStateException("weblog-selection-single-title element encountered while not in weblog single entry selection state"); - WeblogSummary wsum = WeblogSummary.getSummary(); + WeblogSummary wsum = WeblogSummary.getSummary(ctx.dc); if ( wsum == null || wsum.entries == null ) throw new IllegalStateException("failed to obtain weblog summary for weblog-selection-single-title"); WeblogSummary.EntrySummary esum diff --git a/org/madore/damlengine/TodoWrefAttr.java b/org/madore/damlengine/TodoWrefAttr.java index 7f69a96..9341919 100644 --- a/org/madore/damlengine/TodoWrefAttr.java +++ b/org/madore/damlengine/TodoWrefAttr.java @@ -42,7 +42,7 @@ public class TodoWrefAttr extends TodoAttr { } String targetFile; String targetStdDir = (ctx.gc.uriToTop==null)?"":(ctx.gc.uriToTop+"weblog/"); - WeblogSummary wsum = WeblogSummary.getSummary(); + WeblogSummary wsum = WeblogSummary.getSummary(ctx.dc); WeblogSummary.EntrySummary esum; // Get entry for target link from weblog summary if ( wsum != null && wsum.entries != null ) diff --git a/org/madore/damlengine/WeblogSelect.java b/org/madore/damlengine/WeblogSelect.java index a15a672..4e759cc 100644 --- a/org/madore/damlengine/WeblogSelect.java +++ b/org/madore/damlengine/WeblogSelect.java @@ -15,6 +15,7 @@ public final class WeblogSelect { } public static void fullProcess(Context.WeblogSelectionContext wsc, + Context.DynamicContext dc, OutputStream out) throws Exception { @@ -52,8 +53,14 @@ public final class WeblogSelect { } DamlEngine.fullProcess(Files.newInputStream(DamlEngine.templatePath.resolve(templateResourceName)), - out, wsc); + out, wsc, dc); } + public static void fullProcess(Context.WeblogSelectionContext wsc, + OutputStream out) + throws Exception { + fullProcess(wsc, null, out); + } + } diff --git a/org/madore/damlengine/WeblogServlet.java b/org/madore/damlengine/WeblogServlet.java index 2722616..b68f114 100644 --- a/org/madore/damlengine/WeblogServlet.java +++ b/org/madore/damlengine/WeblogServlet.java @@ -34,7 +34,7 @@ import javax.servlet.http.HttpServletResponse; public class WeblogServlet extends HttpServlet { - private static final long serialVersionUID = 2014100901L; + private static final long serialVersionUID = 2014100902L; @Override public void init(ServletConfig cfg) @@ -137,6 +137,8 @@ public class WeblogServlet extends HttpServlet { response.setCharacterEncoding("UTF-8"); OutputStream out = response.getOutputStream(); try { + Context.DynamicContext dc = new Context.DynamicContext(); + dc.modTime = ((SingleBlogEntryPath)rp).lastModified; WeblogSelect.fullProcess(new Context.WeblogSingleSelectionContext(number), out); } catch (Exception e) { diff --git a/org/madore/damlengine/WeblogSummary.java b/org/madore/damlengine/WeblogSummary.java index b067987..4f560e8 100644 --- a/org/madore/damlengine/WeblogSummary.java +++ b/org/madore/damlengine/WeblogSummary.java @@ -30,6 +30,7 @@ public final class WeblogSummary { } public HashMap entries; + private long obtainedTime; private static WeblogSummary singleton; @@ -37,11 +38,13 @@ public final class WeblogSummary { this.entries = new HashMap(); } - public static synchronized WeblogSummary getSummary() { - if ( singleton != null ) + public static synchronized WeblogSummary getSummary(Context.DynamicContext dc) { + if ( singleton != null + && ( dc == null || singleton.obtainedTime < dc.modTime ) ) return singleton; singleton = new WeblogSummary(); try { + singleton.obtainedTime = System.currentTimeMillis(); final Connection conn = WeblogDatabaseConnection.getConnection(); final PreparedStatement selSt = conn.prepareStatement("SELECT id , edate , lang , title , title_xml , do_single_page FROM entries"); -- cgit v1.2.3