Coverage Report - org.owasp.dependencycheck.data.nvdcve.DriverShim
 
Classes in this File Line Coverage Branch Coverage Complexity
DriverShim
9%
3/32
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 logger.
 44  
      */
 45  1
     private static final Logger LOGGER = Logger.getLogger(DriverShim.class.getName());
 46  
     /**
 47  
      * The database driver being wrapped.
 48  
      */
 49  
     private final Driver driver;
 50  
 
 51  
     /**
 52  
      * Constructs a new wrapper around a Driver.
 53  
      *
 54  
      * @param driver the database driver to wrap
 55  
      */
 56  
     DriverShim(Driver driver) {
 57  
         this.driver = driver;
 58  
     }
 59  
 
 60  
     /**
 61  
      * Wraps the underlying driver's call to acceptsURL. Returns whether or not the driver can open a connection to the
 62  
      * given URL.
 63  
      *
 64  
      * @param url the URL of the database
 65  
      * @return true if the wrapped driver can connect to the specified URL
 66  
      * @throws SQLException thrown if there is an error connecting to the database
 67  
      * @see java.sql.Driver#acceptsURL(java.lang.String)
 68  
      */
 69  
     @Override
 70  
     public boolean acceptsURL(String url) throws SQLException {
 71  2
         return this.driver.acceptsURL(url);
 72  
     }
 73  
 
 74  
     /**
 75  
      * Wraps the call to the underlying driver's connect method.
 76  
      *
 77  
      * @param url the URL of the database
 78  
      * @param info a collection of string/value pairs
 79  
      * @return a Connection object
 80  
      * @throws SQLException thrown if there is an error connecting to the database
 81  
      * @see java.sql.Driver#connect(java.lang.String, java.util.Properties)
 82  
      */
 83  
     @Override
 84  
     public Connection connect(String url, Properties info) throws SQLException {
 85  0
         return this.driver.connect(url, info);
 86  
     }
 87  
 
 88  
     /**
 89  
      * Returns the wrapped driver's major version number.
 90  
      *
 91  
      * @return the wrapped driver's major version number
 92  
      * @see java.sql.Driver#getMajorVersion()
 93  
      */
 94  
     @Override
 95  
     public int getMajorVersion() {
 96  0
         return this.driver.getMajorVersion();
 97  
     }
 98  
 
 99  
     /**
 100  
      * Returns the wrapped driver's minor version number.
 101  
      *
 102  
      * @return the wrapped driver's minor version number
 103  
      * @see java.sql.Driver#getMinorVersion()
 104  
      */
 105  
     @Override
 106  
     public int getMinorVersion() {
 107  0
         return this.driver.getMinorVersion();
 108  
     }
 109  
 
 110  
     /**
 111  
      * Wraps the call to the underlying driver's getParentLogger method.
 112  
      *
 113  
      * @return the parent's Logger
 114  
      * @throws SQLFeatureNotSupportedException thrown if the feature is not supported
 115  
      * @see java.sql.Driver#getParentLogger()
 116  
      */
 117  
     //@Override
 118  
     public Logger getParentLogger() throws SQLFeatureNotSupportedException {
 119  
         //return driver.getParentLogger();
 120  0
         Method m = null;
 121  
         try {
 122  0
             m = driver.getClass().getMethod("getParentLogger");
 123  0
         } catch (Throwable e) {
 124  0
             throw new SQLFeatureNotSupportedException();
 125  0
         }
 126  0
         if (m != null) {
 127  
             try {
 128  0
                 return (Logger) m.invoke(m);
 129  0
             } catch (IllegalAccessException ex) {
 130  0
                 LOGGER.log(Level.FINER, null, ex);
 131  0
             } catch (IllegalArgumentException ex) {
 132  0
                 LOGGER.log(Level.FINER, null, ex);
 133  0
             } catch (InvocationTargetException ex) {
 134  0
                 LOGGER.log(Level.FINER, null, ex);
 135  0
             }
 136  
         }
 137  0
         throw new SQLFeatureNotSupportedException();
 138  
     }
 139  
 
 140  
     /**
 141  
      * Wraps the call to the underlying driver's getPropertyInfo method.
 142  
      *
 143  
      * @param url the URL of the database
 144  
      * @param info a collection of string/value pairs
 145  
      * @return an array of DriverPropertyInfo objects
 146  
      * @throws SQLException thrown if there is an error accessing the database
 147  
      * @see java.sql.Driver#getPropertyInfo(java.lang.String, java.util.Properties)
 148  
      */
 149  
     @Override
 150  
     public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
 151  0
         return this.driver.getPropertyInfo(url, info);
 152  
     }
 153  
 
 154  
     /**
 155  
      * Returns whether or not the wrapped driver is jdbcCompliant.
 156  
      *
 157  
      * @return true if the wrapped driver is JDBC compliant; otherwise false
 158  
      * @see java.sql.Driver#jdbcCompliant()
 159  
      */
 160  
     @Override
 161  
     public boolean jdbcCompliant() {
 162  0
         return this.driver.jdbcCompliant();
 163  
     }
 164  
 
 165  
     /**
 166  
      * Standard implementation of hashCode.
 167  
      *
 168  
      * @return the hashCode of the object
 169  
      */
 170  
     @Override
 171  
     public int hashCode() {
 172  0
         int hash = 7;
 173  0
         hash = 97 * hash + (this.driver != null ? this.driver.hashCode() : 0);
 174  0
         return hash;
 175  
     }
 176  
 
 177  
     /**
 178  
      * Standard implementation of equals.
 179  
      *
 180  
      * @param obj the object to compare
 181  
      * @return returns true if the objects are equal; otherwise false
 182  
      */
 183  
     @Override
 184  
     public boolean equals(Object obj) {
 185  0
         if (obj == null) {
 186  0
             return false;
 187  
         }
 188  0
         if (getClass() != obj.getClass()) {
 189  0
             return false;
 190  
         }
 191  0
         final DriverShim other = (DriverShim) obj;
 192  0
         return this.driver == other.driver || (this.driver != null && this.driver.equals(other.driver));
 193  
     }
 194  
 
 195  
     /**
 196  
      * Standard implementation of toString().
 197  
      *
 198  
      * @return the String representation of the object
 199  
      */
 200  
     @Override
 201  
     public String toString() {
 202  7
         return "DriverShim{" + "driver=" + driver + '}';
 203  
     }
 204  
 }