001: package com.ecyrd.jspwiki;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.PrintWriter;
006: import java.net.URL;
007: import java.net.URLClassLoader;
008: import java.security.AccessController;
009: import java.security.PrivilegedAction;
010: import java.sql.Connection;
011: import java.sql.Driver;
012: import java.sql.SQLException;
013: import java.util.Properties;
014:
015: import javax.sql.DataSource;
016:
017: /**
018: * Mock JDBC DataSource class that manages JDBC connections to a database whose
019: * driver class, JDBC JAR location and connection details are specified in an
020: * arbitrary propreties file. Gemerally, we pass on any exceptions encountered
021: * as unchecked, since it means that the test case that references this class is
022: * failing somehow.
023: * @author Andrew R. Jaquith
024: */
025: public class TestJDBCDataSource implements DataSource {
026: private static Driver m_driver;
027:
028: protected static final String PROPERTY_DRIVER_CLASS = "jdbc.driver.class";
029:
030: protected static final String PROPERTY_DRIVER_JAR = "jdbc.driver.jar";
031:
032: protected static final String PROPERTY_DRIVER_URL = "jdbc.driver.url";
033:
034: protected static final String PROPERTY_USER_ID = "jdbc.user.id";
035:
036: protected static final String PROPERTY_USER_PASSWORD = "jdbc.user.password";
037:
038: protected String m_jdbcPassword = null;
039:
040: protected String m_jdbcURL = null;
041:
042: protected String m_jdbcUser = null;
043:
044: protected int m_timeout = 0;
045:
046: protected PrintWriter m_writer = null;
047:
048: /**
049: * Constructs a new instance of this class, using a supplied properties
050: * File as the source for JDBC driver properties.
051: * @param file the properties file containing JDBC properties
052: * @throws Exception
053: */
054: public TestJDBCDataSource(File file) throws Exception {
055: super ();
056: initializeJDBC(file);
057: }
058:
059: /**
060: * Returns a JDBC connection using the specified username and password.
061: * @return the database connection
062: * @see javax.sql.DataSource#getConnection()
063: */
064: public Connection getConnection() throws SQLException {
065: return getConnection(m_jdbcUser, m_jdbcPassword);
066: }
067:
068: /**
069: * Returns a JDBC connection to the database.
070: * @return the database connection
071: * @see javax.sql.DataSource#getConnection(java.lang.String,
072: * java.lang.String)
073: */
074: public Connection getConnection(String username, String password)
075: throws SQLException {
076: Properties connProperties = new Properties();
077: connProperties.put("user", m_jdbcUser);
078: connProperties.put("password", m_jdbcPassword);
079: Connection connection = m_driver.connect(m_jdbcURL,
080: connProperties);
081: return connection;
082: }
083:
084: /**
085: * Returns the login timeout for the data source.
086: * @return the login timeout, in seconds
087: * @see javax.sql.DataSource#getLoginTimeout()
088: */
089: public int getLoginTimeout() throws SQLException {
090: return m_timeout;
091: }
092:
093: /**
094: * Returns the log writer for the data source.
095: * @return the log writer
096: * @see javax.sql.DataSource#getLogWriter()
097: */
098: public PrintWriter getLogWriter() throws SQLException {
099: return m_writer;
100: }
101:
102: /**
103: * Sets the login timeout for the data source. Doesn't do anything, really.
104: * @param seconds the login timeout, in seconds
105: * @see javax.sql.DataSource#setLoginTimeout(int)
106: */
107: public void setLoginTimeout(int seconds) throws SQLException {
108: this .m_timeout = seconds;
109: }
110:
111: /**
112: * Sets the log writer for the data source. Isn't used for anything, really.
113: * @param out the log writer
114: * @see javax.sql.DataSource#setLogWriter(java.io.PrintWriter)
115: */
116: public void setLogWriter(PrintWriter out) throws SQLException {
117: this .m_writer = out;
118: }
119:
120: /**
121: * Initialization method that reads a File, and attempts to locate and load
122: * the JDBC driver from properties specified therein.
123: * @throws SQLException
124: * @param file the file containing the JDBC properties
125: */
126: protected void initializeJDBC(File file) throws Exception {
127: // Load the properties JDBC properties file
128: Properties properties;
129: properties = new Properties();
130: FileInputStream is = new FileInputStream(file);
131: properties.load(is);
132: is.close();
133: m_jdbcURL = properties.getProperty(PROPERTY_DRIVER_URL);
134: m_jdbcUser = properties.getProperty(PROPERTY_USER_ID);
135: m_jdbcPassword = properties.getProperty(PROPERTY_USER_PASSWORD);
136:
137: // Identifiy the class and JAR we need to load
138: String clazz = properties.getProperty(PROPERTY_DRIVER_CLASS);
139: String driverFile = properties.getProperty(PROPERTY_DRIVER_JAR);
140:
141: // Construct an URL for loading the file
142: final URL driverURL = new URL("file:" + driverFile);
143:
144: // Load the driver using the sytem class loader
145: final ClassLoader parent = ClassLoader.getSystemClassLoader();
146: URLClassLoader loader = (URLClassLoader) AccessController
147: .doPrivileged(new PrivilegedAction() {
148: public Object run() {
149: return new URLClassLoader(
150: new URL[] { driverURL }, parent);
151: }
152: });
153: Class driverClass = loader.loadClass(clazz);
154:
155: // Cache the driver
156: m_driver = (Driver) driverClass.newInstance();
157: }
158:
159: }
|