001: package helpers;
002:
003: import java.net.URL;
004: import java.sql.Connection;
005: import java.sql.DriverManager;
006: import java.sql.SQLException;
007: import java.util.HashMap;
008: import java.util.Iterator;
009: import java.util.Map;
010: import javax.xml.bind.JAXBContext;
011: import javax.xml.bind.Unmarshaller;
012: import org.jaffa.config.Config;
013: import org.jaffa.persistence.engines.jdbcengine.configservice.initdomain.Database;
014: import org.jaffa.persistence.engines.jdbcengine.configservice.initdomain.Init;
015: import org.jaffa.persistence.engines.jdbcengine.configservice.initdomain.Param;
016: import org.jaffa.util.URLHelper;
017: import org.jaffa.util.XmlHelper;
018:
019: /*
020: * ConnectionHelper.java
021: *
022: * Created on March 1, 2004, 11:55 AM
023: */
024:
025: /**
026: *
027: * @author PaulE
028: */
029: public class ConnectionHelper {
030:
031: static final String ENGINE = "engine";
032: static final String URL = "url";
033: static final String DRIVER_CLASS = "driverClass";
034: static final String USER = "user";
035: static final String PASSWORD = "password";
036: static final String MAXIMUM_CONNECTIONS = "maximumConnections";
037:
038: public static Connection getConnection() throws Exception {
039: Map info = ConnectionHelper.getDatabaseInfo();
040: return ConnectionHelper.getConnection((String) info
041: .get(DRIVER_CLASS), (String) info.get(URL),
042: (String) info.get(USER), (String) info.get(PASSWORD));
043: }
044:
045: private static Map getDatabaseInfo() throws Exception {
046: URL initUrl = URLHelper.newExtendedURL((String) Config
047: .getProperty(Config.PROP_JDBC_ENGINE_INIT));
048:
049: // create a JAXBContext capable of handling classes generated into the package
050: JAXBContext jc = JAXBContext
051: .newInstance("org.jaffa.persistence.engines.jdbcengine.configservice.initdomain");
052:
053: // create an Unmarshaller
054: Unmarshaller u = jc.createUnmarshaller();
055:
056: // enable validation
057: u.setValidating(true);
058:
059: // unmarshal a document into a tree of Java content objects composed of classes from the package.
060: Init init = (Init) u.unmarshal(XmlHelper
061: .stripDoctypeDeclaration(initUrl));
062: Database database = null;
063: for (Iterator i = init.getDatabase().iterator(); i.hasNext();) {
064: database = (Database) i.next();
065: if (database.getName().equals("default"))
066: break;
067: }
068:
069: if (database == null)
070: throw new Exception(
071: "The 'default' database has not been defined in init.xml ");
072:
073: Map info = new HashMap();
074: info.put(ENGINE, database.getEngine());
075: for (Iterator i = database.getConnectionFactory().getParam()
076: .iterator(); i.hasNext();) {
077: Param param = (Param) i.next();
078: if (URL.equals(param.getName()))
079: info.put(URL, param.getValue());
080: else if (DRIVER_CLASS.equals(param.getName()))
081: info.put(DRIVER_CLASS, param.getValue());
082: else if (USER.equals(param.getName()))
083: info.put(USER, param.getValue());
084: else if (PASSWORD.equals(param.getName()))
085: info.put(PASSWORD, param.getValue());
086: else if (MAXIMUM_CONNECTIONS.equals(param.getName()))
087: info.put(MAXIMUM_CONNECTIONS, param.getValue());
088: }
089: return info;
090: }
091:
092: private static Connection getConnection(String driverClass,
093: String url, String user, String password)
094: throws ClassNotFoundException, SQLException {
095: Class.forName(driverClass);
096: Connection connection = DriverManager.getConnection(url, user,
097: password);
098: connection.setAutoCommit(false);
099: return connection;
100: }
101:
102: }
|