Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/BuilderPool.java @ 5784:efbbfe32e9fe
Fix dateFormat for Oracle by invoking dateValue on demand
Oracle does not return a date object but a oracle.sql.TIMESTAMP
object when querying a date. We now convert to a java date
by inviking the method dateValue if it is available.
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Mon, 22 Apr 2013 16:18:16 +0200 |
parents | ebec12def170 |
children | d38004f0c52f |
line wrap: on
line source
package de.intevation.flys.artifacts.datacage.templating; import java.util.ArrayDeque; import java.util.Deque; import java.util.List; import java.util.Map; import java.sql.SQLException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; import org.apache.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.Node; /** A little round robin pool of builders to mitigate * the fact the XML DOM documents are not thread safe. */ public class BuilderPool { private static Logger log = Logger.getLogger(BuilderPool.class); private static final int DEFAULT_POOL_SIZE = 4; private static final int POOL_SIZE = Math.max( Integer.getInteger("flys.datacage.pool.size", DEFAULT_POOL_SIZE), 1); private Deque<Builder> pool; public BuilderPool(Document document) { this(document, POOL_SIZE); } public BuilderPool(Document document, int poolSize) { if (log.isDebugEnabled()) { log.debug("Create build pool with " + poolSize + " elements."); } pool = new ArrayDeque<Builder>(poolSize); for (int i = 0; i < poolSize; ++i) { Document doc = i > 0 // Clone all but the first. ? cloneDocument(document) : document; pool.add(new Builder(doc)); } } private final static Document cloneDocument(Document document) { try { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer xformer = tfactory.newTransformer(); DOMSource src = new DOMSource(document); DOMResult dst = new DOMResult(); xformer.transform(src, dst); return (Document)dst.getNode(); } catch (TransformerConfigurationException tce) { log.error(tce); } catch (TransformerException te) { log.error(te); } log.error( "Returning original DOM document. " + "This will result in threading errors!"); return document; } public void build( List<Builder.NamedConnection> connections, Node output, Map<String, Object> parameters ) throws SQLException { Builder builder; synchronized (pool) { try { while ((builder = pool.poll()) == null) { pool.wait(); } } catch (InterruptedException ie) { log.debug("Waiting for builder interrupted. Build canceled."); return; } } try { builder.build(connections, output, parameters); } finally { synchronized (pool) { pool.add(builder); pool.notify(); } } } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :