Coverage Report - org.owasp.dependencycheck.data.nvdcve.DriverShim
 
Classes in this File Line Coverage Branch Coverage Complexity
DriverShim
6%
2/31
0%
0/14
2.364
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2014 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.data.nvdcve;
 19  
 
 20  
 import java.lang.reflect.InvocationTargetException;
 21  
 import java.lang.reflect.Method;
 22  
 import java.sql.Connection;
 23  
 import java.sql.Driver;
 24  
 import java.sql.DriverPropertyInfo;
 25  
 import java.sql.SQLException;
 26  
 import java.sql.SQLFeatureNotSupportedException;
 27  
 import java.util.Properties;
 28  
 import java.util.logging.Level;
 29  
 import java.util.logging.Logger;
 30  
 
 31  
 /**
 32  
  * <p>
 33  
  * Driver shim to get around the class loader issue with the DriverManager. The following code is a nearly identical
 34  
  * copy (with more comments and a few more methods implemented) of the DriverShim from:</p>
 35  
  * <blockquote>http://www.kfu.com/~nsayer/Java/dyn-jdbc.html</blockquote>
 36  
  *
 37  
  * @author Jeremy Long <jeremy.long@owasp.org>
 38  
  * @see java.sql.Driver
 39  
  */
 40  
 class DriverShim implements Driver {
 41  
 
 42  
     /**
 43  
      * The database driver being wrapped.
 44  
      */
 45  
     private final Driver driver;
 46  
 
 47  
     /**
 48  
      * Constructs a new wrapper around a Driver.
 49  
      *
 50  
      * @param driver the database driver to wrap
 51  
      */
 52  
     DriverShim(Driver driver) {
 53  
         this.driver = driver;
 54  
     }
 55  
 
 56  
     /**
 57  
      * Wraps the underlying driver's call to acceptsURL. Returns whether or not the driver can open a connection to the
 58  
      * given URL.
 59  
      *
 60  
      * @param url the URL of the database
 61  
      * @return true if the wrapped driver can connect to the specified URL
 62  
      * @throws SQLException thrown if there is an error connecting to the database
 63  
      * @see java.sql.Driver#acceptsURL(java.lang.String)
 64  
      */
 65  
     @Override
 66  
     public boolean acceptsURL(String url) throws SQLException {
 67  2
         return this.driver.acceptsURL(url);
 68  
     }
 69  
 
 70  
     /**
 71  
      * Wraps the call to the underlying driver's connect method.
 72  
      *
 73  
      * @param url the URL of the database
 74  
      * @param info a collection of string/value pairs
 75  
      * @return a Connection object
 76  
      * @throws SQLException thrown if there is an error connecting to the database
 77  
      * @see java.sql.Driver#connect(java.lang.String, java.util.Properties)
 78  
      */
 79  
     @Override
 80  
     public Connection connect(String url, Properties info) throws SQLException {
 81  0
         return this.driver.connect(url, info);
 82  
     }
 83  
 
 84  
     /**
 85  
      * Returns the wrapped driver's major version number.
 86  
      *
 87  
      * @return the wrapped driver's major version number
 88  
      * @see java.sql.Driver#getMajorVersion()
 89  
      */
 90  
     @Override
 91  
     public int getMajorVersion() {
 92  0
         return this.driver.getMajorVersion();
 93  
     }
 94  
 
 95  
     /**
 96  
      * Returns the wrapped driver's minor version number.
 97  
      *
 98  
      * @return the wrapped driver's minor version number
 99  
      * @see java.sql.Driver#getMinorVersion()
 100  
      */
 101  
     @Override
 102  
     public int getMinorVersion() {
 103  0
         return this.driver.getMinorVersion();
 104  
     }
 105  
 
 106  
     /**
 107  
      * Wraps the call to the underlying driver's getParentLogger method.
 108  
      *
 109  
      * @return the parent's Logger
 110  
      * @throws SQLFeatureNotSupportedException thrown if the feature is not supported
 111  
      * @see java.sql.Driver#getParentLogger()
 112  
      */
 113  
     //@Override
 114  
     public Logger getParentLogger() throws SQLFeatureNotSupportedException {
 115  
         //return driver.getParentLogger();
 116  0
         Method m = null;
 117  
         try {
 118  0
             m = driver.getClass().getMethod("getParentLogger");
 119  0
         } catch (Throwable e) {
 120  0
             throw new SQLFeatureNotSupportedException();
 121  0
         }
 122  0
         if (m != null) {
 123  
             try {
 124  0
                 return (Logger) m.invoke(m);
 125  0
             } catch (IllegalAccessException ex) {
 126  0
                 Logger.getLogger(DriverShim.class.getName()).log(Level.FINER, null, ex);
 127  0
             } catch (IllegalArgumentException ex) {
 128  0
                 Logger.getLogger(DriverShim.class.getName()).log(Level.FINER, null, ex);
 129  0
             } catch (InvocationTargetException ex) {
 130  0
                 Logger.getLogger(DriverShim.class.getName()).log(Level.FINER, null, ex);
 131  0
             }
 132  
         }
 133  0
         throw new SQLFeatureNotSupportedException();
 134  
     }
 135  
 
 136  
     /**
 137  
      * Wraps the call to the underlying driver's getPropertyInfo method.
 138  
      *
 139  
      * @param url the URL of the database
 140  
      * @param info a collection of string/value pairs
 141  
      * @return an array of DriverPropertyInfo objects
 142  
      * @throws SQLException thrown if there is an error accessing the database
 143  
      * @see java.sql.Driver#getPropertyInfo(java.lang.String, java.util.Properties)
 144  
      */
 145  
     @Override
 146  
     public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
 147  0
         return this.driver.getPropertyInfo(url, info);
 148  
     }
 149  
 
 150  
     /**
 151  
      * Returns whether or not the wrapped driver is jdbcCompliant.
 152  
      *
 153  
      * @return true if the wrapped driver is JDBC compliant; otherwise false
 154  
      * @see java.sql.Driver#jdbcCompliant()
 155  
      */
 156  
     @Override
 157  
     public boolean jdbcCompliant() {
 158  0
         return this.driver.jdbcCompliant();
 159  
     }
 160  
 
 161  
     /**
 162  
      * Standard implementation of hashCode.
 163  
      *
 164  
      * @return the hashCode of the object
 165  
      */
 166  
     @Override
 167  
     public int hashCode() {
 168  0
         int hash = 7;
 169  0
         hash = 97 * hash + (this.driver != null ? this.driver.hashCode() : 0);
 170  0
         return hash;
 171  
     }
 172  
 
 173  
     /**
 174  
      * Standard implementation of equals.
 175  
      *
 176  
      * @param obj the object to compare
 177  
      * @return returns true if the objects are equal; otherwise false
 178  
      */
 179  
     @Override
 180  
     public boolean equals(Object obj) {
 181  0
         if (obj == null) {
 182  0
             return false;
 183  
         }
 184  0
         if (getClass() != obj.getClass()) {
 185  0
             return false;
 186  
         }
 187  0
         final DriverShim other = (DriverShim) obj;
 188  0
         return this.driver == other.driver || (this.driver != null && this.driver.equals(other.driver));
 189  
     }
 190  
 
 191  
     /**
 192  
      * Standard implementation of toString().
 193  
      *
 194  
      * @return the String representation of the object
 195  
      */
 196  
     @Override
 197  
     public String toString() {
 198  18
         return "DriverShim{" + "driver=" + driver + '}';
 199  
     }
 200  
 }