001    package com.mockrunner.mock.jms;
002    
003    import javax.jms.BytesMessage;
004    import javax.jms.DeliveryMode;
005    import javax.jms.Destination;
006    import javax.jms.InvalidDestinationException;
007    import javax.jms.JMSException;
008    import javax.jms.Message;
009    import javax.jms.MessageProducer;
010    import javax.jms.StreamMessage;
011    
012    /**
013     * Mock implementation of JMS <code>MessageProducer</code>.
014     */
015    public class MockMessageProducer implements MessageProducer
016    {
017        private MockConnection connection;
018        private MockDestination destination;
019        private MockSession session;
020        private boolean closed;
021        private boolean disableMessageId;
022        private boolean disableTimestamp;
023        private int deliveryMode;
024        private int priority;
025        private long timeToLive;
026        
027        public MockMessageProducer(MockConnection connection, MockSession session, MockDestination destination)
028        {
029            this.connection = connection;
030            this.destination = destination;
031            this.session = session;
032            closed = false;
033            disableMessageId = false;
034            disableTimestamp = false;
035            deliveryMode = DeliveryMode.PERSISTENT;
036            priority = 4;
037            timeToLive = 0;
038        }
039        
040        /**
041         * Returns if this producer was closed.
042         * @return <code>true</code> if this sender is closed
043         */
044        public boolean isClosed()
045        {
046            return closed;
047        }
048        
049        public void send(Message message) throws JMSException
050        {
051            send(destination, message, deliveryMode, priority, timeToLive);
052        }
053        
054        public void send(Message message, int deliveryMode, int priority, long timeToLive) throws JMSException
055        {
056            send(destination, message, deliveryMode, priority, timeToLive);
057        }
058        
059        public void send(Destination destination, Message message) throws JMSException
060        {
061            send(destination, message, deliveryMode, priority, timeToLive);
062        }
063        
064        public void send(Destination destination, Message message, int deliveryMode, int priority, long timeToLive) throws JMSException
065        {
066            connection.throwJMSException();
067            if(isClosed())
068            {
069                throw new JMSException("Producer is closed");
070            }
071            if(null == destination)
072            {
073                throw new InvalidDestinationException("destination must not be null");
074            }
075            if((message instanceof MockMessage) && connection.getConfigurationManager().getDoCloneOnSend())
076            {
077                message = (MockMessage)((MockMessage)message).clone();
078            }
079            if(destination instanceof MockQueue)
080            {
081                setJMSMessageHeaders(message, destination, deliveryMode, priority, timeToLive);
082                session.addSessionToQueue((MockQueue)destination);
083                ((MockQueue)destination).addMessage(message);
084            }
085            else if(destination instanceof MockTopic)
086            {
087                setJMSMessageHeaders(message, destination, deliveryMode, priority, timeToLive);
088                session.addSessionToTopic((MockTopic)destination);
089                ((MockTopic)destination).addMessage(message);
090            }
091            else
092            {
093                throw new InvalidDestinationException("destination must be an instance of MockQueue or MockTopic");
094            }
095        }
096        
097        public Destination getDestination() throws JMSException
098        {
099            connection.throwJMSException();
100            return destination;
101        }
102    
103        public void close() throws JMSException
104        {
105            connection.throwJMSException();
106            closed = true;
107        }
108        
109        public void setDisableMessageID(boolean disableMessageId) throws JMSException
110        {
111            connection.throwJMSException();
112            this.disableMessageId = disableMessageId;
113        }
114    
115        public boolean getDisableMessageID() throws JMSException
116        {
117            connection.throwJMSException();
118            return disableMessageId;
119        }
120    
121        public void setDisableMessageTimestamp(boolean disableTimestamp) throws JMSException
122        {
123            connection.throwJMSException();
124            this.disableTimestamp = disableTimestamp;
125        }
126    
127        public boolean getDisableMessageTimestamp() throws JMSException
128        {
129            connection.throwJMSException();
130            return disableTimestamp;
131        }
132    
133        public void setDeliveryMode(int deliveryMode) throws JMSException
134        {
135            connection.throwJMSException();
136            this.deliveryMode = deliveryMode;
137        }
138    
139        public int getDeliveryMode() throws JMSException
140        {
141            connection.throwJMSException();
142            return deliveryMode;
143        }
144    
145        public void setPriority(int priority) throws JMSException
146        {
147            connection.throwJMSException();
148            this.priority = priority;
149        }
150    
151        public int getPriority() throws JMSException
152        {
153            connection.throwJMSException();
154            return priority;
155        }
156    
157        public void setTimeToLive(long timeToLive) throws JMSException
158        {
159            connection.throwJMSException();
160            this.timeToLive = timeToLive;
161        }
162    
163        public long getTimeToLive() throws JMSException
164        {
165            connection.throwJMSException();
166            return timeToLive;
167        }
168    
169        private void setJMSMessageHeaders(Message message, Destination destination, int deliveryMode, int priority, long timeToLive) throws JMSException
170        {
171            message.setJMSDeliveryMode(deliveryMode);
172            message.setJMSPriority(priority);
173            message.setJMSDestination(destination);
174            long currentTime = System.currentTimeMillis();
175            if(!disableTimestamp)
176            {
177                message.setJMSTimestamp(currentTime);
178            }
179            if(0 == timeToLive)
180            {
181                message.setJMSExpiration(0);
182            }
183            else
184            {
185                message.setJMSExpiration(currentTime + timeToLive);
186            }
187            if(!disableMessageId)
188            {
189                message.setJMSMessageID("ID:" + String.valueOf(Math.random()));
190            }
191            if(message instanceof MockMessage)
192            {
193                ((MockMessage)message).setReadOnly(true);
194                ((MockMessage)message).setReadOnlyProperties(true);
195            }
196            if(message instanceof BytesMessage)
197            {
198                ((BytesMessage)message).reset();
199            }
200            if(message instanceof StreamMessage)
201            {
202                ((StreamMessage)message).reset();
203            }
204        }
205    }