package org.mockejb.jms;
import java.io.Serializable;
import org.mockejb.MethodNotImplementedException;
import javax.jms.*;
import java.util.*;
abstract class MockSession implements Session {
private boolean closed = false;
private boolean transacted;
private int acknowledgeMode;
private MockConnection connection;
private final List consumers = new ArrayList();
private final List producers = new ArrayList();
public MockSession(boolean transacted, int acknowledgeMode, MockConnection connection) {
this.transacted = transacted;
this.acknowledgeMode = acknowledgeMode;
this.connection = connection;
}
public boolean getTransacted() throws JMSException {
checkClosed();
return transacted;
}
public int getAcknowledgeMode() throws JMSException {
checkClosed();
return acknowledgeMode;
}
public void close() throws JMSException {
closed = true;
Iterator it = consumers.iterator();
while (it.hasNext()) {
((MockConsumer)it.next()).close();
}
it = producers.iterator();
while (it.hasNext()) {
((MockProducer)it.next()).close();
}
}
protected boolean isClosed() {
return closed;
}
protected void checkClosed() throws javax.jms.IllegalStateException {
if (isClosed()) {
throw new javax.jms.IllegalStateException(
"Can not invoke methods on closed session!");
}
}
public BytesMessage createBytesMessage() throws JMSException {
checkClosed();
return new BytesMessageImpl();
}
public MapMessage createMapMessage() throws JMSException {
checkClosed();
return new MapMessageImpl();
}
public Message createMessage() throws JMSException {
checkClosed();
return new MessageImpl();
}
public ObjectMessage createObjectMessage() throws JMSException {
checkClosed();
return new ObjectMessageImpl();
}
public ObjectMessage createObjectMessage(Serializable object)
throws JMSException {
checkClosed();
return new ObjectMessageImpl(object);
}
public StreamMessage createStreamMessage() throws JMSException {
checkClosed();
return new StreamMessageImpl();
}
public TextMessage createTextMessage() throws JMSException {
checkClosed();
return new TextMessageImpl();
}
public TextMessage createTextMessage(String text) throws JMSException {
checkClosed();
return new TextMessageImpl(text);
}
public void commit() throws JMSException {
throw new MethodNotImplementedException("commit", "MockSession");
}
public void rollback() throws JMSException {
throw new MethodNotImplementedException("rollback", "MockSession");
}
public void recover() throws JMSException {
throw new MethodNotImplementedException("recover", "MockSession");
}
public MessageListener getMessageListener() throws JMSException {
throw new MethodNotImplementedException(
"getMessageListener",
"MockSession");
}
public void setMessageListener(MessageListener listener)
throws JMSException {
throw new MethodNotImplementedException(
"setMessageListener",
"MockSession");
}
public void run() {
throw new MethodNotImplementedException("run", "MockSession");
}
public MessageConsumer createConsumer(Destination destination)
throws JMSException {
checkClosed();
if (destination instanceof MockDestination) {
MockDestination dest = (MockDestination)destination;
MockConsumer consumer = createMockConsumer(dest);
dest.registerConsumer(consumer);
consumers.add(consumer);
return consumer;
}
throw new InvalidDestinationException("Unsupported destination!");
}
public MessageConsumer createConsumer(
Destination destination,
java.lang.String messageSelector)
throws JMSException {
throw new MethodNotImplementedException(
"createConsumer",
"MockSession");
}
public MessageConsumer createConsumer(
Destination destination,
java.lang.String messageSelector,
boolean NoLocal)
throws JMSException {
throw new MethodNotImplementedException(
"createConsumer",
"MockSession");
}
public MessageProducer createProducer(Destination destination)
throws JMSException {
checkClosed();
if (destination instanceof MockDestination) {
MockProducer result = createMockProducer((MockDestination) destination);
producers.add(result);
return result;
}
throw new InvalidDestinationException("Unsupported destination!");
}
abstract MockConsumer createMockConsumer(MockDestination destination) throws JMSException;
abstract MockProducer createMockProducer(MockDestination destination) throws JMSException;
MockConnection getConnection() {
return connection;
}
void consumeMessages() throws JMSException {
Iterator it = consumers.iterator();
while (it.hasNext()) {
((MockConsumer)it.next()).consume();
}
}
}