001 package com.mockrunner.mock.jms;
002
003 import java.io.Serializable;
004 import java.util.ArrayList;
005 import java.util.List;
006
007 import javax.jms.Connection;
008 import javax.jms.JMSException;
009 import javax.jms.QueueConnection;
010 import javax.jms.QueueConnectionFactory;
011 import javax.jms.TopicConnection;
012 import javax.jms.TopicConnectionFactory;
013
014 import com.mockrunner.jms.ConfigurationManager;
015 import com.mockrunner.jms.DestinationManager;
016
017 /**
018 * Mock implementation of JMS <code>ConnectionFactory</code>.
019 * Can be used as generic factory for JMS 1.1.
020 * Also implements <code>QueueConnectionFactory</code> and
021 * <code>TopicConnectionFactory</code> and can be used to
022 * create queue and topic connections as well as generic
023 * JMS 1.1 connections. It is recommended to use
024 * {@link com.mockrunner.mock.jms.MockQueueConnectionFactory}
025 * if you only use queues and
026 * {@link com.mockrunner.mock.jms.MockTopicConnectionFactory}
027 * if you only use topics.
028 * This implementation is primary for generic JMS 1.1 connections
029 * but can also be used, if a server provides one implementation
030 * for both domains (which is not portable).
031 */
032 public class MockConnectionFactory implements QueueConnectionFactory, TopicConnectionFactory, Serializable
033 {
034 private DestinationManager destinationManager;
035 private ConfigurationManager configurationManager;
036 private List connections;
037 private JMSException exception;
038
039 public MockConnectionFactory(DestinationManager destinationManager, ConfigurationManager configurationManager)
040 {
041 connections = new ArrayList();
042 this.destinationManager = destinationManager;
043 this.configurationManager = configurationManager;
044 exception = null;
045 }
046
047 public Connection createConnection() throws JMSException
048 {
049 return createConnection(null, null);
050 }
051
052 public Connection createConnection(String name, String password) throws JMSException
053 {
054 MockConnection connection = new MockConnection(destinationManager, configurationManager, name, password);
055 connection.setJMSException(exception);
056 connections.add(connection);
057 return connection;
058 }
059
060 public QueueConnection createQueueConnection() throws JMSException
061 {
062 return createQueueConnection(null, null);
063 }
064
065 public QueueConnection createQueueConnection(String name, String password) throws JMSException
066 {
067 MockQueueConnection connection = new MockQueueConnection(destinationManager(), configurationManager(), name, password);
068 connection.setJMSException(exception());
069 connections().add(connection);
070 return connection;
071 }
072
073 public TopicConnection createTopicConnection() throws JMSException
074 {
075 return createTopicConnection(null, null);
076 }
077
078 public TopicConnection createTopicConnection(String name, String password) throws JMSException
079 {
080 MockTopicConnection connection = new MockTopicConnection(destinationManager(), configurationManager(), name, password);
081 connection.setJMSException(exception());
082 connections().add(connection);
083 return connection;
084 }
085
086 /**
087 * Set an exception that will be passed to all
088 * created connections. This can be used to
089 * simulate server errors. Check out
090 * {@link MockConnection#setJMSException}
091 * for details.
092 * @param exception the exception
093 */
094 public void setJMSException(JMSException exception)
095 {
096 this.exception = exception;
097 }
098
099 /**
100 * Clears the list of connections
101 */
102 public void clearConnections()
103 {
104 connections.clear();
105 }
106
107 /**
108 * Returns the connection with the specified index
109 * or <code>null</code> if no such connection
110 * exists.
111 * @param index the index
112 * @return the connection
113 */
114 public MockConnection getConnection(int index)
115 {
116 if(connections.size() <= index) return null;
117 return (MockConnection)connections.get(index);
118 }
119
120 /**
121 * Returns the latest created connection
122 * or <code>null</code> if no such connection
123 * exists.
124 * @return the connection
125 */
126 public MockConnection getLatestConnection()
127 {
128 if(connections.size() == 0) return null;
129 return (MockConnection)connections.get(connections.size() - 1);
130 }
131
132 protected DestinationManager destinationManager()
133 {
134 return destinationManager;
135 }
136
137 protected ConfigurationManager configurationManager()
138 {
139 return configurationManager;
140 }
141
142 protected List connections()
143 {
144 return connections;
145 }
146
147 protected JMSException exception()
148 {
149 return exception;
150 }
151 }