KickJava   Java API By Example, From Geeks To Geeks.

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


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.jms.Message JavaDoc;
22 import javax.servlet.jsp.JspException JavaDoc;
23
24 import org.apache.commons.messenger.Messenger;
25
26 /** Creates a JMS message
27   *
28   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
29   * @version $Revision: 1.2 $
30   */

31 public class MessageTag extends AbstractTag {
32
33     /** The name of the Message variable that is created */
34     private String JavaDoc var;
35     
36     /** The JMS Message created */
37     private Message JavaDoc message;
38     
39     /** The Messenger used to access the JMS connection */
40     private Messenger connection;
41     
42     public MessageTag() {
43     }
44     
45     /** Adds a JMS property to the message */
46     public void addProperty(String JavaDoc name, Object JavaDoc value) throws JspException JavaDoc, JMSException JavaDoc {
47         Message JavaDoc message = getMessage();
48         message.setObjectProperty(name, value);
49     }
50     
51     // BodyTag interface
52
//-------------------------------------------------------------------------
53
public int doStartTag() throws JspException JavaDoc {
54         return EVAL_BODY_INCLUDE;
55     }
56     
57     public int doEndTag() throws JspException JavaDoc {
58         try {
59             if ( var == null ) {
60                 // expose message to parent message consumer
61
SendTag tag = (SendTag) findAncestorWithClass( this, SendTag.class );
62                 if ( tag == null ) {
63                     throw new JspException JavaDoc("<jms:message> tags must either have the 'var' attribute specified or be used inside a <jms:send> tag");
64                 }
65                 tag.setMessage( getMessage() );
66             }
67             else {
68                 pageContext.setAttribute( var, getMessage() );
69             }
70         }
71         catch (JMSException JavaDoc e) {
72             throw new JspException JavaDoc( "Failed to create message: " + e, e );
73         }
74         return EVAL_PAGE;
75     }
76     
77     public void release() {
78         super.release();
79         var = null;
80         connection = null;
81         message = null;
82     }
83     
84     
85     // Properties
86
//-------------------------------------------------------------------------
87

88     /** Sets the name of the variable that the message will be exported to */
89     public void setVar(String JavaDoc var) {
90         this.var = var;
91     }
92     
93     public Messenger getConnection() throws JspException JavaDoc, JMSException JavaDoc {
94         if ( connection == null ) {
95             return findConnection();
96         }
97         return connection;
98     }
99     
100     public void setConnection(Messenger connection) {
101         this.connection = connection;
102     }
103     
104     public Message JavaDoc getMessage() throws JspException JavaDoc, JMSException JavaDoc {
105         if ( message == null ) {
106             message = createMessage();
107         }
108         return message;
109     }
110     
111
112     // JMS related properties
113

114     public void setCorrelationID(String JavaDoc correlationID) throws JspException JavaDoc, JMSException JavaDoc {
115         getMessage().setJMSCorrelationID(correlationID);
116     }
117     
118     public void setReplyTo(Destination JavaDoc destination) throws JspException JavaDoc, JMSException JavaDoc {
119         getMessage().setJMSReplyTo(destination);
120     }
121     
122     public void setType(String JavaDoc type) throws JspException JavaDoc, JMSException JavaDoc {
123         getMessage().setJMSType(type);
124     }
125     
126     // Implementation methods
127
//-------------------------------------------------------------------------
128
protected Messenger findConnection() throws JspException JavaDoc, JMSException JavaDoc {
129         ConnectionContext messengerTag = (ConnectionContext) findAncestorWithClass( this, ConnectionContext.class );
130         if ( messengerTag == null ) {
131             throw new JspException JavaDoc("This tag must be within a <jms:connection> tag or the 'connection' attribute should be specified");
132         }
133         return messengerTag.getConnection();
134     }
135     
136     protected Message JavaDoc createMessage() throws JspException JavaDoc, JMSException JavaDoc {
137         return getConnection().createMessage();
138     }
139 }
140
Popular Tags