View Javadoc
1   package net.sumaris.core.dao.technical.hibernate;
2   
3   /*-
4    * #%L
5    * SUMARiS :: Sumaris Core Shared
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2018 SUMARiS Consortium
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (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 General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  
28  import org.hibernate.HibernateException;
29  import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
30  import org.hibernate.exception.spi.Configurable;
31  import org.hibernate.service.UnknownUnwrapTypeException;
32  import org.hibernate.service.spi.Stoppable;
33  
34  import javax.sql.DataSource;
35  import java.sql.Connection;
36  import java.sql.SQLException;
37  import java.util.Properties;
38  
39  /**
40   * <p>HibernateConnectionProvider class.</p>
41   */
42  public class HibernateConnectionProvider implements ConnectionProvider,Configurable,Stoppable {
43  
44      private static final long serialVersionUID = 6463355546534159477L;
45      
46  	private static DataSource dataSource = null;
47  
48      private static Connection connection = null;
49  
50      /** {@inheritDoc} */
51      @Override
52      public void configure(Properties props) throws HibernateException {
53          if (dataSource == null && connection == null) {
54              throw new HibernateException("DataSource must be set before using ConnectionProvider.");
55          }
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public Connection getConnection() throws SQLException {
61          return dataSource != null ? dataSource.getConnection() : connection;
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      public void closeConnection(Connection conn) throws SQLException {
67          if (conn != null && !conn.isClosed()) {
68              conn.close();
69          }
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public void stop(){
75          // Release datasource
76          dataSource = null;
77          connection = null;
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public boolean supportsAggressiveRelease() {
83          // TODO A quoi cela sert il ?
84          return false;
85      }
86  
87      /**
88       * <p>Getter for the field <code>dataSource</code>.</p>
89       *
90       * @return a {@link DataSource} object.
91       */
92      public static DataSource getDataSource() {
93          return dataSource;
94      }
95  
96      /**
97       * <p>Setter for the field <code>dataSource</code>.</p>
98       *
99       * @param dataSource a {@link DataSource} object.
100      */
101     public static void setDataSource(DataSource dataSource) {
102         HibernateConnectionProvider.dataSource = dataSource;
103     }
104 
105     /**
106      * <p>Setter for the field <code>dataSource</code>.</p>
107      *
108      * @param dataSource a {@link DataSource} object.
109      */
110     public static void setConnection(Connection connection) {
111         HibernateConnectionProvider.connection = connection;
112     }
113 
114     /** {@inheritDoc} */
115     @SuppressWarnings("rawtypes")
116 	@Override
117     public boolean isUnwrappableAs(Class unwrapType) {
118 		return ConnectionProvider.class.equals(unwrapType) ||
119 		HibernateConnectionProvider.class.isAssignableFrom(unwrapType);
120     }
121 
122 	/** {@inheritDoc} */
123 	@SuppressWarnings("unchecked")
124     @Override
125     public <T> T unwrap(Class<T> unwrapType) {
126 		if (ConnectionProvider.class.equals(unwrapType) ||
127 				HibernateConnectionProvider.class.isAssignableFrom(unwrapType)) {
128 			return (T) this;
129 		} else {
130 			throw new UnknownUnwrapTypeException(unwrapType);
131 		}
132     }
133 
134 }