TomcatConnectionService.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 java.sql.Connection;
  36. import java.sql.SQLException;

  37. import java.util.Map;

  38. import javax.naming.Context;
  39. import javax.naming.InitialContext;

  40. import javax.sql.DataSource;

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

  42. /**
  43.  * This class provides a ConnectionService based on Tomcat
  44.  */
  45. public class TomcatConnectionService implements ConnectionService
  46. {
  47.     private DataSource _ds;
  48.     private String _strPoolName;
  49.     private Logger _logger;

  50.     /**
  51.      * {@inheritDoc}
  52.      */
  53.     @Override
  54.     public Connection getConnection( )
  55.     {
  56.         Connection conn = null;

  57.         try
  58.         {
  59.             if ( _ds != null )
  60.             {
  61.                 conn = _ds.getConnection( );
  62.                 _logger.debug( "The connexion is get" );

  63.             }
  64.         }
  65.         catch( Exception e )
  66.         {
  67.             _logger.error( "Erreur when getting the connexion with the pool : {}", getPoolName( ), e );
  68.         }

  69.         return conn;
  70.     }

  71.     /**
  72.      * {@inheritDoc}
  73.      */
  74.     @Override
  75.     public void freeConnection( Connection conn )
  76.     {
  77.         try
  78.         {
  79.             conn.close( );
  80.         }
  81.         catch( SQLException e )
  82.         {
  83.             _logger.error( "SQL error when releasing the connexion with the pool : {}", getPoolName( ), e );
  84.         }
  85.         catch( Exception e )
  86.         {
  87.             _logger.error( "Error while releasing the connexion with the pool : {}", getPoolName( ), e );
  88.         }
  89.     }

  90.     /**
  91.      * {@inheritDoc}
  92.      */
  93.     @Override
  94.     public void init( Map<String, String> htParamsConnectionPool )
  95.     {
  96.         try
  97.         {
  98.             String strDs = htParamsConnectionPool.get( getPoolName( ) + ".ds" );
  99.             Context ctx = new InitialContext( );

  100.             _ds = (DataSource) ctx.lookup( "java:comp/env/" + strDs );
  101.         }
  102.         catch( Exception e )
  103.         {
  104.             _logger.error( "Error while initializing the pool {}", getPoolName( ), e );
  105.         }

  106.         _logger.info( "Initialization of the pool {}", getPoolName( ) );
  107.     }

  108.     /**
  109.      * {@inheritDoc}
  110.      */
  111.     @Override
  112.     public void setPoolName( String strPoolName )
  113.     {
  114.         _strPoolName = strPoolName;
  115.     }

  116.     /**
  117.      * {@inheritDoc}
  118.      */
  119.     @Override
  120.     public String getPoolName( )
  121.     {
  122.         return _strPoolName;
  123.     }

  124.     /**
  125.      * Sets the logger
  126.      *
  127.      * @param logger
  128.      *            The logger
  129.      */
  130.     @Override
  131.     public void setLogger( Logger logger )
  132.     {
  133.         _logger = logger;
  134.     }

  135.     /**
  136.      * {@inheritDoc}
  137.      */
  138.     @Override
  139.     public Logger getLogger( )
  140.     {
  141.         return _logger;
  142.     }

  143.     /**
  144.      * {@inheritDoc}
  145.      */
  146.     @Override
  147.     public void release( )
  148.     {
  149.         // Nothing to do
  150.     }

  151.     /**
  152.      * {@inheritDoc}
  153.      */
  154.     @Override
  155.     public int getCurrentConnections( )
  156.     {
  157.         return ConnectionService.INFO_NOT_AVAILABLE;
  158.     }

  159.     /**
  160.      * {@inheritDoc}
  161.      */
  162.     @Override
  163.     public int getMaxConnections( )
  164.     {
  165.         return ConnectionService.INFO_NOT_AVAILABLE;
  166.     }

  167.     /**
  168.      * {@inheritDoc }
  169.      */
  170.     @Override
  171.     public String getPoolProvider( )
  172.     {
  173.         return "Tomcat";
  174.     }

  175.     /**
  176.      * {@inheritDoc }
  177.      */
  178.     @Override
  179.     public DataSource getDataSource( )
  180.     {
  181.         return _ds;
  182.     }
  183. }