1 package de.juplo.plugins.hibernate;
2
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;
25
26
27
28
29
30 class SimpleConnectionProvider implements ConnectionProvider
31 {
32 private final Log log;
33
34 private Connection connection;
35
36
37 SimpleConnectionProvider(Log log)
38 {
39 this.log = log;
40 }
41
42
43 void open(ClassLoaderService classLoaderService, Properties properties)
44 throws
45 MojoFailureException
46 {
47
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);
52
53 if (driver == null || url == null)
54 {
55 log.info("No connection opened, because connection information is incomplete");
56 log.info("Driver-Class: " + driver);
57 log.info("URL: " + url);
58 return;
59 }
60
61 try
62 {
63 Class driverClass = classLoaderService.classForName(driver);
64
65 log.debug("Registering JDBC-driver " + driverClass.getName());
66 DriverManager
67 .registerDriver(new DriverProxy((Driver) driverClass.newInstance()));
68
69 log.debug(
70 "Opening JDBC-connection to " + url +
71 " as " + user +
72 " with password " + password
73 );
74
75 connection = DriverManager.getConnection(url, user, password);
76 }
77 catch (Exception e)
78 {
79 log.info("Could not open the JDBC-connection: " + e.getMessage());
80 }
81 }
82
83 void close()
84 {
85 if (connection == null)
86 return;
87
88 log.debug("Closing the JDBC-connection.");
89 try
90 {
91 connection.close();
92 }
93 catch (SQLException e)
94 {
95 log.error("Error while closing the JDBC-connection: " + e.getMessage());
96 }
97 }
98
99 @Override
100 public Connection getConnection() throws SQLException
101 {
102 log.debug("Connection aquired.");
103
104 if (connection == null)
105 throw new SQLException("No connection available, because of insufficient connection information!");
106
107 return connection;
108 }
109
110 @Override
111 public void closeConnection(Connection conn) throws SQLException
112 {
113 log.debug("Connection released");
114 }
115
116 @Override
117 public boolean supportsAggressiveRelease()
118 {
119 return false;
120 }
121
122 @Override
123 public boolean isUnwrappableAs(Class unwrapType)
124 {
125 return false;
126 }
127
128 @Override
129 public <T> T unwrap(Class<T> unwrapType)
130 {
131 throw new UnsupportedOperationException("Not supported.");
132 }
133
134
135
136
137
138
139
140 static final class DriverProxy implements Driver
141 {
142 private final Driver target;
143
144 DriverProxy(Driver target)
145 {
146 if (target == null)
147 throw new NullPointerException();
148 this.target = target;
149 }
150
151 public java.sql.Driver getTarget()
152 {
153 return target;
154 }
155
156 @Override
157 public boolean acceptsURL(String url) throws SQLException
158 {
159 return target.acceptsURL(url);
160 }
161
162 @Override
163 public java.sql.Connection connect(
164 String url,
165 java.util.Properties info
166 )
167 throws
168 SQLException
169 {
170 return target.connect(url, info);
171 }
172
173 @Override
174 public int getMajorVersion()
175 {
176 return target.getMajorVersion();
177 }
178
179 @Override
180 public int getMinorVersion()
181 {
182 return target.getMinorVersion();
183 }
184
185 @Override
186 public DriverPropertyInfo[] getPropertyInfo(
187 String url,
188 Properties info
189 )
190 throws
191 SQLException
192 {
193 return target.getPropertyInfo(url, info);
194 }
195
196 @Override
197 public boolean jdbcCompliant()
198 {
199 return target.jdbcCompliant();
200 }
201
202
203
204
205
206 public Logger getParentLogger() throws SQLFeatureNotSupportedException
207 {
208 throw new SQLFeatureNotSupportedException("Not supported, for backward-compatibility with Java 1.6");
209 }
210
211 @Override
212 public String toString()
213 {
214 return "Proxy: " + target;
215 }
216
217 @Override
218 public int hashCode()
219 {
220 return target.hashCode();
221 }
222
223 @Override
224 public boolean equals(Object obj)
225 {
226 if (!(obj instanceof DriverProxy))
227 return false;
228 DriverProxy other = (DriverProxy) obj;
229 return this.target.equals(other.target);
230 }
231 }
232 }