package org.mockejb.jms;
import java.util.*;
import javax.jms.*;
abstract class MockDestination implements Destination {
private String name;
private List messages = new ArrayList();
private List consumers = new ArrayList();
MockDestination(String name) {
this.name = name;
}
public String getName() {
return name;
}
void addMessage(Message msg) throws JMSException {
MessageImpl sentMsg = MessageUtility.copyMessage(msg, true);
int i = 0;
while (i < messages.size()) {
if (msg.getJMSPriority() > ((Message)messages.get(i)).getJMSPriority()) {
break;
}
i++;
}
messages.add(i, sentMsg);
Iterator it = consumers.iterator();
while (it.hasNext()) {
MockConsumer cons = (MockConsumer)it.next();
cons.consume(sentMsg);
}
}
public void clear() {
messages.clear();
}
public String toString() {
return this.getClass().getName() + " with name: " + getName();
}
public int size() {
return messages.size();
}
public Message getMessageAt(int index) {
return (Message)messages.get(index);
}
public List getMessages() {
return messages;
}
void registerConsumer(MockConsumer consumer) throws JMSException {
cleanConsumers();
consumer.consume(messages);
consumers.add(consumer);
}
void removeConsumer(MockConsumer consumer) {
consumers.remove(consumer);
}
private void cleanConsumers() {
ListIterator it = consumers.listIterator();
while (it.hasNext()) {
MockConsumer cons = (MockConsumer)it.next();
if (cons.isClosed()) {
it.remove();
}
}
}
}