| 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 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
package org.ietf.ldap.util; |
| 39 |
|
| 40 |
import java.util.Date; |
| 41 |
|
| 42 |
import org.ietf.ldap.LDAPConnection; |
| 43 |
import org.ietf.ldap.LDAPException; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public class ConnectionPool { |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public ConnectionPool( int min, int max, |
| 99 |
String host, int port, |
| 100 |
String authdn, byte[] authpw ) |
| 101 |
throws LDAPException { |
| 102 |
this( min, max, host, port, authdn, authpw, null ); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public ConnectionPool( int min, int max, |
| 116 |
String host, int port ) |
| 117 |
throws LDAPException { |
| 118 |
this( min, max, host, port, "", new byte[0]); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public ConnectionPool( String host, int port ) |
| 129 |
throws LDAPException { |
| 130 |
|
| 131 |
|
| 132 |
this( 10, 20, host, port, "", new byte[0] ); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public ConnectionPool( int min, int max, |
| 146 |
LDAPConnection ldc, byte[] authpwd ) |
| 147 |
throws LDAPException { |
| 148 |
this( min, max, ldc.getHost(), ldc.getPort(), |
| 149 |
ldc.getAuthenticationDN(), authpwd, |
| 150 |
ldc ); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
private ConnectionPool( int min, int max, |
| 167 |
String host, int port, |
| 168 |
String authdn, byte[] authpw, |
| 169 |
LDAPConnection ldc ) |
| 170 |
throws LDAPException { |
| 171 |
this.poolSize = min; |
| 172 |
this.poolMax = max; |
| 173 |
this.host = host; |
| 174 |
this.port = port; |
| 175 |
this.authdn = authdn; |
| 176 |
this.authpw = authpw; |
| 177 |
this.ldc = ldc; |
| 178 |
this.debugMode = false; |
| 179 |
createPool(); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public void destroy() { |
| 186 |
for ( int i = 0; i < pool.size(); i++ ) { |
| 187 |
disconnect( |
| 188 |
pool.elementAt(i) ); |
| 189 |
} |
| 190 |
pool.removeAllElements(); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public LDAPConnection getConnection() { |
| 204 |
LDAPConnection con; |
| 205 |
|
| 206 |
while( (con = getConnFromPool()) == null ) { |
| 207 |
synchronized( pool ) { |
| 208 |
try { |
| 209 |
pool.wait(); |
| 210 |
} catch ( InterruptedException e ) { |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
return con; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public LDAPConnection getConnection(int timeout) { |
| 230 |
LDAPConnection con; |
| 231 |
|
| 232 |
while( (con = getConnFromPool()) == null ) { |
| 233 |
long t1, t0 = System.currentTimeMillis(); |
| 234 |
|
| 235 |
if (timeout <= 0) { |
| 236 |
return con; |
| 237 |
} |
| 238 |
|
| 239 |
synchronized( pool ) { |
| 240 |
try { |
| 241 |
pool.wait(timeout); |
| 242 |
} catch ( InterruptedException e ) { |
| 243 |
return null; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
t1 = System.currentTimeMillis(); |
| 248 |
timeout -= (t1 - t0); |
| 249 |
} |
| 250 |
return con; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
protected synchronized LDAPConnection getConnFromPool() { |
| 264 |
LDAPConnection con = null; |
| 265 |
LDAPConnectionObject ldapconnobj = null; |
| 266 |
|
| 267 |
int pSize = pool.size(); |
| 268 |
|
| 269 |
|
| 270 |
for ( int i = 0; i < pSize; i++ ) { |
| 271 |
|
| 272 |
|
| 273 |
LDAPConnectionObject co = |
| 274 |
pool.elementAt(i); |
| 275 |
|
| 276 |
if ( co.isAvailable() ) { |
| 277 |
ldapconnobj = co; |
| 278 |
break; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
if ( ldapconnobj == null ) { |
| 283 |
|
| 284 |
|
| 285 |
if ( (poolMax < 0) || |
| 286 |
( (poolMax > 0) && |
| 287 |
(pSize < poolMax)) ) { |
| 288 |
|
| 289 |
|
| 290 |
int i = addConnection(); |
| 291 |
|
| 292 |
|
| 293 |
if ( i >= 0 ) { |
| 294 |
ldapconnobj = |
| 295 |
pool.elementAt(i); |
| 296 |
} |
| 297 |
} else { |
| 298 |
debug("All pool connections in use"); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
if ( ldapconnobj != null ) { |
| 303 |
ldapconnobj.setInUse( true ); |
| 304 |
con = ldapconnobj.getLDAPConn(); |
| 305 |
} |
| 306 |
return con; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public synchronized void close( LDAPConnection ld ) { |
| 316 |
|
| 317 |
int index = find( ld ); |
| 318 |
if ( index != -1 ) { |
| 319 |
LDAPConnectionObject co = |
| 320 |
pool.elementAt(index); |
| 321 |
co.setInUse( false ); |
| 322 |
synchronized( pool ) { |
| 323 |
pool.notifyAll(); |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
public void printPool(){ |
| 332 |
System.out.println("--ConnectionPool--"); |
| 333 |
for ( int i = 0; i < pool.size(); i++ ) { |
| 334 |
LDAPConnectionObject co = |
| 335 |
pool.elementAt(i); |
| 336 |
System.out.println( "" + i + "=" + co ); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
private void disconnect( |
| 341 |
LDAPConnectionObject ldapconnObject ) { |
| 342 |
if ( ldapconnObject != null ) { |
| 343 |
if (ldapconnObject.isAvailable()) { |
| 344 |
LDAPConnection ld = ldapconnObject.getLDAPConn(); |
| 345 |
if ( (ld != null) && (ld.isConnected()) ) { |
| 346 |
try { |
| 347 |
ld.disconnect(); |
| 348 |
} catch (LDAPException e) { |
| 349 |
debug("disconnect: "+e.toString()); |
| 350 |
} |
| 351 |
} |
| 352 |
ldapconnObject.setLDAPConn(null); |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
private void createPool() throws LDAPException { |
| 358 |
|
| 359 |
if ( poolSize <= 0 ) { |
| 360 |
throw new LDAPException("ConnectionPoolSize invalid"); |
| 361 |
} |
| 362 |
if ( poolMax < poolSize ) { |
| 363 |
debug("ConnectionPoolMax is invalid, set to " + |
| 364 |
poolSize); |
| 365 |
poolMax = poolSize; |
| 366 |
} |
| 367 |
|
| 368 |
debug("****Initializing LDAP Pool****"); |
| 369 |
debug("LDAP host = "+host+" on port "+port); |
| 370 |
debug("Number of connections="+poolSize); |
| 371 |
debug("Maximum number of connections="+poolMax); |
| 372 |
debug("******"); |
| 373 |
|
| 374 |
pool = new java.util.Vector<>(); |
| 375 |
setUpPool( poolSize ); |
| 376 |
} |
| 377 |
|
| 378 |
private int addConnection() { |
| 379 |
int index = -1; |
| 380 |
|
| 381 |
debug("adding a connection to pool..."); |
| 382 |
try { |
| 383 |
int size = pool.size() + 1; |
| 384 |
setUpPool( size ); |
| 385 |
|
| 386 |
if ( size == pool.size() ) { |
| 387 |
|
| 388 |
index = size - 1; |
| 389 |
} |
| 390 |
} catch (Exception ex) { |
| 391 |
debug("Adding a connection: "+ex.toString()); |
| 392 |
} |
| 393 |
return index; |
| 394 |
} |
| 395 |
|
| 396 |
private synchronized void setUpPool( int size ) |
| 397 |
throws LDAPException { |
| 398 |
|
| 399 |
while( pool.size() < size ) { |
| 400 |
LDAPConnectionObject co = |
| 401 |
new LDAPConnectionObject(); |
| 402 |
|
| 403 |
LDAPConnection newConn = |
| 404 |
(ldc != null) ? (LDAPConnection)ldc.clone() : |
| 405 |
new LDAPConnection(); |
| 406 |
co.setLDAPConn(newConn); |
| 407 |
try { |
| 408 |
if ( newConn.isConnected() ) { |
| 409 |
|
| 410 |
|
| 411 |
newConn.reconnect(); |
| 412 |
} else { |
| 413 |
|
| 414 |
|
| 415 |
newConn.connect( host, port ); |
| 416 |
newConn.bind( 3, authdn, authpw ); |
| 417 |
} |
| 418 |
} catch ( LDAPException le ) { |
| 419 |
debug("Creating pool:"+le.toString()); |
| 420 |
debug("aborting...."); |
| 421 |
throw le; |
| 422 |
} |
| 423 |
co.setInUse( false ); |
| 424 |
pool.addElement( co ); |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
private int find( LDAPConnection con ) { |
| 429 |
|
| 430 |
if ( con != null ) { |
| 431 |
for ( int i = 0; i < pool.size(); i++ ) { |
| 432 |
LDAPConnectionObject co = |
| 433 |
pool.elementAt(i); |
| 434 |
if ( co.getLDAPConn() == con ) { |
| 435 |
return i; |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
return -1; |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
public synchronized void setDebug( boolean mode ) { |
| 448 |
debugMode = mode; |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
public boolean getDebug() { |
| 457 |
return debugMode; |
| 458 |
} |
| 459 |
|
| 460 |
private void debug( String s ) { |
| 461 |
if ( debugMode ) |
| 462 |
System.out.println("ConnectionPool ("+ |
| 463 |
new Date()+") : " + s); |
| 464 |
} |
| 465 |
|
| 466 |
private void debug(String s, boolean severe) { |
| 467 |
if ( debugMode || severe ) { |
| 468 |
System.out.println("ConnectionPool ("+ |
| 469 |
new Date()+") : " + s); |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
class LDAPConnectionObject{ |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
LDAPConnection getLDAPConn() { |
| 485 |
return this.ld; |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
void setLDAPConn( LDAPConnection ld ) { |
| 495 |
this.ld = ld; |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
void setInUse( boolean inUse ) { |
| 505 |
this.inUse = inUse; |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
boolean isAvailable() { |
| 515 |
return !inUse; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
public String toString() { |
| 524 |
return "LDAPConnection=" + ld + ",inUse=" + inUse; |
| 525 |
} |
| 526 |
|
| 527 |
private LDAPConnection ld; |
| 528 |
private boolean inUse; |
| 529 |
} |
| 530 |
|
| 531 |
private int poolSize; |
| 532 |
private int poolMax; |
| 533 |
private String host; |
| 534 |
private int port; |
| 535 |
private String authdn; |
| 536 |
private byte[] authpw; |
| 537 |
private LDAPConnection ldc = null; |
| 538 |
private java.util.Vector<LDAPConnectionObject> pool; |
| 539 |
private boolean debugMode; |
| 540 |
} |