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 java.sql.Connection;
37  import java.sql.SQLException;
38  
39  import java.util.Map;
40  
41  import javax.naming.Context;
42  import javax.naming.InitialContext;
43  
44  import javax.sql.DataSource;
45  
46  import org.apache.logging.log4j.Logger;
47  
48  /**
49   * This class provides a ConnectionService based on Tomcat
50   */
51  public class TomcatConnectionService implements ConnectionService
52  {
53      private DataSource _ds;
54      private String _strPoolName;
55      private Logger _logger;
56  
57      /**
58       * {@inheritDoc}
59       */
60      @Override
61      public Connection getConnection( )
62      {
63          Connection conn = null;
64  
65          try
66          {
67              if ( _ds != null )
68              {
69                  conn = _ds.getConnection( );
70                  _logger.debug( "The connexion is get" );
71  
72              }
73          }
74          catch( Exception e )
75          {
76              _logger.error( "Erreur when getting the connexion with the pool : {}", getPoolName( ), e );
77          }
78  
79          return conn;
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public void freeConnection( Connection conn )
87      {
88          try
89          {
90              conn.close( );
91          }
92          catch( SQLException e )
93          {
94              _logger.error( "SQL error when releasing the connexion with the pool : {}", getPoolName( ), e );
95          }
96          catch( Exception e )
97          {
98              _logger.error( "Error while releasing the connexion with the pool : {}", getPoolName( ), e );
99          }
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public void init( Map<String, String> htParamsConnectionPool )
107     {
108         try
109         {
110             String strDs = htParamsConnectionPool.get( getPoolName( ) + ".ds" );
111             Context ctx = new InitialContext( );
112 
113             _ds = (DataSource) ctx.lookup( "java:comp/env/" + strDs );
114         }
115         catch( Exception e )
116         {
117             _logger.error( "Error while initializing the pool {}", getPoolName( ), e );
118         }
119 
120         _logger.info( "Initialization of the pool {}", getPoolName( ) );
121     }
122 
123     /**
124      * {@inheritDoc}
125      */
126     @Override
127     public void setPoolName( String strPoolName )
128     {
129         _strPoolName = strPoolName;
130     }
131 
132     /**
133      * {@inheritDoc}
134      */
135     @Override
136     public String getPoolName( )
137     {
138         return _strPoolName;
139     }
140 
141     /**
142      * Sets the logger
143      * 
144      * @param logger
145      *            The logger
146      */
147     @Override
148     public void setLogger( Logger logger )
149     {
150         _logger = logger;
151     }
152 
153     /**
154      * {@inheritDoc}
155      */
156     @Override
157     public Logger getLogger( )
158     {
159         return _logger;
160     }
161 
162     /**
163      * {@inheritDoc}
164      */
165     @Override
166     public void release( )
167     {
168         // Nothing to do
169     }
170 
171     /**
172      * {@inheritDoc}
173      */
174     @Override
175     public int getCurrentConnections( )
176     {
177         return ConnectionService.INFO_NOT_AVAILABLE;
178     }
179 
180     /**
181      * {@inheritDoc}
182      */
183     @Override
184     public int getMaxConnections( )
185     {
186         return ConnectionService.INFO_NOT_AVAILABLE;
187     }
188 
189     /**
190      * {@inheritDoc }
191      */
192     @Override
193     public String getPoolProvider( )
194     {
195         return "Tomcat";
196     }
197 
198     /**
199      * {@inheritDoc }
200      */
201     @Override
202     public DataSource getDataSource( )
203     {
204         return _ds;
205     }
206 }