001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.db.jdbc;
031:
032: import com.caucho.db.Database;
033: import com.caucho.util.L10N;
034: import com.caucho.vfs.Path;
035: import com.caucho.vfs.Vfs;
036:
037: import javax.sql.ConnectionPoolDataSource;
038: import javax.sql.PooledConnection;
039: import java.io.PrintWriter;
040: import java.sql.SQLException;
041: import java.util.logging.Logger;
042:
043: /**
044: * Driver for the internal database.
045: */
046: public class ConnectionPoolDataSourceImpl implements
047: ConnectionPoolDataSource {
048: private static final Logger log = Logger
049: .getLogger(ConnectionPoolDataSourceImpl.class.getName());
050: private static final L10N L = new L10N(
051: ConnectionPoolDataSourceImpl.class);
052:
053: private Database _database;
054:
055: private boolean _createDatabase;
056: private boolean _isInit;
057:
058: /**
059: * Creates a new data source
060: */
061: public ConnectionPoolDataSourceImpl() {
062: _database = new Database();
063: }
064:
065: /**
066: * Sets the path to the database.
067: */
068: public void setPath(Path path) {
069: _database.setPath(path);
070: }
071:
072: /**
073: * Sets the url to the database.
074: */
075: public void setURL(String url) {
076: if (url.startsWith("jdbc:"))
077: url = url.substring(5);
078: if (url.startsWith("resin:"))
079: url = url.substring(6);
080:
081: _database.setPath(Vfs.lookup(url));
082: }
083:
084: /**
085: * If true, creates the database on init.
086: */
087: public void setCreateDatabase(boolean create) {
088: _createDatabase = create;
089: }
090:
091: /**
092: * If true, removes bad tables on init.
093: */
094: public void setRemoveOnError(boolean remove) {
095: _database.setRemoveOnError(remove);
096: }
097:
098: /**
099: * Initialize the data source.
100: */
101: public void init() throws SQLException {
102: synchronized (this ) {
103: if (_isInit)
104: return;
105:
106: try {
107: _database.init();
108: } finally {
109: _isInit = true;
110: }
111: }
112: }
113:
114: public int getLoginTimeout() {
115: return 0;
116: }
117:
118: public void setLoginTimeout(int foo) {
119: }
120:
121: public PrintWriter getLogWriter() {
122: return null;
123: }
124:
125: public void setLogWriter(PrintWriter log) {
126: }
127:
128: /**
129: * Driver interface to create a new connection.
130: */
131: public PooledConnection getPooledConnection(String user,
132: String password) throws SQLException {
133: return getPooledConnection();
134: }
135:
136: /**
137: * Driver interface to create a new connection.
138: */
139: public PooledConnection getPooledConnection() throws SQLException {
140: init();
141:
142: return new PooledConnectionImpl(_database);
143: }
144:
145: public String toString() {
146: return "ConnectionPoolDataSourceImpl[" + _database.getPath()
147: + "]";
148: }
149:
150: protected void finalize() throws Throwable {
151: super.finalize();
152:
153: _database.close();
154: }
155: }
|