comparison 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
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.artifacts.datacage.templating;
2
3 import java.util.ArrayDeque;
4 import java.util.Deque;
5 import java.util.List;
6 import java.util.Map;
7
8 import java.sql.SQLException;
9
10 import javax.xml.parsers.DocumentBuilder;
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.parsers.ParserConfigurationException;
13
14 import org.apache.log4j.Logger;
15
16 import org.w3c.dom.Document;
17 import org.w3c.dom.Node;
18
19 /** A little round robin pool of builders to mitigate
20 * the fact the XML DOM documents are not thread safe.
21 */
22 public class BuilderPool
23 {
24 private static Logger log = Logger.getLogger(BuilderPool.class);
25
26 private static final int DEFAULT_POOL_SIZE = 4;
27
28 private static final int POOL_SIZE = Math.max(
29 Integer.getInteger("flys.datacage.pool.size", DEFAULT_POOL_SIZE), 1);
30
31 private Deque<Builder> pool;
32
33 public BuilderPool(Document document) {
34 this(document, POOL_SIZE);
35 }
36
37 public BuilderPool(Document document, int poolSize) {
38
39 if (log.isDebugEnabled()) {
40 log.debug("Create build pool with " + poolSize + " elements.");
41 }
42
43 pool = new ArrayDeque<Builder>(poolSize);
44 for (int i = 0; i < poolSize; ++i) {
45 pool.add(new Builder(cloneDocument(document)));
46 }
47 }
48
49 private final static Document cloneDocument(Document document) {
50
51 try {
52 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
53 DocumentBuilder db = dbf.newDocumentBuilder();
54
55 Node origRoot = document.getDocumentElement();
56
57 Document copy = db.newDocument();
58 Node copyRoot = copy.importNode(origRoot, true);
59 copy.appendChild(copyRoot);
60
61 return copy;
62 }
63 catch (ParserConfigurationException pce) {
64 log.error(pce);
65 }
66
67 log.error("Returning original document. This will lead to threading issues.");
68
69 return document;
70 }
71
72 public void build(
73 List<Builder.NamedConnection> connections,
74 Node output,
75 Map<String, Object> parameters
76 )
77 throws SQLException
78 {
79 Builder builder;
80 synchronized (pool) {
81 try {
82 while ((builder = pool.poll()) == null) {
83 pool.wait();
84 }
85 }
86 catch (InterruptedException ie) {
87 log.debug("Waiting for builder interrupted. Build canceled.");
88 return;
89 }
90 }
91 try {
92 builder.build(connections, output, parameters);
93 }
94 finally {
95 synchronized (pool) {
96 pool.add(builder);
97 pool.notify();
98 }
99 }
100 }
101 }
102 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org