1 package org.jahia.tools.migration; 2 3 import org.w3c.dom.Element ; 4 import org.w3c.dom.NodeList ; 5 6 import java.sql.Connection ; 7 import java.sql.SQLException ; 8 9 import org.apache.commons.dbcp.BasicDataSource; 10 11 17 public class DBConnector { 18 19 private static org.apache.log4j.Logger logger = 20 org.apache.log4j.Logger.getLogger (DBConnector.class); 21 22 private static final String DB_SERVER_ROOT_TAG = "db-server"; 24 private static final String DB_SERVER_URL_TAG = "url"; 25 private static final String DB_SERVER_USERNAME_TAG = "username"; 26 private static final String DB_SERVER_PASSWORD_TAG = "password"; 27 private static final String DB_SERVER_DRIVER_TAG = "driver"; 28 29 private BasicDataSource bds; 31 32 private boolean initialized = false; 34 35 38 public DBConnector(Element rootElement) { 39 try { 40 initialized = init(rootElement); 41 } catch (Exception e) {} 42 } 43 44 49 public boolean isInitialized() { 50 return initialized; 51 } 52 53 private boolean init(Element rootElement) throws Exception { 54 logger.info("intialization started"); 55 boolean result = false; 56 57 NodeList serversList = rootElement.getElementsByTagName (DB_SERVER_ROOT_TAG); 58 59 if (serversList.getLength () != 1) 60 result = false; 61 else { 62 bds = getDataSourceFromDOM((Element )serversList.item(0)); 63 result = true; 64 } 65 66 logger.info("intialization ended"); 67 return result; 68 } 69 70 private BasicDataSource getDataSourceFromDOM (Element element) throws Exception { 71 BasicDataSource result = new BasicDataSource(); 72 73 result.setDriverClassName(XMLUtility.getElementValue(DB_SERVER_DRIVER_TAG, element)); 74 result.setUsername(XMLUtility.getElementValue(DB_SERVER_USERNAME_TAG, element)); 75 result.setPassword(XMLUtility.getElementValue(DB_SERVER_PASSWORD_TAG, element)); 76 result.setUrl(XMLUtility.getElementValue(DB_SERVER_URL_TAG, element)); 77 78 return result; 79 } 80 81 88 public Connection getConnection() throws SQLException { 89 return bds.getConnection(); 90 } 91 } | Popular Tags |