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)
49 (properties.containsKey(DRIVER)
50 ? properties.getProperty(DRIVER)
51 : properties.getProperty(JDBC_DRIVER)
54 (properties.containsKey(URL)
55 ? properties.getProperty(URL)
56 : properties.getProperty(JDBC_URL)
58 String user = (String)
59 (properties.containsKey(USER)
60 ? properties.getProperty(USER)
61 : properties.getProperty(JDBC_USER)
63 String password = (String)
64 (properties.containsKey(PASS)
65 ? properties.getProperty(PASS)
66 : properties.getProperty(JDBC_PASSWORD)
69 if (driver == null || url == null || user == null)
71 log.info("No connection opened, because connection information is incomplete");
72 log.info("Driver-Class: " + driver);
73 log.info("URL: " + url);
74 log.info("User: " + user);
80 Class driverClass = classLoaderService.classForName(driver);
82 log.debug("Registering JDBC-driver " + driverClass.getName());
84 .registerDriver(new DriverProxy((Driver) driverClass.newInstance()));
87 "Opening JDBC-connection to " + properties.getProperty(URL) +
88 " as " + properties.getProperty(USERNAME) +
89 " with password " + properties.getProperty(PASSWORD)
92 connection = DriverManager.getConnection(url, user, password);
96 throw new MojoFailureException("Could not open the JDBC-connection", e);
102 if (connection == null)
105 log.debug("Closing the JDBC-connection.");
110 catch (SQLException e)
112 log.error("Error while closing the JDBC-connection: " + e.getMessage());
117 public Connection getConnection() throws SQLException
119 log.debug("Connection aquired.");
121 if (connection == null)
122 throw new SQLException("No connection available, because of insufficient connection information!");
128 public void closeConnection(Connection conn) throws SQLException
130 log.debug("Connection released");
134 public boolean supportsAggressiveRelease()
140 public boolean isUnwrappableAs(Class unwrapType)
146 public <T> T unwrap(Class<T> unwrapType)
148 throw new UnsupportedOperationException("Not supported.");
152 * Needed, because DriverManager won't pick up drivers, that were not
153 * loaded by the system-classloader!
155 * http://stackoverflow.com/questions/288828/how-to-use-a-jdbc-driver-fromodifiedm-an-arbitrary-location
157 static final class DriverProxy implements Driver
159 private final Driver target;
161 DriverProxy(Driver target)
164 throw new NullPointerException();
165 this.target = target;
168 public java.sql.Driver getTarget()
174 public boolean acceptsURL(String url) throws SQLException
176 return target.acceptsURL(url);
180 public java.sql.Connection connect(
182 java.util.Properties info
187 return target.connect(url, info);
191 public int getMajorVersion()
193 return target.getMajorVersion();
197 public int getMinorVersion()
199 return target.getMinorVersion();
203 public DriverPropertyInfo[] getPropertyInfo(
210 return target.getPropertyInfo(url, info);
214 public boolean jdbcCompliant()
216 return target.jdbcCompliant();
220 * This Method cannot be annotated with @Override, becaus the plugin
221 * will not compile then under Java 1.6!
223 public Logger getParentLogger() throws SQLFeatureNotSupportedException
225 throw new SQLFeatureNotSupportedException("Not supported, for backward-compatibility with Java 1.6");
229 public String toString()
231 return "Proxy: " + target;
235 public int hashCode()
237 return target.hashCode();
241 public boolean equals(Object obj)
243 if (!(obj instanceof DriverProxy))
245 DriverProxy other = (DriverProxy) obj;
246 return this.target.equals(other.target);