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