# HG changeset patch # User Sascha L. Teichmann # Date 1323779616 0 # Node ID 88f801888d85ec21af43408f9aeb8db0ef261bad # Parent 0f5cc88a0f7436d1a10b9822c0a94a515e756569 load configuration an setup db connections. flys-aft/trunk@3401 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 0f5cc88a0f74 -r 88f801888d85 flys-aft/ChangeLog --- a/flys-aft/ChangeLog Tue Dec 13 10:11:23 2011 +0000 +++ b/flys-aft/ChangeLog Tue Dec 13 12:33:36 2011 +0000 @@ -1,3 +1,17 @@ +2011-12-13 Sascha L. Teichmann + + * doc/conf.xml: Adjusted structure to be more generic. + + * src/main/java/de/intevation/utils/XML.java: Allow namespace aware + file parsing. + + * src/main/java/de/intevation/db/ConnectionBuilder.java: New. Evaluate + config and builds a new db connection. + + * src/main/java/de/intevation/aft/Sync.java: Load config file. + + * pom.xml: Added dependency to SQLite JDBC driver. + 2011-12-13 Sascha L. Teichmann * doc/conf.xml: New. Configuration file. diff -r 0f5cc88a0f74 -r 88f801888d85 flys-aft/doc/conf.xml --- a/flys-aft/doc/conf.xml Tue Dec 13 10:11:23 2011 +0000 +++ b/flys-aft/doc/conf.xml Tue Dec 13 12:33:36 2011 +0000 @@ -1,21 +1,21 @@ - + + + org.postgresql.Driver + flys + flys + jdbc:postgresql://localhost:5432/flys + + + + - + jdbc:sqlite:/path/to/aft.db - - - - - - - - - - + diff -r 0f5cc88a0f74 -r 88f801888d85 flys-aft/pom.xml --- a/flys-aft/pom.xml Tue Dec 13 10:11:23 2011 +0000 +++ b/flys-aft/pom.xml Tue Dec 13 12:33:36 2011 +0000 @@ -41,5 +41,10 @@ log4j 1.2.14 + + org.xerial + sqlite-jdbc + 3.7.2 + diff -r 0f5cc88a0f74 -r 88f801888d85 flys-aft/src/main/java/de/intevation/aft/Sync.java --- a/flys-aft/src/main/java/de/intevation/aft/Sync.java Tue Dec 13 10:11:23 2011 +0000 +++ b/flys-aft/src/main/java/de/intevation/aft/Sync.java Tue Dec 13 12:33:36 2011 +0000 @@ -1,8 +1,43 @@ package de.intevation.aft; +import java.io.File; + +import org.w3c.dom.Document; + +import de.intevation.utils.XML; + +import de.intevation.db.ConnectionBuilder; + public class Sync { + public static final String FLYS = "flys"; + public static final String AFT = "aft"; + + public static final String CONFIG_FILE = + System.getProperty("config.file", "config.xml"); + public static void main(String [] args) { + + File configFile = new File(CONFIG_FILE); + + if (!configFile.isFile() || !configFile.canRead()) { + System.err.println("cannot read config file"); + System.exit(1); + } + + Document config = XML.parseDocument(configFile, Boolean.FALSE); + + if (config == null) { + System.err.println("cannot load config"); + System.exit(1); + } + + ConnectionBuilder flysConnectionBuilder = + new ConnectionBuilder(FLYS, config); + + ConnectionBuilder aftConnectionBuilder = + new ConnectionBuilder(AFT, config); + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 0f5cc88a0f74 -r 88f801888d85 flys-aft/src/main/java/de/intevation/db/ConnectionBuilder.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-aft/src/main/java/de/intevation/db/ConnectionBuilder.java Tue Dec 13 12:33:36 2011 +0000 @@ -0,0 +1,72 @@ +package de.intevation.db; + +import de.intevation.utils.XML; + +import java.util.HashMap; + +import org.w3c.dom.Document; + +import javax.xml.xpath.XPathConstants; + +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.DriverManager; + +import org.apache.log4j.Logger; + +public class ConnectionBuilder +{ + private static Logger log = Logger.getLogger(ConnectionBuilder.class); + + public static final String XPATH_DRIVER = "/sync/side[@name=$type]/db/driver/text()"; + public static final String XPATH_USER = "/sync/side[@name=$type]/db/user/text()"; + public static final String XPATH_PASSWORD = "/sync/side[@name=$type]/db/password/text()"; + public static final String XPATH_URL = "/sync/side[@name=$type]/db/url/text()"; + + protected String type; + protected String driver; + protected String user; + protected String password; + protected String url; + + public ConnectionBuilder(String type, Document document) { + this.type = type; + extractCredentials(document); + } + + protected void extractCredentials(Document document) { + HashMap map = new HashMap(); + map.put("type", type); + + driver = (String)XML.xpath( + document, XPATH_DRIVER, XPathConstants.STRING, null, map); + user = (String)XML.xpath( + document, XPATH_USER, XPathConstants.STRING, null, map); + password = (String)XML.xpath( + document, XPATH_PASSWORD, XPathConstants.STRING, null, map); + url = (String)XML.xpath( + document, XPATH_URL, XPathConstants.STRING, null, map); + + if (log.isDebugEnabled()) { + log.debug("driver: " + driver); + log.debug("user: " + user); + log.debug("password: *******"); + log.debug("url: " + url); + } + } + + public Connection getConnection() throws SQLException { + + if (driver != null && driver.length() > 0) { + try { + Class.forName(driver); + } + catch (ClassNotFoundException cnfe) { + throw new SQLException(cnfe); + } + } + + return DriverManager.getConnection(url, user, password); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 0f5cc88a0f74 -r 88f801888d85 flys-aft/src/main/java/de/intevation/utils/XML.java --- a/flys-aft/src/main/java/de/intevation/utils/XML.java Tue Dec 13 10:11:23 2011 +0000 +++ b/flys-aft/src/main/java/de/intevation/utils/XML.java Tue Dec 13 12:33:36 2011 +0000 @@ -69,10 +69,14 @@ * during loading. */ public static final Document parseDocument(File file) { + return parseDocument(file, Boolean.TRUE); + } + + public static final Document parseDocument(File file, Boolean namespaceAware) { InputStream inputStream = null; try { inputStream = new BufferedInputStream(new FileInputStream(file)); - return parseDocument(inputStream); + return parseDocument(inputStream, namespaceAware); } catch (IOException ioe) { log.error(ioe.getLocalizedMessage(), ioe);