1 19 package craftsman.spy; 20 21 import java.sql.Connection ; 22 import java.sql.Driver ; 23 import java.sql.DriverManager ; 24 import java.sql.DriverPropertyInfo ; 25 import java.sql.SQLException ; 26 import java.util.Properties ; 27 28 51 public class SpyDriver implements Driver { 52 55 private Driver real = null; 56 57 58 61 private final static String DRIVER_CLASS_NAME_PROPERTY = "spy.driver"; 62 63 64 75 private final static String CONNECTION_ID_SQL_PROPERTY = "spy.connection.id"; 76 77 78 82 private final static String URL_PREFIX = "jdbc:spy:"; 83 84 85 static { 86 try { 87 new SpyDriver(); 88 } catch ( Exception e) { 89 e.printStackTrace(); 90 } 91 } 92 93 94 103 public SpyDriver () throws ClassNotFoundException , InstantiationException , IllegalAccessException , SQLException { 104 init(); 105 } 106 107 108 117 private void init() throws ClassNotFoundException , InstantiationException , IllegalAccessException , SQLException { 118 DriverManager.registerDriver(this); 119 registerRealDriver(System.getProperty(DRIVER_CLASS_NAME_PROPERTY)); 120 } 121 122 123 135 private void registerRealDriver ( String className) throws InstantiationException , IllegalAccessException , ClassNotFoundException , SQLException { 136 if ( className!=null) { 137 real = (Driver )Class.forName(className).newInstance(); 138 DriverManager.deregisterDriver(real); 139 } 140 } 141 142 143 149 private String getRealDriverClassName(String url) { 150 String result = null; 151 int index = url.indexOf(':',URL_PREFIX.length()+1); 152 153 if ( index!=-1) { 154 result = url.substring(URL_PREFIX.length(),index); 155 } 156 157 return result; 158 } 159 160 161 167 private String getRealUrl(String url) { 168 return url!=null?url.startsWith(URL_PREFIX)?("jdbc"+url.substring(url.indexOf(':',URL_PREFIX.length()+1))):url:url; 169 } 170 171 172 178 public int getMajorVersion() { 179 return real!=null?real.getMajorVersion():1; 180 } 181 182 183 189 public int getMinorVersion() { 190 return real!=null?real.getMinorVersion():0; 191 } 192 193 194 200 public boolean jdbcCompliant() { 201 return real.jdbcCompliant(); 202 } 203 204 205 214 public boolean acceptsURL(String url) throws SQLException { 215 boolean result = false; 216 217 218 if ( real==null && url.startsWith(URL_PREFIX)) { 219 try { 220 registerRealDriver(getRealDriverClassName(url)); 221 } catch ( IllegalAccessException e) { 222 e.printStackTrace(); 223 } catch ( InstantiationException e) { 224 e.printStackTrace(); 225 } catch ( ClassNotFoundException e) { 226 e.printStackTrace(); 227 } 228 } 229 230 if ( real!=null) { 231 result = real.acceptsURL(getRealUrl(url)); 232 } 233 234 return result; 235 } 236 237 238 248 public Connection connect(String url, Properties info) throws SQLException { 249 Connection con = null; 250 251 252 if ( real==null && url.startsWith(URL_PREFIX)) { 253 try { 254 registerRealDriver(getRealDriverClassName(url)); 255 } catch ( IllegalAccessException e) { 256 e.printStackTrace(); 257 } catch ( InstantiationException e) { 258 e.printStackTrace(); 259 } catch ( ClassNotFoundException e) { 260 e.printStackTrace(); 261 } 262 } 263 264 if ( real!=null) { 265 con = new SpyConnection ( real.connect(getRealUrl(url),info),System.getProperty(CONNECTION_ID_SQL_PROPERTY)); 266 } 267 268 return con; 269 } 270 271 272 282 public DriverPropertyInfo [] getPropertyInfo(String url, Properties info) throws SQLException { 283 return real!=null?real.getPropertyInfo(url,info):null; 284 } 285 } 286 | Popular Tags |