annotate artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/BuilderPool.java @ 9801:1d7a72a50183 3.2.x tip

Assume Compose V2, consistently
author Tom Gottfried <tom@intevation.de>
date Thu, 23 Nov 2023 10:14:13 +0100
parents 0a5239a1e46e
children
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
9726
0a5239a1e46e Upgrade to Log4j 2
Tom Gottfried <tom@intevation.de>
parents: 8856
diff changeset
22 import org.apache.logging.log4j.Logger;
0a5239a1e46e Upgrade to Log4j 2
Tom Gottfried <tom@intevation.de>
parents: 8856
diff changeset
23 import org.apache.logging.log4j.LogManager;
5779
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
24
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.Document;
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
26 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
27
5994
af13ceeba52a Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5863
diff changeset
28 /** 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
29 * 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
30 */
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
31 public class BuilderPool
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
32 {
9726
0a5239a1e46e Upgrade to Log4j 2
Tom Gottfried <tom@intevation.de>
parents: 8856
diff changeset
33 private static Logger log = LogManager.getLogger(BuilderPool.class);
5779
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
34
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
35 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
36
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
37 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
38 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
39
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
40 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
41
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
42 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
43 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
44 }
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
45
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
46 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
47
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
48 if (log.isDebugEnabled()) {
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
49 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
50 }
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
51
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
52 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
53 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
54 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
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
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
58 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
59
5779
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
60 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
61 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
62 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
63
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 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
65
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 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
67 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
68 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
69
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
70 return copy;
5779
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
71 }
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
72 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
73 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
74 }
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
75
8856
5e38e2924c07 Fix code style.
Tom Gottfried <tom@intevation.de>
parents: 5994
diff changeset
76 log.error("Returning original document. "
5e38e2924c07 Fix code style.
Tom Gottfried <tom@intevation.de>
parents: 5994
diff changeset
77 + "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
78
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
79 return document;
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
80 }
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
81
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
82 public void build(
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
83 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
84 Node output,
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
85 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
86 )
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
87 throws SQLException
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
88 {
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
89 Builder builder;
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
90 synchronized (pool) {
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 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
93 pool.wait();
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
94 }
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
95 }
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
96 catch (InterruptedException ie) {
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
97 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
98 return;
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 try {
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
102 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
103 }
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
104 finally {
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
105 synchronized (pool) {
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
106 pool.add(builder);
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
107 pool.notify();
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 }
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
111 }
ebec12def170 Datacage: Add a pool of builders to make it multi threadable.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
112 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org