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 * A factory that looks up the DataSource using the JDBC2 pool methods.
37 *
38 * @author <a href="mailto:jmcnally@apache.org">John McNally</a>
39 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
40 * @version $Id: PerUserPoolDataSourceFactory.java 476550 2006-11-18 16:08:37Z tfischer $
41 */
42 public class PerUserPoolDataSourceFactory
43 extends AbstractDataSourceFactory
44 {
45
46 /*** The log. */
47 private static Log log
48 = LogFactory.getLog(PerUserPoolDataSourceFactory.class);
49
50 /*** The wrapped <code>DataSource</code>. */
51 private PerUserPoolDataSource ds = null;
52
53 /***
54 * @see org.apache.torque.dsfactory.DataSourceFactory#getDataSource
55 */
56 public DataSource getDataSource()
57 {
58 return ds;
59 }
60
61 /***
62 * @see org.apache.torque.dsfactory.DataSourceFactory#initialize
63 */
64 public void initialize(Configuration configuration) throws TorqueException
65 {
66 super.initialize(configuration);
67
68 ConnectionPoolDataSource cpds = initCPDS(configuration);
69 PerUserPoolDataSource dataSource = initJdbc2Pool(configuration);
70 dataSource.setConnectionPoolDataSource(cpds);
71 this.ds = dataSource;
72 }
73
74 /***
75 * Initializes the Jdbc2PoolDataSource.
76 *
77 * @param configuration where to read the settings from
78 * @throws TorqueException if a property set fails
79 * @return a configured <code>Jdbc2PoolDataSource</code>
80 */
81 private PerUserPoolDataSource initJdbc2Pool(Configuration configuration)
82 throws TorqueException
83 {
84 log.debug("Starting initJdbc2Pool");
85 PerUserPoolDataSource dataSource = new PerUserPoolDataSource();
86 Configuration c = Torque.getConfiguration();
87
88 if (c == null || c.isEmpty())
89 {
90 log.warn("Global Configuration not set,"
91 + " no Default pool data source configured!");
92 }
93 else
94 {
95 Configuration conf = c.subset(DEFAULT_POOL_KEY);
96 applyConfiguration(conf, dataSource);
97 }
98
99 Configuration conf = configuration.subset(POOL_KEY);
100 applyConfiguration(conf, dataSource);
101 return dataSource;
102 }
103
104 /***
105 * Closes the pool associated with this factory and releases it.
106 * @throws TorqueException if the pool cannot be closed properly
107 */
108 public void close() throws TorqueException
109 {
110 try
111 {
112 ds.close();
113 }
114 catch (Exception e)
115 {
116 log.error("Exception caught during close()", e);
117 throw new TorqueException(e);
118 }
119 ds = null;
120 }
121
122 }