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     @Override
119     public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
120         //return driver.getParentLogger();
121         Method m = null;
122         try {
123             m = driver.getClass().getMethod("getParentLogger");
124         } catch (Throwable e) {
125             throw new SQLFeatureNotSupportedException();
126         }
127         if (m != null) {
128             try {
129                 return (java.util.logging.Logger) m.invoke(m);
130             } catch (IllegalAccessException ex) {
131                 LOGGER.trace("", ex);
132             } catch (IllegalArgumentException ex) {
133                 LOGGER.trace("", ex);
134             } catch (InvocationTargetException ex) {
135                 LOGGER.trace("", ex);
136             }
137         }
138         throw new SQLFeatureNotSupportedException();
139     }
140 
141     /**
142      * Wraps the call to the underlying driver's getPropertyInfo method.
143      *
144      * @param url the URL of the database
145      * @param info a collection of string/value pairs
146      * @return an array of DriverPropertyInfo objects
147      * @throws SQLException thrown if there is an error accessing the database
148      * @see java.sql.Driver#getPropertyInfo(java.lang.String, java.util.Properties)
149      */
150     @Override
151     public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
152         return this.driver.getPropertyInfo(url, info);
153     }
154 
155     /**
156      * Returns whether or not the wrapped driver is jdbcCompliant.
157      *
158      * @return true if the wrapped driver is JDBC compliant; otherwise false
159      * @see java.sql.Driver#jdbcCompliant()
160      */
161     @Override
162     public boolean jdbcCompliant() {
163         return this.driver.jdbcCompliant();
164     }
165 
166     /**
167      * Standard implementation of hashCode.
168      *
169      * @return the hashCode of the object
170      */
171     @Override
172     public int hashCode() {
173         int hash = 7;
174         hash = 97 * hash + (this.driver != null ? this.driver.hashCode() : 0);
175         return hash;
176     }
177 
178     /**
179      * Standard implementation of equals.
180      *
181      * @param obj the object to compare
182      * @return returns true if the objects are equal; otherwise false
183      */
184     @Override
185     public boolean equals(Object obj) {
186         if (obj == null) {
187             return false;
188         }
189         if (getClass() != obj.getClass()) {
190             return false;
191         }
192         final DriverShim other = (DriverShim) obj;
193         return this.driver == other.driver || (this.driver != null && this.driver.equals(other.driver));
194     }
195 
196     /**
197      * Standard implementation of toString().
198      *
199      * @return the String representation of the object
200      */
201     @Override
202     public String toString() {
203         return "DriverShim{" + "driver=" + driver + '}';
204     }
205 }