Mercurial > dive4elements > river
annotate flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/BuilderPool.java @ 5800:397ff862b417
WOutProcessor: Renaming, doc.
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Wed, 24 Apr 2013 08:24:56 +0200 |
parents | d38004f0c52f |
children |
rev | line source |
---|---|
5779
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.flys.artifacts.datacage.templating; |
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 : |