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.Driver;
40  import java.sql.DriverManager;
41  import java.sql.SQLException;
42  
43  import java.util.Hashtable;
44  
45  import javax.sql.DataSource;
46  
47  
48  /**
49   * Lutece Connection Service
50   */
51  public class LuteceConnectionService implements ConnectionService
52  {
53      private String _strPoolName;
54      private Logger _logger;
55      private ConnectionPool _connPool;
56  
57      /**
58       * Sets the pool name
59       * @param strPoolName The pool name
60       */
61      public void setPoolName( String strPoolName )
62      {
63          _strPoolName = strPoolName;
64      }
65  
66      /**
67       * Returns the pool name
68       * @return The pool name
69       */
70      public String getPoolName(  )
71      {
72          return _strPoolName;
73      }
74  
75      /**
76       * Sets the logger
77       * @param logger The logger
78       */
79      public void setLogger( Logger logger )
80      {
81          _logger = logger;
82      }
83  
84      /**
85       * Gets the pool logger
86       * @return The logger
87       */
88      public Logger getLogger(  )
89      {
90          return _logger;
91      }
92  
93      /**
94       * Initializes the connection pool
95       * @param htParamsConnectionPool Pool parameters
96       */
97      public void init( Hashtable<String, String> htParamsConnectionPool )
98      {
99          String url = htParamsConnectionPool.get( getPoolName(  ) + ".url" );
100 
101         if ( url == null )
102         {
103             _logger.error( "No URL specified for the pool " + getPoolName(  ) );
104         }
105 
106         String user = htParamsConnectionPool.get( getPoolName(  ) + ".user" );
107 
108         if ( user == null )
109         {
110             _logger.error( "No user specified for the pool " + getPoolName(  ) );
111         }
112 
113         String password = htParamsConnectionPool.get( getPoolName(  ) + ".password" );
114 
115         if ( password == null )
116         {
117             _logger.error( "No password specified for the pool " + getPoolName(  ) );
118         }
119 
120         //load of the driver
121         String strDiverClassName = htParamsConnectionPool.get( getPoolName(  ) + ".driver" );
122 
123         try
124         {
125             Driver driver = (Driver) Class.forName( strDiverClassName ).newInstance(  );
126             DriverManager.registerDriver( driver );
127             _logger.info( "Registered JDBC driver " + strDiverClassName );
128         }
129         catch ( NullPointerException e )
130         {
131             _logger.error( "Can't register JDBC driver: " + strDiverClassName +
132                 " because the property driver is not defined", e );
133         }
134         catch ( Exception e )
135         {
136             _logger.error( "Can't register JDBC driver: " + strDiverClassName, e );
137         }
138 
139         int maxConns = ( htParamsConnectionPool.get( getPoolName(  ) + ".maxconns" ) == null ) ? 0
140                                                                                                : Integer.parseInt( htParamsConnectionPool.get( getPoolName(  ) +
141                     ".maxconns" ) );
142 
143         int initConns = ( htParamsConnectionPool.get( getPoolName(  ) + ".initconns" ) == null ) ? 0
144                                                                                                  : Integer.parseInt( htParamsConnectionPool.get( getPoolName(  ) +
145                     ".initconns" ) );
146 
147         int timeOut = ( htParamsConnectionPool.get( getPoolName(  ) + ".logintimeout" ) == null ) ? 5
148                                                                                                   : Integer.parseInt( htParamsConnectionPool.get( getPoolName(  ) +
149                     ".logintimeout" ) );
150 
151         String checkValidConnectionSql = ( htParamsConnectionPool.get( getPoolName(  ) + ".checkvalidconnectionsql" ) == null )
152             ? "" : htParamsConnectionPool.get( getPoolName(  ) + ".checkvalidconnectionsql" );
153 
154         _connPool = new ConnectionPool( getPoolName(  ), url, user, password, maxConns, initConns, timeOut, _logger,
155                 checkValidConnectionSql );
156     }
157 
158     /**
159      * Get a connection
160      * @return A connection
161      */
162     public Connection getConnection(  )
163     {
164         try
165         {
166             Connection connection = _connPool.getConnection(  );
167 
168             return connection;
169         }
170         catch ( SQLException e )
171         {
172             _logger.error( e.getMessage(  ), e );
173 
174             return null;
175         }
176     }
177 
178     /**
179      * Free the connection
180      * @param conn The connection to release
181      */
182     public void freeConnection( Connection conn )
183     {
184         _connPool.freeConnection( conn );
185     }
186 
187     /**
188      * Release the pool
189      */
190     public void release(  )
191     {
192         _connPool.release(  );
193     }
194 
195     /**
196      * Returns the connection pool
197      * @return the connection pool
198      */
199     public ConnectionPool getConnectionPool(  )
200     {
201         return _connPool;
202     }
203 
204     /**
205      * {@inheritDoc}
206      */
207     public int getCurrentConnections(  )
208     {
209         return _connPool.getConnectionCount(  );
210     }
211 
212     /**
213      * {@inheritDoc}
214      */
215     public int getMaxConnections(  )
216     {
217         return _connPool.getMaxConnectionCount(  );
218     }
219 
220     /**
221      * {@inheritDoc }
222      */
223     public String getPoolProvider(  )
224     {
225         return "Lutece";
226     }
227 
228     /**
229      * {@inheritDoc }
230      */
231     public DataSource getDataSource(  )
232     {
233         return _connPool;
234     }
235 }