View Javadoc
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   * @author Kai Moritz
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)
49          (properties.containsKey(DRIVER)
50              ? properties.getProperty(DRIVER)
51              : properties.getProperty(JDBC_DRIVER)
52              );
53      String url = (String)
54          (properties.containsKey(URL)
55              ? properties.getProperty(URL)
56              : properties.getProperty(JDBC_URL)
57              );
58      String user = (String)
59          (properties.containsKey(USER)
60              ? properties.getProperty(USER)
61              : properties.getProperty(JDBC_USER)
62              );
63      String password = (String)
64          (properties.containsKey(PASS)
65              ? properties.getProperty(PASS)
66              : properties.getProperty(JDBC_PASSWORD)
67              );
68  
69      if (driver == null || url == null || user == null)
70      {
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);
75        return;
76      }
77  
78      try
79      {
80        Class driverClass = classLoaderService.classForName(driver);
81  
82        log.debug("Registering JDBC-driver " + driverClass.getName());
83        DriverManager
84            .registerDriver(new DriverProxy((Driver) driverClass.newInstance()));
85  
86        log.debug(
87            "Opening JDBC-connection to " + properties.getProperty(URL) +
88            " as " + properties.getProperty(USERNAME) +
89            " with password " + properties.getProperty(PASSWORD)
90            );
91      
92        connection = DriverManager.getConnection(url, user, password);
93      }
94      catch (Exception e)
95      {
96        throw new MojoFailureException("Could not open the JDBC-connection", e);
97      }
98    }
99  
100   void close()
101   {
102     if (connection == null)
103       return;
104 
105     log.debug("Closing the JDBC-connection.");
106     try
107     {
108       connection.close();
109     }
110     catch (SQLException e)
111     {
112       log.error("Error while closing the JDBC-connection: " + e.getMessage());
113     }
114   }
115 
116   @Override
117   public Connection getConnection() throws SQLException
118   {
119     log.debug("Connection aquired.");
120 
121     if (connection == null)
122       throw new SQLException("No connection available, because of insufficient connection information!");
123 
124     return connection;
125   }
126 
127   @Override
128   public void closeConnection(Connection conn) throws SQLException
129   {
130     log.debug("Connection released");
131   }
132 
133   @Override
134   public boolean supportsAggressiveRelease()
135   {
136     return false;
137   }
138 
139   @Override
140   public boolean isUnwrappableAs(Class unwrapType)
141   {
142     return false;
143   }
144 
145   @Override
146   public <T> T unwrap(Class<T> unwrapType)
147   {
148     throw new UnsupportedOperationException("Not supported.");
149   }
150 
151   /**
152    * Needed, because DriverManager won't pick up drivers, that were not
153    * loaded by the system-classloader!
154    * See:
155    * http://stackoverflow.com/questions/288828/how-to-use-a-jdbc-driver-fromodifiedm-an-arbitrary-location
156    */
157   static final class DriverProxy implements Driver
158   {
159     private final Driver target;
160 
161     DriverProxy(Driver target)
162     {
163       if (target == null)
164         throw new NullPointerException();
165       this.target = target;
166     }
167 
168     public java.sql.Driver getTarget()
169     {
170       return target;
171     }
172 
173     @Override
174     public boolean acceptsURL(String url) throws SQLException
175     {
176       return target.acceptsURL(url);
177     }
178 
179     @Override
180     public java.sql.Connection connect(
181         String url,
182         java.util.Properties info
183       )
184       throws
185         SQLException
186     {
187       return target.connect(url, info);
188     }
189 
190     @Override
191     public int getMajorVersion()
192     {
193       return target.getMajorVersion();
194     }
195 
196     @Override
197     public int getMinorVersion()
198     {
199       return target.getMinorVersion();
200     }
201 
202     @Override
203     public DriverPropertyInfo[] getPropertyInfo(
204         String url,
205         Properties info
206       )
207       throws
208         SQLException
209     {
210       return target.getPropertyInfo(url, info);
211     }
212 
213     @Override
214     public boolean jdbcCompliant()
215     {
216       return target.jdbcCompliant();
217     }
218 
219     /**
220      * This Method cannot be annotated with @Override, becaus the plugin
221      * will not compile then under Java 1.6!
222      */
223     public Logger getParentLogger() throws SQLFeatureNotSupportedException
224     {
225       throw new SQLFeatureNotSupportedException("Not supported, for backward-compatibility with Java 1.6");
226     }
227 
228     @Override
229     public String toString()
230     {
231       return "Proxy: " + target;
232     }
233 
234     @Override
235     public int hashCode()
236     {
237       return target.hashCode();
238     }
239 
240     @Override
241     public boolean equals(Object obj)
242     {
243       if (!(obj instanceof DriverProxy))
244         return false;
245       DriverProxy other = (DriverProxy) obj;
246       return this.target.equals(other.target);
247     }
248   }
249 }