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.log.Log;
034: import com.caucho.util.L10N;
035: import com.caucho.vfs.Path;
036:
037: import javax.sql.DataSource;
038: import java.io.PrintWriter;
039: import java.sql.Connection;
040: import java.sql.SQLException;
041: import java.util.logging.Logger;
042:
043: /**
044: * Non-pooled data source.
045: */
046: public class DataSourceImpl implements DataSource {
047: private static final Logger log = Log.open(DataSourceImpl.class);
048: private static final L10N L = new L10N(DataSourceImpl.class);
049:
050: private Database _database;
051:
052: private boolean _createDatabase;
053: private boolean _isInit;
054:
055: /**
056: * Creates a new data source
057: */
058: public DataSourceImpl() {
059: _database = new Database();
060: }
061:
062: /**
063: * Creates a new data source
064: */
065: public DataSourceImpl(Path path) throws SQLException {
066: this ();
067:
068: setPath(path);
069:
070: init();
071: }
072:
073: /**
074: * Sets the path to the database.
075: */
076: public void setPath(Path path) {
077: _database.setPath(path);
078: }
079:
080: /**
081: * If true, creates the database on init.
082: */
083: public void setCreateDatabase(boolean create) {
084: _createDatabase = create;
085: }
086:
087: /**
088: * If true, removes bad tables on init.
089: */
090: public void setRemoveOnError(boolean remove) {
091: _database.setRemoveOnError(remove);
092: }
093:
094: /**
095: * Initialize the data source.
096: */
097: public void init() throws SQLException {
098: synchronized (this ) {
099: if (_isInit)
100: return;
101:
102: try {
103: _database.init();
104: } finally {
105: _isInit = true;
106: }
107: }
108: }
109:
110: public int getLoginTimeout() {
111: return 0;
112: }
113:
114: public void setLoginTimeout(int foo) {
115: }
116:
117: public PrintWriter getLogWriter() {
118: return null;
119: }
120:
121: public void setLogWriter(PrintWriter log) {
122: }
123:
124: /**
125: * Driver interface to create a new connection.
126: */
127: public Connection getConnection(String user, String password)
128: throws SQLException {
129: return getConnection();
130: }
131:
132: /**
133: * Driver interface to create a new connection.
134: */
135: public Connection getConnection() throws SQLException {
136: init();
137:
138: return new ConnectionImpl(_database);
139: }
140:
141: public void close() {
142: _database.close();
143: }
144:
145: public String toString() {
146: return "DataSourceImpl[" + _database.getPath() + "]";
147: }
148:
149: protected void finalize() throws Throwable {
150: super .finalize();
151:
152: _database.close();
153: }
154:
155: public <T> T unwrap(Class<T> iface) throws SQLException {
156: throw new UnsupportedOperationException("Not supported yet.");
157: }
158:
159: public boolean isWrapperFor(Class<?> iface) throws SQLException {
160: throw new UnsupportedOperationException("Not supported yet.");
161: }
162: }
|