Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/activemq/ra/ActiveMQActivationSpec.java


1   /**
2    *
3    * Copyright 2004 Hiram Chirino
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    *
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   *
17   **/
18  package org.activemq.ra;
19  
20  import java.beans.IntrospectionException;
21  import java.beans.PropertyDescriptor;
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import javax.jms.Queue;
27  import javax.jms.Session;
28  import javax.jms.Topic;
29  import javax.resource.ResourceException;
30  import javax.resource.spi.ActivationSpec;
31  import javax.resource.spi.InvalidPropertyException;
32  import javax.resource.spi.ResourceAdapter;
33  
34  import org.activemq.message.ActiveMQDestination;
35  import org.activemq.message.ActiveMQQueue;
36  import org.activemq.message.ActiveMQTopic;
37  import org.activemq.selector.SelectorParser;
38  
39  /**
40   * @version $Revision: 1.1.1.1 $ $Date: 2005/03/11 21:15:09 $
41   */
42  public class ActiveMQActivationSpec implements ActivationSpec {
43  
44      /** Auto-acknowledge constant for <code>acknowledgeMode</code> property **/
45      public static final String AUTO_ACKNOWLEDGE_MODE = "Auto-acknowledge";
46      /** Dups-ok-acknowledge constant for <code>acknowledgeMode</code> property * */
47      public static final String DUPS_OK_ACKNOWLEDGE_MODE = "Dups-ok-acknowledge";    
48      /** Durable constant for <code>subscriptionDurability</code> property * */
49      public static final String DURABLE_SUBSCRIPTION = "Durable";
50      /** NonDurable constant for <code>subscriptionDurability</code> property * */
51      public static final String NON_DURABLE_SUBSCRIPTION = "NonDurable";
52      
53      public static final int INVALID_ACKNOWLEDGE_MODE = -1;
54      
55      private ActiveMQResourceAdapter resourceAdapter;
56      private String destinationType;
57      private String messageSelector;
58      private String destination;
59      private String acknowledgeMode = AUTO_ACKNOWLEDGE_MODE;
60      private String userName;
61      private String password;
62      private String clientId;
63      private String subscriptionName;
64      private String subscriptionDurability = NON_DURABLE_SUBSCRIPTION;    
65      private String noLocal = "false";
66      private String useRAManagedTransaction = "false";
67      private String maxSessions="10";
68      private String maxMessagesPerSessions="10";
69      private String enableBatch = "false";
70      private String maxMessagesPerBatch = "10";    
71      
72      /**
73       * @see javax.resource.spi.ActivationSpec#validate()
74       */
75      public void validate() throws InvalidPropertyException {
76          List errorMessages = new ArrayList();
77          List propsNotSet = new ArrayList();
78          try {
79              if (!isValidDestination(errorMessages))
80                  propsNotSet.add(new PropertyDescriptor("destination", ActiveMQActivationSpec.class));
81              if (!isValidDestinationType(errorMessages))
82                  propsNotSet.add(new PropertyDescriptor("destinationType", ActiveMQActivationSpec.class));
83              if (!isValidAcknowledgeMode(errorMessages))
84                  propsNotSet.add(new PropertyDescriptor("acknowledgeMode", ActiveMQActivationSpec.class));
85              if (!isValidSubscriptionDurability(errorMessages))
86                  propsNotSet.add(new PropertyDescriptor("subscriptionDurability", ActiveMQActivationSpec.class));
87              if (!isValidClientId(errorMessages))
88                  propsNotSet.add(new PropertyDescriptor("clientId", ActiveMQActivationSpec.class));
89              if (!isValidSubscriptionName(errorMessages))
90                  propsNotSet.add(new PropertyDescriptor("subscriptionName", ActiveMQActivationSpec.class));
91              if (!isValidMaxMessagesPerSessions(errorMessages))
92                  propsNotSet.add(new PropertyDescriptor("maxMessagesPerSessions", ActiveMQActivationSpec.class));
93              if (!isValidMaxSessions(errorMessages))
94                  propsNotSet.add(new PropertyDescriptor("maxSessions", ActiveMQActivationSpec.class));
95              if (!isValidMessageSelector(errorMessages))
96                  propsNotSet.add(new PropertyDescriptor("messageSelector", ActiveMQActivationSpec.class));
97              if (!isValidNoLocal(errorMessages))
98                  propsNotSet.add(new PropertyDescriptor("noLocal", ActiveMQActivationSpec.class));
99              if (!isValidUseRAManagedTransaction(errorMessages))
100                 propsNotSet.add(new PropertyDescriptor("useRAManagedTransaction", ActiveMQActivationSpec.class));
101             if (!isValidEnableBatch(errorMessages))
102                 propsNotSet.add(new PropertyDescriptor("enableBatch", ActiveMQActivationSpec.class));
103             if (!isValidMaxMessagesPerBatch(errorMessages))
104                 propsNotSet.add(new PropertyDescriptor("maxMessagesPerBatch", ActiveMQActivationSpec.class));
105             
106         } catch (IntrospectionException e) {
107             e.printStackTrace();
108         }
109         
110         if (propsNotSet.size() > 0) {
111             StringBuffer b = new StringBuffer();
112             b.append("Invalid settings:");
113             for (Iterator iter = errorMessages.iterator(); iter.hasNext();) {
114                 b.append(" ");
115                 b.append(iter.next());
116             }
117             InvalidPropertyException e = new InvalidPropertyException(b.toString());
118             final PropertyDescriptor[] descriptors = (PropertyDescriptor[]) propsNotSet.toArray(new PropertyDescriptor[propsNotSet.size()]);
119             e.setInvalidPropertyDescriptors(descriptors);
120             throw e;
121         }
122     }
123 
124     private boolean isValidUseRAManagedTransaction(List errorMessages) {
125         try {
126             new Boolean(noLocal);
127             return true;
128         } catch (Throwable e) {
129         }
130         errorMessages.add("noLocal must be set to: true or false.");
131         return false;
132     }
133 
134     private boolean isValidNoLocal(List errorMessages) {
135         try {
136             new Boolean(noLocal);
137             return true;
138         } catch (Throwable e) {
139         }
140         errorMessages.add("noLocal must be set to: true or false.");
141         return false;
142     }
143 
144     private boolean isValidMessageSelector(List errorMessages) {
145         try {
146             if( !isEmpty(messageSelector) ) {
147                 new SelectorParser().parse(messageSelector);
148             }
149             return true;
150         } catch (Throwable e) {
151             errorMessages.add("messageSelector not set to valid message selector: "+e.getMessage());
152             return false;
153         }
154     }
155 
156     private boolean isValidMaxSessions(List errorMessages) {
157         try {
158             if( Integer.parseInt(maxSessions) > 0 ) {
159                 return true;
160             }
161         } catch (NumberFormatException e) {
162         }
163         errorMessages.add("maxSessions must be set to number > 0");
164         return false;
165     }
166 
167     private boolean isValidMaxMessagesPerSessions(List errorMessages) {
168         try {
169             if( Integer.parseInt(maxMessagesPerSessions) > 0 ) {
170                 return true;
171             }
172         } catch (NumberFormatException e) {
173         }
174         errorMessages.add("maxMessagesPerSessions must be set to number > 0");
175         return false;
176     }
177 
178     private boolean isValidMaxMessagesPerBatch(List errorMessages) {
179         try {
180             if( Integer.parseInt(maxMessagesPerBatch) > 0 ) {
181                 return true;
182             }
183         } catch (NumberFormatException e) {
184         }
185         errorMessages.add("maxMessagesPerBatch must be set to number > 0");
186         return false;
187     }
188 
189     private boolean isValidEnableBatch(List errorMessages) {
190         try {
191             new Boolean(enableBatch);
192             return true;
193         } catch (Throwable e) {
194         }
195         errorMessages.add("enableBatch must be set to: true or false");
196         return false;
197     }
198 
199     /**
200      * @see javax.resource.spi.ResourceAdapterAssociation#getResourceAdapter()
201      */
202     public ResourceAdapter getResourceAdapter() {
203         return resourceAdapter;
204     }
205 
206     /**
207      * @see javax.resource.spi.ResourceAdapterAssociation#setResourceAdapter(javax.resource.spi.ResourceAdapter)
208      */
209     public void setResourceAdapter(ResourceAdapter resourceAdapter) throws ResourceException {
210         //spec section 5.3.3
211         if (this.resourceAdapter != null) {
212             throw new ResourceException("ResourceAdapter already set");
213         }
214         if (!(resourceAdapter instanceof ActiveMQResourceAdapter)) {
215             throw new ResourceException("ResourceAdapter is not of type: " + ActiveMQResourceAdapter.class.getName());
216         }
217         this.resourceAdapter = (ActiveMQResourceAdapter) resourceAdapter;
218     }
219 
220 
221     /////////////////////////////////////////////////////////////////////////
222     //
223     // Java Bean getters and setters for this ActivationSpec class.
224     //
225     /////////////////////////////////////////////////////////////////////////
226     /**
227      * @return Returns the destinationType.
228      */
229     public String getDestinationType() {
230         if (!isEmpty(destinationType)) {
231             return destinationType;
232         }
233         return null;
234     }
235 
236     /**
237      * @param destinationType The destinationType to set.
238      */
239     public void setDestinationType(String destinationType) {
240         this.destinationType = destinationType;
241     }
242     
243     public String getPassword() {
244         if (!isEmpty(password)) {                
245             return password;
246         }
247         return null;
248     }
249     
250     public void setPassword(String password) {
251         this.password = password;
252     }
253 
254     public String getUserName() {
255         if (!isEmpty(userName)) {        
256             return userName;
257         }
258         return null;
259     }
260     
261     public void setUserName(String userName) {
262         this.userName = userName;
263     }
264     
265     /**
266      * @return Returns the messageSelector.
267      */
268     public String getMessageSelector() {
269         if (!isEmpty(messageSelector)) {
270             return messageSelector;
271         }
272         return null;
273     }
274 
275     /**
276      * @param messageSelector The messageSelector to set.
277      */
278     public void setMessageSelector(String messageSelector) {
279         this.messageSelector = messageSelector;
280     }
281 
282     /**
283      * @return Returns the noLocal.
284      */
285     public String getNoLocal() {
286         return noLocal;
287     }
288 
289     /**
290      * @param noLocal The noLocal to set.
291      */
292     public void setNoLocal(String noLocal) {
293         if( noLocal!=null ) {
294             this.noLocal = noLocal;
295         }
296     }
297 
298     public String getAcknowledgeMode() {
299         if (!isEmpty(acknowledgeMode)) {
300             return acknowledgeMode;
301         }
302         return null;
303     }
304 
305     public void setAcknowledgeMode(String acknowledgeMode) {
306         this.acknowledgeMode = acknowledgeMode;
307     }
308     
309     public String getClientId() {
310         if (!isEmpty(clientId)) {
311             return clientId;
312         }
313         return null;
314     }
315 
316     public void setClientId(String clientId) {        
317         this.clientId = clientId;            
318     }
319 
320     public String getDestination() {
321         if (!isEmpty(destination)) {
322             return destination;
323         }
324         return null;
325     }
326 
327     public void setDestination(String destination) {
328         this.destination = destination;
329     }
330 
331     public String getSubscriptionDurability() {
332         if (!isEmpty(subscriptionDurability)) {                
333             return subscriptionDurability;
334         }
335         return null;
336     }
337 
338     public void setSubscriptionDurability(String subscriptionDurability) {
339         this.subscriptionDurability = subscriptionDurability;
340     }
341 
342     public String getSubscriptionName() {
343         if (!isEmpty(subscriptionName)) {
344             return subscriptionName;
345         }
346         return null;
347     }
348 
349     public void setSubscriptionName(String subscriptionName) {
350         this.subscriptionName = subscriptionName;
351     }
352     
353     private boolean isValidSubscriptionName(List errorMessages) {
354         if( !isDurableSubscription() ? true : subscriptionName != null && subscriptionName.trim().length() > 0 ) {
355             return true;
356         } 
357         errorMessages.add("subscriptionName must be set since durable subscription was requested.");
358         return false;
359     }
360 
361     private boolean isValidClientId(List errorMessages) {
362         if( !isDurableSubscription() ? true : clientId != null && clientId.trim().length() > 0 ) {
363             return true;
364         }
365         errorMessages.add("clientId must be set since durable subscription was requested.");
366         return false;
367     }
368 
369     public boolean isDurableSubscription() {
370         return DURABLE_SUBSCRIPTION.equals(subscriptionDurability);
371     }
372 
373     private boolean isValidSubscriptionDurability(List errorMessages) {
374         if (NON_DURABLE_SUBSCRIPTION.equals(subscriptionDurability) || DURABLE_SUBSCRIPTION.equals(subscriptionDurability))
375             return true;
376         errorMessages.add("subscriptionDurability must be set to: "+NON_DURABLE_SUBSCRIPTION+" or "+DURABLE_SUBSCRIPTION+".");
377         return false;
378     }
379 
380     private boolean isValidAcknowledgeMode(List errorMessages) {
381         if (AUTO_ACKNOWLEDGE_MODE.equals(acknowledgeMode) || DUPS_OK_ACKNOWLEDGE_MODE.equals(acknowledgeMode))
382             return true;
383         errorMessages.add("acknowledgeMode must be set to: "+AUTO_ACKNOWLEDGE_MODE+" or "+DUPS_OK_ACKNOWLEDGE_MODE+".");
384         return false;
385     }
386 
387     private boolean isValidDestinationType(List errorMessages) {
388         if (Queue.class.getName().equals(destinationType) || Topic.class.getName().equals(destinationType))
389             return true;
390         errorMessages.add("destinationType must be set to: "+Queue.class.getName()+" or "+Topic.class.getName()+".");
391         return false;
392     }
393 
394     private boolean isValidDestination(List errorMessages) {
395         if(!(destination == null || destination.equals(""))) 
396             return true;
397         errorMessages.add("destination is a required field and must be set to the destination name.");
398         return false;
399     }
400      
401     private boolean isEmpty(String value) {
402         return value == null || "".equals(value.trim());
403     }
404 
405    public String toString() {
406         return "ActiveMQActivationSpec{" +
407                 "acknowledgeMode='" + acknowledgeMode + "'" +
408                 ", destinationType='" + destinationType + "'" +
409                 ", messageSelector='" + messageSelector + "'" +
410                 ", destination='" + destination + "'" +
411                 ", clientId='" + clientId + "'" +
412                 ", subscriptionName='" + subscriptionName + "'" +
413                 ", subscriptionDurability='" + subscriptionDurability + "'" +
414                 "}";
415     }
416 
417     public int getAcknowledgeModeForSession() { 
418         if( AUTO_ACKNOWLEDGE_MODE.equals(acknowledgeMode) ) {
419             return Session.AUTO_ACKNOWLEDGE;
420         } else if( DUPS_OK_ACKNOWLEDGE_MODE.equals(acknowledgeMode) ) {
421             return Session.DUPS_OK_ACKNOWLEDGE;
422         } else {
423             return INVALID_ACKNOWLEDGE_MODE;
424         }
425     }
426 
427     public ActiveMQDestination createDestination() {
428         if( isEmpty(destinationType) || isEmpty(destination) )
429             return null;
430         
431         ActiveMQDestination dest = null;
432         if (Queue.class.getName().equals(destinationType)) {
433             dest = new ActiveMQQueue(destination);
434         } else if (Topic.class.getName().equals(destinationType)) {
435             dest = new ActiveMQTopic(destination);
436         } else {
437             assert false : "Execution should never reach here";
438         }
439         return dest;
440     }
441 
442     public String getMaxMessagesPerSessions() {
443         return maxMessagesPerSessions.toString();
444     }
445     
446     public void setMaxMessagesPerSessions(String maxMessagesPerSessions) {
447         if( maxMessagesPerSessions!=null ) {
448             this.maxMessagesPerSessions = maxMessagesPerSessions;
449         }
450     }
451     
452 
453     public String getMaxSessions() {
454         return maxSessions;
455     }
456     public void setMaxSessions(String maxSessions) {
457         if( maxSessions!=null ) {
458             this.maxSessions = maxSessions;
459         }
460     }
461     
462     public String getUseRAManagedTransaction() {
463         return useRAManagedTransaction;
464     }
465     
466     public void setUseRAManagedTransaction(String useRAManagedTransaction) {
467         if( useRAManagedTransaction!=null ) {
468             this.useRAManagedTransaction = useRAManagedTransaction;
469         }
470     }
471 
472     public int getMaxMessagesPerSessionsIntValue() {
473         return Integer.parseInt(maxMessagesPerSessions);
474     }
475 
476     public int getMaxSessionsIntValue() {
477         return Integer.parseInt(maxSessions);
478     }
479 
480     public boolean isUseRAManagedTransactionEnabled() {
481         return new Boolean(useRAManagedTransaction).booleanValue();
482     }
483 
484     public boolean getNoLocalBooleanValue() {
485         return new Boolean(noLocal).booleanValue();
486     }
487 
488     public String getEnableBatch() {
489       return enableBatch;
490     }
491     
492     public void setEnableBatch(String enableBatch) {
493       if (enableBatch != null) {
494         this.enableBatch = enableBatch;
495       }
496     }
497     
498     public boolean getEnableBatchBooleanValue() {
499         return new Boolean(enableBatch).booleanValue();
500     }
501 
502     public int getMaxMessagesPerBatchIntValue() {
503         return Integer.parseInt(maxMessagesPerBatch);
504     }
505 
506     public String getMaxMessagesPerBatch() {
507         return maxMessagesPerBatch.toString();
508     }
509     
510     public void setMaxMessagesPerBatch(String maxMessagesPerBatch) {
511         if (maxMessagesPerBatch != null) {
512             this.maxMessagesPerBatch = maxMessagesPerBatch;
513         }
514     }
515     
516 }