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.apache.commons.lang3.builder.EqualsBuilder;
21  import org.apache.commons.lang3.builder.HashCodeBuilder;
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import java.lang.reflect.InvocationTargetException;
26  import java.lang.reflect.Method;
27  import java.sql.Connection;
28  import java.sql.Driver;
29  import java.sql.DriverPropertyInfo;
30  import java.sql.SQLException;
31  import java.sql.SQLFeatureNotSupportedException;
32  import java.util.Properties;
33  import javax.annotation.concurrent.ThreadSafe;
34  
35  /**
36   * <p>
37   * Driver shim to get around the class loader issue with the DriverManager. The
38   * following code is a nearly identical copy (with more comments and a few more
39   * methods implemented) of the DriverShim from:</p>
40   * <blockquote>http://www.kfu.com/~nsayer/Java/dyn-jdbc.html</blockquote>;
41   *
42   * @author Jeremy Long
43   * @see java.sql.Driver
44   */
45  @ThreadSafe
46  class DriverShim implements Driver {
47  
48      /**
49       * The logger.
50       */
51      private static final Logger LOGGER = LoggerFactory.getLogger(DriverShim.class);
52      /**
53       * The database driver being wrapped.
54       */
55      private final Driver driver;
56  
57      /**
58       * Constructs a new wrapper around a Driver.
59       *
60       * @param driver the database driver to wrap
61       */
62      DriverShim(Driver driver) {
63          this.driver = driver;
64      }
65  
66      /**
67       * Wraps the underlying driver's call to acceptsURL. Returns whether or not
68       * the driver can open a connection to the given URL.
69       *
70       * @param url the URL of the database
71       * @return true if the wrapped driver can connect to the specified URL
72       * @throws SQLException thrown if there is an error connecting to the
73       * database
74       * @see java.sql.Driver#acceptsURL(java.lang.String)
75       */
76      @Override
77      public boolean acceptsURL(String url) throws SQLException {
78          return this.driver.acceptsURL(url);
79      }
80  
81      /**
82       * Wraps the call to the underlying driver's connect method.
83       *
84       * @param url the URL of the database
85       * @param info a collection of string/value pairs
86       * @return a Connection object
87       * @throws SQLException thrown if there is an error connecting to the
88       * database
89       * @see java.sql.Driver#connect(java.lang.String, java.util.Properties)
90       */
91      @Override
92      public Connection connect(String url, Properties info) throws SQLException {
93          return this.driver.connect(url, info);
94      }
95  
96      /**
97       * Returns the wrapped driver's major version number.
98       *
99       * @return the wrapped driver's major version number
100      * @see java.sql.Driver#getMajorVersion()
101      */
102     @Override
103     public int getMajorVersion() {
104         return this.driver.getMajorVersion();
105     }
106 
107     /**
108      * Returns the wrapped driver's minor version number.
109      *
110      * @return the wrapped driver's minor version number
111      * @see java.sql.Driver#getMinorVersion()
112      */
113     @Override
114     public int getMinorVersion() {
115         return this.driver.getMinorVersion();
116     }
117 
118     /**
119      * Wraps the call to the underlying driver's getParentLogger method.
120      *
121      * @return the parent's Logger
122      * @throws SQLFeatureNotSupportedException thrown if the feature is not
123      * supported
124      * @see java.sql.Driver#getParentLogger()
125      */
126     @Override
127     public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
128         //return driver.getParentLogger();
129         final Method m;
130         try {
131             m = driver.getClass().getMethod("getParentLogger");
132         } catch (Throwable e) {
133             throw new SQLFeatureNotSupportedException();
134         }
135         if (m != null) {
136             try {
137                 return (java.util.logging.Logger) m.invoke(m);
138             } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
139                 LOGGER.trace("", ex);
140             }
141         }
142         throw new SQLFeatureNotSupportedException();
143     }
144 
145     /**
146      * Wraps the call to the underlying driver's getPropertyInfo method.
147      *
148      * @param url the URL of the database
149      * @param info a collection of string/value pairs
150      * @return an array of DriverPropertyInfo objects
151      * @throws SQLException thrown if there is an error accessing the database
152      * @see java.sql.Driver#getPropertyInfo(java.lang.String,
153      * java.util.Properties)
154      */
155     @Override
156     public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
157         return this.driver.getPropertyInfo(url, info);
158     }
159 
160     /**
161      * Returns whether or not the wrapped driver is jdbcCompliant.
162      *
163      * @return true if the wrapped driver is JDBC compliant; otherwise false
164      * @see java.sql.Driver#jdbcCompliant()
165      */
166     @Override
167     public boolean jdbcCompliant() {
168         return this.driver.jdbcCompliant();
169     }
170 
171     /**
172      * Standard implementation of hashCode.
173      *
174      * @return the hashCode of the object
175      */
176     @Override
177     public int hashCode() {
178         return new HashCodeBuilder(7, 97)
179                 .append(driver)
180                 .toHashCode();
181     }
182 
183     /**
184      * Standard implementation of equals.
185      *
186      * @param obj the object to compare
187      * @return returns true if the objects are equal; otherwise false
188      */
189     @Override
190     public boolean equals(Object obj) {
191         if (obj == null || !(obj instanceof DriverShim)) {
192             return false;
193         }
194         if (this == obj) {
195             return true;
196         }
197         final DriverShim rhs = (DriverShim) obj;
198         return new EqualsBuilder()
199                 .append(driver, rhs.driver)
200                 .isEquals();
201     }
202 
203     /**
204      * Standard implementation of toString().
205      *
206      * @return the String representation of the object
207      */
208     @Override
209     public String toString() {
210         return "DriverShim{" + "driver=" + driver + '}';
211     }
212 }