From e915feb1602a7d88c39623da055fe68af422d27c Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Sun, 5 Oct 2014 01:13:30 +0200 Subject: Introduce at least a semblance of thread-safety. --- org/madore/damlengine/DamlEngine.java | 5 +++- .../damlengine/WeblogDatabaseConnection.java | 5 +++- org/madore/damlengine/WeblogServlet.java | 35 +++++++++++----------- org/madore/damlengine/WeblogSummary.java | 2 +- 4 files changed, 27 insertions(+), 20 deletions(-) (limited to 'org/madore/damlengine') diff --git a/org/madore/damlengine/DamlEngine.java b/org/madore/damlengine/DamlEngine.java index 9ac0c08..cffd589 100644 --- a/org/madore/damlengine/DamlEngine.java +++ b/org/madore/damlengine/DamlEngine.java @@ -81,8 +81,10 @@ public final class DamlEngine { } public static final class IncantDOM { - static DOMImplementation domi; + static ThreadLocal pdomi + = new ThreadLocal(); public static DOMImplementation getDOMI() { + DOMImplementation domi = pdomi.get(); if ( domi == null ) { DOMImplementationSource source = new DOMImplementationSourceImpl(); @@ -90,6 +92,7 @@ public final class DamlEngine { if ( domi == null ) throw new MissingResourceException("failed to obtain DOM implementation", "org.w3c.dom.ls.DOMImplementationLS", ""); + pdomi.set(domi); } return domi; } diff --git a/org/madore/damlengine/WeblogDatabaseConnection.java b/org/madore/damlengine/WeblogDatabaseConnection.java index fc485a6..16d0973 100644 --- a/org/madore/damlengine/WeblogDatabaseConnection.java +++ b/org/madore/damlengine/WeblogDatabaseConnection.java @@ -15,10 +15,12 @@ public final class WeblogDatabaseConnection { throw new AssertionError("WeblogDatabaseConnection cannot be instantiated"); } - public static Connection conn; + public static ThreadLocal pconn + = new ThreadLocal(); public static Connection getConnection() throws SQLException { + Connection conn = pconn.get(); if ( conn == null ) { String dbHost = null; if ( ! DamlEngine.runAsServlet ) @@ -106,6 +108,7 @@ public final class WeblogDatabaseConnection { dbProps.setProperty("sslfactory", "org.postgresql.ssl.NonValidatingFactory"); conn = (new Driver()).connect(dbUrl, dbProps); conn.createStatement().execute("SET TIME ZONE 0"); + pconn.set(conn); } return conn; } diff --git a/org/madore/damlengine/WeblogServlet.java b/org/madore/damlengine/WeblogServlet.java index 6b9b738..9cf1a75 100644 --- a/org/madore/damlengine/WeblogServlet.java +++ b/org/madore/damlengine/WeblogServlet.java @@ -28,7 +28,7 @@ import javax.servlet.http.HttpServletResponse; public class WeblogServlet extends HttpServlet { - private static final long serialVersionUID = 2014100406L; + private static final long serialVersionUID = 2014100407L; @Override public void doGet(HttpServletRequest request, @@ -36,22 +36,23 @@ public class WeblogServlet extends HttpServlet { throws IOException, ServletException { - DamlEngine.runAsServlet = true; - DamlEngine.appProps = new Properties(); - ServletConfig cfg = getServletConfig(); - for (Enumeration e = cfg.getInitParameterNames() ; e.hasMoreElements() ; ) { - String k = e.nextElement(); - DamlEngine.appProps.setProperty(k, cfg.getInitParameter(k)); - } - - if ( DamlEngine.appProps.getProperty("base_path") != null ) - DamlEngine.basePath = Paths.get(DamlEngine.appProps.getProperty("base_path")); - if ( DamlEngine.basePath == null ) - DamlEngine.basePath = Paths.get(getServletContext().getRealPath("/")); - if ( DamlEngine.appProps.getProperty("template_path") != null ) - DamlEngine.templatePath = Paths.get(DamlEngine.appProps.getProperty("template_path")); - if ( DamlEngine.templatePath == null ) - DamlEngine.templatePath = DamlEngine.basePath.resolve("templates"); + synchronized ( DamlEngine.class ) { if ( DamlEngine.appProps == null ) { + DamlEngine.runAsServlet = true; + DamlEngine.appProps = new Properties(); + ServletConfig cfg = getServletConfig(); + for (Enumeration e = cfg.getInitParameterNames() ; e.hasMoreElements() ; ) { + String k = e.nextElement(); + DamlEngine.appProps.setProperty(k, cfg.getInitParameter(k)); + } + if ( DamlEngine.appProps.getProperty("base_path") != null ) + DamlEngine.basePath = Paths.get(DamlEngine.appProps.getProperty("base_path")); + if ( DamlEngine.basePath == null ) + DamlEngine.basePath = Paths.get(getServletContext().getRealPath("/")); + if ( DamlEngine.appProps.getProperty("template_path") != null ) + DamlEngine.templatePath = Paths.get(DamlEngine.appProps.getProperty("template_path")); + if ( DamlEngine.templatePath == null ) + DamlEngine.templatePath = DamlEngine.basePath.resolve("templates"); + } } response.setContentType("text/html; charset=utf-8"); response.setCharacterEncoding("UTF-8"); diff --git a/org/madore/damlengine/WeblogSummary.java b/org/madore/damlengine/WeblogSummary.java index 50ab143..b067987 100644 --- a/org/madore/damlengine/WeblogSummary.java +++ b/org/madore/damlengine/WeblogSummary.java @@ -37,7 +37,7 @@ public final class WeblogSummary { this.entries = new HashMap(); } - public static WeblogSummary getSummary() { + public static synchronized WeblogSummary getSummary() { if ( singleton != null ) return singleton; singleton = new WeblogSummary(); -- cgit v1.2.3