sascha@4072: package de.intevation.db; sascha@4072: sascha@4072: import de.intevation.utils.XML; sascha@4072: sascha@4072: import java.util.HashMap; sascha@4072: sascha@4072: import org.w3c.dom.Document; sascha@4072: sascha@4072: import javax.xml.xpath.XPathConstants; sascha@4072: sascha@4072: import java.sql.Connection; sascha@4072: import java.sql.SQLException; sascha@4072: import java.sql.DriverManager; sascha@4072: sascha@4072: import org.apache.log4j.Logger; sascha@4072: sascha@4072: public class ConnectionBuilder sascha@4072: { sascha@4072: private static Logger log = Logger.getLogger(ConnectionBuilder.class); sascha@4072: sascha@4072: public static final String XPATH_DRIVER = "/sync/side[@name=$type]/db/driver/text()"; sascha@4072: public static final String XPATH_USER = "/sync/side[@name=$type]/db/user/text()"; sascha@4072: public static final String XPATH_PASSWORD = "/sync/side[@name=$type]/db/password/text()"; sascha@4072: public static final String XPATH_URL = "/sync/side[@name=$type]/db/url/text()"; sascha@4072: sascha@4072: protected String type; sascha@4072: protected String driver; sascha@4072: protected String user; sascha@4072: protected String password; sascha@4072: protected String url; sascha@4072: sascha@4072: public ConnectionBuilder(String type, Document document) { sascha@4072: this.type = type; sascha@4072: extractCredentials(document); sascha@4072: } sascha@4072: sascha@4072: protected void extractCredentials(Document document) { sascha@4072: HashMap map = new HashMap(); sascha@4072: map.put("type", type); sascha@4072: sascha@4072: driver = (String)XML.xpath( sascha@4072: document, XPATH_DRIVER, XPathConstants.STRING, null, map); sascha@4072: user = (String)XML.xpath( sascha@4072: document, XPATH_USER, XPathConstants.STRING, null, map); sascha@4072: password = (String)XML.xpath( sascha@4072: document, XPATH_PASSWORD, XPathConstants.STRING, null, map); sascha@4072: url = (String)XML.xpath( sascha@4072: document, XPATH_URL, XPathConstants.STRING, null, map); sascha@4072: sascha@4072: if (log.isDebugEnabled()) { sascha@4072: log.debug("driver: " + driver); sascha@4072: log.debug("user: " + user); sascha@4072: log.debug("password: *******"); sascha@4072: log.debug("url: " + url); sascha@4072: } sascha@4072: } sascha@4072: sascha@4072: public Connection getConnection() throws SQLException { sascha@4072: sascha@4072: if (driver != null && driver.length() > 0) { sascha@4072: try { sascha@4072: Class.forName(driver); sascha@4072: } sascha@4072: catch (ClassNotFoundException cnfe) { sascha@4072: throw new SQLException(cnfe); sascha@4072: } sascha@4072: } sascha@4072: sascha@4072: return DriverManager.getConnection(url, user, password); sascha@4072: } sascha@4072: } sascha@4072: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :