KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > tools > migration > DBConnector


1 package org.jahia.tools.migration;
2
3 import org.w3c.dom.Element JavaDoc;
4 import org.w3c.dom.NodeList JavaDoc;
5
6 import java.sql.Connection JavaDoc;
7 import java.sql.SQLException JavaDoc;
8
9 import org.apache.commons.dbcp.BasicDataSource;
10
11 /**
12  * This class will handle the connection to the Jahia database to migrate
13  *
14  * @author PELTIER Olivier
15  * @version 1.0
16  */

17 public class DBConnector {
18
19     private static org.apache.log4j.Logger logger =
20             org.apache.log4j.Logger.getLogger (DBConnector.class);
21             
22     // Strings to get parameters from xml Element
23
private static final String JavaDoc DB_SERVER_ROOT_TAG = "db-server";
24     private static final String JavaDoc DB_SERVER_URL_TAG = "url";
25     private static final String JavaDoc DB_SERVER_USERNAME_TAG = "username";
26     private static final String JavaDoc DB_SERVER_PASSWORD_TAG = "password";
27     private static final String JavaDoc DB_SERVER_DRIVER_TAG = "driver";
28     
29     // the internal datasource for DB access
30
private BasicDataSource bds;
31         
32     // initilization status
33
private boolean initialized = false;
34
35     /**
36      * Default Constructor.
37      */

38     public DBConnector(Element JavaDoc rootElement) {
39         try {
40             initialized = init(rootElement);
41         } catch (Exception JavaDoc e) {}
42     }
43     
44     /**
45      * Return the initalization status.
46      *
47      * @return boolean.
48      */

49     public boolean isInitialized() {
50         return initialized;
51     }
52     
53     private boolean init(Element JavaDoc rootElement) throws Exception JavaDoc {
54         logger.info("intialization started");
55         boolean result = false;
56         
57         NodeList JavaDoc serversList = rootElement.getElementsByTagName (DB_SERVER_ROOT_TAG);
58
59         if (serversList.getLength () != 1)
60             result = false;
61         else {
62             bds = getDataSourceFromDOM((Element JavaDoc)serversList.item(0));
63             result = true;
64         }
65         
66         logger.info("intialization ended");
67         return result;
68     }
69     
70     private BasicDataSource getDataSourceFromDOM (Element JavaDoc element) throws Exception JavaDoc {
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     /**
82      * Return a connection to the DB through datasource API.
83      *
84      * @return Connection.
85      *
86      * @throws SQLException.
87      */

88     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
89         return bds.getConnection();
90     }
91 }
Popular Tags