diff artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/BuilderPool.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/BuilderPool.java@bd047b71ab37
children 4897a58c8746
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/BuilderPool.java	Thu Apr 25 15:23:37 2013 +0200
@@ -0,0 +1,102 @@
+package org.dive4elements.river.artifacts.datacage.templating;
+
+import java.util.ArrayDeque;
+import java.util.Deque;
+import java.util.List;
+import java.util.Map;
+
+import java.sql.SQLException;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.apache.log4j.Logger;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+
+/** A little round robin pool of builders to mitigate 
+  * the fact the XML DOM documents are not thread safe.
+  */
+public class BuilderPool
+{
+    private static Logger log = Logger.getLogger(BuilderPool.class);
+
+    private static final int DEFAULT_POOL_SIZE = 4;
+
+    private static final int POOL_SIZE = Math.max(
+        Integer.getInteger("flys.datacage.pool.size", DEFAULT_POOL_SIZE), 1);
+
+    private Deque<Builder> pool;
+
+    public BuilderPool(Document document) {
+        this(document, POOL_SIZE);
+    }
+
+    public BuilderPool(Document document, int poolSize) {
+
+        if (log.isDebugEnabled()) {
+            log.debug("Create build pool with " + poolSize + " elements.");
+        }
+
+        pool = new ArrayDeque<Builder>(poolSize);
+        for (int i = 0; i < poolSize; ++i) {
+            pool.add(new Builder(cloneDocument(document)));
+        }
+    }
+
+    private final static Document cloneDocument(Document document) {
+
+        try {
+            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+            DocumentBuilder db = dbf.newDocumentBuilder();
+
+            Node origRoot = document.getDocumentElement();
+
+            Document copy = db.newDocument();
+            Node copyRoot = copy.importNode(origRoot, true);
+            copy.appendChild(copyRoot);
+
+            return copy;
+        }
+        catch (ParserConfigurationException pce) {
+            log.error(pce);
+        }
+
+        log.error("Returning original document. This will lead to threading issues.");
+
+        return document;
+    }
+
+    public void build(
+        List<Builder.NamedConnection> connections,
+        Node                          output,
+        Map<String, Object>           parameters
+    )
+    throws SQLException
+    {
+        Builder builder;
+        synchronized (pool) {
+            try {
+                while ((builder = pool.poll()) == null) {
+                    pool.wait();
+                }
+            }
+            catch (InterruptedException ie) {
+                log.debug("Waiting for builder interrupted. Build canceled.");
+                return;
+            }
+        }
+        try {
+            builder.build(connections, output, parameters);
+        }
+        finally {
+            synchronized (pool) {
+                pool.add(builder);
+                pool.notify();
+            }
+        }
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org