001: package org.mockejb.jms;
002:
003: import java.io.Serializable;
004: import org.mockejb.MethodNotImplementedException;
005: import javax.jms.*;
006: import java.util.*;
007:
008: /**
009: * Represents session with MockEjb JMS provider.
010: * Currently contains common methods for <code>QueueSession</code> and
011: * <code>TopicSession</code>
012: * @author Dimitar Gospodinov
013: */
014: abstract class MockSession implements Session {
015:
016: private boolean closed = false;
017: private boolean transacted;
018: private int acknowledgeMode;
019:
020: private MockConnection connection;
021: private final List consumers = new ArrayList();
022: private final List producers = new ArrayList();
023:
024: /**
025: * Creates new session with the specified attributes, for connection <code>connection</code>
026: * @param transacted
027: * @param acknowledgeMode
028: * @param connection
029: */
030: public MockSession(boolean transacted, int acknowledgeMode,
031: MockConnection connection) {
032: this .transacted = transacted;
033: this .acknowledgeMode = acknowledgeMode;
034: this .connection = connection;
035: }
036:
037: /**
038: * Gets session transacted property.
039: * @return <code>true</code> if session is transacted or <code>false</code>
040: * if not
041: * @throws JMSException
042: * @see javax.jms.Session#getTransacted()
043: */
044: public boolean getTransacted() throws JMSException {
045: checkClosed();
046: return transacted;
047: }
048:
049: /**
050: * Gets session acknowledge mode.
051: * @return session acknowledge mode
052: * @throws JMSException
053: */
054: public int getAcknowledgeMode() throws JMSException {
055: checkClosed();
056: return acknowledgeMode;
057: }
058:
059: /**
060: * Closes this session. Any further operation, except <code>close</code>, on
061: * this session will result in <code>IllegalStateException</code>
062: * @throws JMSException
063: * @see javax.jms.Session#close()
064: */
065: public void close() throws JMSException {
066: closed = true;
067: Iterator it = consumers.iterator();
068: while (it.hasNext()) {
069: ((MockConsumer) it.next()).close();
070: }
071: it = producers.iterator();
072: while (it.hasNext()) {
073: ((MockProducer) it.next()).close();
074: }
075: }
076:
077: /**
078: * Gets session closed status.
079: * @return <code>true</code> if this session has been closed, or <code>false</code>
080: * if not.
081: */
082: protected boolean isClosed() {
083: return closed;
084: }
085:
086: /**
087: * Checks if this session has been closed and if yes throws
088: * <code>IllegalStateException</code>
089: * @throws javax.jms.IllegalStateException if this session has been closed
090: */
091: protected void checkClosed() throws javax.jms.IllegalStateException {
092: if (isClosed()) {
093: throw new javax.jms.IllegalStateException(
094: "Can not invoke methods on closed session!");
095: }
096: }
097:
098: /**
099: * @return
100: * @throws JMSException
101: * @see javax.jms.Session#createBytesMessage()
102: */
103: public BytesMessage createBytesMessage() throws JMSException {
104: checkClosed();
105: return new BytesMessageImpl();
106: }
107:
108: /**
109: * @return
110: * @throws JMSException
111: * @see javax.jms.Session#createMapMessage()
112: */
113: public MapMessage createMapMessage() throws JMSException {
114: checkClosed();
115: return new MapMessageImpl();
116: }
117:
118: /**
119: * @return
120: * @throws JMSException
121: * @see javax.jms.Session#createMessage()
122: */
123: public Message createMessage() throws JMSException {
124: checkClosed();
125: return new MessageImpl();
126: }
127:
128: /**
129: * @return
130: * @throws JMSException
131: * @see javax.jms.Session#createObjectMessage()
132: */
133: public ObjectMessage createObjectMessage() throws JMSException {
134: checkClosed();
135: return new ObjectMessageImpl();
136: }
137:
138: /**
139: * Creates <code>ObjectMessage</code> initialized with the
140: * specified serialized object.
141: * @throws JMSException
142: */
143: public ObjectMessage createObjectMessage(Serializable object)
144: throws JMSException {
145:
146: checkClosed();
147: return new ObjectMessageImpl(object);
148: }
149:
150: /**
151: * @return
152: * @throws JMSException
153: * @see javax.jms.Session#createStreamMessage()
154: */
155: public StreamMessage createStreamMessage() throws JMSException {
156: checkClosed();
157: return new StreamMessageImpl();
158: }
159:
160: /**
161: * @return
162: * @throws JMSException
163: * @see javax.jms.Session#createTextMessage()
164: */
165: public TextMessage createTextMessage() throws JMSException {
166: checkClosed();
167: return new TextMessageImpl();
168: }
169:
170: /**
171: * @return
172: * @throws JMSException
173: * @see javax.jms.Session#createTextMessage(String)
174: */
175: public TextMessage createTextMessage(String text)
176: throws JMSException {
177: checkClosed();
178: return new TextMessageImpl(text);
179: }
180:
181: /**
182: * Not implemented.
183: * @throws JMSException
184: * @see javax.jms.Session#commit()
185: */
186: public void commit() throws JMSException {
187: throw new MethodNotImplementedException("commit", "MockSession");
188: }
189:
190: /**
191: * Not implemented.
192: * @throws JMSException
193: * @see javax.jms.Session#rollback()
194: */
195: public void rollback() throws JMSException {
196: throw new MethodNotImplementedException("rollback",
197: "MockSession");
198: }
199:
200: /**
201: * Not implemented.
202: * @throws JMSException
203: * @see javax.jms.Session#recover()
204: */
205: public void recover() throws JMSException {
206: throw new MethodNotImplementedException("recover",
207: "MockSession");
208: }
209:
210: /**
211: * Not implemented.
212: * @return
213: * @throws JMSException
214: * @see javax.jms.Session#getMessageListener()
215: */
216: public MessageListener getMessageListener() throws JMSException {
217: throw new MethodNotImplementedException("getMessageListener",
218: "MockSession");
219: }
220:
221: /**
222: * Not implemented.
223: * @throws JMSException
224: * @see javax.jms.Session#setMessageListener(javax.jms.MessageListener)
225: */
226: public void setMessageListener(MessageListener listener)
227: throws JMSException {
228: throw new MethodNotImplementedException("setMessageListener",
229: "MockSession");
230: }
231:
232: /**
233: * Not implemented.
234: */
235: public void run() {
236: throw new MethodNotImplementedException("run", "MockSession");
237: }
238:
239: /**
240: * Creates message consumer for the specified destination.
241: * @param destination
242: * @throws JMSException
243: */
244: public MessageConsumer createConsumer(Destination destination)
245: throws JMSException {
246:
247: checkClosed();
248:
249: if (destination instanceof MockDestination) {
250: MockDestination dest = (MockDestination) destination;
251: MockConsumer consumer = createMockConsumer(dest);
252: dest.registerConsumer(consumer);
253: consumers.add(consumer);
254: return consumer;
255: }
256: throw new InvalidDestinationException(
257: "Unsupported destination!");
258: }
259:
260: /**
261: * Not implemented.
262: * @return
263: * @throws JMSException
264: */
265: public MessageConsumer createConsumer(Destination destination,
266: java.lang.String messageSelector) throws JMSException {
267:
268: throw new MethodNotImplementedException("createConsumer",
269: "MockSession");
270: }
271:
272: /**
273: * Not implemented.
274: * @throws JMSException
275: */
276: public MessageConsumer createConsumer(Destination destination,
277: java.lang.String messageSelector, boolean NoLocal)
278: throws JMSException {
279:
280: throw new MethodNotImplementedException("createConsumer",
281: "MockSession");
282: }
283:
284: /**
285: * Creates message producer for the specified destination.
286: */
287: public MessageProducer createProducer(Destination destination)
288: throws JMSException {
289:
290: checkClosed();
291: if (destination instanceof MockDestination) {
292: MockProducer result = createMockProducer((MockDestination) destination);
293: producers.add(result);
294: return result;
295: }
296: throw new InvalidDestinationException(
297: "Unsupported destination!");
298: }
299:
300: // Non-standard methods
301:
302: abstract MockConsumer createMockConsumer(MockDestination destination)
303: throws JMSException;
304:
305: abstract MockProducer createMockProducer(MockDestination destination)
306: throws JMSException;
307:
308: MockConnection getConnection() {
309: return connection;
310: }
311:
312: void consumeMessages() throws JMSException {
313: Iterator it = consumers.iterator();
314: while (it.hasNext()) {
315: ((MockConsumer) it.next()).consume();
316: }
317: }
318:
319: }
|