| 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; |
| 39 |
|
| 40 |
import java.io.Serializable; |
| 41 |
import java.util.ArrayList; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
class LDAPMessageQueueImpl implements Serializable, LDAPMessageQueue { |
| 54 |
|
| 55 |
static final long serialVersionUID = -7163312406176592277L; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
private static class RequestEntry { |
| 61 |
int id; |
| 62 |
LDAPConnection connection; |
| 63 |
LDAPConnThread connThread; |
| 64 |
long timeToComplete; |
| 65 |
|
| 66 |
RequestEntry( int id, LDAPConnection connection, |
| 67 |
LDAPConnThread connThread, int timeLimit ) { |
| 68 |
this.id = id; |
| 69 |
this.connection = connection; |
| 70 |
this.connThread = connThread; |
| 71 |
this.timeToComplete = (timeLimit == 0) ? |
| 72 |
Long.MAX_VALUE : (System.currentTimeMillis() + timeLimit); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
protected ArrayList<LDAPMessage> _messageQueue = new ArrayList<>(1); |
| 80 |
private ArrayList<RequestEntry> _requestList = new ArrayList<>(1); |
| 81 |
private LDAPException _exception; |
| 82 |
private boolean _asynchOp; |
| 83 |
|
| 84 |
|
| 85 |
private boolean _timeConstrained; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
LDAPMessageQueueImpl( boolean asynchOp ) { |
| 95 |
_asynchOp = asynchOp; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public int getMessageCount() { |
| 104 |
return _messageQueue.size(); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
synchronized public int[] getMessageIDs() { |
| 113 |
int[] ids = new int[_requestList.size()]; |
| 114 |
for ( int i = 0; i < ids.length; i++ ) { |
| 115 |
RequestEntry entry = _requestList.get(i); |
| 116 |
ids[i] = entry.id; |
| 117 |
} |
| 118 |
return ids; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public synchronized LDAPMessage getResponse() throws LDAPException { |
| 130 |
|
| 131 |
while( true ) { |
| 132 |
if ( !waitForSomething() ) { |
| 133 |
return null; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
LDAPMessage msg = _messageQueue.get( 0 ); |
| 138 |
_messageQueue.remove( 0 ); |
| 139 |
|
| 140 |
|
| 141 |
if ( msg instanceof LDAPResponse ) { |
| 142 |
removeRequest( msg.getMessageID() ); |
| 143 |
} |
| 144 |
return msg; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public synchronized LDAPMessage getResponse( int msgid ) |
| 163 |
throws LDAPException { |
| 164 |
|
| 165 |
if ( !isValidMessageID( msgid ) ) { |
| 166 |
throw new IllegalArgumentException( "Invalid msg ID: " + msgid ); |
| 167 |
} |
| 168 |
|
| 169 |
LDAPMessage msg = null; |
| 170 |
while( ( _requestList.size() != 0 ) && |
| 171 |
( _exception == null ) && |
| 172 |
( (msg = getMessageForID( msgid, true )) == null ) ) { |
| 173 |
waitForMessage(); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
if ( _exception != null ) { |
| 178 |
LDAPException ex = _exception; |
| 179 |
_exception = null; |
| 180 |
throw ex; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
if ( _requestList.size() == 0 ) { |
| 185 |
return null; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
if ( msg instanceof LDAPResponse ) { |
| 190 |
removeRequest( msg.getMessageID() ); |
| 191 |
} |
| 192 |
|
| 193 |
return msg; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public boolean isResponseReceived() { |
| 202 |
return ( _messageQueue.size() != 0 ); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public boolean isResponseReceived( int msgid ) { |
| 215 |
if ( !isValidMessageID( msgid ) ) { |
| 216 |
throw new IllegalArgumentException( "Invalid msg ID: " + msgid ); |
| 217 |
} |
| 218 |
|
| 219 |
return ( getMessageForID( msgid, false ) != null ); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public void merge( LDAPMessageQueue mq2 ) { |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
Thread.yield(); |
| 248 |
|
| 249 |
synchronized( this ) { |
| 250 |
LDAPMessageQueueImpl mq = (LDAPMessageQueueImpl)mq2; |
| 251 |
synchronized( mq ) { |
| 252 |
ArrayList<LDAPMessage> queue2 = mq.getAllMessages(); |
| 253 |
for( int i = 0; i < queue2.size(); i++ ) { |
| 254 |
_messageQueue.add( queue2.get( i ) ); |
| 255 |
} |
| 256 |
if ( mq.getException() != null ) { |
| 257 |
_exception = mq.getException(); |
| 258 |
} |
| 259 |
ArrayList<RequestEntry> list2 = mq.getAllRequests(); |
| 260 |
for( int i = 0; i < list2.size(); i++ ) { |
| 261 |
RequestEntry entry = list2.get( i ); |
| 262 |
_requestList.add( entry ); |
| 263 |
|
| 264 |
|
| 265 |
entry.connThread.changeQueue( entry.id, this ); |
| 266 |
} |
| 267 |
|
| 268 |
mq.reset(); |
| 269 |
notifyAll(); |
| 270 |
} |
| 271 |
|
| 272 |
notifyAll(); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
synchronized public String toString() { |
| 282 |
StringBuffer sb = new StringBuffer( "LDAPMessageQueueImpl:" ); |
| 283 |
sb.append(" requestIDs={"); |
| 284 |
for ( int i = 0; i < _requestList.size(); i++ ) { |
| 285 |
if ( i > 0 ) { |
| 286 |
sb.append( "," ); |
| 287 |
} |
| 288 |
sb.append( _requestList.get(i).id ); |
| 289 |
} |
| 290 |
sb.append( "} messageCount=" + _messageQueue.size() ); |
| 291 |
|
| 292 |
return sb.toString(); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
synchronized LDAPMessage getMessageForID( int msgid, boolean remove ) { |
| 305 |
LDAPMessage msg = null; |
| 306 |
for ( int i = 0; i < _messageQueue.size(); i++ ) { |
| 307 |
msg = _messageQueue.get(i); |
| 308 |
if ( msg.getMessageID() == msgid ) { |
| 309 |
if ( remove ) { |
| 310 |
_messageQueue.remove( i ); |
| 311 |
} |
| 312 |
break; |
| 313 |
} |
| 314 |
} |
| 315 |
return msg; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
boolean isAsynchOp() { |
| 326 |
return _asynchOp; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
synchronized LDAPResponse completeRequest() throws LDAPException { |
| 340 |
while ( true ) { |
| 341 |
if ( !waitForSomething() ) { |
| 342 |
return null; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
for ( int i = _messageQueue.size()-1; i >= 0; i-- ) { |
| 347 |
LDAPMessage msg = _messageQueue.get(i); |
| 348 |
if ( msg instanceof LDAPResponse ) { |
| 349 |
|
| 350 |
|
| 351 |
_messageQueue.remove( i ); |
| 352 |
return (LDAPResponse)msg; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
synchronized private void waitForMessage() throws LDAPException { |
| 364 |
if ( !_timeConstrained ) { |
| 365 |
try { |
| 366 |
wait(); |
| 367 |
return; |
| 368 |
} catch (InterruptedException e) { |
| 369 |
throw new LDAPInterruptedException( |
| 370 |
"Interrupted LDAP operation" ); |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
long minTimeToComplete = Long.MAX_VALUE; |
| 378 |
long now = System.currentTimeMillis(); |
| 379 |
for ( int i = 0; i < _requestList.size(); i++ ) { |
| 380 |
RequestEntry entry = _requestList.get( i ); |
| 381 |
|
| 382 |
|
| 383 |
if ( entry.timeToComplete <= now ) { |
| 384 |
entry.connection.abandon( entry.id ); |
| 385 |
throw new LDAPException( "Time to complete operation exceeded", |
| 386 |
LDAPException.LDAP_TIMEOUT ); |
| 387 |
} |
| 388 |
|
| 389 |
if ( entry.timeToComplete < minTimeToComplete ) { |
| 390 |
minTimeToComplete = entry.timeToComplete; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
long timeLimit = ( minTimeToComplete == Long.MAX_VALUE ) ? |
| 395 |
0 : ( minTimeToComplete - now ); |
| 396 |
|
| 397 |
try { |
| 398 |
_timeConstrained = ( timeLimit != 0 ); |
| 399 |
wait( timeLimit ); |
| 400 |
} catch ( InterruptedException e ) { |
| 401 |
throw new LDAPInterruptedException( "Interrupted LDAP operation" ); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
boolean waitForSomething() throws LDAPException { |
| 413 |
while( (_requestList.size() != 0) && |
| 414 |
(_exception == null) && |
| 415 |
(_messageQueue.size() == 0) ) { |
| 416 |
waitForMessage(); |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
if ( _exception != null ) { |
| 421 |
LDAPException ex = _exception; |
| 422 |
_exception = null; |
| 423 |
throw ex; |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
if ( _requestList.size() == 0 ) { |
| 428 |
return false; |
| 429 |
} |
| 430 |
|
| 431 |
return true; |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
synchronized ArrayList<LDAPMessage> getAllMessages() { |
| 442 |
ArrayList<LDAPMessage> result = _messageQueue; |
| 443 |
_messageQueue = new ArrayList<>(1); |
| 444 |
return result; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
synchronized ArrayList<RequestEntry> getAllRequests() { |
| 454 |
ArrayList<RequestEntry> result = _requestList; |
| 455 |
_requestList = new ArrayList<>(1); |
| 456 |
return result; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
synchronized void addMessage( LDAPMessage msg ) { |
| 466 |
_messageQueue.add(msg); |
| 467 |
|
| 468 |
|
| 469 |
if ( isAsynchOp() && |
| 470 |
(msg.getType() == LDAPMessage.BIND_RESPONSE) ) { |
| 471 |
if ( ((LDAPResponse)msg).getResultCode() == 0 ) { |
| 472 |
getConnection( msg.getMessageID() ).markConnAsBound(); |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
notifyAll(); |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
LDAPException getException() { |
| 485 |
return _exception; |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
synchronized boolean isValidMessageID( int msgid ) { |
| 495 |
return ( getRequestEntry( msgid ) != null ); |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
synchronized void setException( LDAPConnThread connThread, |
| 507 |
LDAPException e ) { |
| 508 |
_exception = e; |
| 509 |
removeAllRequests( connThread ); |
| 510 |
notifyAll(); |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
private int removeAllMessages( int id ) { |
| 523 |
int removeCount = 0; |
| 524 |
for ( int i = (_messageQueue.size()-1); i >= 0; i-- ) { |
| 525 |
LDAPMessage msg = _messageQueue.get( i ); |
| 526 |
if ( msg.getMessageID() == id ) { |
| 527 |
_messageQueue.remove( i ); |
| 528 |
removeCount++; |
| 529 |
} |
| 530 |
} |
| 531 |
return removeCount; |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
void reset() { |
| 542 |
_exception = null; |
| 543 |
_messageQueue.clear(); |
| 544 |
_requestList.clear(); |
| 545 |
_timeConstrained = false; |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
synchronized LDAPConnection getConnection( int id ) { |
| 555 |
RequestEntry entry = getRequestEntry( id ); |
| 556 |
if ( entry != null ) { |
| 557 |
return entry.connection; |
| 558 |
} |
| 559 |
return null; |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
synchronized LDAPConnThread getConnThread( int id ) { |
| 569 |
RequestEntry entry = getRequestEntry( id ); |
| 570 |
if ( entry != null ) { |
| 571 |
return entry.connThread; |
| 572 |
} |
| 573 |
return null; |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
synchronized RequestEntry getRequestEntry( int id ) { |
| 583 |
for ( int i = 0; i < _requestList.size(); i++ ) { |
| 584 |
RequestEntry entry = _requestList.get( i ); |
| 585 |
if ( id == entry.id ) { |
| 586 |
return entry; |
| 587 |
} |
| 588 |
} |
| 589 |
return null; |
| 590 |
|
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
synchronized int getMessageID() { |
| 599 |
int reqCnt = _requestList.size(); |
| 600 |
if ( reqCnt == 0 ) { |
| 601 |
return -1; |
| 602 |
} else { |
| 603 |
RequestEntry entry = _requestList.get( reqCnt-1 ); |
| 604 |
return entry.id; |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
synchronized void addRequest( int id, LDAPConnection connection, |
| 618 |
LDAPConnThread connThread, int timeLimit ) { |
| 619 |
|
| 620 |
_requestList.add( new RequestEntry( id, connection, |
| 621 |
connThread, timeLimit ) ); |
| 622 |
if ( timeLimit != 0 ) { |
| 623 |
_timeConstrained = true; |
| 624 |
} |
| 625 |
notifyAll(); |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
public int getRequestCount() { |
| 633 |
return _requestList.size(); |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
synchronized boolean removeRequest( int id ) { |
| 645 |
for ( int i = 0; i < _requestList.size(); i++ ) { |
| 646 |
RequestEntry entry = _requestList.get( i ); |
| 647 |
if ( id == entry.id ) { |
| 648 |
_requestList.remove( i ); |
| 649 |
removeAllMessages( id ); |
| 650 |
notifyAll(); |
| 651 |
return true; |
| 652 |
} |
| 653 |
} |
| 654 |
return false; |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
synchronized int removeAllRequests( LDAPConnThread connThread ) { |
| 664 |
int removeCount = 0; |
| 665 |
for ( int i = (_requestList.size()-1); i >= 0; i-- ) { |
| 666 |
RequestEntry entry = _requestList.get( i ); |
| 667 |
if ( connThread == entry.connThread ) { |
| 668 |
_requestList.remove( i ); |
| 669 |
removeCount++; |
| 670 |
|
| 671 |
|
| 672 |
removeAllMessages( entry.id ); |
| 673 |
} |
| 674 |
} |
| 675 |
notifyAll(); |
| 676 |
return removeCount; |
| 677 |
} |
| 678 |
} |