001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.jms;
017:
018: import java.util.Date;
019: import java.util.UUID;
020:
021: import javax.jms.Destination;
022: import javax.jms.IllegalStateException;
023: import javax.jms.JMSException;
024: import javax.jms.Message;
025: import javax.jms.MessageProducer;
026: import javax.servlet.ServletContext;
027:
028: import org.directwebremoting.Hub;
029: import org.directwebremoting.HubFactory;
030:
031: /**
032: * An implementation of {@link MessageProducer} that sends messages out over
033: * Reverse Ajax to a client side hub
034: * @author Joe Walker [joe at getahead dot ltd dot uk]
035: */
036: public class DwrMessageProducer implements MessageProducer {
037: /**
038: * @param destination
039: */
040: public DwrMessageProducer(Destination destination,
041: DwrConnection connection) {
042: this .destination = destination;
043: ServletContext servletContext = connection.getServletContext();
044:
045: if (servletContext != null) {
046: hub = HubFactory.get(servletContext);
047: } else {
048: hub = HubFactory.get();
049: }
050: }
051:
052: /* (non-Javadoc)
053: * @see javax.jms.MessageProducer#close()
054: */
055: public void close() throws JMSException {
056: state = State.CLOSED;
057: }
058:
059: /* (non-Javadoc)
060: * @see javax.jms.MessageProducer#getDeliveryMode()
061: */
062: public int getDeliveryMode() throws JMSException {
063: return deliveryMode;
064: }
065:
066: /* (non-Javadoc)
067: * @see javax.jms.MessageProducer#getDestination()
068: */
069: public Destination getDestination() throws JMSException {
070: return destination;
071: }
072:
073: /* (non-Javadoc)
074: * @see javax.jms.MessageProducer#getDisableMessageID()
075: */
076: public boolean getDisableMessageID() throws JMSException {
077: return disableMessageID;
078: }
079:
080: /* (non-Javadoc)
081: * @see javax.jms.MessageProducer#getDisableMessageTimestamp()
082: */
083: public boolean getDisableMessageTimestamp() throws JMSException {
084: return disableMessageTimestamp;
085: }
086:
087: /* (non-Javadoc)
088: * @see javax.jms.MessageProducer#getPriority()
089: */
090: public int getPriority() throws JMSException {
091: return priority;
092: }
093:
094: /* (non-Javadoc)
095: * @see javax.jms.MessageProducer#getTimeToLive()
096: */
097: public long getTimeToLive() throws JMSException {
098: return timeToLive;
099: }
100:
101: /* (non-Javadoc)
102: * @see javax.jms.MessageProducer#send(javax.jms.Message)
103: */
104: public void send(Message message) throws JMSException {
105: send(destination, message, deliveryMode, priority, timeToLive);
106: }
107:
108: /* (non-Javadoc)
109: * @see javax.jms.MessageProducer#send(javax.jms.Destination, javax.jms.Message)
110: */
111: public void send(Destination realDestination, Message message)
112: throws JMSException {
113: send(realDestination, message, deliveryMode, priority,
114: timeToLive);
115: }
116:
117: /* (non-Javadoc)
118: * @see javax.jms.MessageProducer#send(javax.jms.Message, int, int, long)
119: */
120: public void send(Message message, int realDeliveryMode,
121: int realPriority, long realTimeToLive) throws JMSException {
122: send(destination, message, realDeliveryMode, realPriority,
123: realTimeToLive);
124: }
125:
126: /* (non-Javadoc)
127: * @see javax.jms.MessageProducer#send(javax.jms.Destination, javax.jms.Message, int, int, long)
128: */
129: public void send(Destination realDestination, Message message,
130: int realDeliveryMode, int realPriority, long realTimeToLive)
131: throws JMSException {
132: if (state == State.CLOSED) {
133: throw new IllegalStateException(
134: "DwrMessageProducer has been closed");
135: }
136:
137: if (!disableMessageID) {
138: message.setJMSMessageID(UUID.randomUUID().toString());
139: }
140:
141: if (!disableMessageTimestamp) {
142: message.setJMSTimestamp(new Date().getTime());
143: }
144:
145: if (realDestination instanceof DwrTopic) {
146: DwrTopic dwrtopic = (DwrTopic) realDestination;
147: String topicName = dwrtopic.getTopicName();
148:
149: hub.publish(topicName, message);
150: } else {
151: throw new IllegalStateException(
152: "Unsuported Destination type ("
153: + realDestination.getClass()
154: .getCanonicalName()
155: + "). Only Topics are currently supported.");
156: }
157: }
158:
159: /* (non-Javadoc)
160: * @see javax.jms.MessageProducer#setDeliveryMode(int)
161: */
162: public void setDeliveryMode(int deliveryMode) throws JMSException {
163: this .deliveryMode = deliveryMode;
164: throw Unsupported.noManualAcknowledgment();
165: }
166:
167: /* (non-Javadoc)
168: * @see javax.jms.MessageProducer#setDisableMessageID(boolean)
169: */
170: public void setDisableMessageID(boolean disableMessageID)
171: throws JMSException {
172: this .disableMessageID = disableMessageID;
173: }
174:
175: /* (non-Javadoc)
176: * @see javax.jms.MessageProducer#setDisableMessageTimestamp(boolean)
177: */
178: public void setDisableMessageTimestamp(
179: boolean disableMessageTimestamp) throws JMSException {
180: this .disableMessageTimestamp = disableMessageTimestamp;
181: }
182:
183: /* (non-Javadoc)
184: * @see javax.jms.MessageProducer#setPriority(int)
185: */
186: public void setPriority(int priority) throws JMSException {
187: this .priority = priority;
188: throw Unsupported.noMessagePriority();
189: }
190:
191: /* (non-Javadoc)
192: * @see javax.jms.MessageProducer#setTimeToLive(long)
193: */
194: public void setTimeToLive(long timeToLive) throws JMSException {
195: this .timeToLive = timeToLive;
196: throw Unsupported.noMessageExpiry();
197: }
198:
199: /**
200: * The DWR hub that does the real work
201: */
202: private Hub hub;
203:
204: /**
205: * The default queue or topic
206: */
207: private Destination destination;
208:
209: /**
210: * For queues: are messages persistent
211: */
212: private int deliveryMode;
213:
214: /**
215: * Have we been closed?
216: */
217: private State state;
218:
219: /**
220: * Do we skip assigning message IDs?
221: */
222: private boolean disableMessageID;
223:
224: /**
225: * Do we skip assigning timestamps
226: */
227: private boolean disableMessageTimestamp;
228:
229: /**
230: * What is the default priority for messages we produce
231: */
232: private int priority;
233:
234: /**
235: * How long before the system can delete messages that we produce
236: */
237: private long timeToLive;
238: }
|