1 package org.apache.torque.dsfactory;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import javax.sql.ConnectionPoolDataSource;
23 import javax.sql.DataSource;
24
25 import org.apache.commons.configuration.Configuration;
26
27 import org.apache.commons.dbcp.datasources.PerUserPoolDataSource;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 import org.apache.torque.Torque;
33 import org.apache.torque.TorqueException;
34
35
36
37
38
39
40
41
42 public class PerUserPoolDataSourceFactory
43 extends AbstractDataSourceFactory
44 {
45
46
47 private static Log log
48 = LogFactory.getLog(PerUserPoolDataSourceFactory.class);
49
50
51 private PerUserPoolDataSource ds = null;
52
53
54
55
56 public DataSource getDataSource()
57 {
58 return ds;
59 }
60
61
62
63
64 public void initialize(Configuration configuration) throws TorqueException
65 {
66 ConnectionPoolDataSource cpds = initCPDS(configuration);
67 PerUserPoolDataSource dataSource = initJdbc2Pool(configuration);
68 dataSource.setConnectionPoolDataSource(cpds);
69 this.ds = dataSource;
70 }
71
72
73
74
75
76
77
78
79 private PerUserPoolDataSource initJdbc2Pool(Configuration configuration)
80 throws TorqueException
81 {
82 log.debug("Starting initJdbc2Pool");
83 PerUserPoolDataSource dataSource = new PerUserPoolDataSource();
84 Configuration c = Torque.getConfiguration();
85
86 if (c == null || c.isEmpty())
87 {
88 log.warn("Global Configuration not set,"
89 + " no Default pool data source configured!");
90 }
91 else
92 {
93 Configuration conf = c.subset(DEFAULT_POOL_KEY);
94 applyConfiguration(conf, dataSource);
95 }
96
97 Configuration conf = configuration.subset(POOL_KEY);
98 applyConfiguration(conf, dataSource);
99 return dataSource;
100 }
101
102
103
104
105
106 public void close() throws TorqueException
107 {
108 try
109 {
110 ds.close();
111 }
112 catch (Exception e)
113 {
114 log.error("Exception caught during close()", e);
115 throw new TorqueException(e);
116 }
117 ds = null;
118 }
119
120 }