LuteceConnectionService.java

  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. import fr.paris.lutece.util.env.EnvUtil;

  36. import java.sql.Connection;
  37. import java.sql.Driver;
  38. import java.sql.DriverManager;
  39. import java.sql.SQLException;

  40. import java.util.Map;

  41. import javax.sql.DataSource;

  42. import org.apache.logging.log4j.Logger;

  43. /**
  44.  * Lutece Connection Service
  45.  */
  46. public class LuteceConnectionService implements ConnectionService
  47. {
  48.     private String _strPoolName;
  49.     private Logger _logger;
  50.     private ConnectionPool _connPool;

  51.     /**
  52.      * Sets the pool name
  53.      *
  54.      * @param strPoolName
  55.      *            The pool name
  56.      */
  57.     @Override
  58.     public void setPoolName( String strPoolName )
  59.     {
  60.         _strPoolName = strPoolName;
  61.     }

  62.     /**
  63.      * Returns the pool name
  64.      *
  65.      * @return The pool name
  66.      */
  67.     @Override
  68.     public String getPoolName( )
  69.     {
  70.         return _strPoolName;
  71.     }

  72.     /**
  73.      * Sets the logger
  74.      *
  75.      * @param logger
  76.      *            The logger
  77.      */
  78.     @Override
  79.     public void setLogger( Logger logger )
  80.     {
  81.         _logger = logger;
  82.     }

  83.     /**
  84.      * Gets the pool logger
  85.      *
  86.      * @return The logger
  87.      */
  88.     @Override
  89.     public Logger getLogger( )
  90.     {
  91.         return _logger;
  92.     }

  93.     /**
  94.      * Initializes the connection pool
  95.      *
  96.      * @param htParamsConnectionPool
  97.      *            Pool parameters
  98.      */
  99.     @Override
  100.     public void init( Map<String, String> htParamsConnectionPool )
  101.     {
  102.         String url = htParamsConnectionPool.get( getPoolName( ) + ".url" );

  103.         if ( url == null )
  104.         {
  105.             _logger.error( "No URL specified for the pool {}", getPoolName( ) );
  106.         }
  107.         else
  108.         {
  109.             url = EnvUtil.evaluate( url, EnvUtil.PREFIX_ENV );
  110.         }

  111.         String user = htParamsConnectionPool.get( getPoolName( ) + ".user" );

  112.         if ( user == null )
  113.         {
  114.             _logger.error( "No user specified for the pool {}", getPoolName( ) );
  115.         }
  116.         else
  117.         {
  118.             user = EnvUtil.evaluate( user, EnvUtil.PREFIX_ENV );
  119.         }

  120.         String password = htParamsConnectionPool.get( getPoolName( ) + ".password" );

  121.         if ( password == null )
  122.         {
  123.             _logger.error( "No password specified for the pool " + getPoolName( ) );
  124.         }
  125.         else
  126.         {
  127.             password = EnvUtil.evaluate( password, EnvUtil.PREFIX_ENV );
  128.         }

  129.         // load of the driver
  130.         String strDiverClassName = htParamsConnectionPool.get( getPoolName( ) + ".driver" );

  131.         try
  132.         {
  133.             Driver driver = (Driver) Class.forName( strDiverClassName ).newInstance( );
  134.             DriverManager.registerDriver( driver );
  135.             _logger.info( "Registered JDBC driver {}", strDiverClassName );
  136.         }
  137.         catch( NullPointerException e )
  138.         {
  139.             _logger.error( "Can't register JDBC driver: {} because the property driver is not defined", strDiverClassName, e );
  140.         }
  141.         catch( Exception e )
  142.         {
  143.             _logger.error( "Can't register JDBC driver: {}", strDiverClassName, e );
  144.         }

  145.         int maxConns = ( htParamsConnectionPool.get( getPoolName( ) + ".maxconns" ) == null ) ? 0
  146.                 : Integer.parseInt( htParamsConnectionPool.get( getPoolName( ) + ".maxconns" ) );

  147.         int initConns = ( htParamsConnectionPool.get( getPoolName( ) + ".initconns" ) == null ) ? 0
  148.                 : Integer.parseInt( htParamsConnectionPool.get( getPoolName( ) + ".initconns" ) );

  149.         int timeOut = ( htParamsConnectionPool.get( getPoolName( ) + ".logintimeout" ) == null ) ? 5
  150.                 : Integer.parseInt( htParamsConnectionPool.get( getPoolName( ) + ".logintimeout" ) );

  151.         String checkValidConnectionSql = ( htParamsConnectionPool.get( getPoolName( ) + ".checkvalidconnectionsql" ) == null ) ? ""
  152.                 : htParamsConnectionPool.get( getPoolName( ) + ".checkvalidconnectionsql" );

  153.         _connPool = new ConnectionPool( getPoolName( ), url, user, password, maxConns, initConns, timeOut, _logger, checkValidConnectionSql );
  154.     }

  155.     /**
  156.      * Get a connection
  157.      *
  158.      * @return A connection
  159.      */
  160.     @Override
  161.     public Connection getConnection( )
  162.     {
  163.         try
  164.         {
  165.             return _connPool.getConnection( );
  166.         }
  167.         catch( SQLException e )
  168.         {
  169.             _logger.error( e.getMessage( ), e );

  170.             return null;
  171.         }
  172.     }

  173.     /**
  174.      * Free the connection
  175.      *
  176.      * @param conn
  177.      *            The connection to release
  178.      */
  179.     @Override
  180.     public void freeConnection( Connection conn )
  181.     {
  182.         _connPool.freeConnection( conn );
  183.     }

  184.     /**
  185.      * Release the pool
  186.      */
  187.     @Override
  188.     public void release( )
  189.     {
  190.         _connPool.release( );
  191.     }

  192.     /**
  193.      * Returns the connection pool
  194.      *
  195.      * @return the connection pool
  196.      */
  197.     public ConnectionPool getConnectionPool( )
  198.     {
  199.         return _connPool;
  200.     }

  201.     /**
  202.      * {@inheritDoc}
  203.      */
  204.     @Override
  205.     public int getCurrentConnections( )
  206.     {
  207.         return _connPool.getConnectionCount( );
  208.     }

  209.     /**
  210.      * {@inheritDoc}
  211.      */
  212.     @Override
  213.     public int getMaxConnections( )
  214.     {
  215.         return _connPool.getMaxConnectionCount( );
  216.     }

  217.     /**
  218.      * {@inheritDoc }
  219.      */
  220.     @Override
  221.     public String getPoolProvider( )
  222.     {
  223.         return "Lutece";
  224.     }

  225.     /**
  226.      * {@inheritDoc }
  227.      */
  228.     @Override
  229.     public DataSource getDataSource( )
  230.     {
  231.         return getConnectionPool( );
  232.     }
  233. }