001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.workflow.util;
006:
007: import com.opensymphony.module.propertyset.PropertySet;
008:
009: import com.opensymphony.workflow.FunctionProvider;
010: import com.opensymphony.workflow.spi.WorkflowEntry;
011:
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014:
015: import java.util.*;
016:
017: import javax.jms.*;
018:
019: import javax.naming.InitialContext;
020:
021: /**
022: * Sends out a JMS TextMessage to a specified Queue or Topic. The following arguments
023: * are expected:
024: *
025: * <ul>
026: * <li>queue-factory-location - the location to be passed to InitialContext.lookup</li>
027: * <li>queue-location - the location to be passed to InitialContext.lookup</li>
028: * <li>topic-factory-location - the location to be passed to InitialContext.lookup</li>
029: * <li>topic-location - the location to be passed to InitialContext.lookup</li>
030: * <li>text - the text message to be included in this JMS message</li>
031: * </ul>
032: *
033: * Also, please note that the entire set of properties will be passed through to the
034: * constructor for InitialContext, meaning that if you need to use an
035: * InintialContextFactory other than the default one, you are free to include arguments
036: * that will do so.
037: *
038: * Also note that all arguments are also passed to the TextMessage using
039: * setObjectProperty(), except for "text" which is set using setText(). An extra
040: * property is always added to denote the workflow entry for this message. This is
041: * stored as a long property, with the name 'workflowEntry'.
042: *
043: * @author Hani Suleiman
044: */
045: public class JMSMessage implements FunctionProvider {
046: //~ Static fields/initializers /////////////////////////////////////////////
047:
048: private static final Log log = LogFactory.getLog(JMSMessage.class);
049:
050: //~ Methods ////////////////////////////////////////////////////////////////
051:
052: public void execute(Map transientVars, Map args, PropertySet ps) {
053: WorkflowEntry entry = (WorkflowEntry) transientVars
054: .get("entry");
055:
056: try {
057: Hashtable env = new Hashtable(args);
058: InitialContext initialContext = new InitialContext(env);
059:
060: if (args.containsKey("queue-factory-location")) {
061: QueueConnectionFactory queueFactory = (QueueConnectionFactory) initialContext
062: .lookup((String) args
063: .get("queue-factory-location"));
064: QueueConnection conn = queueFactory
065: .createQueueConnection();
066: conn.start();
067:
068: QueueSession queueSession = conn.createQueueSession(
069: false, Session.AUTO_ACKNOWLEDGE);
070: javax.jms.Queue queue = (javax.jms.Queue) initialContext
071: .lookup((String) args.get("queue-location"));
072: QueueSender sender = queueSession.createSender(queue);
073: TextMessage message = queueSession.createTextMessage();
074: populateMessage(message, entry, args);
075: sender.send(message);
076: } else if (args.containsKey("topic-factory-location")) {
077: TopicConnectionFactory topicFactory = (TopicConnectionFactory) initialContext
078: .lookup((String) args
079: .get("topic-factory-location"));
080: TopicConnection conn = topicFactory
081: .createTopicConnection();
082: conn.start();
083:
084: TopicSession topicSession = conn.createTopicSession(
085: false, Session.AUTO_ACKNOWLEDGE);
086: Topic topic = (Topic) initialContext
087: .lookup((String) args.get("topic-location"));
088: TopicPublisher publisher = topicSession
089: .createPublisher(topic);
090: TextMessage message = topicSession.createTextMessage();
091: populateMessage(message, entry, args);
092: publisher.publish(message);
093: }
094: } catch (Exception ex) {
095: log.error("Error sending JMS message", ex);
096: }
097: }
098:
099: private void populateMessage(TextMessage message,
100: WorkflowEntry entry, Map properties) throws JMSException {
101: message.setText((String) properties.get("text"));
102: message.setLongProperty("workflowEntry", entry.getId());
103:
104: for (Iterator iterator = properties.entrySet().iterator(); iterator
105: .hasNext();) {
106: Map.Entry mapEntry = (Map.Entry) iterator.next();
107:
108: // don't include "text", it was already done
109: if (!"text".equals(mapEntry.getKey())) {
110: message.setObjectProperty((String) mapEntry.getKey(),
111: mapEntry.getValue());
112: }
113: }
114: }
115: }
|