package org.mockejb.jms;
import javax.jms.*;
import java.util.*;
import org.mockejb.MethodNotImplementedException;
abstract class MockConnection implements Connection {
private String clientId;
private boolean closed = false;
private boolean started = false;
private final List sessions = new ArrayList();
MockConnection(String id) {
clientId = id;
}
public Session createSession(boolean transacted, int acknowledgeMode) throws JMSException {
checkClosed();
MockSession sess = createMockSession(transacted, acknowledgeMode);
sessions.add(sess);
return sess;
}
public String getClientID() throws JMSException {
checkClosed();
return clientId;
}
public void setClientID(String arg0) throws JMSException {
throw new javax.jms.IllegalStateException("Client ID can not be set!");
}
public ConnectionMetaData getMetaData() throws JMSException {
checkClosed();
return new ConnectionMetaDataImpl();
}
public ExceptionListener getExceptionListener() throws JMSException {
throw new MethodNotImplementedException(
"getExceptionListener",
"QueueConnectionImpl");
}
public void setExceptionListener(ExceptionListener listener)
throws JMSException {
throw new MethodNotImplementedException(
"setExceptionListener",
"QueueConnectionImpl");
}
public void close() throws JMSException {
if (closed) {
return;
}
stop();
ListIterator it = sessions.listIterator();
while (it.hasNext()) {
Session s = (Session)it.next();
s.close();
it.remove();
}
closed = true;
}
public void start() throws JMSException {
checkClosed();
started = true;
Iterator it = sessions.iterator();
while (it.hasNext()) {
MockSession sess = (MockSession)it.next();
sess.consumeMessages();
}
}
public void stop() throws JMSException {
checkClosed();
started = false;
}
public ConnectionConsumer createConnectionConsumer(
Destination destination,
java.lang.String messageSelector,
ServerSessionPool sessionPool,
int maxMessages)
throws JMSException {
throw new MethodNotImplementedException("createConnectionConsumer", "MockConnection");
}
public ConnectionConsumer createDurableConnectionConsumer(
Topic topic,
java.lang.String subscriptionName,
java.lang.String messageSelector,
ServerSessionPool sessionPool,
int maxMessages)
throws JMSException {
throw new MethodNotImplementedException("createDurableConnectionConsumer", "MockConnection");
}
void checkClosed() throws javax.jms.IllegalStateException {
if (closed) {
throw new javax.jms.IllegalStateException("Unable to perform operation on closed connection!");
}
}
abstract MockSession createMockSession(boolean transacted, int acknowledgeMode);
boolean isStarted() {
return started;
}
}