KickJava   Java API By Example, From Geeks To Geeks.

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


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.log.Log;
34 import com.caucho.util.L10N;
35 import com.caucho.vfs.Path;
36
37 import javax.sql.DataSource JavaDoc;
38 import java.io.PrintWriter JavaDoc;
39 import java.sql.Connection JavaDoc;
40 import java.sql.SQLException JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * Non-pooled data source.
45  */

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

58   public DataSourceImpl()
59   {
60     _database = new Database();
61   }
62
63   /**
64    * Creates a new data source
65    */

66   public DataSourceImpl(Path path)
67     throws SQLException JavaDoc
68   {
69     this();
70
71     setPath(path);
72
73     init();
74   }
75
76   /**
77    * Sets the path to the database.
78    */

79   public void setPath(Path path)
80   {
81     _database.setPath(path);
82   }
83
84   /**
85    * If true, creates the database on init.
86    */

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

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

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

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

148   public Connection JavaDoc getConnection()
149     throws SQLException JavaDoc
150   {
151     init();
152       
153     return new ConnectionImpl(_database);
154   }
155
156   public void close()
157   {
158     _database.close();
159   }
160
161   public String JavaDoc toString()
162   {
163     return "DataSourceImpl[" + _database.getPath() + "]";
164   }
165
166   protected void finalize()
167     throws Throwable JavaDoc
168   {
169     super.finalize();
170
171     _database.close();
172   }
173 }
174
Popular Tags