View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.util.pool.service;
35  
36  import fr.paris.lutece.util.env.EnvUtil;
37  
38  import java.sql.Connection;
39  import java.sql.Driver;
40  import java.sql.DriverManager;
41  import java.sql.SQLException;
42  
43  import java.util.Map;
44  
45  import javax.sql.DataSource;
46  
47  import org.apache.logging.log4j.Logger;
48  
49  /**
50   * Lutece Connection Service
51   */
52  public class LuteceConnectionService implements ConnectionService
53  {
54      private String _strPoolName;
55      private Logger _logger;
56      private ConnectionPool _connPool;
57  
58      /**
59       * Sets the pool name
60       * 
61       * @param strPoolName
62       *            The pool name
63       */
64      @Override
65      public void setPoolName( String strPoolName )
66      {
67          _strPoolName = strPoolName;
68      }
69  
70      /**
71       * Returns the pool name
72       * 
73       * @return The pool name
74       */
75      @Override
76      public String getPoolName( )
77      {
78          return _strPoolName;
79      }
80  
81      /**
82       * Sets the logger
83       * 
84       * @param logger
85       *            The logger
86       */
87      @Override
88      public void setLogger( Logger logger )
89      {
90          _logger = logger;
91      }
92  
93      /**
94       * Gets the pool logger
95       * 
96       * @return The logger
97       */
98      @Override
99      public Logger getLogger( )
100     {
101         return _logger;
102     }
103 
104     /**
105      * Initializes the connection pool
106      * 
107      * @param htParamsConnectionPool
108      *            Pool parameters
109      */
110     @Override
111     public void init( Map<String, String> htParamsConnectionPool )
112     {
113         String url = htParamsConnectionPool.get( getPoolName( ) + ".url" );
114 
115         if ( url == null )
116         {
117             _logger.error( "No URL specified for the pool {}", getPoolName( ) );
118         }
119         else
120         {
121             url = EnvUtil.evaluate( url, EnvUtil.PREFIX_ENV );
122         }
123 
124         String user = htParamsConnectionPool.get( getPoolName( ) + ".user" );
125 
126         if ( user == null )
127         {
128             _logger.error( "No user specified for the pool {}", getPoolName( ) );
129         }
130         else
131         {
132             user = EnvUtil.evaluate( user, EnvUtil.PREFIX_ENV );
133         }
134 
135         String password = htParamsConnectionPool.get( getPoolName( ) + ".password" );
136 
137         if ( password == null )
138         {
139             _logger.error( "No password specified for the pool " + getPoolName( ) );
140         }
141         else
142         {
143             password = EnvUtil.evaluate( password, EnvUtil.PREFIX_ENV );
144         }
145 
146         // load of the driver
147         String strDiverClassName = htParamsConnectionPool.get( getPoolName( ) + ".driver" );
148 
149         try
150         {
151             Driver driver = (Driver) Class.forName( strDiverClassName ).newInstance( );
152             DriverManager.registerDriver( driver );
153             _logger.info( "Registered JDBC driver {}", strDiverClassName );
154         }
155         catch( NullPointerException e )
156         {
157             _logger.error( "Can't register JDBC driver: {} because the property driver is not defined", strDiverClassName, e );
158         }
159         catch( Exception e )
160         {
161             _logger.error( "Can't register JDBC driver: {}", strDiverClassName, e );
162         }
163 
164         int maxConns = ( htParamsConnectionPool.get( getPoolName( ) + ".maxconns" ) == null ) ? 0
165                 : Integer.parseInt( htParamsConnectionPool.get( getPoolName( ) + ".maxconns" ) );
166 
167         int initConns = ( htParamsConnectionPool.get( getPoolName( ) + ".initconns" ) == null ) ? 0
168                 : Integer.parseInt( htParamsConnectionPool.get( getPoolName( ) + ".initconns" ) );
169 
170         int timeOut = ( htParamsConnectionPool.get( getPoolName( ) + ".logintimeout" ) == null ) ? 5
171                 : Integer.parseInt( htParamsConnectionPool.get( getPoolName( ) + ".logintimeout" ) );
172 
173         String checkValidConnectionSql = ( htParamsConnectionPool.get( getPoolName( ) + ".checkvalidconnectionsql" ) == null ) ? ""
174                 : htParamsConnectionPool.get( getPoolName( ) + ".checkvalidconnectionsql" );
175 
176         _connPool = new ConnectionPool( getPoolName( ), url, user, password, maxConns, initConns, timeOut, _logger, checkValidConnectionSql );
177     }
178 
179     /**
180      * Get a connection
181      * 
182      * @return A connection
183      */
184     @Override
185     public Connection getConnection( )
186     {
187         try
188         {
189             return _connPool.getConnection( );
190         }
191         catch( SQLException e )
192         {
193             _logger.error( e.getMessage( ), e );
194 
195             return null;
196         }
197     }
198 
199     /**
200      * Free the connection
201      * 
202      * @param conn
203      *            The connection to release
204      */
205     @Override
206     public void freeConnection( Connection conn )
207     {
208         _connPool.freeConnection( conn );
209     }
210 
211     /**
212      * Release the pool
213      */
214     @Override
215     public void release( )
216     {
217         _connPool.release( );
218     }
219 
220     /**
221      * Returns the connection pool
222      * 
223      * @return the connection pool
224      */
225     public ConnectionPool getConnectionPool( )
226     {
227         return _connPool;
228     }
229 
230     /**
231      * {@inheritDoc}
232      */
233     @Override
234     public int getCurrentConnections( )
235     {
236         return _connPool.getConnectionCount( );
237     }
238 
239     /**
240      * {@inheritDoc}
241      */
242     @Override
243     public int getMaxConnections( )
244     {
245         return _connPool.getMaxConnectionCount( );
246     }
247 
248     /**
249      * {@inheritDoc }
250      */
251     @Override
252     public String getPoolProvider( )
253     {
254         return "Lutece";
255     }
256 
257     /**
258      * {@inheritDoc }
259      */
260     @Override
261     public DataSource getDataSource( )
262     {
263         return getConnectionPool( );
264     }
265 }