package org.mockejb.jms;
import javax.jms.*;
class MockProducer implements MessageProducer {
private MockDestination destination;
private int deliveryMode = DeliveryMode.PERSISTENT;
private int priority = 4;
private long timeToLive = 0;
private static int messageId = 1;
MockProducer(MockDestination destination) {
this.destination = destination;
}
public void send(Message msg) throws JMSException {
send(msg, getDeliveryMode(), getPriority(), getTimeToLive());
}
public void send(
Message msg,
int deliveryMode,
int priority,
long timeToLive)
throws JMSException {
checkDestination(false);
MockProducer.sendMessage(
(MockDestination) getDestination(),
msg,
deliveryMode,
priority,
timeToLive);
}
protected static synchronized int getMessageId() {
return messageId++;
}
protected static void sendMessage(
MockDestination destination,
Message msg,
int deliveryMode,
int priority,
long timeToLive)
throws JMSException {
msg.setJMSMessageID("ID:MockMessage " + MockProducer.getMessageId());
msg.setJMSDestination(destination);
msg.setJMSDeliveryMode(deliveryMode);
msg.setJMSPriority(priority);
msg.setJMSTimestamp(System.currentTimeMillis());
if (timeToLive == 0) {
msg.setJMSExpiration(0);
} else {
msg.setJMSExpiration(msg.getJMSTimestamp() + timeToLive);
}
destination.addMessage(msg);
}
public void send(Destination destination, Message msg)
throws JMSException {
send(
destination,
msg,
getDeliveryMode(),
getPriority(),
getTimeToLive());
}
public void send(
Destination destination,
Message msg,
int deliveryMode,
int priority,
long timeToLive)
throws JMSException {
checkDestination(true);
if (destination instanceof MockDestination) {
MockProducer.sendMessage(
(MockDestination) destination,
msg,
deliveryMode,
priority,
timeToLive);
}
throw new InvalidDestinationException("Invalid destination specified!");
}
public Destination getDestination() throws JMSException {
return destination;
}
public void close() throws JMSException {
}
public void setDisableMessageID(boolean arg0) throws JMSException {
}
public boolean getDisableMessageID() throws JMSException {
return false;
}
public void setDisableMessageTimestamp(boolean arg0) throws JMSException {
}
public boolean getDisableMessageTimestamp() throws JMSException {
return false;
}
public void setDeliveryMode(int deliveryMode) throws JMSException {
this.deliveryMode = deliveryMode;
}
public int getDeliveryMode() throws JMSException {
return deliveryMode;
}
public void setPriority(int priority) throws JMSException {
this.priority = priority;
}
public int getPriority() throws JMSException {
return priority;
}
public void setTimeToLive(long timeToLive) throws JMSException {
this.timeToLive = timeToLive;
}
public long getTimeToLive() throws JMSException {
return timeToLive;
}
protected void checkDestination(boolean unidentifiedDestinationCheck) {
if (unidentifiedDestinationCheck) {
if (destination != null) {
throw new UnsupportedOperationException();
}
} else {
if (destination == null) {
throw new UnsupportedOperationException();
}
}
}
}