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

http://dive4elements.wald.intevation.org