comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/BuilderPool.java @ 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.
author Sascha L. Teichmann <teichmann@intevation.de>
date Mon, 22 Apr 2013 17:01:17 +0200
parents ebec12def170
children
comparison
equal deleted inserted replaced
5784:efbbfe32e9fe 5785:d38004f0c52f
5 import java.util.List; 5 import java.util.List;
6 import java.util.Map; 6 import java.util.Map;
7 7
8 import java.sql.SQLException; 8 import java.sql.SQLException;
9 9
10 import javax.xml.transform.Transformer; 10 import javax.xml.parsers.DocumentBuilder;
11 import javax.xml.transform.TransformerConfigurationException; 11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.transform.TransformerException; 12 import javax.xml.parsers.ParserConfigurationException;
13 import javax.xml.transform.TransformerFactory;
14
15 import javax.xml.transform.dom.DOMResult;
16 import javax.xml.transform.dom.DOMSource;
17 13
18 import org.apache.log4j.Logger; 14 import org.apache.log4j.Logger;
19 15
20 import org.w3c.dom.Document; 16 import org.w3c.dom.Document;
21 import org.w3c.dom.Node; 17 import org.w3c.dom.Node;
44 log.debug("Create build pool with " + poolSize + " elements."); 40 log.debug("Create build pool with " + poolSize + " elements.");
45 } 41 }
46 42
47 pool = new ArrayDeque<Builder>(poolSize); 43 pool = new ArrayDeque<Builder>(poolSize);
48 for (int i = 0; i < poolSize; ++i) { 44 for (int i = 0; i < poolSize; ++i) {
49 Document doc = i > 0 // Clone all but the first. 45 pool.add(new Builder(cloneDocument(document)));
50 ? cloneDocument(document)
51 : document;
52 pool.add(new Builder(doc));
53 } 46 }
54 } 47 }
55 48
56 private final static Document cloneDocument(Document document) { 49 private final static Document cloneDocument(Document document) {
50
57 try { 51 try {
58 TransformerFactory tfactory = TransformerFactory.newInstance(); 52 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
59 Transformer xformer = tfactory.newTransformer(); 53 DocumentBuilder db = dbf.newDocumentBuilder();
60 DOMSource src = new DOMSource(document); 54
61 DOMResult dst = new DOMResult(); 55 Node origRoot = document.getDocumentElement();
62 xformer.transform(src, dst); 56
63 return (Document)dst.getNode(); 57 Document copy = db.newDocument();
58 Node copyRoot = copy.importNode(origRoot, true);
59 copy.appendChild(copyRoot);
60
61 return copy;
64 } 62 }
65 catch (TransformerConfigurationException tce) { 63 catch (ParserConfigurationException pce) {
66 log.error(tce); 64 log.error(pce);
67 }
68 catch (TransformerException te) {
69 log.error(te);
70 } 65 }
71 66
72 log.error( 67 log.error("Returning original document. This will lead to threading issues.");
73 "Returning original DOM document. " +
74 "This will result in threading errors!");
75 68
76 return document; 69 return document;
77 } 70 }
78 71
79 public void build( 72 public void build(

http://dive4elements.wald.intevation.org