diff options
author | David A. Madore <david+git@madore.org> | 2014-10-05 01:13:30 +0200 |
---|---|---|
committer | David A. Madore <david+git@madore.org> | 2014-10-05 01:13:30 +0200 |
commit | e915feb1602a7d88c39623da055fe68af422d27c (patch) | |
tree | 422ff495c24f6185c28071a950f6c8d688e72800 | |
parent | 81105d8785341817023d3ee2d19f6f814748ae77 (diff) | |
download | damlengine-e915feb1602a7d88c39623da055fe68af422d27c.tar.gz damlengine-e915feb1602a7d88c39623da055fe68af422d27c.tar.bz2 damlengine-e915feb1602a7d88c39623da055fe68af422d27c.zip |
Introduce at least a semblance of thread-safety.
-rw-r--r-- | org/madore/damlengine/DamlEngine.java | 5 | ||||
-rw-r--r-- | org/madore/damlengine/WeblogDatabaseConnection.java | 5 | ||||
-rw-r--r-- | org/madore/damlengine/WeblogServlet.java | 35 | ||||
-rw-r--r-- | org/madore/damlengine/WeblogSummary.java | 2 |
4 files changed, 27 insertions, 20 deletions
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<DOMImplementation> pdomi + = new ThreadLocal<DOMImplementation>(); 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<Connection> pconn + = new ThreadLocal<Connection>(); 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<String> 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<String> 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<Integer,EntrySummary>(); } - public static WeblogSummary getSummary() { + public static synchronized WeblogSummary getSummary() { if ( singleton != null ) return singleton; singleton = new WeblogSummary(); |