KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > ra > ActiveMQActivationSpec


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.ra;
19
20 import java.beans.IntrospectionException JavaDoc;
21 import java.beans.PropertyDescriptor JavaDoc;
22 import java.io.Serializable JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.jms.Queue JavaDoc;
28 import javax.jms.Session JavaDoc;
29 import javax.jms.Topic JavaDoc;
30 import javax.resource.ResourceException JavaDoc;
31 import javax.resource.spi.InvalidPropertyException JavaDoc;
32 import javax.resource.spi.ResourceAdapter JavaDoc;
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 /**
41  * Configures the inbound JMS consumer specification using ActiveMQ
42  *
43  * @org.apache.xbean.XBean element="activationSpec"
44  *
45  * @version $Revision: 515576 $ $Date: 2007-03-07 14:14:23 +0000 (Wed, 07 Mar 2007) $
46  */

47 public class ActiveMQActivationSpec implements MessageActivationSpec, Serializable JavaDoc {
48
49     private static final long serialVersionUID = -7153087544100459975L;
50     
51     /** Auto-acknowledge constant for <code>acknowledgeMode</code> property **/
52     public static final String JavaDoc AUTO_ACKNOWLEDGE_MODE = "Auto-acknowledge";
53     /** Dups-ok-acknowledge constant for <code>acknowledgeMode</code> property * */
54     public static final String JavaDoc DUPS_OK_ACKNOWLEDGE_MODE = "Dups-ok-acknowledge";
55     /** Durable constant for <code>subscriptionDurability</code> property * */
56     public static final String JavaDoc DURABLE_SUBSCRIPTION = "Durable";
57     /** NonDurable constant for <code>subscriptionDurability</code> property * */
58     public static final String JavaDoc NON_DURABLE_SUBSCRIPTION = "NonDurable";
59     
60     /**
61      *
62      */

63     public static final int INVALID_ACKNOWLEDGE_MODE = -1;
64     
65     private transient MessageResourceAdapter resourceAdapter;
66     private String JavaDoc destinationType;
67     private String JavaDoc messageSelector;
68     private String JavaDoc destination;
69     private String JavaDoc acknowledgeMode = AUTO_ACKNOWLEDGE_MODE;
70     private String JavaDoc userName;
71     private String JavaDoc password;
72     private String JavaDoc clientId;
73     private String JavaDoc subscriptionName;
74     private String JavaDoc subscriptionDurability = NON_DURABLE_SUBSCRIPTION;
75     private String JavaDoc noLocal = "false";
76     private String JavaDoc useRAManagedTransaction = "false";
77     private String JavaDoc maxSessions="10";
78     private String JavaDoc maxMessagesPerSessions="10";
79     private String JavaDoc enableBatch = "false";
80     private String JavaDoc maxMessagesPerBatch = "10";
81     private RedeliveryPolicy redeliveryPolicy;
82
83     
84     /**
85      * @see javax.resource.spi.ActivationSpec#validate()
86      */

87     public void validate() throws InvalidPropertyException JavaDoc {
88         List JavaDoc errorMessages = new ArrayList JavaDoc();
89         List JavaDoc propsNotSet = new ArrayList JavaDoc();
90         try {
91             if (!isValidDestination(errorMessages))
92                 propsNotSet.add(new PropertyDescriptor JavaDoc("destination", ActiveMQActivationSpec.class));
93             if (!isValidDestinationType(errorMessages))
94                 propsNotSet.add(new PropertyDescriptor JavaDoc("destinationType", ActiveMQActivationSpec.class));
95             if (!isValidAcknowledgeMode(errorMessages))
96                 propsNotSet.add(new PropertyDescriptor JavaDoc("acknowledgeMode", ActiveMQActivationSpec.class));
97             if (!isValidSubscriptionDurability(errorMessages))
98                 propsNotSet.add(new PropertyDescriptor JavaDoc("subscriptionDurability", ActiveMQActivationSpec.class));
99             if (!isValidClientId(errorMessages))
100                 propsNotSet.add(new PropertyDescriptor JavaDoc("clientId", ActiveMQActivationSpec.class));
101             if (!isValidSubscriptionName(errorMessages))
102                 propsNotSet.add(new PropertyDescriptor JavaDoc("subscriptionName", ActiveMQActivationSpec.class));
103             if (!isValidMaxMessagesPerSessions(errorMessages))
104                 propsNotSet.add(new PropertyDescriptor JavaDoc("maxMessagesPerSessions", ActiveMQActivationSpec.class));
105             if (!isValidMaxSessions(errorMessages))
106                 propsNotSet.add(new PropertyDescriptor JavaDoc("maxSessions", ActiveMQActivationSpec.class));
107             if (!isValidMessageSelector(errorMessages))
108                 propsNotSet.add(new PropertyDescriptor JavaDoc("messageSelector", ActiveMQActivationSpec.class));
109             if (!isValidNoLocal(errorMessages))
110                 propsNotSet.add(new PropertyDescriptor JavaDoc("noLocal", ActiveMQActivationSpec.class));
111             if (!isValidUseRAManagedTransaction(errorMessages))
112                 propsNotSet.add(new PropertyDescriptor JavaDoc("useRAManagedTransaction", ActiveMQActivationSpec.class));
113             if (!isValidEnableBatch(errorMessages))
114                 propsNotSet.add(new PropertyDescriptor JavaDoc("enableBatch", ActiveMQActivationSpec.class));
115             if (!isValidMaxMessagesPerBatch(errorMessages))
116                 propsNotSet.add(new PropertyDescriptor JavaDoc("maxMessagesPerBatch", ActiveMQActivationSpec.class));
117
118             
119         } catch (IntrospectionException JavaDoc e) {
120             e.printStackTrace();
121         }
122         
123         if (propsNotSet.size() > 0) {
124             StringBuffer JavaDoc b = new StringBuffer JavaDoc();
125             b.append("Invalid settings:");
126             for (Iterator JavaDoc iter = errorMessages.iterator(); iter.hasNext();) {
127                 b.append(" ");
128                 b.append(iter.next());
129             }
130             InvalidPropertyException JavaDoc e = new InvalidPropertyException JavaDoc(b.toString());
131             final PropertyDescriptor JavaDoc[] descriptors = (PropertyDescriptor JavaDoc[]) propsNotSet.toArray(new PropertyDescriptor JavaDoc[propsNotSet.size()]);
132             e.setInvalidPropertyDescriptors(descriptors);
133             throw e;
134         }
135     }
136
137     private boolean isValidUseRAManagedTransaction(List JavaDoc errorMessages) {
138         try {
139             new Boolean JavaDoc(noLocal);
140             return true;
141         } catch (Throwable JavaDoc e) {
142             //
143
}
144         errorMessages.add("noLocal must be set to: true or false.");
145         return false;
146     }
147
148     private boolean isValidNoLocal(List JavaDoc errorMessages) {
149         try {
150             new Boolean JavaDoc(noLocal);
151             return true;
152         } catch (Throwable JavaDoc e) {
153             //
154
}
155         errorMessages.add("noLocal must be set to: true or false.");
156         return false;
157     }
158
159     private boolean isValidMessageSelector(List JavaDoc errorMessages) {
160         try {
161             if( !isEmpty(messageSelector) ) {
162                 new SelectorParser().parse(messageSelector);
163             }
164             return true;
165         } catch (Throwable JavaDoc e) {
166             errorMessages.add("messageSelector not set to valid message selector: "+e.getMessage());
167             return false;
168         }
169     }
170
171     private boolean isValidMaxSessions(List JavaDoc errorMessages) {
172         try {
173             if( Integer.parseInt(maxSessions) > 0 ) {
174                 return true;
175             }
176         } catch (NumberFormatException JavaDoc e) {
177             //
178
}
179         errorMessages.add("maxSessions must be set to number > 0");
180         return false;
181     }
182
183     private boolean isValidMaxMessagesPerSessions(List JavaDoc errorMessages) {
184         try {
185             if( Integer.parseInt(maxMessagesPerSessions) > 0 ) {
186                 return true;
187             }
188         } catch (NumberFormatException JavaDoc e) {
189             //
190
}
191         errorMessages.add("maxMessagesPerSessions must be set to number > 0");
192         return false;
193     }
194
195     private boolean isValidMaxMessagesPerBatch(List JavaDoc errorMessages) {
196         try {
197             if( Integer.parseInt(maxMessagesPerBatch) > 0 ) {
198                 return true;
199             }
200         } catch (NumberFormatException JavaDoc e) {
201             //
202
}
203         errorMessages.add("maxMessagesPerBatch must be set to number > 0");
204         return false;
205     }
206
207     private boolean isValidEnableBatch(List JavaDoc errorMessages) {
208         try {
209             new Boolean JavaDoc(enableBatch);
210             return true;
211         } catch (Throwable JavaDoc e) {
212             //
213
}
214         errorMessages.add("enableBatch must be set to: true or false");
215         return false;
216     }
217
218     /**
219      * @see javax.resource.spi.ResourceAdapterAssociation#getResourceAdapter()
220      */

221     public ResourceAdapter JavaDoc getResourceAdapter() {
222         return resourceAdapter;
223     }
224
225     /**
226      * @see javax.resource.spi.ResourceAdapterAssociation#setResourceAdapter(javax.resource.spi.ResourceAdapter)
227      */

228     public void setResourceAdapter(ResourceAdapter JavaDoc resourceAdapter) throws ResourceException JavaDoc {
229         //spec section 5.3.3
230
if (this.resourceAdapter != null) {
231             throw new ResourceException JavaDoc("ResourceAdapter already set");
232         }
233         if (!(resourceAdapter instanceof MessageResourceAdapter)) {
234             throw new ResourceException JavaDoc("ResourceAdapter is not of type: " + MessageResourceAdapter.class.getName());
235         }
236         this.resourceAdapter = (MessageResourceAdapter) resourceAdapter;
237     }
238
239
240     /////////////////////////////////////////////////////////////////////////
241
//
242
// Java Bean getters and setters for this ActivationSpec class.
243
//
244
/////////////////////////////////////////////////////////////////////////
245
/**
246      * @return Returns the destinationType.
247      */

248     public String JavaDoc getDestinationType() {
249         if (!isEmpty(destinationType)) {
250             return destinationType;
251         }
252         return null;
253     }
254
255     /**
256      * @param destinationType The destinationType to set.
257      */

258     public void setDestinationType(String JavaDoc destinationType) {
259         this.destinationType = destinationType;
260     }
261     
262     /**
263      *
264      */

265     public String JavaDoc getPassword() {
266         if (!isEmpty(password)) {
267             return password;
268         }
269         return null;
270     }
271     
272     /**
273      *
274      */

275     public void setPassword(String JavaDoc password) {
276         this.password = password;
277     }
278
279     /**
280      *
281      */

282     public String JavaDoc getUserName() {
283         if (!isEmpty(userName)) {
284             return userName;
285         }
286         return null;
287     }
288     
289     /**
290      *
291      */

292     public void setUserName(String JavaDoc userName) {
293         this.userName = userName;
294     }
295     
296     /**
297      * @return Returns the messageSelector.
298      */

299     public String JavaDoc getMessageSelector() {
300         if (!isEmpty(messageSelector)) {
301             return messageSelector;
302         }
303         return null;
304     }
305
306     /**
307      * @param messageSelector The messageSelector to set.
308      */

309     public void setMessageSelector(String JavaDoc messageSelector) {
310         this.messageSelector = messageSelector;
311     }
312
313     /**
314      * @return Returns the noLocal.
315      */

316     public String JavaDoc getNoLocal() {
317         return noLocal;
318     }
319
320     /**
321      * @param noLocal The noLocal to set.
322      */

323     public void setNoLocal(String JavaDoc noLocal) {
324         if( noLocal!=null ) {
325             this.noLocal = noLocal;
326         }
327     }
328
329     /**
330      *
331      */

332     public String JavaDoc getAcknowledgeMode() {
333         if (!isEmpty(acknowledgeMode)) {
334             return acknowledgeMode;
335         }
336         return null;
337     }
338
339     /**
340      *
341      */

342     public void setAcknowledgeMode(String JavaDoc acknowledgeMode) {
343         this.acknowledgeMode = acknowledgeMode;
344     }
345     
346     /**
347      *
348      */

349     public String JavaDoc getClientId() {
350         if (!isEmpty(clientId)) {
351             return clientId;
352         }
353         return null;
354     }
355
356     /**
357      *
358      */

359     public void setClientId(String JavaDoc clientId) {
360         this.clientId = clientId;
361     }
362
363     /**
364      *
365      */

366     public String JavaDoc getDestination() {
367         if (!isEmpty(destination)) {
368             return destination;
369         }
370         return null;
371     }
372
373     /**
374      *
375      */

376     public void setDestination(String JavaDoc destination) {
377         this.destination = destination;
378     }
379
380     /**
381      *
382      */

383     public String JavaDoc getSubscriptionDurability() {
384         if (!isEmpty(subscriptionDurability)) {
385             return subscriptionDurability;
386         }
387         return null;
388     }
389
390     /**
391      *
392      */

393     public void setSubscriptionDurability(String JavaDoc subscriptionDurability) {
394         this.subscriptionDurability = subscriptionDurability;
395     }
396
397     /**
398      *
399      */

400     public String JavaDoc getSubscriptionName() {
401         if (!isEmpty(subscriptionName)) {
402             return subscriptionName;
403         }
404         return null;
405     }
406
407     /**
408      *
409      */

410     public void setSubscriptionName(String JavaDoc subscriptionName) {
411         this.subscriptionName = subscriptionName;
412     }
413     
414     private boolean isValidSubscriptionName(List JavaDoc 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 JavaDoc 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     /**
431      *
432      */

433     public boolean isDurableSubscription() {
434         return DURABLE_SUBSCRIPTION.equals(subscriptionDurability);
435     }
436
437     private boolean isValidSubscriptionDurability(List JavaDoc errorMessages) {
438         // subscriptionDurability only applies to Topics
439
if ( DURABLE_SUBSCRIPTION.equals(subscriptionDurability) &&
440              getDestinationType() != null && !Topic JavaDoc.class.getName().equals(getDestinationType())) {
441             errorMessages.add("subscriptionDurability cannot be set to: "+DURABLE_SUBSCRIPTION+" when destinationType is set to "+
442                 Queue JavaDoc.class.getName()+" as it is only valid when destinationType is set to "+Topic JavaDoc.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 JavaDoc 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 JavaDoc errorMessages) {
459         if (Queue JavaDoc.class.getName().equals(destinationType) || Topic JavaDoc.class.getName().equals(destinationType))
460             return true;
461         errorMessages.add("destinationType must be set to: "+Queue JavaDoc.class.getName()+" or "+Topic JavaDoc.class.getName()+".");
462         return false;
463     }
464
465     private boolean isValidDestination(List JavaDoc 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 JavaDoc value) {
473         return value == null || "".equals(value.trim());
474     }
475
476     /**
477      *
478      */

479    @Override JavaDoc
480 public String JavaDoc 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     /**
503      * A helper method mostly for use in Dependency Injection containers
504      * which allows you to customize the destination and destinationType properties
505      * from a single ActiveMQDestination POJO
506      */

507     public void setActiveMQDestination(ActiveMQDestination destination) {
508         setDestination(destination.getPhysicalName());
509         if (destination instanceof Queue JavaDoc) {
510             setDestinationType(Queue JavaDoc.class.getName());
511         }
512         else {
513             setDestinationType(Topic JavaDoc.class.getName());
514         }
515     }
516
517     /**
518      *
519      */

520     public ActiveMQDestination createDestination() {
521         if( isEmpty(destinationType) || isEmpty(destination) )
522             return null;
523         
524         ActiveMQDestination dest = null;
525         if (Queue JavaDoc.class.getName().equals(destinationType)) {
526             dest = new ActiveMQQueue(destination);
527         } else if (Topic JavaDoc.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 JavaDoc getMaxMessagesPerSessions() {
536         return maxMessagesPerSessions.toString();
537     }
538     
539     /**
540      *
541      */

542     public void setMaxMessagesPerSessions(String JavaDoc maxMessagesPerSessions) {
543         if( maxMessagesPerSessions!=null ) {
544             this.maxMessagesPerSessions = maxMessagesPerSessions;
545         }
546     }
547
548     /**
549      *
550      */

551     public String JavaDoc getMaxSessions() {
552         return maxSessions;
553     }
554
555     /**
556      *
557      */

558     public void setMaxSessions(String JavaDoc maxSessions) {
559         if( maxSessions!=null ) {
560             this.maxSessions = maxSessions;
561         }
562     }
563     
564     /**
565      *
566      */

567     public String JavaDoc getUseRAManagedTransaction() {
568         return useRAManagedTransaction;
569     }
570     
571     /**
572      *
573      */

574     public void setUseRAManagedTransaction(String JavaDoc useRAManagedTransaction) {
575         if( useRAManagedTransaction!=null ) {
576             this.useRAManagedTransaction = useRAManagedTransaction;
577         }
578     }
579
580     /**
581      *
582      */

583     public int getMaxMessagesPerSessionsIntValue() {
584         return Integer.parseInt(maxMessagesPerSessions);
585     }
586
587     /**
588      *
589      */

590     public int getMaxSessionsIntValue() {
591         return Integer.parseInt(maxSessions);
592     }
593
594     public boolean isUseRAManagedTransactionEnabled() {
595         return new Boolean JavaDoc(useRAManagedTransaction).booleanValue();
596     }
597
598     /**
599      *
600      */

601     public boolean getNoLocalBooleanValue() {
602         return new Boolean JavaDoc(noLocal).booleanValue();
603     }
604
605     public String JavaDoc getEnableBatch() {
606         return enableBatch;
607     }
608
609     /**
610      *
611      */

612     public void setEnableBatch(String JavaDoc enableBatch) {
613         if (enableBatch != null) {
614             this.enableBatch = enableBatch;
615         }
616     }
617
618     public boolean getEnableBatchBooleanValue() {
619         return new Boolean JavaDoc(enableBatch).booleanValue();
620     }
621
622     public int getMaxMessagesPerBatchIntValue() {
623         return Integer.parseInt(maxMessagesPerBatch);
624     }
625
626     public String JavaDoc getMaxMessagesPerBatch() {
627         return maxMessagesPerBatch.toString();
628     }
629
630     /**
631      *
632      */

633     public void setMaxMessagesPerBatch(String JavaDoc maxMessagesPerBatch) {
634         if (maxMessagesPerBatch != null) {
635             this.maxMessagesPerBatch = maxMessagesPerBatch;
636         }
637     }
638
639     /**
640      *
641      */

642     public short getBackOffMultiplier() {
643         if (redeliveryPolicy == null) {
644             return 0;
645         }
646         return redeliveryPolicy.getBackOffMultiplier();
647     }
648
649     /**
650      *
651      */

652     public long getInitialRedeliveryDelay() {
653         if (redeliveryPolicy == null) {
654             return 0;
655         }
656         return redeliveryPolicy.getInitialRedeliveryDelay();
657     }
658
659     /**
660      *
661      */

662     public int getMaximumRedeliveries() {
663         if (redeliveryPolicy == null) {
664             return 0;
665         }
666         return redeliveryPolicy.getMaximumRedeliveries();
667     }
668
669     /**
670      *
671      */

672     public boolean isUseExponentialBackOff() {
673         if (redeliveryPolicy == null) {
674             return false;
675         }
676         return redeliveryPolicy.isUseExponentialBackOff();
677     }
678
679     /**
680      *
681      */

682     public void setBackOffMultiplier(short backOffMultiplier) {
683         lazyCreateRedeliveryPolicy().setBackOffMultiplier(backOffMultiplier);
684     }
685
686     /**
687      *
688      */

689     public void setInitialRedeliveryDelay(long initialRedeliveryDelay) {
690         lazyCreateRedeliveryPolicy().setInitialRedeliveryDelay(initialRedeliveryDelay);
691     }
692
693     /**
694      *
695      */

696     public void setMaximumRedeliveries(int maximumRedeliveries) {
697         lazyCreateRedeliveryPolicy().setMaximumRedeliveries(maximumRedeliveries);
698     }
699
700     /**
701      *
702      */

703     public void setUseExponentialBackOff(boolean useExponentialBackOff) {
704         lazyCreateRedeliveryPolicy().setUseExponentialBackOff(useExponentialBackOff);
705     }
706
707     // don't use getter to avoid causing introspection errors in containers
708
/**
709      *
710      */

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