1 package org.apache.commons.jcs.auxiliary.disk.jdbc.dsfactory;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.sql.SQLException;
23
24 import javax.sql.ConnectionPoolDataSource;
25 import javax.sql.DataSource;
26
27 import org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS;
28 import org.apache.commons.dbcp.datasources.InstanceKeyDataSource;
29 import org.apache.commons.dbcp.datasources.SharedPoolDataSource;
30 import org.apache.commons.jcs.auxiliary.disk.jdbc.JDBCDiskCacheAttributes;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34
35
36
37
38
39
40
41
42 public class SharedPoolDataSourceFactory implements DataSourceFactory
43 {
44
45 private static Log log = LogFactory.getLog(SharedPoolDataSourceFactory.class);
46
47
48 private String name;
49
50
51 private SharedPoolDataSource ds = null;
52
53
54
55
56 @Override
57 public String getName()
58 {
59 return name;
60 }
61
62
63
64
65 @Override
66 public DataSource getDataSource()
67 {
68 return ds;
69 }
70
71
72
73
74 @Override
75 public void initialize(JDBCDiskCacheAttributes config) throws SQLException
76 {
77 this.name = config.getConnectionPoolName();
78 ConnectionPoolDataSource cpds = initCPDS(config);
79 SharedPoolDataSource dataSource = new SharedPoolDataSource();
80 initJdbc2Pool(dataSource, config);
81 dataSource.setConnectionPoolDataSource(cpds);
82 dataSource.setMaxActive(config.getMaxActive());
83 this.ds = dataSource;
84 }
85
86
87
88
89
90 @Override
91 public void close() throws SQLException
92 {
93 try
94 {
95 ds.close();
96 }
97 catch (Exception e)
98 {
99 throw new SQLException("Exception caught closing data source", e);
100 }
101 ds = null;
102 }
103
104
105
106
107
108
109
110
111 private ConnectionPoolDataSource initCPDS(final JDBCDiskCacheAttributes config)
112 throws SQLException
113 {
114 log.debug("Starting initCPDS");
115
116 DriverAdapterCPDS cpds = new DriverAdapterCPDS();
117
118 try
119 {
120 cpds.setDriver(config.getDriverClassName());
121 }
122 catch (ClassNotFoundException e)
123 {
124 throw new SQLException("Driver class not found " + config.getDriverClassName(), e);
125 }
126
127 cpds.setUrl(config.getUrl());
128 cpds.setUser(config.getUserName());
129 cpds.setPassword(config.getPassword());
130 cpds.setMaxActive(config.getMaxActive());
131
132 return cpds;
133 }
134
135
136
137
138
139
140
141
142
143 private void initJdbc2Pool(final InstanceKeyDataSource dataSource, final JDBCDiskCacheAttributes config)
144 throws SQLException
145 {
146 log.debug("Starting initJdbc2Pool");
147
148 dataSource.setDescription(config.getConnectionPoolName());
149 }
150 }