1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.apache.commons.messenger; |
11 | |
|
12 | |
import java.util.Properties; |
13 | |
|
14 | |
import javax.jms.Connection; |
15 | |
import javax.jms.ConnectionFactory; |
16 | |
import javax.jms.JMSException; |
17 | |
import javax.jms.MessageListener; |
18 | |
import javax.jms.QueueConnection; |
19 | |
import javax.jms.QueueConnectionFactory; |
20 | |
import javax.jms.ServerSessionPool; |
21 | |
import javax.jms.Session; |
22 | |
import javax.jms.TopicConnection; |
23 | |
import javax.jms.TopicConnectionFactory; |
24 | |
|
25 | |
import org.apache.commons.logging.Log; |
26 | |
import org.apache.commons.logging.LogFactory; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | public class SessionFactory { |
36 | |
|
37 | |
|
38 | 0 | private static final Log log = LogFactory.getLog(SessionFactory.class); |
39 | |
|
40 | |
|
41 | |
private Connection connection; |
42 | |
|
43 | |
|
44 | |
private ConnectionFactory connectionFactory; |
45 | |
|
46 | |
|
47 | 0 | private int acknowlegeMode = Session.AUTO_ACKNOWLEDGE; |
48 | |
|
49 | |
|
50 | |
private boolean transacted; |
51 | |
|
52 | |
|
53 | |
private String username; |
54 | |
|
55 | |
|
56 | |
private String password; |
57 | |
|
58 | |
|
59 | |
protected Properties properties; |
60 | |
|
61 | |
|
62 | 0 | private boolean topic = true; |
63 | |
|
64 | |
|
65 | |
private String clientID; |
66 | |
|
67 | |
|
68 | |
public Session createSession(Connection connection) throws JMSException { |
69 | 0 | if ( log.isDebugEnabled() ) { |
70 | 0 | log.debug( |
71 | |
"Creating a JMS session in transacted mode: " |
72 | |
+ isTransacted() + " with ack mode: " + getAcknowledgeMode() |
73 | |
); |
74 | |
} |
75 | |
|
76 | 0 | if (topic) { |
77 | 0 | TopicConnection topicConnection = (TopicConnection) connection; |
78 | 0 | return topicConnection.createTopicSession(isTransacted(), getAcknowledgeMode()); |
79 | |
} |
80 | |
else { |
81 | 0 | QueueConnection queueConnection = (QueueConnection) connection; |
82 | 0 | return queueConnection.createQueueSession(isTransacted(), getAcknowledgeMode()); |
83 | |
} |
84 | |
} |
85 | |
|
86 | |
|
87 | |
public Session createSession() throws JMSException { |
88 | 0 | Connection connection = getConnection(); |
89 | 0 | if (topic) { |
90 | 0 | TopicConnection topicConnection = (TopicConnection) connection; |
91 | 0 | return topicConnection.createTopicSession(isTransacted(), getAcknowledgeMode()); |
92 | |
} |
93 | |
else { |
94 | 0 | QueueConnection queueConnection = (QueueConnection) connection; |
95 | 0 | return queueConnection.createQueueSession(isTransacted(), getAcknowledgeMode()); |
96 | |
} |
97 | |
} |
98 | |
|
99 | |
public ServerSessionPool createServerSessionPool( |
100 | |
MessageListener messageListener, |
101 | |
int maxThreads) |
102 | |
throws JMSException { |
103 | 0 | return new DefaultServerSessionPool(this, messageListener, maxThreads); |
104 | |
} |
105 | |
|
106 | |
|
107 | |
public Connection createConnection() throws JMSException { |
108 | 0 | ConnectionFactory factory = getConnectionFactory(); |
109 | 0 | if (factory == null) { |
110 | 0 | throw new JMSException("No ConnectionFactory configured. Cannot create a JMS Session"); |
111 | |
} |
112 | 0 | if (log.isDebugEnabled()) { |
113 | 0 | log.debug("About to create a connection from: " + factory); |
114 | |
} |
115 | 0 | if (topic) { |
116 | 0 | return createTopicConnection((TopicConnectionFactory) factory); |
117 | |
} |
118 | |
else { |
119 | 0 | return createQueueConnection((QueueConnectionFactory) factory); |
120 | |
} |
121 | |
} |
122 | |
|
123 | |
|
124 | |
public synchronized void close() throws JMSException { |
125 | 0 | if (connection != null) { |
126 | 0 | Connection tmp = connection; |
127 | 0 | connection = null; |
128 | 0 | tmp.close(); |
129 | |
} |
130 | 0 | } |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
public Connection getConnection() throws JMSException { |
137 | 0 | if (connection == null) { |
138 | 0 | setConnection(createConnection()); |
139 | 0 | connection.start(); |
140 | |
} |
141 | 0 | return connection; |
142 | |
} |
143 | |
|
144 | |
public void setConnection(Connection connection) throws JMSException { |
145 | 0 | this.connection = connection; |
146 | |
|
147 | 0 | if (topic) { |
148 | 0 | if (!(connection instanceof TopicConnection)) { |
149 | 0 | setTopic(false); |
150 | |
} |
151 | |
} |
152 | |
else { |
153 | 0 | if (!(connection instanceof QueueConnection)) { |
154 | 0 | setTopic(true); |
155 | |
} |
156 | |
} |
157 | |
|
158 | |
|
159 | 0 | if (clientID != null) { |
160 | 0 | connection.setClientID(clientID); |
161 | |
} |
162 | 0 | } |
163 | |
|
164 | |
|
165 | |
public ConnectionFactory getConnectionFactory() throws JMSException { |
166 | 0 | if (connectionFactory == null) { |
167 | 0 | setConnectionFactory(createConnectionFactory()); |
168 | |
} |
169 | 0 | return connectionFactory; |
170 | |
} |
171 | |
|
172 | |
public void setConnectionFactory(ConnectionFactory connectionFactory) { |
173 | 0 | this.connectionFactory = connectionFactory; |
174 | |
|
175 | 0 | if (topic) { |
176 | 0 | if (!(connectionFactory instanceof TopicConnectionFactory)) { |
177 | 0 | setTopic(false); |
178 | |
} |
179 | |
} |
180 | |
else { |
181 | 0 | if (!(connectionFactory instanceof QueueConnectionFactory)) { |
182 | 0 | setTopic(true); |
183 | |
} |
184 | |
} |
185 | 0 | } |
186 | |
|
187 | |
|
188 | |
public boolean isTransacted() { |
189 | 0 | return transacted; |
190 | |
} |
191 | |
|
192 | |
public void setTransacted(boolean transacted) { |
193 | 0 | this.transacted = transacted; |
194 | 0 | } |
195 | |
|
196 | |
|
197 | |
public int getAcknowledgeMode() { |
198 | 0 | return acknowlegeMode; |
199 | |
} |
200 | |
|
201 | |
public void setAcknowledgeMode(int acknowlegeMode) { |
202 | 0 | this.acknowlegeMode = acknowlegeMode; |
203 | 0 | } |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
public void setAcknowledge(String value) { |
213 | 0 | if (value != null) { |
214 | 0 | if (value.equalsIgnoreCase("auto")) { |
215 | 0 | setAcknowledgeMode(Session.AUTO_ACKNOWLEDGE); |
216 | |
} |
217 | 0 | else if (value.equalsIgnoreCase("client")) { |
218 | 0 | setAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE); |
219 | |
} |
220 | 0 | else if (value.equalsIgnoreCase("dups_ok")) { |
221 | 0 | setAcknowledgeMode(Session.DUPS_OK_ACKNOWLEDGE); |
222 | |
} |
223 | |
else { |
224 | 0 | throw new IllegalArgumentException( |
225 | |
"Value: " + value |
226 | |
+ " is invalid. Must be 'auto', 'client' or 'dups_ok'" |
227 | |
); |
228 | |
} |
229 | |
} |
230 | 0 | } |
231 | |
|
232 | |
|
233 | |
public String getUsername() { |
234 | 0 | return username; |
235 | |
} |
236 | |
|
237 | |
public void setUsername(String username) { |
238 | 0 | this.username = username; |
239 | 0 | } |
240 | |
|
241 | |
|
242 | |
public String getPassword() { |
243 | 0 | return password; |
244 | |
} |
245 | |
|
246 | |
public void setPassword(String password) { |
247 | 0 | this.password = password; |
248 | 0 | } |
249 | |
|
250 | |
|
251 | |
public Properties getProperties() { |
252 | 0 | if (properties == null) { |
253 | 0 | properties = createProperties(); |
254 | |
} |
255 | 0 | return properties; |
256 | |
} |
257 | |
|
258 | |
public void setProperties(Properties properties) { |
259 | 0 | this.properties = properties; |
260 | 0 | } |
261 | |
|
262 | |
public void addProperty(String name, String value) { |
263 | 0 | getProperties().setProperty(name, value); |
264 | 0 | } |
265 | |
|
266 | |
|
267 | |
public boolean isTopic() { |
268 | 0 | return topic; |
269 | |
} |
270 | |
|
271 | |
|
272 | |
public void setTopic(boolean topic) { |
273 | 0 | this.topic = topic; |
274 | 0 | } |
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
public String getClientID() { |
281 | 0 | return clientID; |
282 | |
} |
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
public void setClientID(String clientID) { |
289 | 0 | this.clientID = clientID; |
290 | 0 | } |
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
protected QueueConnection createQueueConnection(QueueConnectionFactory queueConnectionFactory) |
296 | |
throws JMSException { |
297 | 0 | if (username != null || password != null) { |
298 | 0 | return queueConnectionFactory.createQueueConnection(username, password); |
299 | |
} |
300 | |
else { |
301 | 0 | return queueConnectionFactory.createQueueConnection(); |
302 | |
} |
303 | |
} |
304 | |
|
305 | |
protected TopicConnection createTopicConnection(TopicConnectionFactory topicConnectionFactory) |
306 | |
throws JMSException { |
307 | 0 | if (username != null || password != null) { |
308 | 0 | return topicConnectionFactory.createTopicConnection(username, password); |
309 | |
} |
310 | |
else { |
311 | 0 | return topicConnectionFactory.createTopicConnection(); |
312 | |
} |
313 | |
} |
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
protected ConnectionFactory createConnectionFactory() throws JMSException { |
319 | 0 | return null; |
320 | |
} |
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
protected Properties createProperties() { |
326 | |
try { |
327 | 0 | return new Properties(System.getProperties()); |
328 | |
} |
329 | 0 | catch (Throwable e) { |
330 | |
|
331 | 0 | return new Properties(); |
332 | |
} |
333 | |
} |
334 | |
} |