%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.jms.MessageTag |
|
|
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.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: 1.5 $ |
|
32 | */ |
|
33 | 0 | 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 | 0 | public MessageTag() { |
45 | 0 | } |
46 | ||
47 | /** Adds a JMS property to the message */ |
|
48 | public void addProperty(String name, Object value) throws JellyTagException { |
|
49 | 0 | Message message = getMessage(); |
50 | ||
51 | try { |
|
52 | 0 | message.setObjectProperty(name, value); |
53 | 0 | } catch (JMSException e) { |
54 | 0 | throw new JellyTagException(e); |
55 | } |
|
56 | 0 | } |
57 | ||
58 | // Tag interface |
|
59 | //------------------------------------------------------------------------- |
|
60 | public void doTag(XMLOutput output) throws JellyTagException { |
|
61 | 0 | if ( var == null ) { |
62 | // expose message to parent message consumer |
|
63 | 0 | SendTag tag = (SendTag) findAncestorWithClass( SendTag.class ); |
64 | 0 | if ( tag == null ) { |
65 | 0 | throw new JellyTagException("<jms:message> tags must either have the 'var' attribute specified or be used inside a <jms:send> tag"); |
66 | } |
|
67 | ||
68 | 0 | tag.setMessage( getMessage() ); |
69 | } |
|
70 | else { |
|
71 | ||
72 | 0 | context.setVariable( var, getMessage() ); |
73 | ||
74 | } |
|
75 | 0 | } |
76 | ||
77 | // Properties |
|
78 | //------------------------------------------------------------------------- |
|
79 | ||
80 | /** Sets the name of the variable that the message will be exported to */ |
|
81 | public void setVar(String var) { |
|
82 | 0 | this.var = class="keyword">var; |
83 | 0 | } |
84 | ||
85 | public Messenger getConnection() throws JellyTagException { |
|
86 | 0 | if ( connection == null ) { |
87 | 0 | return findConnection(); |
88 | } |
|
89 | 0 | 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 | 0 | this.connection = connection; |
97 | 0 | } |
98 | ||
99 | public Message getMessage() throws JellyTagException { |
|
100 | 0 | if ( message == null ) { |
101 | 0 | message = createMessage(); |
102 | } |
|
103 | 0 | return message; |
104 | } |
|
105 | ||
106 | ||
107 | // JMS related properties |
|
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 | 0 | getMessage().setJMSCorrelationID(correlationID); |
115 | 0 | } |
116 | catch (JMSException e) { |
|
117 | 0 | throw new JellyTagException(e); |
118 | } |
|
119 | 0 | } |
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 | 0 | getMessage().setJMSReplyTo(destination); |
127 | 0 | } |
128 | catch (JMSException e) { |
|
129 | 0 | throw new JellyTagException(e); |
130 | } |
|
131 | 0 | } |
132 | ||
133 | /** |
|
134 | * Sets the type name of the message |
|
135 | */ |
|
136 | public void setType(String type) throws JellyTagException { |
|
137 | try { |
|
138 | 0 | getMessage().setJMSType(type); |
139 | 0 | } |
140 | catch (JMSException e) { |
|
141 | 0 | throw new JellyTagException(e); |
142 | } |
|
143 | 0 | } |
144 | ||
145 | // Implementation methods |
|
146 | //------------------------------------------------------------------------- |
|
147 | protected Messenger findConnection() throws JellyTagException { |
|
148 | 0 | ConnectionContext messengerTag = (ConnectionContext) findAncestorWithClass( ConnectionContext.class ); |
149 | 0 | if ( messengerTag == null ) { |
150 | 0 | throw new JellyTagException("This tag must be within a <jms:connection> tag or the 'connection' attribute should be specified"); |
151 | } |
|
152 | ||
153 | try { |
|
154 | 0 | return messengerTag.getConnection(); |
155 | } |
|
156 | catch (JMSException e) { |
|
157 | 0 | throw new JellyTagException(e); |
158 | } |
|
159 | } |
|
160 | ||
161 | protected Message createMessage() throws JellyTagException { |
|
162 | try { |
|
163 | 0 | return getConnection().createMessage(); |
164 | } catch (JMSException e) { |
|
165 | 0 | throw new JellyTagException(e); |
166 | } |
|
167 | } |
|
168 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |