1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.jms;
17
18 import javax.jms.Destination;
19 import javax.jms.Message;
20 import javax.jms.JMSException;
21
22 import org.apache.commons.jelly.JellyTagException;
23 import org.apache.commons.jelly.TagSupport;
24 import org.apache.commons.jelly.XMLOutput;
25
26 import org.apache.commons.messenger.Messenger;
27
28 /*** A tag which creates a JMS message
29 *
30 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31 * @version $Revision: 155420 $
32 */
33 public class MessageTag extends TagSupport {
34
35 /*** The name of the Message variable that is created */
36 private String var;
37
38 /*** The JMS Message created */
39 private Message message;
40
41 /*** The Messenger used to access the JMS connection */
42 private Messenger connection;
43
44 public MessageTag() {
45 }
46
47 /*** Adds a JMS property to the message */
48 public void addProperty(String name, Object value) throws JellyTagException {
49 Message message = getMessage();
50
51 try {
52 message.setObjectProperty(name, value);
53 } catch (JMSException e) {
54 throw new JellyTagException(e);
55 }
56 }
57
58
59
60 public void doTag(XMLOutput output) throws JellyTagException {
61 if ( var == null ) {
62
63 SendTag tag = (SendTag) findAncestorWithClass( SendTag.class );
64 if ( tag == null ) {
65 throw new JellyTagException("<jms:message> tags must either have the 'var' attribute specified or be used inside a <jms:send> tag");
66 }
67
68 tag.setMessage( getMessage() );
69 }
70 else {
71
72 context.setVariable( var, getMessage() );
73
74 }
75 }
76
77
78
79
80 /*** Sets the name of the variable that the message will be exported to */
81 public void setVar(String var) {
82 this.var = var;
83 }
84
85 public Messenger getConnection() throws JellyTagException {
86 if ( connection == null ) {
87 return findConnection();
88 }
89 return connection;
90 }
91
92 /***
93 * Sets the Messenger (the JMS connection pool) that will be used to send the message
94 */
95 public void setConnection(Messenger connection) {
96 this.connection = connection;
97 }
98
99 public Message getMessage() throws JellyTagException {
100 if ( message == null ) {
101 message = createMessage();
102 }
103 return message;
104 }
105
106
107
108
109 /***
110 * Sets the JMS Correlation ID to be used on the message
111 */
112 public void setCorrelationID(String correlationID) throws JellyTagException {
113 try {
114 getMessage().setJMSCorrelationID(correlationID);
115 }
116 catch (JMSException e) {
117 throw new JellyTagException(e);
118 }
119 }
120
121 /***
122 * Sets the reply-to destination to add to the message
123 */
124 public void setReplyTo(Destination destination) throws JellyTagException {
125 try {
126 getMessage().setJMSReplyTo(destination);
127 }
128 catch (JMSException e) {
129 throw new JellyTagException(e);
130 }
131 }
132
133 /***
134 * Sets the type name of the message
135 */
136 public void setType(String type) throws JellyTagException {
137 try {
138 getMessage().setJMSType(type);
139 }
140 catch (JMSException e) {
141 throw new JellyTagException(e);
142 }
143 }
144
145
146
147 protected Messenger findConnection() throws JellyTagException {
148 ConnectionContext messengerTag = (ConnectionContext) findAncestorWithClass( ConnectionContext.class );
149 if ( messengerTag == null ) {
150 throw new JellyTagException("This tag must be within a <jms:connection> tag or the 'connection' attribute should be specified");
151 }
152
153 try {
154 return messengerTag.getConnection();
155 }
156 catch (JMSException e) {
157 throw new JellyTagException(e);
158 }
159 }
160
161 protected Message createMessage() throws JellyTagException {
162 try {
163 return getConnection().createMessage();
164 } catch (JMSException e) {
165 throw new JellyTagException(e);
166 }
167 }
168 }