001: package com.bm.utils;
002:
003: import java.io.PrintWriter;
004: import java.sql.Connection;
005: import java.sql.DriverManager;
006: import java.sql.PreparedStatement;
007: import java.sql.SQLException;
008:
009: import javax.sql.DataSource;
010:
011: import com.bm.cfg.Ejb3UnitCfg;
012:
013: /**
014: * <p>
015: * Basic implementation of <code>javax.sql.DataSource</code> that is
016: * configured via Ejb3Prop properties.
017: *
018: * @author Daniel Wiese
019: */
020: public class BasicDataSource implements DataSource {
021:
022: private final Ejb3UnitCfg config;
023:
024: private PrintWriter logger = null;
025:
026: private int loginTimeout = 0;
027:
028: /**
029: * Constructor.
030: *
031: * @param config -
032: * die konfiguration.
033: */
034: public BasicDataSource(Ejb3UnitCfg config) {
035: this .config = config;
036: }
037:
038: /**
039: * The connection.
040: * @author Daniel Wiese
041: * @since 08.11.2005
042: * @return connection
043: * @see javax.sql.DataSource#getConnection()
044: */
045: public synchronized Connection getConnection() throws SQLException {
046: if (Ejb3UnitCfg.getConfiguration().isInMemory()) {
047: return this .getConnection("sa", "");
048: } else {
049: final String user = config
050: .getValue(Ejb3UnitCfg.KEY_CONNECTION_USERNAME);
051: final String pw = config
052: .getValue(Ejb3UnitCfg.KEY_CONNECTION_PASSWORD);
053: return this .getConnection(user, pw);
054: }
055: }
056:
057: /**
058: * {@inheritDoc}
059: */
060: public synchronized Connection getConnection(String username,
061: String password) throws SQLException {
062: try {
063: if (Ejb3UnitCfg.getConfiguration().isInMemory()) {
064: Class.forName("org.hsqldb.jdbcDriver");
065: final Connection cn = DriverManager.getConnection(
066: "jdbc:hsqldb:mem:ejb3unit", username, password);
067: return cn;
068: } else {
069: Class
070: .forName(config
071: .getValue(Ejb3UnitCfg.KEY_CONNECTION_DRIVER_CLASS));
072: final Connection cn = DriverManager
073: .getConnection(
074: config
075: .getValue(Ejb3UnitCfg.KEY_CONNECTION_URL),
076: username, password);
077: return cn;
078: }
079: } catch (ClassNotFoundException e) {
080: throw new RuntimeException(e);
081: } catch (SQLException e) {
082: throw new RuntimeException(e);
083: }
084: }
085:
086: /**
087: * {@inheritDoc}
088: */
089: public PrintWriter getLogWriter() throws SQLException {
090: return this .logger;
091: }
092:
093: /**
094: * {@inheritDoc}
095: */
096: public void setLogWriter(PrintWriter logger) throws SQLException {
097: this .logger = logger;
098:
099: }
100:
101: /**
102: * {@inheritDoc}
103: */
104: public void setLoginTimeout(int arg0) throws SQLException {
105: this .loginTimeout = arg0;
106:
107: }
108:
109: /**
110: * {@inheritDoc}
111: */
112: public int getLoginTimeout() throws SQLException {
113: return this .loginTimeout;
114: }
115:
116: /**
117: * If the datasource is in memory this will shutdown the database.
118: */
119: public void shutdownInMemoryDatabase() throws Exception {
120: Connection con = null;
121: PreparedStatement ps = null;
122: try {
123: con = this .getConnection();
124: ps = con.prepareStatement("SHUTDOWN");
125: ps.execute();
126: } finally {
127: SQLUtils.cleanup(con, ps);
128: }
129: }
130:
131: /**
132: * Does not wrap anything.
133: * @param forInterface for which interface
134: * @return always false
135: */
136: public boolean isWrapperFor(Class<?> forInterface)
137: throws SQLException {
138: return false;
139: }
140:
141: /**
142: * {@inheritDoc}
143: */
144: public <T> T unwrap(Class<T> arg0) throws SQLException {
145: return null;
146: }
147:
148: }
|