comparison flys-aft/src/main/java/de/intevation/db/ConnectionBuilder.java @ 4773:f0b3ad10f67f

Added after login SQL statements for database connection. Can be used to alter the current schema to avoid naming clashes.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sat, 05 Jan 2013 13:30:57 +0100
parents f939e1e6cfa4
children d9f1202cef78
comparison
equal deleted inserted replaced
4772:f939e1e6cfa4 4773:f0b3ad10f67f
4 4
5 import java.sql.Connection; 5 import java.sql.Connection;
6 import java.sql.DatabaseMetaData; 6 import java.sql.DatabaseMetaData;
7 import java.sql.DriverManager; 7 import java.sql.DriverManager;
8 import java.sql.SQLException; 8 import java.sql.SQLException;
9 import java.sql.Statement;
9 10
11 import java.util.ArrayList;
10 import java.util.HashMap; 12 import java.util.HashMap;
13 import java.util.List;
11 14
12 import javax.xml.xpath.XPathConstants; 15 import javax.xml.xpath.XPathConstants;
13 16
14 import org.apache.log4j.Logger; 17 import org.apache.log4j.Logger;
15 18
16 import org.w3c.dom.Document; 19 import org.w3c.dom.Document;
20 import org.w3c.dom.Node;
21 import org.w3c.dom.NodeList;
17 22
18 public class ConnectionBuilder 23 public class ConnectionBuilder
19 { 24 {
20 private static Logger log = Logger.getLogger(ConnectionBuilder.class); 25 private static Logger log = Logger.getLogger(ConnectionBuilder.class);
21 26
22 public static final String XPATH_DRIVER = "/sync/side[@name=$type]/db/driver/text()"; 27 public static final String XPATH_DRIVER = "/sync/side[@name=$type]/db/driver/text()";
23 public static final String XPATH_USER = "/sync/side[@name=$type]/db/user/text()"; 28 public static final String XPATH_USER = "/sync/side[@name=$type]/db/user/text()";
24 public static final String XPATH_PASSWORD = "/sync/side[@name=$type]/db/password/text()"; 29 public static final String XPATH_PASSWORD = "/sync/side[@name=$type]/db/password/text()";
25 public static final String XPATH_URL = "/sync/side[@name=$type]/db/url/text()"; 30 public static final String XPATH_URL = "/sync/side[@name=$type]/db/url/text()";
31 public static final String XPATH_EXEC_LOGIN = "/sync/side[@name=$type]/db/execute-login/statement";
26 32
27 protected String type; 33 protected String type;
28 protected String driver; 34 protected String driver;
29 protected String user; 35 protected String user;
30 protected String password; 36 protected String password;
31 protected String url; 37 protected String url;
38 protected List<String> loginStatements;
32 39
33 public ConnectionBuilder(String type, Document document) { 40 public ConnectionBuilder(String type, Document document) {
34 this.type = type; 41 this.type = type;
35 extractCredentials(document); 42 extractCredentials(document);
43 }
44
45 protected static List<String> extractStrings(NodeList nodes) {
46 int N = nodes.getLength();
47 List<String> result = new ArrayList<String>(N);
48 for (int i = 0; i < N; ++i) {
49 result.add(nodes.item(i).getTextContent());
50 }
51 return result;
36 } 52 }
37 53
38 protected void extractCredentials(Document document) { 54 protected void extractCredentials(Document document) {
39 HashMap<String, String> map = new HashMap<String, String>(); 55 HashMap<String, String> map = new HashMap<String, String>();
40 map.put("type", type); 56 map.put("type", type);
45 document, XPATH_USER, XPathConstants.STRING, null, map); 61 document, XPATH_USER, XPathConstants.STRING, null, map);
46 password = (String)XML.xpath( 62 password = (String)XML.xpath(
47 document, XPATH_PASSWORD, XPathConstants.STRING, null, map); 63 document, XPATH_PASSWORD, XPathConstants.STRING, null, map);
48 url = (String)XML.xpath( 64 url = (String)XML.xpath(
49 document, XPATH_URL, XPathConstants.STRING, null, map); 65 document, XPATH_URL, XPathConstants.STRING, null, map);
66 loginStatements = extractStrings((NodeList)XML.xpath(
67 document, XPATH_EXEC_LOGIN, XPathConstants.NODESET, null, map));
50 68
51 if (log.isDebugEnabled()) { 69 if (log.isDebugEnabled()) {
52 log.debug("driver: " + driver); 70 log.debug("driver: " + driver);
53 log.debug("user: " + user); 71 log.debug("user: " + user);
54 log.debug("password: *******"); 72 log.debug("password: *******");
55 log.debug("url: " + url); 73 log.debug("url: " + url);
74 log.debug("number of login statements: " + loginStatements.size());
56 } 75 }
57 } 76 }
58 77
59 public Connection getConnection() throws SQLException { 78 public Connection getConnection() throws SQLException {
60 79
78 Connection.TRANSACTION_READ_UNCOMMITTED)) { 97 Connection.TRANSACTION_READ_UNCOMMITTED)) {
79 connection.setTransactionIsolation( 98 connection.setTransactionIsolation(
80 Connection.TRANSACTION_READ_UNCOMMITTED); 99 Connection.TRANSACTION_READ_UNCOMMITTED);
81 } 100 }
82 101
102 for (String sql: loginStatements) {
103 Statement stmnt = connection.createStatement();
104 try {
105 stmnt.execute(sql);
106 }
107 finally {
108 stmnt.close();
109 }
110 }
111
83 return connection; 112 return connection;
84 } 113 }
85 114
86 public ConnectedStatements getConnectedStatements() throws SQLException { 115 public ConnectedStatements getConnectedStatements() throws SQLException {
87 return new ConnectedStatements( 116 return new ConnectedStatements(

http://dive4elements.wald.intevation.org