KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > jdbc > ConnectionPoolDataSourceImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.db.jdbc;
31
32 import com.caucho.db.Database;
33 import com.caucho.util.L10N;
34 import com.caucho.vfs.Path;
35 import com.caucho.vfs.Vfs;
36
37 import javax.sql.ConnectionPoolDataSource JavaDoc;
38 import javax.sql.PooledConnection JavaDoc;
39 import java.io.PrintWriter JavaDoc;
40 import java.sql.SQLException JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * Driver for the internal database.
45  */

46 public class ConnectionPoolDataSourceImpl implements ConnectionPoolDataSource JavaDoc {
47   private static final Logger JavaDoc log
48     = Logger.getLogger(ConnectionPoolDataSourceImpl.class.getName());
49   private static final L10N L = new L10N(ConnectionPoolDataSourceImpl.class);
50
51   private Database _database;
52
53   private boolean _createDatabase;
54   private boolean _isInit;
55
56   /**
57    * Creates a new data source
58    */

59   public ConnectionPoolDataSourceImpl()
60   {
61     _database = new Database();
62   }
63
64   /**
65    * Sets the path to the database.
66    */

67   public void setPath(Path path)
68   {
69     _database.setPath(path);
70   }
71
72   /**
73    * Sets the url to the database.
74    */

75   public void setURL(String JavaDoc url)
76   {
77     if (url.startsWith("jdbc:"))
78       url = url.substring(5);
79     if (url.startsWith("resin:"))
80       url = url.substring(6);
81
82     _database.setPath(Vfs.lookup(url));
83   }
84
85   /**
86    * If true, creates the database on init.
87    */

88   public void setCreateDatabase(boolean create)
89   {
90     _createDatabase = create;
91   }
92
93   /**
94    * If true, removes bad tables on init.
95    */

96   public void setRemoveOnError(boolean remove)
97   {
98     _database.setRemoveOnError(remove);
99   }
100
101   /**
102    * Initialize the data source.
103    */

104   public void init()
105     throws SQLException JavaDoc
106   {
107     synchronized (this) {
108       if (_isInit)
109     return;
110
111       try {
112     _database.init();
113       } finally {
114     _isInit = true;
115       }
116     }
117   }
118
119   public int getLoginTimeout()
120   {
121     return 0;
122   }
123
124   public void setLoginTimeout(int foo)
125   {
126   }
127
128   public PrintWriter JavaDoc getLogWriter()
129   {
130     return null;
131   }
132
133   public void setLogWriter(PrintWriter JavaDoc log)
134   {
135   }
136   
137   /**
138    * Driver interface to create a new connection.
139    */

140   public PooledConnection JavaDoc getPooledConnection(String JavaDoc user, String JavaDoc password)
141     throws SQLException JavaDoc
142   {
143     return getPooledConnection();
144   }
145   
146   /**
147    * Driver interface to create a new connection.
148    */

149   public PooledConnection JavaDoc getPooledConnection()
150     throws SQLException JavaDoc
151   {
152     init();
153       
154     return new PooledConnectionImpl(_database);
155   }
156
157   public String JavaDoc toString()
158   {
159     return "ConnectionPoolDataSourceImpl[" + _database.getPath() + "]";
160   }
161
162   protected void finalize()
163     throws Throwable JavaDoc
164   {
165     super.finalize();
166
167     _database.close();
168   }
169 }
170
Popular Tags