View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.tags.jms;
17  
18  import javax.jms.Destination;
19  import javax.jms.JMSException;
20  
21  import org.apache.commons.jelly.JellyTagException;
22  import org.apache.commons.jelly.TagSupport;
23  import org.apache.commons.messenger.Messenger;
24  
25  /*** An abstract base class for JMS Message operation tags such as send, receive or call.
26    *
27    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
28    * @version $Revision: 155420 $
29    */
30  public abstract class MessageOperationTag extends TagSupport implements ConnectionContext {
31  
32      /*** The Messenger used to access the JMS connection */
33      private Messenger connection;
34  
35      /*** The Destination */
36      private Destination destination;
37  
38      /*** The String subject used to find a destination */
39      private String subject;
40  
41      public MessageOperationTag() {
42      }
43  
44      // Properties
45      //-------------------------------------------------------------------------
46      public Messenger getConnection() throws JellyTagException, JMSException {
47          if ( connection == null ) {
48              return findConnection();
49          }
50          return connection;
51      }
52  
53      /***
54       * Sets the Messenger (the JMS connection pool) that will be used to send the message
55       */
56      public void setConnection(Messenger connection) {
57          this.connection = connection;
58      }
59  
60      public Destination getDestination() throws JellyTagException, JMSException {
61          if (destination == null) {
62              // if we have a subject defined, lets use it to find the destination
63              if (subject != null) {
64                  destination = findDestination(subject);
65              }
66          }
67          return destination;
68      }
69  
70      /***
71       * Sets the JMS Destination to be used by this tag
72       */
73      public void setDestination(Destination destination) {
74          this.destination = destination;
75      }
76  
77      /***
78       * Sets the subject as a String which is used to create the
79       * JMS Destination to be used by this tag
80       */
81      public void setSubject(String subject) {
82          this.subject = subject;
83      }
84  
85      // Implementation methods
86      //-------------------------------------------------------------------------
87  
88      /***
89       * Strategy Method allowing derived classes to change this behaviour
90       */
91      protected Messenger findConnection() throws JellyTagException, JMSException {
92          ConnectionContext messengerTag = (ConnectionContext) findAncestorWithClass( ConnectionContext.class );
93          if ( messengerTag == null ) {
94              throw new JellyTagException("This tag must be within a <jms:connection> tag or the 'connection' attribute should be specified");
95          }
96          return messengerTag.getConnection();
97      }
98  
99      /***
100      * Strategy Method allowing derived classes to change this behaviour
101      */
102     protected Destination findDestination(String subject) throws JellyTagException, JMSException {
103         return getConnection().getDestination(subject);
104     }
105 }