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;
35
36 import fr.paris.lutece.portal.service.init.LuteceInitException;
37 import fr.paris.lutece.util.ReferenceList;
38 import fr.paris.lutece.util.pool.service.ConnectionService;
39 import fr.paris.lutece.util.pool.service.LuteceConnectionService;
40
41 import org.apache.log4j.Logger;
42
43 import java.io.InputStream;
44
45 import java.sql.Connection;
46
47 import java.util.Collection;
48 import java.util.Enumeration;
49 import java.util.HashMap;
50 import java.util.Hashtable;
51 import java.util.Map;
52 import java.util.Properties;
53
54 import javax.sql.DataSource;
55
56
57
58
59
60
61 public final class PoolManager
62 {
63 private static final String LOGGER_NAME = "lutece.pool";
64 private static PoolManager _instance;
65 private Logger _logger;
66 private Map<String, ConnectionService> _pools = new HashMap<String, ConnectionService>( );
67
68
69
70
71
72
73
74
75 private PoolManager( InputStream isDbProperties ) throws LuteceInitException
76 {
77 init( isDbProperties );
78 }
79
80
81
82
83
84
85
86
87 public static synchronized PoolManager getInstance( InputStream isDbProperties )
88 throws LuteceInitException
89 {
90 if ( _instance == null )
91 {
92 _instance = new PoolManager( isDbProperties );
93 }
94
95 return _instance;
96 }
97
98
99
100
101
102
103
104 private void init( InputStream is ) throws LuteceInitException
105 {
106 _logger = Logger.getLogger( LOGGER_NAME );
107
108 Properties dbProps = new Properties( );
109
110 try
111 {
112 dbProps.load( is );
113 }
114 catch ( Exception e )
115 {
116 throw new LuteceInitException( "Can't read the properties file. Make sure db.properties is in the CLASSPATH",
117 e );
118 }
119
120 createPools( dbProps );
121 }
122
123
124
125
126
127
128
129 private void createPools( Properties props ) throws LuteceInitException
130 {
131 Enumeration propNames = props.propertyNames( );
132 String strPoolName = "";
133
134 Hashtable<String, Hashtable<String, String>> htPools = new Hashtable<String, Hashtable<String, String>>( );
135
136 while ( propNames.hasMoreElements( ) )
137 {
138 String name = (String) propNames.nextElement( );
139
140 try
141 {
142 strPoolName = name.substring( 0, name.lastIndexOf( '.' ) );
143
144
145 Hashtable<String, String> htParamsPool;
146
147
148 if ( htPools.get( strPoolName ) == null )
149 {
150 htParamsPool = new Hashtable<String, String>( );
151 }
152 else
153 {
154 htParamsPool = htPools.get( strPoolName );
155 }
156
157 htParamsPool.put( name, props.getProperty( name ) );
158 htPools.put( strPoolName, htParamsPool );
159
160 _logger.debug( "property " + name );
161 _logger.debug( "pool name " + strPoolName );
162 }
163 catch ( Exception e )
164 {
165 throw new LuteceInitException(
166 "Invalid initialization of the pools. Problem encoutered with the property : " + name, e );
167 }
168 }
169
170 Enumeration enumKeys = htPools.keys( );
171
172 while ( enumKeys.hasMoreElements( ) )
173 {
174 String key = "";
175
176 try
177 {
178 key = (String) enumKeys.nextElement( );
179
180 Hashtable<String, String> htParamsPool = htPools.get( key );
181 ConnectionService cs = null;
182
183 try
184 {
185 String strConnectionService = htParamsPool.get( key + ".poolservice" );
186
187 cs = (ConnectionService) Class.forName( strConnectionService ).newInstance( );
188 }
189 catch ( NullPointerException nullEx )
190 {
191 cs = new LuteceConnectionService( );
192 }
193 catch ( Exception e )
194 {
195 throw new LuteceInitException( "Exception when getting the property poolservice", e );
196 }
197
198 if ( cs != null )
199 {
200 cs.setPoolName( key );
201 cs.setLogger( _logger );
202 cs.init( htParamsPool );
203 _pools.put( key, cs );
204 }
205 }
206 catch ( Exception e )
207 {
208 throw new LuteceInitException( "Exception when getting the pool '" + key +
209 "'. Please check your '/WEB-INF/conf/db.properties' file.", e );
210 }
211 }
212 }
213
214
215
216
217
218
219
220 public Connection getConnection( String strPoolName )
221 {
222 Connection conn = null;
223 ConnectionService pool = (ConnectionService) _pools.get( strPoolName );
224
225 if ( pool != null )
226 {
227 conn = pool.getConnection( );
228 }
229
230 return conn;
231 }
232
233
234
235
236
237
238
239 public void freeConnection( String strPoolName, Connection con )
240 {
241 ConnectionService cs = (ConnectionService) _pools.get( strPoolName );
242
243 if ( cs != null )
244 {
245 cs.freeConnection( con );
246 }
247 }
248
249
250
251
252 public synchronized void release( )
253 {
254 for ( ConnectionService pool : _pools.values( ) )
255 {
256 pool.release( );
257 }
258 }
259
260
261
262
263
264 public Collection<ConnectionService> getPools( )
265 {
266 return _pools.values( );
267 }
268
269
270
271
272
273 public ReferenceList getPoolsInfos( )
274 {
275 ReferenceList listPoolsInfos = new ReferenceList( );
276 Collection<ConnectionService> listPools = getPools( );
277
278 for ( ConnectionService cs : listPools )
279 {
280 String strCurrentConnections = ( cs.getCurrentConnections( ) == cs.INFO_NOT_AVAILABLE ) ? "-"
281 : ( "" +
282 cs.getCurrentConnections( ) );
283 String strMaxConnections = ( cs.getMaxConnections( ) == cs.INFO_NOT_AVAILABLE ) ? "-"
284 : ( "" +
285 cs.getMaxConnections( ) );
286 listPoolsInfos.addItem( cs.getPoolName( ),
287 strCurrentConnections + " / " + strMaxConnections + " (" + cs.getPoolProvider( ) + ")" );
288 }
289
290 return listPoolsInfos;
291 }
292
293
294
295
296
297
298 public DataSource getDataSource( String strPoolName )
299 {
300 return _pools.get( strPoolName ).getDataSource( );
301 }
302 }