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