|
| 1 | +package org.seasar.doma.gradle.codegen.jdbc; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.Driver; |
| 5 | +import java.sql.DriverPropertyInfo; |
| 6 | +import java.sql.SQLException; |
| 7 | +import java.sql.SQLFeatureNotSupportedException; |
| 8 | +import java.util.Objects; |
| 9 | +import java.util.Properties; |
| 10 | +import java.util.logging.Logger; |
| 11 | + |
| 12 | +/** |
| 13 | + * A wrapper for JDBC Driver that manages class loader context switching. |
| 14 | + * |
| 15 | + * <p>This wrapper ensures that all driver operations are executed with the correct class loader |
| 16 | + * context, preventing class loading issues when the driver and calling code are loaded by different |
| 17 | + * class loaders. |
| 18 | + * |
| 19 | + * @see java.sql.Driver |
| 20 | + */ |
| 21 | +public class DriverWrapper implements Driver { |
| 22 | + |
| 23 | + private final Driver driver; |
| 24 | + |
| 25 | + /** |
| 26 | + * Constructs a new DriverWrapper with the specified driver. |
| 27 | + * |
| 28 | + * @param driver the driver to wrap, must not be null |
| 29 | + * @throws NullPointerException if driver is null |
| 30 | + */ |
| 31 | + public DriverWrapper(Driver driver) { |
| 32 | + this.driver = Objects.requireNonNull(driver); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * {@inheritDoc} |
| 37 | + * |
| 38 | + * <p>Executes with the driver's class loader context. |
| 39 | + */ |
| 40 | + @Override |
| 41 | + public Connection connect(String url, Properties info) throws SQLException { |
| 42 | + return execute(() -> driver.connect(url, info)); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * {@inheritDoc} |
| 47 | + * |
| 48 | + * <p>Executes with the driver's class loader context. |
| 49 | + */ |
| 50 | + @Override |
| 51 | + public boolean acceptsURL(String url) throws SQLException { |
| 52 | + return execute(() -> driver.acceptsURL(url)); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * {@inheritDoc} |
| 57 | + * |
| 58 | + * <p>Executes with the driver's class loader context. |
| 59 | + */ |
| 60 | + @Override |
| 61 | + public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { |
| 62 | + return execute(() -> driver.getPropertyInfo(url, info)); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * {@inheritDoc} |
| 67 | + * |
| 68 | + * <p>Executes with the driver's class loader context. |
| 69 | + */ |
| 70 | + @Override |
| 71 | + public int getMajorVersion() { |
| 72 | + return execute(driver::getMajorVersion); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * {@inheritDoc} |
| 77 | + * |
| 78 | + * <p>Executes with the driver's class loader context. |
| 79 | + */ |
| 80 | + @Override |
| 81 | + public int getMinorVersion() { |
| 82 | + return execute(driver::getMinorVersion); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * {@inheritDoc} |
| 87 | + * |
| 88 | + * <p>Executes with the driver's class loader context. |
| 89 | + */ |
| 90 | + @Override |
| 91 | + public boolean jdbcCompliant() { |
| 92 | + return execute(driver::jdbcCompliant); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * {@inheritDoc} |
| 97 | + * |
| 98 | + * <p>Executes with the driver's class loader context. |
| 99 | + */ |
| 100 | + @Override |
| 101 | + public Logger getParentLogger() throws SQLFeatureNotSupportedException { |
| 102 | + return execute(driver::getParentLogger); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Executes an operation with the driver's class loader as the context class loader. |
| 107 | + * |
| 108 | + * <p>This method temporarily switches the thread's context class loader to the driver's class |
| 109 | + * loader, executes the operation, and then restores the original class loader. This ensures that |
| 110 | + * the driver can properly load its internal classes and resources. |
| 111 | + * |
| 112 | + * @param <R> the return type of the executable |
| 113 | + * @param <TH> the type of exception that may be thrown |
| 114 | + * @param executable the operation to execute |
| 115 | + * @return the result of the executable operation |
| 116 | + * @throws TH if the executable operation throws an exception |
| 117 | + */ |
| 118 | + private <R, TH extends Exception> R execute(Executable<R, TH> executable) throws TH { |
| 119 | + var classLoader = Thread.currentThread().getContextClassLoader(); |
| 120 | + Thread.currentThread().setContextClassLoader(driver.getClass().getClassLoader()); |
| 121 | + try { |
| 122 | + return executable.execute(); |
| 123 | + } finally { |
| 124 | + Thread.currentThread().setContextClassLoader(classLoader); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Represents an executable operation that can return a result and throw an exception. |
| 130 | + * |
| 131 | + * @param <R> the return type |
| 132 | + * @param <TH> the type of exception that may be thrown |
| 133 | + */ |
| 134 | + private interface Executable<R, TH extends Exception> { |
| 135 | + /** |
| 136 | + * Executes the operation. |
| 137 | + * |
| 138 | + * @return the result of the operation |
| 139 | + * @throws TH if an error occurs during execution |
| 140 | + */ |
| 141 | + R execute() throws TH; |
| 142 | + } |
| 143 | +} |
0 commit comments