1 18 package org.apache.activemq.ra; 19 20 import java.beans.IntrospectionException ; 21 import java.beans.PropertyDescriptor ; 22 import java.io.Serializable ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import javax.jms.Queue ; 28 import javax.jms.Session ; 29 import javax.jms.Topic ; 30 import javax.resource.ResourceException ; 31 import javax.resource.spi.InvalidPropertyException ; 32 import javax.resource.spi.ResourceAdapter ; 33 34 import org.apache.activemq.RedeliveryPolicy; 35 import org.apache.activemq.command.ActiveMQDestination; 36 import org.apache.activemq.command.ActiveMQQueue; 37 import org.apache.activemq.command.ActiveMQTopic; 38 import org.apache.activemq.selector.SelectorParser; 39 40 47 public class ActiveMQActivationSpec implements MessageActivationSpec, Serializable { 48 49 private static final long serialVersionUID = -7153087544100459975L; 50 51 52 public static final String AUTO_ACKNOWLEDGE_MODE = "Auto-acknowledge"; 53 54 public static final String DUPS_OK_ACKNOWLEDGE_MODE = "Dups-ok-acknowledge"; 55 56 public static final String DURABLE_SUBSCRIPTION = "Durable"; 57 58 public static final String NON_DURABLE_SUBSCRIPTION = "NonDurable"; 59 60 63 public static final int INVALID_ACKNOWLEDGE_MODE = -1; 64 65 private transient MessageResourceAdapter resourceAdapter; 66 private String destinationType; 67 private String messageSelector; 68 private String destination; 69 private String acknowledgeMode = AUTO_ACKNOWLEDGE_MODE; 70 private String userName; 71 private String password; 72 private String clientId; 73 private String subscriptionName; 74 private String subscriptionDurability = NON_DURABLE_SUBSCRIPTION; 75 private String noLocal = "false"; 76 private String useRAManagedTransaction = "false"; 77 private String maxSessions="10"; 78 private String maxMessagesPerSessions="10"; 79 private String enableBatch = "false"; 80 private String maxMessagesPerBatch = "10"; 81 private RedeliveryPolicy redeliveryPolicy; 82 83 84 87 public void validate() throws InvalidPropertyException { 88 List errorMessages = new ArrayList (); 89 List propsNotSet = new ArrayList (); 90 try { 91 if (!isValidDestination(errorMessages)) 92 propsNotSet.add(new PropertyDescriptor ("destination", ActiveMQActivationSpec.class)); 93 if (!isValidDestinationType(errorMessages)) 94 propsNotSet.add(new PropertyDescriptor ("destinationType", ActiveMQActivationSpec.class)); 95 if (!isValidAcknowledgeMode(errorMessages)) 96 propsNotSet.add(new PropertyDescriptor ("acknowledgeMode", ActiveMQActivationSpec.class)); 97 if (!isValidSubscriptionDurability(errorMessages)) 98 propsNotSet.add(new PropertyDescriptor ("subscriptionDurability", ActiveMQActivationSpec.class)); 99 if (!isValidClientId(errorMessages)) 100 propsNotSet.add(new PropertyDescriptor ("clientId", ActiveMQActivationSpec.class)); 101 if (!isValidSubscriptionName(errorMessages)) 102 propsNotSet.add(new PropertyDescriptor ("subscriptionName", ActiveMQActivationSpec.class)); 103 if (!isValidMaxMessagesPerSessions(errorMessages)) 104 propsNotSet.add(new PropertyDescriptor ("maxMessagesPerSessions", ActiveMQActivationSpec.class)); 105 if (!isValidMaxSessions(errorMessages)) 106 propsNotSet.add(new PropertyDescriptor ("maxSessions", ActiveMQActivationSpec.class)); 107 if (!isValidMessageSelector(errorMessages)) 108 propsNotSet.add(new PropertyDescriptor ("messageSelector", ActiveMQActivationSpec.class)); 109 if (!isValidNoLocal(errorMessages)) 110 propsNotSet.add(new PropertyDescriptor ("noLocal", ActiveMQActivationSpec.class)); 111 if (!isValidUseRAManagedTransaction(errorMessages)) 112 propsNotSet.add(new PropertyDescriptor ("useRAManagedTransaction", ActiveMQActivationSpec.class)); 113 if (!isValidEnableBatch(errorMessages)) 114 propsNotSet.add(new PropertyDescriptor ("enableBatch", ActiveMQActivationSpec.class)); 115 if (!isValidMaxMessagesPerBatch(errorMessages)) 116 propsNotSet.add(new PropertyDescriptor ("maxMessagesPerBatch", ActiveMQActivationSpec.class)); 117 118 119 } catch (IntrospectionException e) { 120 e.printStackTrace(); 121 } 122 123 if (propsNotSet.size() > 0) { 124 StringBuffer b = new StringBuffer (); 125 b.append("Invalid settings:"); 126 for (Iterator iter = errorMessages.iterator(); iter.hasNext();) { 127 b.append(" "); 128 b.append(iter.next()); 129 } 130 InvalidPropertyException e = new InvalidPropertyException (b.toString()); 131 final PropertyDescriptor [] descriptors = (PropertyDescriptor []) propsNotSet.toArray(new PropertyDescriptor [propsNotSet.size()]); 132 e.setInvalidPropertyDescriptors(descriptors); 133 throw e; 134 } 135 } 136 137 private boolean isValidUseRAManagedTransaction(List errorMessages) { 138 try { 139 new Boolean (noLocal); 140 return true; 141 } catch (Throwable e) { 142 } 144 errorMessages.add("noLocal must be set to: true or false."); 145 return false; 146 } 147 148 private boolean isValidNoLocal(List errorMessages) { 149 try { 150 new Boolean (noLocal); 151 return true; 152 } catch (Throwable e) { 153 } 155 errorMessages.add("noLocal must be set to: true or false."); 156 return false; 157 } 158 159 private boolean isValidMessageSelector(List errorMessages) { 160 try { 161 if( !isEmpty(messageSelector) ) { 162 new SelectorParser().parse(messageSelector); 163 } 164 return true; 165 } catch (Throwable e) { 166 errorMessages.add("messageSelector not set to valid message selector: "+e.getMessage()); 167 return false; 168 } 169 } 170 171 private boolean isValidMaxSessions(List errorMessages) { 172 try { 173 if( Integer.parseInt(maxSessions) > 0 ) { 174 return true; 175 } 176 } catch (NumberFormatException e) { 177 } 179 errorMessages.add("maxSessions must be set to number > 0"); 180 return false; 181 } 182 183 private boolean isValidMaxMessagesPerSessions(List errorMessages) { 184 try { 185 if( Integer.parseInt(maxMessagesPerSessions) > 0 ) { 186 return true; 187 } 188 } catch (NumberFormatException e) { 189 } 191 errorMessages.add("maxMessagesPerSessions must be set to number > 0"); 192 return false; 193 } 194 195 private boolean isValidMaxMessagesPerBatch(List errorMessages) { 196 try { 197 if( Integer.parseInt(maxMessagesPerBatch) > 0 ) { 198 return true; 199 } 200 } catch (NumberFormatException e) { 201 } 203 errorMessages.add("maxMessagesPerBatch must be set to number > 0"); 204 return false; 205 } 206 207 private boolean isValidEnableBatch(List errorMessages) { 208 try { 209 new Boolean (enableBatch); 210 return true; 211 } catch (Throwable e) { 212 } 214 errorMessages.add("enableBatch must be set to: true or false"); 215 return false; 216 } 217 218 221 public ResourceAdapter getResourceAdapter() { 222 return resourceAdapter; 223 } 224 225 228 public void setResourceAdapter(ResourceAdapter resourceAdapter) throws ResourceException { 229 if (this.resourceAdapter != null) { 231 throw new ResourceException ("ResourceAdapter already set"); 232 } 233 if (!(resourceAdapter instanceof MessageResourceAdapter)) { 234 throw new ResourceException ("ResourceAdapter is not of type: " + MessageResourceAdapter.class.getName()); 235 } 236 this.resourceAdapter = (MessageResourceAdapter) resourceAdapter; 237 } 238 239 240 248 public String getDestinationType() { 249 if (!isEmpty(destinationType)) { 250 return destinationType; 251 } 252 return null; 253 } 254 255 258 public void setDestinationType(String destinationType) { 259 this.destinationType = destinationType; 260 } 261 262 265 public String getPassword() { 266 if (!isEmpty(password)) { 267 return password; 268 } 269 return null; 270 } 271 272 275 public void setPassword(String password) { 276 this.password = password; 277 } 278 279 282 public String getUserName() { 283 if (!isEmpty(userName)) { 284 return userName; 285 } 286 return null; 287 } 288 289 292 public void setUserName(String userName) { 293 this.userName = userName; 294 } 295 296 299 public String getMessageSelector() { 300 if (!isEmpty(messageSelector)) { 301 return messageSelector; 302 } 303 return null; 304 } 305 306 309 public void setMessageSelector(String messageSelector) { 310 this.messageSelector = messageSelector; 311 } 312 313 316 public String getNoLocal() { 317 return noLocal; 318 } 319 320 323 public void setNoLocal(String noLocal) { 324 if( noLocal!=null ) { 325 this.noLocal = noLocal; 326 } 327 } 328 329 332 public String getAcknowledgeMode() { 333 if (!isEmpty(acknowledgeMode)) { 334 return acknowledgeMode; 335 } 336 return null; 337 } 338 339 342 public void setAcknowledgeMode(String acknowledgeMode) { 343 this.acknowledgeMode = acknowledgeMode; 344 } 345 346 349 public String getClientId() { 350 if (!isEmpty(clientId)) { 351 return clientId; 352 } 353 return null; 354 } 355 356 359 public void setClientId(String clientId) { 360 this.clientId = clientId; 361 } 362 363 366 public String getDestination() { 367 if (!isEmpty(destination)) { 368 return destination; 369 } 370 return null; 371 } 372 373 376 public void setDestination(String destination) { 377 this.destination = destination; 378 } 379 380 383 public String getSubscriptionDurability() { 384 if (!isEmpty(subscriptionDurability)) { 385 return subscriptionDurability; 386 } 387 return null; 388 } 389 390 393 public void setSubscriptionDurability(String subscriptionDurability) { 394 this.subscriptionDurability = subscriptionDurability; 395 } 396 397 400 public String getSubscriptionName() { 401 if (!isEmpty(subscriptionName)) { 402 return subscriptionName; 403 } 404 return null; 405 } 406 407 410 public void setSubscriptionName(String subscriptionName) { 411 this.subscriptionName = subscriptionName; 412 } 413 414 private boolean isValidSubscriptionName(List errorMessages) { 415 if( !isDurableSubscription() ? true : subscriptionName != null && subscriptionName.trim().length() > 0 ) { 416 return true; 417 } 418 errorMessages.add("subscriptionName must be set since durable subscription was requested."); 419 return false; 420 } 421 422 private boolean isValidClientId(List errorMessages) { 423 if( !isDurableSubscription() ? true : clientId != null && clientId.trim().length() > 0 ) { 424 return true; 425 } 426 errorMessages.add("clientId must be set since durable subscription was requested."); 427 return false; 428 } 429 430 433 public boolean isDurableSubscription() { 434 return DURABLE_SUBSCRIPTION.equals(subscriptionDurability); 435 } 436 437 private boolean isValidSubscriptionDurability(List errorMessages) { 438 if ( DURABLE_SUBSCRIPTION.equals(subscriptionDurability) && 440 getDestinationType() != null && !Topic .class.getName().equals(getDestinationType())) { 441 errorMessages.add("subscriptionDurability cannot be set to: "+DURABLE_SUBSCRIPTION+" when destinationType is set to "+ 442 Queue .class.getName()+" as it is only valid when destinationType is set to "+Topic .class.getName()+"."); 443 return false; 444 } 445 if (NON_DURABLE_SUBSCRIPTION.equals(subscriptionDurability) || DURABLE_SUBSCRIPTION.equals(subscriptionDurability)) 446 return true; 447 errorMessages.add("subscriptionDurability must be set to: "+NON_DURABLE_SUBSCRIPTION+" or "+DURABLE_SUBSCRIPTION+"."); 448 return false; 449 } 450 451 private boolean isValidAcknowledgeMode(List errorMessages) { 452 if (AUTO_ACKNOWLEDGE_MODE.equals(acknowledgeMode) || DUPS_OK_ACKNOWLEDGE_MODE.equals(acknowledgeMode)) 453 return true; 454 errorMessages.add("acknowledgeMode must be set to: "+AUTO_ACKNOWLEDGE_MODE+" or "+DUPS_OK_ACKNOWLEDGE_MODE+"."); 455 return false; 456 } 457 458 private boolean isValidDestinationType(List errorMessages) { 459 if (Queue .class.getName().equals(destinationType) || Topic .class.getName().equals(destinationType)) 460 return true; 461 errorMessages.add("destinationType must be set to: "+Queue .class.getName()+" or "+Topic .class.getName()+"."); 462 return false; 463 } 464 465 private boolean isValidDestination(List errorMessages) { 466 if(!(destination == null || destination.equals(""))) 467 return true; 468 errorMessages.add("destination is a required field and must be set to the destination name."); 469 return false; 470 } 471 472 private boolean isEmpty(String value) { 473 return value == null || "".equals(value.trim()); 474 } 475 476 479 @Override 480 public String toString() { 481 return "ActiveMQActivationSpec{" + 482 "acknowledgeMode='" + acknowledgeMode + "'" + 483 ", destinationType='" + destinationType + "'" + 484 ", messageSelector='" + messageSelector + "'" + 485 ", destination='" + destination + "'" + 486 ", clientId='" + clientId + "'" + 487 ", subscriptionName='" + subscriptionName + "'" + 488 ", subscriptionDurability='" + subscriptionDurability + "'" + 489 "}"; 490 } 491 492 public int getAcknowledgeModeForSession() { 493 if( AUTO_ACKNOWLEDGE_MODE.equals(acknowledgeMode) ) { 494 return Session.AUTO_ACKNOWLEDGE; 495 } else if( DUPS_OK_ACKNOWLEDGE_MODE.equals(acknowledgeMode) ) { 496 return Session.DUPS_OK_ACKNOWLEDGE; 497 } else { 498 return INVALID_ACKNOWLEDGE_MODE; 499 } 500 } 501 502 507 public void setActiveMQDestination(ActiveMQDestination destination) { 508 setDestination(destination.getPhysicalName()); 509 if (destination instanceof Queue ) { 510 setDestinationType(Queue .class.getName()); 511 } 512 else { 513 setDestinationType(Topic .class.getName()); 514 } 515 } 516 517 520 public ActiveMQDestination createDestination() { 521 if( isEmpty(destinationType) || isEmpty(destination) ) 522 return null; 523 524 ActiveMQDestination dest = null; 525 if (Queue .class.getName().equals(destinationType)) { 526 dest = new ActiveMQQueue(destination); 527 } else if (Topic .class.getName().equals(destinationType)) { 528 dest = new ActiveMQTopic(destination); 529 } else { 530 assert false : "Execution should never reach here"; 531 } 532 return dest; 533 } 534 535 public String getMaxMessagesPerSessions() { 536 return maxMessagesPerSessions.toString(); 537 } 538 539 542 public void setMaxMessagesPerSessions(String maxMessagesPerSessions) { 543 if( maxMessagesPerSessions!=null ) { 544 this.maxMessagesPerSessions = maxMessagesPerSessions; 545 } 546 } 547 548 551 public String getMaxSessions() { 552 return maxSessions; 553 } 554 555 558 public void setMaxSessions(String maxSessions) { 559 if( maxSessions!=null ) { 560 this.maxSessions = maxSessions; 561 } 562 } 563 564 567 public String getUseRAManagedTransaction() { 568 return useRAManagedTransaction; 569 } 570 571 574 public void setUseRAManagedTransaction(String useRAManagedTransaction) { 575 if( useRAManagedTransaction!=null ) { 576 this.useRAManagedTransaction = useRAManagedTransaction; 577 } 578 } 579 580 583 public int getMaxMessagesPerSessionsIntValue() { 584 return Integer.parseInt(maxMessagesPerSessions); 585 } 586 587 590 public int getMaxSessionsIntValue() { 591 return Integer.parseInt(maxSessions); 592 } 593 594 public boolean isUseRAManagedTransactionEnabled() { 595 return new Boolean (useRAManagedTransaction).booleanValue(); 596 } 597 598 601 public boolean getNoLocalBooleanValue() { 602 return new Boolean (noLocal).booleanValue(); 603 } 604 605 public String getEnableBatch() { 606 return enableBatch; 607 } 608 609 612 public void setEnableBatch(String enableBatch) { 613 if (enableBatch != null) { 614 this.enableBatch = enableBatch; 615 } 616 } 617 618 public boolean getEnableBatchBooleanValue() { 619 return new Boolean (enableBatch).booleanValue(); 620 } 621 622 public int getMaxMessagesPerBatchIntValue() { 623 return Integer.parseInt(maxMessagesPerBatch); 624 } 625 626 public String getMaxMessagesPerBatch() { 627 return maxMessagesPerBatch.toString(); 628 } 629 630 633 public void setMaxMessagesPerBatch(String maxMessagesPerBatch) { 634 if (maxMessagesPerBatch != null) { 635 this.maxMessagesPerBatch = maxMessagesPerBatch; 636 } 637 } 638 639 642 public short getBackOffMultiplier() { 643 if (redeliveryPolicy == null) { 644 return 0; 645 } 646 return redeliveryPolicy.getBackOffMultiplier(); 647 } 648 649 652 public long getInitialRedeliveryDelay() { 653 if (redeliveryPolicy == null) { 654 return 0; 655 } 656 return redeliveryPolicy.getInitialRedeliveryDelay(); 657 } 658 659 662 public int getMaximumRedeliveries() { 663 if (redeliveryPolicy == null) { 664 return 0; 665 } 666 return redeliveryPolicy.getMaximumRedeliveries(); 667 } 668 669 672 public boolean isUseExponentialBackOff() { 673 if (redeliveryPolicy == null) { 674 return false; 675 } 676 return redeliveryPolicy.isUseExponentialBackOff(); 677 } 678 679 682 public void setBackOffMultiplier(short backOffMultiplier) { 683 lazyCreateRedeliveryPolicy().setBackOffMultiplier(backOffMultiplier); 684 } 685 686 689 public void setInitialRedeliveryDelay(long initialRedeliveryDelay) { 690 lazyCreateRedeliveryPolicy().setInitialRedeliveryDelay(initialRedeliveryDelay); 691 } 692 693 696 public void setMaximumRedeliveries(int maximumRedeliveries) { 697 lazyCreateRedeliveryPolicy().setMaximumRedeliveries(maximumRedeliveries); 698 } 699 700 703 public void setUseExponentialBackOff(boolean useExponentialBackOff) { 704 lazyCreateRedeliveryPolicy().setUseExponentialBackOff(useExponentialBackOff); 705 } 706 707 711 public RedeliveryPolicy redeliveryPolicy() { 712 return redeliveryPolicy; 713 } 714 715 protected RedeliveryPolicy lazyCreateRedeliveryPolicy() { 716 if (redeliveryPolicy == null) { 717 redeliveryPolicy = new RedeliveryPolicy(); 718 } 719 return redeliveryPolicy; 720 } 721 } 722 723 | Popular Tags |