Mercurial > dive4elements > river
comparison etl/src/main/java/org/dive4elements/river/etl/db/ConnectionBuilder.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-aft/src/main/java/org/dive4elements/river/etl/db/ConnectionBuilder.java@9438e9259213 |
children | 8bd9b551456c |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.etl.db; | |
2 | |
3 import org.dive4elements.river.etl.utils.XML; | |
4 | |
5 import java.sql.Connection; | |
6 import java.sql.DatabaseMetaData; | |
7 import java.sql.DriverManager; | |
8 import java.sql.SQLException; | |
9 import java.sql.Statement; | |
10 | |
11 import java.util.ArrayList; | |
12 import java.util.HashMap; | |
13 import java.util.List; | |
14 | |
15 import javax.xml.xpath.XPathConstants; | |
16 | |
17 import org.apache.log4j.Logger; | |
18 | |
19 import org.w3c.dom.Document; | |
20 import org.w3c.dom.NodeList; | |
21 | |
22 public class ConnectionBuilder | |
23 { | |
24 private static Logger log = Logger.getLogger(ConnectionBuilder.class); | |
25 | |
26 public static final String XPATH_DRIVER = "/sync/side[@name=$type]/db/driver/text()"; | |
27 public static final String XPATH_USER = "/sync/side[@name=$type]/db/user/text()"; | |
28 public static final String XPATH_PASSWORD = "/sync/side[@name=$type]/db/password/text()"; | |
29 public static final String XPATH_URL = "/sync/side[@name=$type]/db/url/text()"; | |
30 public static final String XPATH_EXEC_LOGIN = "/sync/side[@name=$type]/db/execute-login/statement"; | |
31 | |
32 protected String type; | |
33 protected String driver; | |
34 protected String user; | |
35 protected String password; | |
36 protected String url; | |
37 protected List<String> loginStatements; | |
38 | |
39 public ConnectionBuilder(String type, Document document) { | |
40 this.type = type; | |
41 extractCredentials(document); | |
42 } | |
43 | |
44 protected static List<String> extractStrings(NodeList nodes) { | |
45 int N = nodes.getLength(); | |
46 List<String> result = new ArrayList<String>(N); | |
47 for (int i = 0; i < N; ++i) { | |
48 result.add(nodes.item(i).getTextContent()); | |
49 } | |
50 return result; | |
51 } | |
52 | |
53 protected void extractCredentials(Document document) { | |
54 HashMap<String, String> map = new HashMap<String, String>(); | |
55 map.put("type", type); | |
56 | |
57 driver = (String)XML.xpath( | |
58 document, XPATH_DRIVER, XPathConstants.STRING, null, map); | |
59 user = (String)XML.xpath( | |
60 document, XPATH_USER, XPathConstants.STRING, null, map); | |
61 password = (String)XML.xpath( | |
62 document, XPATH_PASSWORD, XPathConstants.STRING, null, map); | |
63 url = (String)XML.xpath( | |
64 document, XPATH_URL, XPathConstants.STRING, null, map); | |
65 loginStatements = extractStrings((NodeList)XML.xpath( | |
66 document, XPATH_EXEC_LOGIN, XPathConstants.NODESET, null, map)); | |
67 | |
68 if (log.isDebugEnabled()) { | |
69 log.debug("driver: " + driver); | |
70 log.debug("user: " + user); | |
71 log.debug("password: *******"); | |
72 log.debug("url: " + url); | |
73 log.debug("number of login statements: " + loginStatements.size()); | |
74 } | |
75 } | |
76 | |
77 public Connection getConnection() throws SQLException { | |
78 | |
79 if (driver != null && driver.length() > 0) { | |
80 try { | |
81 Class.forName(driver); | |
82 } | |
83 catch (ClassNotFoundException cnfe) { | |
84 throw new SQLException(cnfe); | |
85 } | |
86 } | |
87 | |
88 Connection connection = | |
89 DriverManager.getConnection(url, user, password); | |
90 | |
91 connection.setAutoCommit(false); | |
92 | |
93 DatabaseMetaData metaData = connection.getMetaData(); | |
94 | |
95 if (metaData.supportsTransactionIsolationLevel( | |
96 Connection.TRANSACTION_READ_UNCOMMITTED)) { | |
97 connection.setTransactionIsolation( | |
98 Connection.TRANSACTION_READ_UNCOMMITTED); | |
99 } | |
100 | |
101 for (String sql: loginStatements) { | |
102 Statement stmnt = connection.createStatement(); | |
103 try { | |
104 stmnt.execute(sql); | |
105 } | |
106 finally { | |
107 stmnt.close(); | |
108 } | |
109 } | |
110 | |
111 return connection; | |
112 } | |
113 | |
114 public ConnectedStatements getConnectedStatements() throws SQLException { | |
115 return new ConnectedStatements( | |
116 getConnection(), | |
117 new Statements(type, driver != null ? driver : "") | |
118 .getStatements()); | |
119 } | |
120 } | |
121 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |