1 package de.juplo.plugins.hibernate;
3 import java.sql.Connection;
4 import java.sql.Driver;
5 import java.sql.DriverManager;
6 import java.sql.DriverPropertyInfo;
7 import java.sql.SQLException;
8 import java.sql.SQLFeatureNotSupportedException;
9 import java.util.Properties;
10 import java.util.logging.Logger;
11 import org.apache.maven.plugin.MojoFailureException;
12 import org.apache.maven.plugin.logging.Log;
13 import static org.eclipse.aether.repository.AuthenticationContext.PASSWORD;
14 import static org.eclipse.aether.repository.AuthenticationContext.USERNAME;
15 import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
16 import static org.hibernate.cfg.AvailableSettings.DRIVER;
17 import static org.hibernate.cfg.AvailableSettings.PASS;
18 import static org.hibernate.cfg.AvailableSettings.URL;
19 import static org.hibernate.cfg.AvailableSettings.USER;
20 import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
21 import static org.hibernate.jpa.AvailableSettings.JDBC_DRIVER;
22 import static org.hibernate.jpa.AvailableSettings.JDBC_PASSWORD;
23 import static org.hibernate.jpa.AvailableSettings.JDBC_URL;
24 import static org.hibernate.jpa.AvailableSettings.JDBC_USER;
30 class SimpleConnectionProvider implements ConnectionProvider
32 private final Log log;
34 private Connection connection;
37 SimpleConnectionProvider(Log log)
43 void open(ClassLoaderService classLoaderService, Properties properties)
48 String driver = (String)properties.getProperty(DRIVER);
49 String url = (String)properties.getProperty(URL);
50 String user = (String)properties.getProperty(USER);
51 String password = (String)properties.getProperty(PASS);
53 if (driver == null || url == null)
55 log.info("No connection opened, because connection information is incomplete");
56 log.info("Driver-Class: " + driver);
57 log.info("URL: " + url);
63 Class driverClass = classLoaderService.classForName(driver);
65 log.debug("Registering JDBC-driver " + driverClass.getName());
67 .registerDriver(new DriverProxy((Driver) driverClass.newInstance()));
70 "Opening JDBC-connection to " + url +
72 " with password " + password
75 connection = DriverManager.getConnection(url, user, password);
79 log.info("Could not open the JDBC-connection: " + e.getMessage());
85 if (connection == null)
88 log.debug("Closing the JDBC-connection.");
93 catch (SQLException e)
95 log.error("Error while closing the JDBC-connection: " + e.getMessage());
100 public Connection getConnection() throws SQLException
102 log.debug("Connection aquired.");
104 if (connection == null)
105 throw new SQLException("No connection available, because of insufficient connection information!");
111 public void closeConnection(Connection conn) throws SQLException
113 log.debug("Connection released");
117 public boolean supportsAggressiveRelease()
123 public boolean isUnwrappableAs(Class unwrapType)
129 public <T> T unwrap(Class<T> unwrapType)
131 throw new UnsupportedOperationException("Not supported.");
135 * Needed, because DriverManager won't pick up drivers, that were not
136 * loaded by the system-classloader!
138 * http://stackoverflow.com/questions/288828/how-to-use-a-jdbc-driver-fromodifiedm-an-arbitrary-location
140 static final class DriverProxy implements Driver
142 private final Driver target;
144 DriverProxy(Driver target)
147 throw new NullPointerException();
148 this.target = target;
151 public java.sql.Driver getTarget()
157 public boolean acceptsURL(String url) throws SQLException
159 return target.acceptsURL(url);
163 public java.sql.Connection connect(
165 java.util.Properties info
170 return target.connect(url, info);
174 public int getMajorVersion()
176 return target.getMajorVersion();
180 public int getMinorVersion()
182 return target.getMinorVersion();
186 public DriverPropertyInfo[] getPropertyInfo(
193 return target.getPropertyInfo(url, info);
197 public boolean jdbcCompliant()
199 return target.jdbcCompliant();
203 * This Method cannot be annotated with @Override, becaus the plugin
204 * will not compile then under Java 1.6!
206 public Logger getParentLogger() throws SQLFeatureNotSupportedException
208 throw new SQLFeatureNotSupportedException("Not supported, for backward-compatibility with Java 1.6");
212 public String toString()
214 return "Proxy: " + target;
218 public int hashCode()
220 return target.hashCode();
224 public boolean equals(Object obj)
226 if (!(obj instanceof DriverProxy))
228 DriverProxy other = (DriverProxy) obj;
229 return this.target.equals(other.target);