View Javadoc
1   package fr.ifremer.quadrige3.core.dao.technical.hibernate;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Core Shared
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  import org.hibernate.HibernateException;
28  import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
29  import org.hibernate.exception.spi.Configurable;
30  import org.hibernate.service.UnknownUnwrapTypeException;
31  import org.hibernate.service.spi.Stoppable;
32  
33  import javax.sql.DataSource;
34  import java.sql.Connection;
35  import java.sql.SQLException;
36  import java.util.Properties;
37  
38  /**
39   * <p>HibernateConnectionProvider class.</p>
40   */
41  public class HibernateConnectionProvider implements ConnectionProvider,Configurable,Stoppable {
42  
43      private static final long serialVersionUID = 6463355546534159477L;
44      
45  	private static DataSource dataSource = null;
46  
47      /** {@inheritDoc} */
48      @Override
49      public void configure(Properties props) throws HibernateException {
50          if (dataSource == null) {
51              throw new HibernateException("DataSource must be set before using ConnectionProvider.");
52          }
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public Connection getConnection() throws SQLException {
58          return dataSource.getConnection();
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public void closeConnection(Connection conn) throws SQLException {
64          if (conn != null && !conn.isClosed()) {
65              conn.close();
66          }
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public void stop(){
72          // Release datasource
73          dataSource = null;
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public boolean supportsAggressiveRelease() {
79          // TODO A quoi cela sert il ?
80          return false;
81      }
82  
83      /**
84       * <p>Getter for the field <code>dataSource</code>.</p>
85       *
86       * @return a {@link javax.sql.DataSource} object.
87       */
88      public static DataSource getDataSource() {
89          return dataSource;
90      }
91  
92      /**
93       * <p>Setter for the field <code>dataSource</code>.</p>
94       *
95       * @param dataSource a {@link javax.sql.DataSource} object.
96       */
97      public static void setDataSource(DataSource dataSource) {
98          HibernateConnectionProvider.dataSource = dataSource;
99      }
100 
101     /** {@inheritDoc} */
102     @SuppressWarnings("rawtypes")
103 	@Override
104     public boolean isUnwrappableAs(Class unwrapType) {
105 		return ConnectionProvider.class.equals(unwrapType) ||
106 		HibernateConnectionProvider.class.isAssignableFrom(unwrapType);
107     }
108 
109 	/** {@inheritDoc} */
110 	@SuppressWarnings("unchecked")
111     @Override
112     public <T> T unwrap(Class<T> unwrapType) {
113 		if (ConnectionProvider.class.equals(unwrapType) ||
114 				HibernateConnectionProvider.class.isAssignableFrom(unwrapType)) {
115 			return (T) this;
116 		} else {
117 			throw new UnknownUnwrapTypeException(unwrapType);
118 		}
119     }
120 
121 }