1 46 47 package org.mr.ra.inbound; 48 49 import java.beans.IntrospectionException ; 50 import java.beans.PropertyDescriptor ; 51 import java.io.Serializable ; 52 import java.util.ArrayList ; 53 import java.util.Iterator ; 54 import java.util.List ; 55 56 import org.mr.api.jms.selector.syntax.Selector; 57 58 import javax.jms.Queue ; 59 import javax.jms.Session ; 60 import javax.jms.Topic ; 61 import javax.resource.ResourceException ; 62 import javax.resource.spi.ActivationSpec ; 63 import javax.resource.spi.InvalidPropertyException ; 64 import javax.resource.spi.ResourceAdapter ; 65 66 import org.mr.ra.ResourceAdapterImpl; 67 68 71 public class ActivationSpecImpl implements ActivationSpec , Serializable { 72 73 74 public static final String AUTO_ACKNOWLEDGE_MODE = "Auto-acknowledge"; 75 76 77 public static final String DUPS_OK_ACKNOWLEDGE_MODE = "Dups-ok-acknowledge"; 78 79 80 public static final String DURABLE_SUBSCRIPTION = "Durable"; 81 82 83 public static final String NON_DURABLE_SUBSCRIPTION = "NonDurable"; 84 85 public static final int INVALID_ACKNOWLEDGE_MODE = -1; 86 87 private ResourceAdapterImpl resourceAdapter = null; 88 private String destination = null; 89 private String destinationType = null; 90 private String messageSelector = null; 91 private String acknowledgeMode = AUTO_ACKNOWLEDGE_MODE; 92 private String userName = null; 93 private String password = null; 94 private String clientId = null; 95 private String subscriptionName = null; 96 private String subscriptionDurability = NON_DURABLE_SUBSCRIPTION; 97 private String noLocal = "false"; 98 private String maxMessagesPerSessions = "10"; 99 private String maxSessions = "10"; 100 private String enableBatch = "false"; 101 private String maxMessagesPerBatch = "10"; 102 private String useRAManagedTransaction = "false"; 103 104 107 public void validate() throws InvalidPropertyException { 108 List errorMessages = new ArrayList (); 109 List propsNotSet = new ArrayList (); 110 try { 111 if (!isValidDestination(errorMessages)) 112 propsNotSet.add(new PropertyDescriptor ("destination", ActivationSpecImpl.class)); 113 if (!isValidDestinationType(errorMessages)) 114 propsNotSet.add(new PropertyDescriptor ("destinationType", ActivationSpecImpl.class)); 115 if (!isValidAcknowledgeMode(errorMessages)) 116 propsNotSet.add(new PropertyDescriptor ("acknowledgeMode", ActivationSpecImpl.class)); 117 if (!isValidSubscriptionDurability(errorMessages)) 118 propsNotSet.add(new PropertyDescriptor ("subscriptionDurability", ActivationSpecImpl.class)); 119 if (!isValidClientId(errorMessages)) 120 propsNotSet.add(new PropertyDescriptor ("clientId", ActivationSpecImpl.class)); 121 if (!isValidSubscriptionName(errorMessages)) 122 propsNotSet.add(new PropertyDescriptor ("subscriptionName", ActivationSpecImpl.class)); 123 if (!isValidMaxMessagesPerSessions(errorMessages)) 124 propsNotSet.add(new PropertyDescriptor ("maxMessagesPerSessions", ActivationSpecImpl.class)); 125 if (!isValidMessageSelector(errorMessages)) 126 propsNotSet.add(new PropertyDescriptor ("messageSelector", ActivationSpecImpl.class)); 127 if (!isValidNoLocal(errorMessages)) 128 propsNotSet.add(new PropertyDescriptor ("noLocal", ActivationSpecImpl.class)); 129 if (!isValidMaxSessions(errorMessages)) 130 propsNotSet.add(new PropertyDescriptor ("maxSessions", ActivationSpecImpl.class)); 131 if (!isValidEnableBatch(errorMessages)) 132 propsNotSet.add(new PropertyDescriptor ("enableBatch", ActivationSpecImpl.class)); 133 if (!isValidMaxMessagesPerBatch(errorMessages)) 134 propsNotSet.add(new PropertyDescriptor ("maxMessagesPerBatch", ActivationSpecImpl.class)); 135 if (!isValidUseRAManagedTransaction(errorMessages)) 136 propsNotSet.add(new PropertyDescriptor ("useRAManagedTransaction", ActivationSpecImpl.class)); 137 } catch (IntrospectionException e) { 138 e.printStackTrace(); 139 } 140 141 if (propsNotSet.size() > 0) { 142 StringBuffer b = new StringBuffer (); 143 b.append("Invalid settings:"); 144 for (Iterator iter = errorMessages.iterator(); iter.hasNext();) { 145 b.append(" "); 146 b.append(iter.next()); 147 } 148 InvalidPropertyException e = new InvalidPropertyException (b.toString()); 149 final PropertyDescriptor [] descriptors = (PropertyDescriptor []) propsNotSet.toArray(new PropertyDescriptor [propsNotSet.size()]); 150 e.setInvalidPropertyDescriptors(descriptors); 151 throw e; 152 } 153 } 154 155 160 private boolean isValidDestination(List errorMessages) { 161 if (!isEmpty(destination)) 162 return true; 163 errorMessages.add("destination is a required field and must be set to the destination name."); 164 return false; 165 } 166 167 172 private boolean isValidDestinationType(List errorMessages) { 173 if (Queue .class.getName().equals(destinationType) || Topic .class.getName().equals(destinationType)) 174 return true; 175 errorMessages.add("destinationType must be set to: "+Queue .class.getName()+" or "+Topic .class.getName()+"."); 176 return false; 177 } 178 179 184 private boolean isValidAcknowledgeMode(List errorMessages) { 185 if (AUTO_ACKNOWLEDGE_MODE.equals(acknowledgeMode) || DUPS_OK_ACKNOWLEDGE_MODE.equals(acknowledgeMode)) 186 return true; 187 errorMessages.add("acknowledgeMode must be set to: "+AUTO_ACKNOWLEDGE_MODE+" or "+DUPS_OK_ACKNOWLEDGE_MODE+"."); 188 return false; 189 } 190 191 196 private boolean isValidSubscriptionDurability(List errorMessages) { 197 if (NON_DURABLE_SUBSCRIPTION.equals(subscriptionDurability) || DURABLE_SUBSCRIPTION.equals(subscriptionDurability)) 198 return true; 199 errorMessages.add("subscriptionDurability must be set to: "+NON_DURABLE_SUBSCRIPTION+" or "+DURABLE_SUBSCRIPTION+"."); 200 return false; 201 } 202 203 208 private boolean isValidClientId(List errorMessages) { 209 if (!isDurableSubscription()) { 210 return true; 211 } 212 if (!isEmpty(clientId)) { 213 return true; 214 } 215 errorMessages.add("clientId must be set since durable subscription was requested."); 216 return false; 217 } 218 219 224 private boolean isValidSubscriptionName(List errorMessages) { 225 if (!isDurableSubscription()) { 226 return true; 227 } 228 if (!isEmpty(subscriptionName)) { 229 return true; 230 } 231 errorMessages.add("subscriptionName must be set since durable subscription was requested."); 232 return false; 233 } 234 235 240 private boolean isValidMaxMessagesPerSessions(List errorMessages) { 241 try { 242 if (Integer.parseInt(maxMessagesPerSessions) > 0) { 243 return true; 244 } 245 } catch (NumberFormatException e) {} 246 errorMessages.add("maxMessagesPerSessions must be set to number > 0."); 247 return false; 248 } 249 250 255 private boolean isValidMessageSelector(List errorMessages) { 256 try { 257 new Selector(messageSelector); 258 return true; 259 } 260 catch (Throwable e) {} 261 errorMessages.add("messageSelector not set to valid message selector."); 262 return false; 263 } 264 265 270 private boolean isValidNoLocal(List errorMessages) { 271 try { 272 new Boolean (noLocal); 273 return true; 274 } catch (Throwable e) {} 275 errorMessages.add("noLocal must be set to: true or false."); 276 return false; 277 } 278 279 284 private boolean isValidMaxSessions(List errorMessages) { 285 try { 286 if (Integer.parseInt(maxSessions) > 0) { 287 return true; 288 } 289 } catch (NumberFormatException e) {} 290 errorMessages.add("maxSessions must be set to number > 0."); 291 return false; 292 } 293 294 299 private boolean isValidMaxMessagesPerBatch(List errorMessages) { 300 if (!getEnableBatchBooleanValue()) { 301 return true; 302 } 303 try { 304 if (Integer.parseInt(maxMessagesPerBatch) > 0) { 305 return true; 306 } 307 } catch (NumberFormatException e) {} 308 errorMessages.add("maxMessagesPerBatch must be set to number > 0."); 309 return false; 310 } 311 312 317 private boolean isValidEnableBatch(List errorMessages) { 318 try { 319 new Boolean (enableBatch); 320 return true; 321 } catch (Throwable e) {} 322 errorMessages.add("enableBatch must be set to: true or false."); 323 return false; 324 } 325 326 331 private boolean isValidUseRAManagedTransaction(List errorMessages) { 332 try { 333 new Boolean (useRAManagedTransaction); 334 return true; 335 } catch (Throwable e) {} 336 errorMessages.add("useRAManagedTransaction must be set to: true or false."); 337 return false; 338 } 339 340 341 342 345 public ResourceAdapter getResourceAdapter() { 346 return resourceAdapter; 347 } 348 349 352 public void setResourceAdapter(ResourceAdapter resourceAdapter) throws ResourceException { 353 if (this.resourceAdapter != null) { 355 throw new ResourceException ("ResourceAdapter already set"); 356 } 357 if (!(resourceAdapter instanceof ResourceAdapterImpl)) { 358 throw new ResourceException ("ResourceAdapter is not of type: " + ResourceAdapterImpl.class.getName()); 359 } 360 this.resourceAdapter = (ResourceAdapterImpl) resourceAdapter; 361 } 362 363 364 370 373 public String getDestination() { 374 if (!isEmpty(destination)) { 375 return destination; 376 } 377 return null; 378 } 379 380 383 public void setDestination(String destination) { 384 if (destination != null) { 385 destination = destination.trim(); 386 } 387 this.destination = destination; 388 } 389 390 393 public String getDestinationType() { 394 if (!isEmpty(destinationType)) { 395 return destinationType; 396 } 397 return null; 398 } 399 400 403 public void setDestinationType(String destinationType) { 404 if (destinationType != null) { 405 destinationType = destinationType.trim(); 406 } 407 this.destinationType = destinationType; 408 } 409 410 413 public String getUserName() { 414 if (!isEmpty(userName)) { 415 return userName; 416 } 417 return null; 418 } 419 420 423 public void setUserName(String userName) { 424 if (userName != null) { 425 userName = userName.trim(); 426 } 427 this.userName = userName; 428 } 429 430 433 public String getPassword() { 434 if (!isEmpty(password)) { 435 return password; 436 } 437 return null; 438 } 439 440 443 public void setPassword(String password) { 444 if (password != null) { 445 password = password.trim(); 446 } 447 this.password = password; 448 } 449 450 453 public String getMessageSelector() { 454 if (!isEmpty(messageSelector)) { 455 return messageSelector; 456 } 457 return null; 458 } 459 460 463 public void setMessageSelector(String messageSelector) { 464 if (messageSelector != null) { 465 messageSelector = messageSelector.trim(); 466 } 467 this.messageSelector = messageSelector; 468 } 469 470 473 public String getAcknowledgeMode() { 474 if (!isEmpty(acknowledgeMode)) { 475 return acknowledgeMode; 476 } 477 return null; 478 } 479 480 483 public void setAcknowledgeMode(String acknowledgeMode) { 484 if (acknowledgeMode != null) { 485 acknowledgeMode = acknowledgeMode.trim(); 486 } 487 this.acknowledgeMode = acknowledgeMode; 488 } 489 490 493 public String getMaxMessagesPerSessions() { 494 if (!isEmpty(maxMessagesPerSessions)) { 495 return maxMessagesPerSessions; 496 } 497 return null; 498 } 499 500 503 public void setMaxMessagesPerSessions(String maxMessagesPerSessions) { 504 if (maxMessagesPerSessions != null) { 505 maxMessagesPerSessions = maxMessagesPerSessions.trim(); 506 } 507 this.maxMessagesPerSessions = maxMessagesPerSessions; 508 } 509 510 513 public String getMaxSessions() { 514 return maxSessions; 515 } 516 517 520 public void setMaxSessions(String maxSessions) { 521 if( maxSessions!=null ) { 522 this.maxSessions = maxSessions; 523 } 524 } 525 526 529 public String getNoLocal() { 530 if (!isEmpty(noLocal)) { 531 return noLocal; 532 } 533 return null; 534 } 535 536 539 public void setNoLocal(String noLocal) { 540 if (noLocal != null) { 541 noLocal = noLocal.trim(); 542 } 543 this.noLocal = noLocal; 544 } 545 546 549 public String getClientId() { 550 if (!isEmpty(clientId)) { 551 return clientId; 552 } 553 return null; 554 } 555 556 559 public void setClientId(String clientId) { 560 if (clientId != null) { 561 clientId = clientId.trim(); 562 } 563 this.clientId = clientId; 564 } 565 566 569 public String getSubscriptionDurability() { 570 if (!isEmpty(subscriptionDurability)) { 571 return subscriptionDurability; 572 } 573 return null; 574 } 575 576 579 public void setSubscriptionDurability(String subscriptionDurability) { 580 if (subscriptionDurability != null) { 581 subscriptionDurability = subscriptionDurability.trim(); 582 } 583 this.subscriptionDurability = subscriptionDurability; 584 } 585 586 589 public String getSubscriptionName() { 590 if (!isEmpty(subscriptionName)) { 591 return subscriptionName; 592 } 593 return null; 594 } 595 596 599 public void setSubscriptionName(String subscriptionName) { 600 if (subscriptionName != null) { 601 subscriptionName = subscriptionName.trim(); 602 } 603 this.subscriptionName = subscriptionName; 604 } 605 606 609 public String getEnableBatch() { 610 return enableBatch; 611 } 612 613 616 public void setEnableBatch(String enableBatch) { 617 if (enableBatch != null) { 618 enableBatch = enableBatch.trim(); 619 } 620 this.enableBatch = enableBatch; 621 } 622 623 626 public String getMaxMessagesPerBatch() { 627 return maxMessagesPerBatch.toString(); 628 } 629 630 633 public void setMaxMessagesPerBatch(String maxMessagesPerBatch) { 634 if (maxMessagesPerBatch != null) { 635 maxMessagesPerBatch = maxMessagesPerBatch.trim(); 636 } 637 this.maxMessagesPerBatch = maxMessagesPerBatch; 638 } 639 640 643 public String getUseRAManagedTransaction() { 644 return useRAManagedTransaction; 645 } 646 647 650 public void setUseRAManagedTransaction(String useRAManagedTransaction) { 651 if (useRAManagedTransaction != null) { 652 useRAManagedTransaction = useRAManagedTransaction.trim(); 653 } 654 this.useRAManagedTransaction = useRAManagedTransaction; 655 } 656 657 658 659 public String toString() { 660 return "ActivationSpecImpl{"+ 661 "destination='"+destination+"'"+ 662 ", destinationType='"+destinationType+"'"+ 663 ", messageSelector='"+messageSelector+"'"+ 664 ", acknowledgeMode='"+acknowledgeMode+"'"+ 665 ", subscriptionDurability='"+subscriptionDurability+"'"+ 666 ", clientId='"+clientId+"'" + 667 ", subscriptionName='"+subscriptionName+"'"+ 668 ", noLocal='"+noLocal+"'"+ 669 ", maxMessagesPerSessions='"+maxMessagesPerSessions+"'"+ 670 ", enableBatch='"+enableBatch+"'"+ 671 ", maxMessagesPerBatch='"+maxMessagesPerBatch+"'"+ 672 ", useRAManagedTransaction='"+useRAManagedTransaction+"'"+ 673 "}"; 674 } 675 676 int getAcknowledgeModeForSession() { 677 if (AUTO_ACKNOWLEDGE_MODE.equals(acknowledgeMode)) { 678 return Session.AUTO_ACKNOWLEDGE; 679 } else if (DUPS_OK_ACKNOWLEDGE_MODE.equals(acknowledgeMode)) { 680 return Session.DUPS_OK_ACKNOWLEDGE; 681 } else { 682 return INVALID_ACKNOWLEDGE_MODE; 683 } 684 } 685 686 public boolean getNoLocalBooleanValue() { 687 return new Boolean (noLocal).booleanValue(); 688 } 689 690 public int getMaxMessagesPerSessionsIntValue() { 691 return Integer.parseInt(maxMessagesPerSessions); 692 } 693 694 public int getMaxSessionsIntValue() { 695 return Integer.parseInt(maxSessions); 696 } 697 698 public boolean getEnableBatchBooleanValue() { 699 return new Boolean (enableBatch).booleanValue(); 700 } 701 702 public int getMaxMessagesPerBatchIntValue() { 703 return Integer.parseInt(maxMessagesPerBatch); 704 } 705 706 public boolean isDurableSubscription() { 707 return DURABLE_SUBSCRIPTION.equals(subscriptionDurability); 708 } 709 710 public boolean isUseRAManagedTransactionEnabled() { 711 return new Boolean (useRAManagedTransaction).booleanValue(); 712 } 713 714 private boolean isEmpty(String value) { 715 return value == null || "".equals(value); 716 } 717 718 719 735 736 } | Popular Tags |