001/*
002MIT License
003
004Copyright (c) 2020 FBSQL Team
005
006Permission is hereby granted, free of charge, to any person obtaining a copy
007of this software and associated documentation files (the "Software"), to deal
008in the Software without restriction, including without limitation the rights
009to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010copies of the Software, and to permit persons to whom the Software is
011furnished to do so, subject to the following conditions:
012
013The above copyright notice and this permission notice shall be included in all
014copies or substantial portions of the Software.
015
016THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
022SOFTWARE.
023
024Home:   https://fbsql.github.io
025E-Mail: fbsql.team@gmail.com
026*/
027
028package org.fbsql.servlet;
029
030import java.sql.Connection;
031import java.sql.Driver;
032import java.sql.DriverPropertyInfo;
033import java.sql.SQLException;
034import java.sql.SQLFeatureNotSupportedException;
035import java.util.Properties;
036import java.util.logging.Logger;
037
038/**
039 * Allows one to load a JDBC driver at runtime.
040 * Workaround which allows to use URLClassLoader for JDBC driver loading,
041 * because the DriverManager will refuse to use a driver not loaded by
042 * the system ClassLoader
043 * The workaround for this is to use a shim class that implements
044 * java.sql.Driver. This shim class will do nothing but call the methods
045 * of an instance of a JDBC driver that is loaded dynamically. This works
046 * because DriverShim is loaded by the system class loader, and DriverManager
047 * doesn't care that it invokes a class that wasn't.
048 * 
049 * Note that we must perform the registration on the instance
050 * ourselves.
051 * Adapted from http://www.kfu.com/~nsayer/Java/dyn-jdbc.html
052 */
053public class DriverShim implements Driver {
054
055        /**
056         * The JDBC driver we're wrapping.
057         */
058        private Driver driver;
059
060        /**
061         * Constructs a DriverShim over the given driver in order to make it look
062         * like it came from this classloader.
063         * 
064         * @param driver the JDBC driver we're wrapping
065         */
066        public DriverShim(Driver driver) {
067                this.driver = driver;
068        }
069
070        /**
071         * Retrieves whether the driver thinks that it can open a connection
072         * to the given URL.  Typically drivers will return <code>true</code> if they
073         * understand the sub-protocol specified in the URL and <code>false</code> if
074         * they do not.
075         *
076         * @param url the URL of the database
077         * @return <code>true</code> if this driver understands the given URL;
078         *         <code>false</code> otherwise
079         * @exception SQLException if a database access error occurs or the url is
080         * {@code null}
081         */
082        @Override
083        public boolean acceptsURL(String jdbcUrl) throws SQLException {
084                return this.driver.acceptsURL(jdbcUrl);
085        }
086
087        @Override
088        public Connection connect(String jdbcUrl, Properties properties) throws SQLException {
089                return this.driver.connect(jdbcUrl, properties);
090        }
091
092        /**
093         * Retrieves the driver's major version number. Initially this should be 1.
094         *
095         * @return this driver's major version number
096         */
097        @Override
098        public int getMajorVersion() {
099                return this.driver.getMajorVersion();
100        }
101
102        /**
103         * Gets the driver's minor version number. Initially this should be 0.
104         * @return this driver's minor version number
105         */
106        @Override
107        public int getMinorVersion() {
108                return this.driver.getMinorVersion();
109        }
110
111        /**
112        * Gets information about the possible properties for this driver.
113        * <P>
114        * The <code>getPropertyInfo</code> method is intended to allow a generic
115        * GUI tool to discover what properties it should prompt
116        * a human for in order to get
117        * enough information to connect to a database.  Note that depending on
118        * the values the human has supplied so far, additional values may become
119        * necessary, so it may be necessary to iterate though several calls
120        * to the <code>getPropertyInfo</code> method.
121        *
122        * @param url the URL of the database to which to connect
123        * @param info a proposed list of tag/value pairs that will be sent on
124        *          connect open
125        * @return an array of <code>DriverPropertyInfo</code> objects describing
126        *          possible properties.  This array may be an empty array if
127        *          no properties are required.
128        * @exception SQLException if a database access error occurs
129        */
130        @Override
131        public DriverPropertyInfo[] getPropertyInfo(String jdbcUrl, Properties properties) throws SQLException {
132                return this.driver.getPropertyInfo(jdbcUrl, properties);
133        }
134
135        /**
136         * Reports whether this driver is a genuine JDBC
137         * Compliant&trade; driver.
138         * A driver may only report <code>true</code> here if it passes the JDBC
139         * compliance tests; otherwise it is required to return <code>false</code>.
140         * <P>
141         * JDBC compliance requires full support for the JDBC API and full support
142         * for SQL 92 Entry Level.  It is expected that JDBC compliant drivers will
143         * be available for all the major commercial databases.
144         * <P>
145         * This method is not intended to encourage the development of non-JDBC
146         * compliant drivers, but is a recognition of the fact that some vendors
147         * are interested in using the JDBC API and framework for lightweight
148         * databases that do not support full database functionality, or for
149         * special databases such as document information retrieval where a SQL
150         * implementation may not be feasible.
151         * 
152         * @return <code>true</code> if this driver is JDBC Compliant;
153         *         <code>false</code> otherwise
154         */
155        @Override
156        public boolean jdbcCompliant() {
157                return this.driver.jdbcCompliant();
158        }
159
160        /**
161        * Return the parent Logger of all the Loggers used by this driver. This
162        * should be the Logger farthest from the root Logger that is
163        * still an ancestor of all of the Loggers used by this driver. Configuring
164        * this Logger will affect all of the log messages generated by the driver.
165        * In the worst case, this may be the root Logger.
166        *
167        * @return the parent Logger for this driver
168        * @throws SQLFeatureNotSupportedException if the driver does not use
169        * {@code java.util.logging}.
170        * @since 1.7
171        */
172        @Override
173        public Logger getParentLogger() throws SQLFeatureNotSupportedException {
174                return this.driver.getParentLogger();
175        }
176}
177
178/*
179Please contact FBSQL Team by E-Mail fbsql.team@gmail.com
180or visit https://fbsql.github.io if you need additional
181information or have any questions.
182*/
183
184/* EOF */