1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.util.pool.service;
35
36 import fr.paris.lutece.util.env.EnvUtil;
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.Map;
44
45 import javax.sql.DataSource;
46
47 import org.apache.logging.log4j.Logger;
48
49
50
51
52 public class LuteceConnectionService implements ConnectionService
53 {
54 private String _strPoolName;
55 private Logger _logger;
56 private ConnectionPool _connPool;
57
58
59
60
61
62
63
64 @Override
65 public void setPoolName( String strPoolName )
66 {
67 _strPoolName = strPoolName;
68 }
69
70
71
72
73
74
75 @Override
76 public String getPoolName( )
77 {
78 return _strPoolName;
79 }
80
81
82
83
84
85
86
87 @Override
88 public void setLogger( Logger logger )
89 {
90 _logger = logger;
91 }
92
93
94
95
96
97
98 @Override
99 public Logger getLogger( )
100 {
101 return _logger;
102 }
103
104
105
106
107
108
109
110 @Override
111 public void init( Map<String, String> htParamsConnectionPool )
112 {
113 String url = htParamsConnectionPool.get( getPoolName( ) + ".url" );
114
115 if ( url == null )
116 {
117 _logger.error( "No URL specified for the pool {}", getPoolName( ) );
118 }
119 else
120 {
121 url = EnvUtil.evaluate( url, EnvUtil.PREFIX_ENV );
122 }
123
124 String user = htParamsConnectionPool.get( getPoolName( ) + ".user" );
125
126 if ( user == null )
127 {
128 _logger.error( "No user specified for the pool {}", getPoolName( ) );
129 }
130 else
131 {
132 user = EnvUtil.evaluate( user, EnvUtil.PREFIX_ENV );
133 }
134
135 String password = htParamsConnectionPool.get( getPoolName( ) + ".password" );
136
137 if ( password == null )
138 {
139 _logger.error( "No password specified for the pool " + getPoolName( ) );
140 }
141 else
142 {
143 password = EnvUtil.evaluate( password, EnvUtil.PREFIX_ENV );
144 }
145
146
147 String strDiverClassName = htParamsConnectionPool.get( getPoolName( ) + ".driver" );
148
149 try
150 {
151 Driver driver = (Driver) Class.forName( strDiverClassName ).newInstance( );
152 DriverManager.registerDriver( driver );
153 _logger.info( "Registered JDBC driver {}", strDiverClassName );
154 }
155 catch( NullPointerException e )
156 {
157 _logger.error( "Can't register JDBC driver: {} because the property driver is not defined", strDiverClassName, e );
158 }
159 catch( Exception e )
160 {
161 _logger.error( "Can't register JDBC driver: {}", strDiverClassName, e );
162 }
163
164 int maxConns = ( htParamsConnectionPool.get( getPoolName( ) + ".maxconns" ) == null ) ? 0
165 : Integer.parseInt( htParamsConnectionPool.get( getPoolName( ) + ".maxconns" ) );
166
167 int initConns = ( htParamsConnectionPool.get( getPoolName( ) + ".initconns" ) == null ) ? 0
168 : Integer.parseInt( htParamsConnectionPool.get( getPoolName( ) + ".initconns" ) );
169
170 int timeOut = ( htParamsConnectionPool.get( getPoolName( ) + ".logintimeout" ) == null ) ? 5
171 : Integer.parseInt( htParamsConnectionPool.get( getPoolName( ) + ".logintimeout" ) );
172
173 String checkValidConnectionSql = ( htParamsConnectionPool.get( getPoolName( ) + ".checkvalidconnectionsql" ) == null ) ? ""
174 : htParamsConnectionPool.get( getPoolName( ) + ".checkvalidconnectionsql" );
175
176 _connPool = new ConnectionPool( getPoolName( ), url, user, password, maxConns, initConns, timeOut, _logger, checkValidConnectionSql );
177 }
178
179
180
181
182
183
184 @Override
185 public Connection getConnection( )
186 {
187 try
188 {
189 return _connPool.getConnection( );
190 }
191 catch( SQLException e )
192 {
193 _logger.error( e.getMessage( ), e );
194
195 return null;
196 }
197 }
198
199
200
201
202
203
204
205 @Override
206 public void freeConnection( Connection conn )
207 {
208 _connPool.freeConnection( conn );
209 }
210
211
212
213
214 @Override
215 public void release( )
216 {
217 _connPool.release( );
218 }
219
220
221
222
223
224
225 public ConnectionPool getConnectionPool( )
226 {
227 return _connPool;
228 }
229
230
231
232
233 @Override
234 public int getCurrentConnections( )
235 {
236 return _connPool.getConnectionCount( );
237 }
238
239
240
241
242 @Override
243 public int getMaxConnections( )
244 {
245 return _connPool.getMaxConnectionCount( );
246 }
247
248
249
250
251 @Override
252 public String getPoolProvider( )
253 {
254 return "Lutece";
255 }
256
257
258
259
260 @Override
261 public DataSource getDataSource( )
262 {
263 return getConnectionPool( );
264 }
265 }