1 16 17 package org.apache.taglibs.jms; 18 19 import javax.jms.Destination ; 20 import javax.jms.JMSException ; 21 import javax.jms.Message ; 22 import javax.servlet.jsp.JspException ; 23 24 import org.apache.commons.messenger.Messenger; 25 26 31 public class MessageTag extends AbstractTag { 32 33 34 private String var; 35 36 37 private Message message; 38 39 40 private Messenger connection; 41 42 public MessageTag() { 43 } 44 45 46 public void addProperty(String name, Object value) throws JspException , JMSException { 47 Message message = getMessage(); 48 message.setObjectProperty(name, value); 49 } 50 51 public int doStartTag() throws JspException { 54 return EVAL_BODY_INCLUDE; 55 } 56 57 public int doEndTag() throws JspException { 58 try { 59 if ( var == null ) { 60 SendTag tag = (SendTag) findAncestorWithClass( this, SendTag.class ); 62 if ( tag == null ) { 63 throw new JspException ("<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 e) { 72 throw new JspException ( "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 88 89 public void setVar(String var) { 90 this.var = var; 91 } 92 93 public Messenger getConnection() throws JspException , JMSException { 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 getMessage() throws JspException , JMSException { 105 if ( message == null ) { 106 message = createMessage(); 107 } 108 return message; 109 } 110 111 112 114 public void setCorrelationID(String correlationID) throws JspException , JMSException { 115 getMessage().setJMSCorrelationID(correlationID); 116 } 117 118 public void setReplyTo(Destination destination) throws JspException , JMSException { 119 getMessage().setJMSReplyTo(destination); 120 } 121 122 public void setType(String type) throws JspException , JMSException { 123 getMessage().setJMSType(type); 124 } 125 126 protected Messenger findConnection() throws JspException , JMSException { 129 ConnectionContext messengerTag = (ConnectionContext) findAncestorWithClass( this, ConnectionContext.class ); 130 if ( messengerTag == null ) { 131 throw new JspException ("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 createMessage() throws JspException , JMSException { 137 return getConnection().createMessage(); 138 } 139 } 140 | Popular Tags |