View Javadoc
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 org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  
23  import java.lang.reflect.InvocationTargetException;
24  import java.lang.reflect.Method;
25  import java.sql.Connection;
26  import java.sql.Driver;
27  import java.sql.DriverPropertyInfo;
28  import java.sql.SQLException;
29  import java.sql.SQLFeatureNotSupportedException;
30  import java.util.Properties;
31  
32  /**
33   * <p>
34   * Driver shim to get around the class loader issue with the DriverManager. The following code is a nearly identical
35   * copy (with more comments and a few more methods implemented) of the DriverShim from:</p>
36   * <blockquote>http://www.kfu.com/~nsayer/Java/dyn-jdbc.html</blockquote>;
37   *
38   * @author Jeremy Long
39   * @see java.sql.Driver
40   */
41  class DriverShim implements Driver {
42  
43      /**
44       * The logger.
45       */
46      private static final Logger LOGGER = LoggerFactory.getLogger(DriverShim.class);
47      /**
48       * The database driver being wrapped.
49       */
50      private final Driver driver;
51  
52      /**
53       * Constructs a new wrapper around a Driver.
54       *
55       * @param driver the database driver to wrap
56       */
57      DriverShim(Driver driver) {
58          this.driver = driver;
59      }
60  
61      /**
62       * Wraps the underlying driver's call to acceptsURL. Returns whether or not the driver can open a connection to the
63       * given URL.
64       *
65       * @param url the URL of the database
66       * @return true if the wrapped driver can connect to the specified URL
67       * @throws SQLException thrown if there is an error connecting to the database
68       * @see java.sql.Driver#acceptsURL(java.lang.String)
69       */
70      @Override
71      public boolean acceptsURL(String url) throws SQLException {
72          return this.driver.acceptsURL(url);
73      }
74  
75      /**
76       * Wraps the call to the underlying driver's connect method.
77       *
78       * @param url the URL of the database
79       * @param info a collection of string/value pairs
80       * @return a Connection object
81       * @throws SQLException thrown if there is an error connecting to the database
82       * @see java.sql.Driver#connect(java.lang.String, java.util.Properties)
83       */
84      @Override
85      public Connection connect(String url, Properties info) throws SQLException {
86          return this.driver.connect(url, info);
87      }
88  
89      /**
90       * Returns the wrapped driver's major version number.
91       *
92       * @return the wrapped driver's major version number
93       * @see java.sql.Driver#getMajorVersion()
94       */
95      @Override
96      public int getMajorVersion() {
97          return this.driver.getMajorVersion();
98      }
99  
100     /**
101      * Returns the wrapped driver's minor version number.
102      *
103      * @return the wrapped driver's minor version number
104      * @see java.sql.Driver#getMinorVersion()
105      */
106     @Override
107     public int getMinorVersion() {
108         return this.driver.getMinorVersion();
109     }
110 
111     /**
112      * Wraps the call to the underlying driver's getParentLogger method.
113      *
114      * @return the parent's Logger
115      * @throws SQLFeatureNotSupportedException thrown if the feature is not supported
116      * @see java.sql.Driver#getParentLogger()
117      */
118     public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
119         //return driver.getParentLogger();
120         Method m = null;
121         try {
122             m = driver.getClass().getMethod("getParentLogger");
123         } catch (Throwable e) {
124             throw new SQLFeatureNotSupportedException();
125         }
126         if (m != null) {
127             try {
128                 return (java.util.logging.Logger) m.invoke(m);
129             } catch (IllegalAccessException ex) {
130                 LOGGER.trace("", ex);
131             } catch (IllegalArgumentException ex) {
132                 LOGGER.trace("", ex);
133             } catch (InvocationTargetException ex) {
134                 LOGGER.trace("", ex);
135             }
136         }
137         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         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         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         int hash = 7;
173         hash = 97 * hash + (this.driver != null ? this.driver.hashCode() : 0);
174         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         if (obj == null) {
186             return false;
187         }
188         if (getClass() != obj.getClass()) {
189             return false;
190         }
191         final DriverShim other = (DriverShim) obj;
192         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         return "DriverShim{" + "driver=" + driver + '}';
203     }
204 }