KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > jms > DestinationTag


1 /*
2  * Copyright 1999,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
17 package org.apache.taglibs.jms;
18
19 import javax.jms.Destination JavaDoc;
20 import javax.jms.JMSException JavaDoc;
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
23
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: 1.2 $
30   */

31 public class DestinationTag extends AbstractBodyTag {
32
33     /** The variable name to create */
34     private String JavaDoc var;
35         
36     /** Stores the name of the map entry */
37     private String JavaDoc name;
38         
39     // BodyTag interface
40
//-------------------------------------------------------------------------
41
public int doStartTag() throws JspException JavaDoc {
42         if ( name != null ) {
43             exportDestination();
44             return SKIP_BODY;
45         }
46         return EVAL_BODY_TAG;
47     }
48     
49     public int doAfterBody() throws JspException JavaDoc {
50         BodyContent JavaDoc body = getBodyContent();
51         name = body.getString();
52         body.clearBody();
53         exportDestination();
54         return EVAL_PAGE;
55     }
56     
57     public void release() {
58         name = null;
59         var = null;
60     }
61     
62     // Properties
63
//-------------------------------------------------------------------------
64

65     /** Sets the name of the Destination
66       */

67     public void setName(String JavaDoc name) {
68         this.name = name;
69     }
70     
71     /** Sets the variable name to use for the Destination
72       */

73     public void setVar(String JavaDoc var) {
74         this.var = var;
75     }
76     
77     // Implementation methods
78
//-------------------------------------------------------------------------
79
protected void exportDestination() throws JspException JavaDoc {
80         try {
81             ConnectionContext messengerTag = (ConnectionContext) findAncestorWithClass( this, ConnectionContext.class );
82             if ( messengerTag == null ) {
83                 throw new JspException JavaDoc("<jms:destination> tag must be within a <jms:connection> or <jms:send> or <jms:receive> tag");
84             }
85             Messenger messenger = messengerTag.getConnection();
86             Destination JavaDoc destination = messenger.getDestination( name );
87             if ( var != null ) {
88                 pageContext.setAttribute( var, destination );
89             }
90             else {
91                 MessageOperationTag tag = (MessageOperationTag) findAncestorWithClass( this, MessageOperationTag.class );
92                 if ( tag == null ) {
93                     throw new JspException JavaDoc("<jms:destination> tag must be within a <jms:send> or <jms:receive> tag or the 'var' attribute should be specified");
94                 }
95                 tag.setDestination( destination );
96             }
97         }
98         catch (JMSException JavaDoc e) {
99             throw new JspException JavaDoc( "Failed to create the Destination for: " + name );
100         }
101     }
102 }
103
Popular Tags