%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.torque.dsfactory.SharedPoolDataSourceFactory |
|
|
1 | package org.apache.torque.dsfactory; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import javax.sql.ConnectionPoolDataSource; |
|
20 | import javax.sql.DataSource; |
|
21 | ||
22 | import org.apache.commons.configuration.Configuration; |
|
23 | ||
24 | import org.apache.commons.dbcp.datasources.SharedPoolDataSource; |
|
25 | ||
26 | import org.apache.commons.logging.Log; |
|
27 | import org.apache.commons.logging.LogFactory; |
|
28 | ||
29 | import org.apache.torque.Torque; |
|
30 | import org.apache.torque.TorqueException; |
|
31 | ||
32 | /** |
|
33 | * A factory that looks up the DataSource using the JDBC2 pool methods. |
|
34 | * |
|
35 | * @author <a href="mailto:jmcnally@apache.org">John McNally</a> |
|
36 | * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> |
|
37 | * @version $Id: SharedPoolDataSourceFactory.java 239636 2005-08-24 12:38:09Z henning $ |
|
38 | */ |
|
39 | 2 | public class SharedPoolDataSourceFactory |
40 | extends AbstractDataSourceFactory |
|
41 | implements DataSourceFactory |
|
42 | { |
|
43 | ||
44 | /** The log. */ |
|
45 | 2 | private static Log log |
46 | 2 | = LogFactory.getLog(SharedPoolDataSourceFactory.class); |
47 | ||
48 | /** The wrapped <code>DataSource</code>. */ |
|
49 | private SharedPoolDataSource ds; |
|
50 | ||
51 | /** |
|
52 | * @see org.apache.torque.dsfactory.DataSourceFactory#getDataSource |
|
53 | */ |
|
54 | public DataSource getDataSource() |
|
55 | { |
|
56 | 1 | return ds; |
57 | } |
|
58 | ||
59 | /** |
|
60 | * @see org.apache.torque.dsfactory.DataSourceFactory#initialize |
|
61 | */ |
|
62 | public void initialize(Configuration configuration) throws TorqueException |
|
63 | { |
|
64 | 2 | super.initialize(configuration); |
65 | ||
66 | 2 | ConnectionPoolDataSource cpds = initCPDS(configuration); |
67 | 2 | SharedPoolDataSource ds = initJdbc2Pool(configuration); |
68 | 2 | ds.setConnectionPoolDataSource(cpds); |
69 | 2 | this.ds = ds; |
70 | 2 | } |
71 | ||
72 | /** |
|
73 | * Initializes the Jdbc2PoolDataSource. |
|
74 | * |
|
75 | * @param configuration where to read the settings from |
|
76 | * @throws TorqueException if a property set fails |
|
77 | * @return a configured <code>Jdbc2PoolDataSource</code> |
|
78 | */ |
|
79 | private SharedPoolDataSource initJdbc2Pool(Configuration configuration) |
|
80 | throws TorqueException |
|
81 | { |
|
82 | 2 | log.debug("Starting initJdbc2Pool"); |
83 | 2 | SharedPoolDataSource ds = new SharedPoolDataSource(); |
84 | 2 | Configuration c = Torque.getConfiguration(); |
85 | ||
86 | 2 | if (c == null || c.isEmpty()) |
87 | { |
|
88 | 0 | log.warn("Global Configuration not set," |
89 | + " no Default pool data source configured!"); |
|
90 | } |
|
91 | else |
|
92 | { |
|
93 | 2 | Configuration conf = c.subset(DEFAULT_POOL_KEY); |
94 | 2 | applyConfiguration(conf, ds); |
95 | } |
|
96 | ||
97 | 2 | Configuration conf = configuration.subset(POOL_KEY); |
98 | 2 | applyConfiguration(conf, ds); |
99 | 2 | return ds; |
100 | } |
|
101 | ||
102 | ||
103 | /** |
|
104 | * Closes the pool associated with this factory and releases it. |
|
105 | * @throws TorqueException if the pool cannot be closed properly |
|
106 | */ |
|
107 | public void close() throws TorqueException |
|
108 | { |
|
109 | try |
|
110 | { |
|
111 | 0 | ds.close(); |
112 | } |
|
113 | 0 | catch (Exception e) |
114 | { |
|
115 | 0 | log.error("Exception caught during close()", e); |
116 | 0 | throw new TorqueException(e); |
117 | 0 | } |
118 | 0 | ds = null; |
119 | 0 | } |
120 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |