001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.jms.client;
023:
024: import javax.jms.Destination;
025: import javax.jms.JMSException;
026: import javax.jms.Message;
027: import javax.jms.MessageProducer;
028: import javax.jms.Queue;
029: import javax.jms.QueueSender;
030: import javax.jms.Topic;
031: import javax.jms.TopicPublisher;
032:
033: import org.jboss.jms.JMSValidator;
034: import org.jboss.jms.message.JBossMessage;
035:
036: /**
037: * A producer
038: *
039: * @author <a href="mailto:adrian@jboss.org>Adrian Brock</a>
040: * @version $Revision: 57195 $
041: */
042: public class JBossProducer implements MessageProducer, QueueSender,
043: TopicPublisher {
044: // Constants -----------------------------------------------------
045:
046: // Attributes ----------------------------------------------------
047:
048: /** The producer delegate */
049: private ProducerDelegate delegate;
050:
051: /** The default destination for this producer */
052: private Destination defaultDestination;
053:
054: /** The default delivery mode */
055: private int defaultDeliveryMode = Message.DEFAULT_DELIVERY_MODE;
056:
057: /** The default priorty */
058: private int defaultPriority = Message.DEFAULT_PRIORITY;
059:
060: /** The default time to live */
061: private long defaultTimeToLive = Message.DEFAULT_TIME_TO_LIVE;
062:
063: /** The disable message id flag */
064: private boolean disableMessageID = false;
065:
066: /** The disable message timestamp flag */
067: private boolean disableTimestamp = false;
068:
069: // Static --------------------------------------------------------
070:
071: // Constructors --------------------------------------------------
072:
073: /**
074: * Create a new JBossProducer
075: *
076: * @param delegate the delegate
077: * @param destination the destination
078: * @throws JMSException for any error
079: */
080: public JBossProducer(ProducerDelegate delegate,
081: Destination destination) throws JMSException {
082: this .delegate = delegate;
083: this .defaultDestination = destination;
084: }
085:
086: // Public --------------------------------------------------------
087:
088: // MessageProducer implementation --------------------------------
089:
090: public void close() throws JMSException {
091: delegate.closing();
092: delegate.close();
093: }
094:
095: public int getDeliveryMode() throws JMSException {
096: return defaultDeliveryMode;
097: }
098:
099: public Destination getDestination() throws JMSException {
100: return defaultDestination;
101: }
102:
103: public boolean getDisableMessageID() throws JMSException {
104: return disableMessageID;
105: }
106:
107: public boolean getDisableMessageTimestamp() throws JMSException {
108: return disableTimestamp;
109: }
110:
111: public int getPriority() throws JMSException {
112: return defaultPriority;
113: }
114:
115: public long getTimeToLive() throws JMSException {
116: return defaultTimeToLive;
117: }
118:
119: public void send(Destination destination, Message message,
120: int deliveryMode, int priority, long timeToLive)
121: throws JMSException {
122: if (destination == null)
123: throw new JMSException("Null destination");
124: if (message == null)
125: throw new JMSException("Null message");
126: JMSValidator.validateDeliveryMode(deliveryMode);
127: JMSValidator.validatePriority(priority);
128: JMSValidator.validateTimeToLive(timeToLive);
129:
130: JBossMessage msg;
131: if ((message instanceof JBossMessage))
132: msg = (JBossMessage) message;
133: else
134: msg = delegate.encapsulateMessage(message);
135:
136: if (disableMessageID == false)
137: msg.generateMessageID();
138:
139: if (disableTimestamp == false)
140: msg.generateTimestamp();
141:
142: msg.setJMSDestination(destination);
143: msg.setJMSDeliveryMode(deliveryMode);
144: msg.setJMSPriority(priority);
145: if (disableTimestamp == false && timeToLive != 0)
146: msg.setJMSExpiration(msg.getJMSTimestamp() + timeToLive);
147:
148: delegate.send(msg);
149: }
150:
151: public void send(Destination destination, Message message)
152: throws JMSException {
153: send(destination, message, defaultDeliveryMode,
154: defaultPriority, defaultTimeToLive);
155: }
156:
157: public void send(Message message, int deliveryMode, int priority,
158: long timeToLive) throws JMSException {
159: send(defaultDestination, message, deliveryMode, priority,
160: timeToLive);
161: }
162:
163: public void send(Message message) throws JMSException {
164: send(defaultDestination, message, defaultDeliveryMode,
165: defaultPriority, defaultTimeToLive);
166: }
167:
168: public void setDeliveryMode(int deliveryMode) throws JMSException {
169: JMSValidator.validateDeliveryMode(deliveryMode);
170: this .defaultDeliveryMode = deliveryMode;
171: }
172:
173: public void setDisableMessageID(boolean value) throws JMSException {
174: this .disableMessageID = value;
175: }
176:
177: public void setDisableMessageTimestamp(boolean value)
178: throws JMSException {
179: this .disableTimestamp = value;
180: }
181:
182: public void setPriority(int defaultPriority) throws JMSException {
183: JMSValidator.validatePriority(defaultPriority);
184: this .defaultPriority = defaultPriority;
185: }
186:
187: public void setTimeToLive(long timeToLive) throws JMSException {
188: JMSValidator.validateTimeToLive(timeToLive);
189: this .defaultTimeToLive = timeToLive;
190: }
191:
192: // QueueReceiver implementation ----------------------------------
193:
194: public Queue getQueue() throws JMSException {
195: return (Queue) getDestination();
196: }
197:
198: public void send(Queue queue, Message message, int deliveryMode,
199: int priority, long timeToLive) throws JMSException {
200: send(queue, message, deliveryMode, priority, timeToLive);
201: }
202:
203: public void send(Queue queue, Message message) throws JMSException {
204: send(queue, message);
205: }
206:
207: // TopicPublisher implementation ---------------------------------
208:
209: public Topic getTopic() throws JMSException {
210: return (Topic) getDestination();
211: }
212:
213: public void publish(Message message, int deliveryMode,
214: int priority, long timeToLive) throws JMSException {
215: send(defaultDestination, message, deliveryMode, priority,
216: timeToLive);
217: }
218:
219: public void publish(Message message) throws JMSException {
220: send(message);
221: }
222:
223: public void publish(Topic topic, Message message, int deliveryMode,
224: int priority, long timeToLive) throws JMSException {
225: send(topic, message, deliveryMode, priority, timeToLive);
226: }
227:
228: public void publish(Topic topic, Message message)
229: throws JMSException {
230: send(topic, message);
231: }
232:
233: // Protected ------------------------------------------------------
234:
235: // Package Private ------------------------------------------------
236:
237: // Private --------------------------------------------------------
238:
239: // Inner Classes --------------------------------------------------
240:
241: }
|