1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springbyexample.jdbc.datasource;
18
19 import java.sql.Driver;
20
21 import org.springbyexample.jdbc.core.SqlScriptProcessor;
22 import org.springframework.beans.factory.InitializingBean;
23 import org.springframework.jdbc.datasource.SimpleDriverDataSource;
24 import org.springframework.util.Assert;
25 import org.springframework.util.ClassUtils;
26 import org.springframework.util.StringUtils;
27
28
29 /**
30 * Initializing version of <code>DriverManagerDataSource</code>.
31 * After the properties are set any database initialization scripts are run.
32 * This is very useful for unit testing.
33 *
34 * @author David Winterfeldt
35 *
36 * @see org.springframework.jdbc.datasource.SimpleDriverDataSource
37 * @see org.springbyexample.jdbc.core.SqlScriptProcessor
38 */
39 public class InitializingDriverManagerDataSource extends SimpleDriverDataSource
40 implements InitializingBean {
41
42 protected String driverClassName = null;
43 protected SqlScriptProcessor sqlScriptProcessor = null;
44
45 /**
46 * Sets driver class name. The class should implement
47 * <code>java.sql.Driver</code>. This is a shortcut for
48 * calling <code>setDriver(Driver driver)</code> on the parent class.
49 */
50 public void setDriverClassName(String driverClassName) {
51 this.driverClassName = driverClassName;
52 }
53
54 /**
55 * Sets SQL script processor.
56 */
57 public void setSqlScriptProcessor(SqlScriptProcessor sqlScriptProcessor) {
58 this.sqlScriptProcessor = sqlScriptProcessor;
59 }
60
61 /**
62 * Implementation of <code>InitializingBean</code>
63 */
64 public void afterPropertiesSet() throws Exception {
65
66
67 if (getDriver() == null && StringUtils.hasText(driverClassName)) {
68 setDriverClass(ClassUtils.forName(driverClassName));
69 }
70
71 if (sqlScriptProcessor != null) {
72 sqlScriptProcessor.setDataSource(this);
73
74 sqlScriptProcessor.process();
75 }
76 }
77
78 }