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.jelly.XMLOutput;
24  import org.apache.commons.messenger.Messenger;
25  
26  /*** Creates a Destination object from a String name.
27    *
28    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
29    * @version $Revision: 155420 $
30    */
31  public class DestinationTag extends TagSupport {
32  
33      /*** The variable name to create */
34      private String var;
35  
36      /*** Stores the name of the map entry */
37      private String name;
38  
39      // Tag interface
40      //-------------------------------------------------------------------------
41      public void doTag(XMLOutput output) throws JellyTagException {
42          ConnectionContext messengerTag = (ConnectionContext) findAncestorWithClass( ConnectionContext.class );
43          if ( messengerTag == null ) {
44              throw new JellyTagException("<jms:destination> tag must be within a <jms:connection> or <jms:send> or <jms:receive> tag");
45          }
46  
47          Destination destination = null;
48          try {
49              Messenger messenger = messengerTag.getConnection();
50              if (messenger == null) {
51                  throw new JellyTagException("No JMS Connection could be found!" );
52              }
53              String subject = (name != null) ? name : getBodyText();
54              destination = messenger.getDestination( subject );
55          }
56          catch (JMSException e) {
57              throw new JellyTagException(e);
58          }
59  
60          if ( var != null ) {
61              context.setVariable( var, destination );
62          }
63          else {
64              MessageOperationTag tag = (MessageOperationTag) findAncestorWithClass( MessageOperationTag.class );
65              if ( tag == null ) {
66                  throw new JellyTagException("<jms:destination> tag must be within a <jms:send> or <jms:receive> tag or the 'var' attribute should be specified");
67              }
68              tag.setDestination( destination );
69          }
70      }
71  
72  
73      // Properties
74      //-------------------------------------------------------------------------
75  
76      /*** Sets the name of the Destination
77        */
78      public void setName(String name) {
79          this.name = name;
80      }
81  
82      /*** Sets the variable name to use for the Destination
83        */
84      public void setVar(String var) {
85          this.var = var;
86      }
87  }