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 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
50
51 public class LuteceConnectionService implements ConnectionService
52 {
53 private String _strPoolName;
54 private Logger _logger;
55 private ConnectionPool _connPool;
56
57
58
59
60
61 public void setPoolName( String strPoolName )
62 {
63 _strPoolName = strPoolName;
64 }
65
66
67
68
69
70 public String getPoolName( )
71 {
72 return _strPoolName;
73 }
74
75
76
77
78
79 public void setLogger( Logger logger )
80 {
81 _logger = logger;
82 }
83
84
85
86
87
88 public Logger getLogger( )
89 {
90 return _logger;
91 }
92
93
94
95
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
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
160
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
180
181
182 public void freeConnection( Connection conn )
183 {
184 _connPool.freeConnection( conn );
185 }
186
187
188
189
190 public void release( )
191 {
192 _connPool.release( );
193 }
194
195
196
197
198
199 public ConnectionPool getConnectionPool( )
200 {
201 return _connPool;
202 }
203
204
205
206
207 public int getCurrentConnections( )
208 {
209 return _connPool.getConnectionCount( );
210 }
211
212
213
214
215 public int getMaxConnections( )
216 {
217 return _connPool.getMaxConnectionCount( );
218 }
219
220
221
222
223 public String getPoolProvider( )
224 {
225 return "Lutece";
226 }
227
228
229
230
231 public DataSource getDataSource( )
232 {
233 return _connPool;
234 }
235 }