KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > api > engine > jasperreports > service > impl > DbcpDataSource


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl;
22
23 import javax.sql.DataSource JavaDoc;
24
25 import org.apache.commons.dbcp.ConnectionFactory;
26 import org.apache.commons.dbcp.DriverManagerConnectionFactory;
27 import org.apache.commons.dbcp.PoolableConnectionFactory;
28 import org.apache.commons.dbcp.PoolingDataSource;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.commons.pool.ObjectPool;
32 import org.apache.commons.pool.ObjectPoolFactory;
33
34 import com.jaspersoft.jasperserver.api.JSExceptionWrapper;
35
36 /**
37  * @author Lucian Chirita (lucianc@users.sourceforge.net)
38  * @version $Id: DbcpDataSource.java 2995 2006-04-07 14:05:32Z tdanciu $
39  */

40 public class DbcpDataSource implements PooledDataSource {
41     
42     private final static Log log = LogFactory.getLog(DbcpDataSource.class);
43
44     private final ObjectPool connectionPool;
45     private final PoolingDataSource dataSource;
46     
47     public DbcpDataSource(ObjectPoolFactory objectPoolFactory,
48             String JavaDoc url, String JavaDoc username, String JavaDoc password) {
49         connectionPool = createPool(objectPoolFactory);
50         createPoolableConnectionFactory(url, username, password);
51
52         dataSource = new PoolingDataSource(connectionPool);
53     }
54
55     protected ObjectPool createPool(ObjectPoolFactory objectPoolFactory) {
56         ObjectPool objectPool = objectPoolFactory.createPool();
57         return objectPool;
58     }
59
60     protected void createPoolableConnectionFactory(String JavaDoc url, String JavaDoc username, String JavaDoc password) {
61         ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, username, password);
62         new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, true, false);
63     }
64     
65     public DataSource JavaDoc getDataSource() {
66         return dataSource;
67     }
68
69     public void release() {
70         try {
71             connectionPool.close();
72         } catch (Exception JavaDoc e) {
73             log.error("Error while closing DBCP connection pool", e);
74             throw new JSExceptionWrapper(e);
75         }
76     }
77
78 }
79
Popular Tags