001: package org.mockejb.jms;
002:
003: import javax.jms.*;
004: import java.util.*;
005: import org.mockejb.MethodNotImplementedException;
006:
007: /**
008: * @author Dimitar Gospodinov
009: */
010: abstract class MockConnection implements Connection {
011:
012: private String clientId;
013: private boolean closed = false;
014: private boolean started = false;
015: private final List sessions = new ArrayList();
016:
017: MockConnection(String id) {
018: clientId = id;
019: }
020:
021: public Session createSession(boolean transacted, int acknowledgeMode)
022: throws JMSException {
023: checkClosed();
024: MockSession sess = createMockSession(transacted,
025: acknowledgeMode);
026: sessions.add(sess);
027: return sess;
028: }
029:
030: /**
031: * Returns client Id for this connection.
032: * @see javax.jms.Connection#getClientID()
033: */
034: public String getClientID() throws JMSException {
035: checkClosed();
036: return clientId;
037: }
038:
039: /**
040: * @see javax.jms.Connection#setClientID(java.lang.String)
041: */
042: public void setClientID(String arg0) throws JMSException {
043: throw new javax.jms.IllegalStateException(
044: "Client ID can not be set!");
045: }
046:
047: /**
048: * Returns metadata for MockEJB JMS connection.
049: * @see javax.jms.Connection#getMetaData()
050: */
051: public ConnectionMetaData getMetaData() throws JMSException {
052: checkClosed();
053: return new ConnectionMetaDataImpl();
054: }
055:
056: /**
057: * Not implemented.
058: * @see javax.jms.Connection#getExceptionListener()
059: */
060: public ExceptionListener getExceptionListener() throws JMSException {
061: throw new MethodNotImplementedException("getExceptionListener",
062: "QueueConnectionImpl");
063: }
064:
065: /**
066: * Not implemented.
067: * @see javax.jms.Connection#setExceptionListener(javax.jms.ExceptionListener)
068: */
069: public void setExceptionListener(ExceptionListener listener)
070: throws JMSException {
071:
072: throw new MethodNotImplementedException("setExceptionListener",
073: "QueueConnectionImpl");
074: }
075:
076: /**
077: * Does nothing.
078: * @see javax.jms.Connection#close()
079: */
080: public void close() throws JMSException {
081: if (closed) {
082: return;
083: }
084: stop();
085: ListIterator it = sessions.listIterator();
086: while (it.hasNext()) {
087: Session s = (Session) it.next();
088: s.close();
089: it.remove();
090: }
091: closed = true;
092: }
093:
094: /**
095: * Not implemented.
096: * @see javax.jms.Connection#start()
097: */
098: public void start() throws JMSException {
099: checkClosed();
100: started = true;
101: // "Asynchronously" deliver any messages received while the connection was stopped.
102: // This will simply invoke MessageListeners if any.
103: Iterator it = sessions.iterator();
104: while (it.hasNext()) {
105: MockSession sess = (MockSession) it.next();
106: sess.consumeMessages();
107: }
108: }
109:
110: /**
111: * Not implemented.
112: * @see javax.jms.Connection#stop()
113: */
114: public void stop() throws JMSException {
115: checkClosed();
116: started = false;
117: }
118:
119: public ConnectionConsumer createConnectionConsumer(
120: Destination destination, java.lang.String messageSelector,
121: ServerSessionPool sessionPool, int maxMessages)
122: throws JMSException {
123:
124: throw new MethodNotImplementedException(
125: "createConnectionConsumer", "MockConnection");
126:
127: }
128:
129: public ConnectionConsumer createDurableConnectionConsumer(
130: Topic topic, java.lang.String subscriptionName,
131: java.lang.String messageSelector,
132: ServerSessionPool sessionPool, int maxMessages)
133: throws JMSException {
134:
135: throw new MethodNotImplementedException(
136: "createDurableConnectionConsumer", "MockConnection");
137: }
138:
139: // Non-standard methods
140:
141: void checkClosed() throws javax.jms.IllegalStateException {
142: if (closed) {
143: throw new javax.jms.IllegalStateException(
144: "Unable to perform operation on closed connection!");
145: }
146: }
147:
148: abstract MockSession createMockSession(boolean transacted,
149: int acknowledgeMode);
150:
151: boolean isStarted() {
152: return started;
153: }
154:
155: }
|