Mercurial > dive4elements > river
comparison flys-aft/src/main/java/de/intevation/db/ConnectionBuilder.java @ 4072:88f801888d85
load configuration an setup db connections.
flys-aft/trunk@3401 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 13 Dec 2011 12:33:36 +0000 |
parents | |
children | 2c70fae83d0c |
comparison
equal
deleted
inserted
replaced
4071:0f5cc88a0f74 | 4072:88f801888d85 |
---|---|
1 package de.intevation.db; | |
2 | |
3 import de.intevation.utils.XML; | |
4 | |
5 import java.util.HashMap; | |
6 | |
7 import org.w3c.dom.Document; | |
8 | |
9 import javax.xml.xpath.XPathConstants; | |
10 | |
11 import java.sql.Connection; | |
12 import java.sql.SQLException; | |
13 import java.sql.DriverManager; | |
14 | |
15 import org.apache.log4j.Logger; | |
16 | |
17 public class ConnectionBuilder | |
18 { | |
19 private static Logger log = Logger.getLogger(ConnectionBuilder.class); | |
20 | |
21 public static final String XPATH_DRIVER = "/sync/side[@name=$type]/db/driver/text()"; | |
22 public static final String XPATH_USER = "/sync/side[@name=$type]/db/user/text()"; | |
23 public static final String XPATH_PASSWORD = "/sync/side[@name=$type]/db/password/text()"; | |
24 public static final String XPATH_URL = "/sync/side[@name=$type]/db/url/text()"; | |
25 | |
26 protected String type; | |
27 protected String driver; | |
28 protected String user; | |
29 protected String password; | |
30 protected String url; | |
31 | |
32 public ConnectionBuilder(String type, Document document) { | |
33 this.type = type; | |
34 extractCredentials(document); | |
35 } | |
36 | |
37 protected void extractCredentials(Document document) { | |
38 HashMap<String, String> map = new HashMap<String, String>(); | |
39 map.put("type", type); | |
40 | |
41 driver = (String)XML.xpath( | |
42 document, XPATH_DRIVER, XPathConstants.STRING, null, map); | |
43 user = (String)XML.xpath( | |
44 document, XPATH_USER, XPathConstants.STRING, null, map); | |
45 password = (String)XML.xpath( | |
46 document, XPATH_PASSWORD, XPathConstants.STRING, null, map); | |
47 url = (String)XML.xpath( | |
48 document, XPATH_URL, XPathConstants.STRING, null, map); | |
49 | |
50 if (log.isDebugEnabled()) { | |
51 log.debug("driver: " + driver); | |
52 log.debug("user: " + user); | |
53 log.debug("password: *******"); | |
54 log.debug("url: " + url); | |
55 } | |
56 } | |
57 | |
58 public Connection getConnection() throws SQLException { | |
59 | |
60 if (driver != null && driver.length() > 0) { | |
61 try { | |
62 Class.forName(driver); | |
63 } | |
64 catch (ClassNotFoundException cnfe) { | |
65 throw new SQLException(cnfe); | |
66 } | |
67 } | |
68 | |
69 return DriverManager.getConnection(url, user, password); | |
70 } | |
71 } | |
72 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |