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.Message;
19  import javax.jms.MessageListener;
20  
21  import org.apache.commons.jelly.JellyContext;
22  import org.apache.commons.jelly.JellyTagException;
23  import org.apache.commons.jelly.Script;
24  import org.apache.commons.jelly.TagSupport;
25  import org.apache.commons.jelly.XMLOutput;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  /***
31   * This tag creates a JMS MessageListener which will invoke this
32   * tag's body whenever a JMS Message is received. The JMS Message
33   * will be available via a variable, which defaults to the 'message'
34   * variable name, but can be overloaded by the var attribute.
35   *
36   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
37   * @version $Revision: 155420 $
38   */
39  public class OnMessageTag extends TagSupport {
40  
41      /*** The Log to which logging calls will be made. */
42      private static final Log log = LogFactory.getLog(OnMessageTag.class);
43  
44      private String var = "message";
45  
46      public OnMessageTag() {
47      }
48  
49      // Tag interface
50      //-------------------------------------------------------------------------
51      public void doTag(XMLOutput output) throws JellyTagException {
52          ConsumerTag tag = (ConsumerTag) findAncestorWithClass(ConsumerTag.class);
53          if (tag == null) {
54              throw new JellyTagException("This tag must be nested within a ConsumerTag like the subscribe tag");
55          }
56  
57          final JellyContext childContext = context.newJellyContext();
58          final Script script = getBody();
59          final XMLOutput childOutput = output;
60  
61          MessageListener listener = new MessageListener() {
62              public void onMessage(Message message) {
63                  childContext.setVariable(var, message);
64                  try {
65                      script.run(childContext, childOutput);
66                  }
67                  catch (Exception e) {
68                      log.error("Caught exception processing message: " + message + ". Exception: " + e, e);
69                  }
70              }
71          };
72  
73          // perform the consumption
74          tag.setMessageListener(listener);
75      }
76  
77  
78      // Properties
79      //-------------------------------------------------------------------------
80  
81      /***
82       * Sets the name of the variable used to make the JMS message available to this tags
83       * body when a message is received.
84       */
85      public void setVar(String var) {
86          this.var = var;
87      }
88  }