001package com.hfg.sql.jdbc; 002 003import com.hfg.security.LoginCredentials; 004 005import javax.sql.DataSource; 006import java.io.PrintWriter; 007import java.sql.Connection; 008import java.sql.SQLException; 009import java.sql.SQLFeatureNotSupportedException; 010import java.util.logging.Logger; 011 012 013//------------------------------------------------------------------------------ 014/** 015 Represents a JDBC-compatible DataSource. 016 <div> 017 @author J. Alex Taylor, hairyfatguy.com 018 </div> 019 */ 020//------------------------------------------------------------------------------ 021// com.hfg XML/HTML Coding Library 022// 023// This library is free software; you can redistribute it and/or 024// modify it under the terms of the GNU Lesser General Public 025// License as published by the Free Software Foundation; either 026// version 2.1 of the License, or (at your option) any later version. 027// 028// This library is distributed in the hope that it will be useful, 029// but WITHOUT ANY WARRANTY; without even the implied warranty of 030// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 031// Lesser General Public License for more details. 032// 033// You should have received a copy of the GNU Lesser General Public 034// License along with this library; if not, write to the Free Software 035// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 036// 037// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 038// jataylor@hairyfatguy.com 039//------------------------------------------------------------------------------ 040 041public class JDBCDataSource<T extends JDBCConnectionSettings> implements DataSource 042{ 043 private String mName; 044 private PrintWriter mLogWriter; 045 private Logger mLogger; 046 private T mSettings; 047 private JDBCServer mServer; 048 private LoginCredentials mCredentials; 049 private String mDatabaseName; 050 051 //########################################################################### 052 // CONSTRUCTORS 053 //########################################################################### 054 055 //--------------------------------------------------------------------------- 056 public JDBCDataSource(JDBCServer inServer, String inDatabaseName) 057 { 058 mServer = inServer; 059 mDatabaseName = inDatabaseName; 060 } 061 062 //########################################################################### 063 // PUBLIC METHODS 064 //########################################################################### 065 066 //--------------------------------------------------------------------------- 067 public JDBCDataSource setName(String inValue) 068 { 069 mName = inValue; 070 return this; 071 } 072 073 //--------------------------------------------------------------------------- 074 public String name() 075 { 076 return mName; 077 } 078 079 //--------------------------------------------------------------------------- 080 public JDBCDataSource setSettings(T inValue) 081 { 082 mSettings = inValue; 083 return this; 084 } 085 086 //--------------------------------------------------------------------------- 087 public T getSettings() 088 { 089 if (null == mSettings) 090 { 091 mSettings = initSettings(); 092 } 093 094 return mSettings; 095 } 096 097 //--------------------------------------------------------------------------- 098 public JDBCServer getServer() 099 { 100 return mServer; 101 } 102 103 //--------------------------------------------------------------------------- 104 public String getDatabaseName() 105 { 106 return mDatabaseName; 107 } 108 109 //--------------------------------------------------------------------------- 110 public JDBCDataSource setCredentials(LoginCredentials inValue) 111 { 112 mCredentials = inValue; 113 return this; 114 } 115 116 //--------------------------------------------------------------------------- 117 protected LoginCredentials getCredentials() 118 { 119 return mCredentials; 120 } 121 122 //--------------------------------------------------------------------------- 123 public JDBCDataSource setParentLogger(Logger inValue) 124 { 125 mLogger = inValue; 126 return this; 127 } 128 129 //--------------------------------------------------------------------------- 130 @Override 131 public Connection getConnection() 132 throws SQLException 133 { 134 if (null == mCredentials) 135 { 136 throw new SQLException("No login credentials have been specified!"); 137 } 138 139 return getServer().getConnection(getDatabaseName(), mCredentials, getSettings()); 140 } 141 142 //--------------------------------------------------------------------------- 143 @Override 144 public Connection getConnection(String inUsername, String inPassword) 145 throws SQLException 146 { 147 setCredentials(new LoginCredentials(inUsername, inPassword.toCharArray())); 148 149 return getConnection(); 150 } 151 152 //--------------------------------------------------------------------------- 153 @Override 154 public PrintWriter getLogWriter() 155 throws SQLException 156 { 157 return mLogWriter; 158 } 159 160 //--------------------------------------------------------------------------- 161 @Override 162 public void setLogWriter(PrintWriter inValue) 163 throws SQLException 164 { 165 mLogWriter = inValue; 166 } 167 168 //--------------------------------------------------------------------------- 169 @Override 170 public void setLoginTimeout(int inValueInSeconds) 171 throws SQLException 172 { 173 getSettings().setConnectTimeoutInSeconds(inValueInSeconds); 174 } 175 176 //--------------------------------------------------------------------------- 177 @Override 178 public int getLoginTimeout() 179 throws SQLException 180 { 181 return getSettings().getConnectTimeoutInSeconds(); 182 } 183 184 //--------------------------------------------------------------------------- 185 @Override 186 public Logger getParentLogger() 187 throws SQLFeatureNotSupportedException 188 { 189 return mLogger; 190 } 191 192 //--------------------------------------------------------------------------- 193 @Override 194 public <T> T unwrap(Class<T> inInterface) 195 throws SQLException 196 { 197 try 198 { 199 return inInterface.cast(this); 200 } 201 catch (ClassCastException e) 202 { 203 throw new SQLException("Unable to unwrap the DataSouce to " + inInterface.getName() + "!", e); 204 } 205 } 206 207 //--------------------------------------------------------------------------- 208 @Override 209 public boolean isWrapperFor(Class<?> inInterface) 210 throws SQLException 211 { 212 return inInterface.isInstance(this); 213 } 214 215 //########################################################################### 216 // PROTECTED METHODS 217 //########################################################################### 218 219 //--------------------------------------------------------------------------- 220 protected T initSettings() 221 { 222 return (T) new JDBCConnectionSettings(); 223 } 224 225}