001 /*****************************************************************************
002 * Copyright (c) PicoContainer Organization. All rights reserved. *
003 * ------------------------------------------------------------------------- *
004 * The software in this package is published under the terms of the BSD *
005 * style license a copy of which has been included with this distribution in *
006 * the license.html file. *
007 * *
008 * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
009 *****************************************************************************/
010 package org.nanocontainer.persistence.jdbc;
011
012 import java.io.PrintWriter;
013 import java.sql.Connection;
014 import java.sql.SQLException;
015
016 import javax.sql.DataSource;
017
018 import org.nanocontainer.persistence.ExceptionHandler;
019
020 /**
021 * Base classe for DataSource components. It delegates all calls to the datasource obtained by getDelegatedDataSource
022 * method. Error handling is also there.
023 *
024 * @version $Id: AbstractDataSource.java 2653 2005-10-16 12:50:07Z juze $
025 * @author Juze Peleteiro <juze -a-t- intelli -dot- biz>
026 */
027 public abstract class AbstractDataSource implements DataSource {
028
029 private final ExceptionHandler jdbcExceptionHandler;
030
031 protected AbstractDataSource(final ExceptionHandler jdbcExceptionHandler) {
032 this.jdbcExceptionHandler = jdbcExceptionHandler;
033 }
034
035 protected AbstractDataSource() {
036 jdbcExceptionHandler = null;
037 }
038
039 protected abstract DataSource getDelegatedDataSource() throws Exception;
040
041 protected abstract void invalidateDelegatedDataSource() throws Exception;
042
043 /**
044 * Invalidates the connection calling {@link #invalidateDelegatedConnection()} and convert the <code>cause</code>
045 * using a {@link ExceptionHandler}. if it's available otherwise just return the <code>cause</code> back.
046 */
047 protected SQLException handleException(final Exception cause) throws RuntimeException {
048 try {
049 invalidateDelegatedDataSource();
050 } catch (Exception e) {
051 // Do nothing, only the original exception should be reported.
052 }
053
054 if (jdbcExceptionHandler == null) {
055 if (cause instanceof SQLException) {
056 return (SQLException) cause;
057 }
058
059 if (cause instanceof Exception) {
060 // TODO Figure out how to deal with it.
061 throw new RuntimeException(cause);
062 }
063
064 throw (RuntimeException) cause;
065 }
066
067 throw jdbcExceptionHandler.handle(cause);
068 }
069
070 /**
071 * @see javax.sql.DataSource#getConnection()
072 */
073 public Connection getConnection() throws SQLException {
074 try {
075 return getDelegatedDataSource().getConnection();
076 } catch (Exception e) {
077 throw handleException(e);
078 }
079 }
080
081 /**
082 * @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String)
083 */
084 public Connection getConnection(final String username, final String password) throws SQLException {
085 try {
086 return getDelegatedDataSource().getConnection(username, password);
087 } catch (Exception e) {
088 throw handleException(e);
089 }
090 }
091
092 /**
093 * @see javax.sql.DataSource#getLogWriter()
094 */
095 public PrintWriter getLogWriter() throws SQLException {
096 try {
097 return getDelegatedDataSource().getLogWriter();
098 } catch (Exception e) {
099 throw handleException(e);
100 }
101 }
102
103 /**
104 * @see javax.sql.DataSource#setLogWriter(java.io.PrintWriter)
105 */
106 public void setLogWriter(final PrintWriter out) throws SQLException {
107 try {
108 getDelegatedDataSource().setLogWriter(out);
109 } catch (Exception e) {
110 throw handleException(e);
111 }
112 }
113
114 /**
115 * @see javax.sql.DataSource#setLoginTimeout(int)
116 */
117 public void setLoginTimeout(final int seconds) throws SQLException {
118 try {
119 getDelegatedDataSource().setLoginTimeout(seconds);
120 } catch (Exception e) {
121 throw handleException(e);
122 }
123 }
124
125 /**
126 * @see javax.sql.DataSource#getLoginTimeout()
127 */
128 public int getLoginTimeout() throws SQLException {
129 try {
130 return getDelegatedDataSource().getLoginTimeout();
131 } catch (Exception e) {
132 throw handleException(e);
133 }
134 }
135
136 }