Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/BuilderPool.java @ 7508:02841af5feff
cosmetics - full stop.
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Fri, 01 Nov 2013 16:41:16 +0100 |
parents | af13ceeba52a |
children | 5e38e2924c07 |
rev | line source |
---|---|
5863
4897a58c8746
River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde |
4897a58c8746
River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
2 * Software engineering by Intevation GmbH |
4897a58c8746
River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
3 * |
5994
af13ceeba52a
Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5863
diff
changeset
|
4 * This file is Free Software under the GNU AGPL (>=v3) |
5863
4897a58c8746
River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the |
5994
af13ceeba52a
Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5863
diff
changeset
|
6 * documentation coming with Dive4Elements River for details. |
5863
4897a58c8746
River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
7 */ |
4897a58c8746
River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
8 |
5831
bd047b71ab37
Repaired internal references
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5785
diff
changeset
|
9 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
|
10 |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
11 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
|
12 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
|
13 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
|
14 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
|
15 |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
16 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
|
17 |
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
|
18 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
|
19 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
|
20 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
|
21 |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
22 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
|
23 |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
24 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
|
25 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
|
26 |
5994
af13ceeba52a
Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5863
diff
changeset
|
27 /** A little round robin pool of builders to mitigate |
5779
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
28 * 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
|
29 */ |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
30 public class BuilderPool |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
31 { |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
32 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
|
33 |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
34 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
|
35 |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
36 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
|
37 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
|
38 |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
39 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
|
40 |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
41 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
|
42 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
|
43 } |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
44 |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
45 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
|
46 |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
47 if (log.isDebugEnabled()) { |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
48 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
|
49 } |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
50 |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
51 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
|
52 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
|
53 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
|
54 } |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
55 } |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
56 |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
57 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
|
58 |
5779
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
59 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
|
60 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
|
61 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
|
62 |
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 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
|
64 |
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
|
65 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
|
66 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
|
67 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
|
68 |
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
|
69 return copy; |
5779
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
70 } |
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
|
71 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
|
72 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
|
73 } |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
74 |
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
|
75 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
|
76 |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
77 return document; |
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 |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
80 public void build( |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
81 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
|
82 Node output, |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
83 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
|
84 ) |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
85 throws SQLException |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
86 { |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
87 Builder builder; |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
88 synchronized (pool) { |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
89 try { |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
90 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
|
91 pool.wait(); |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
92 } |
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 catch (InterruptedException ie) { |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
95 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
|
96 return; |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
97 } |
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 try { |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
100 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
|
101 } |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
102 finally { |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
103 synchronized (pool) { |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
104 pool.add(builder); |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
105 pool.notify(); |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
106 } |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
107 } |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
108 } |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
109 } |
ebec12def170
Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
110 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |