1
2
3
4
5
6
7
8
9
10 package org.apache.commons.messenger;
11
12 import javax.jms.Connection;
13 import javax.jms.ConnectionFactory;
14 import javax.jms.JMSException;
15 import javax.jms.QueueConnection;
16 import javax.jms.QueueConnectionFactory;
17 import javax.jms.Session;
18 import javax.jms.TopicConnection;
19 import javax.jms.TopicConnectionFactory;
20 import javax.naming.Context;
21 import javax.naming.InitialContext;
22 import javax.naming.NamingException;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27
28
29
30
31
32
33
34 public class JNDISessionFactory extends SessionFactory {
35
36
37 private static final Log log = LogFactory.getLog( JNDISessionFactory.class );
38
39
40 public Context context;
41
42
43 private String lookupName = "TopicConnectionFactory";
44
45
46
47
48
49
50 public String getLookupName() {
51 return lookupName;
52 }
53
54
55 public void setLookupName(String lookupName) {
56 this.lookupName = lookupName;
57 }
58
59
60 public Context getContext() throws NamingException {
61 if ( context == null ) {
62 context = createContext();
63 }
64 return context;
65 }
66
67 public void setContext(Context context) {
68 this.context = context;
69 }
70
71
72
73
74
75
76
77 protected ConnectionFactory createConnectionFactory() throws JMSException {
78 try {
79 if ( log.isInfoEnabled() ) {
80 log.info( "Looking up: " + getLookupName() + " in JNDI" );
81 }
82 return (ConnectionFactory) getContext().lookup(getLookupName());
83 }
84 catch (NamingException e) {
85 JMSException jmsException = new JMSException( "Failed to lookup: " + getLookupName() + " using JNDI. " + e );
86 jmsException.setLinkedException(e);
87 throw jmsException;
88 }
89 }
90
91
92 public Connection createConnection() throws JMSException {
93 ConnectionFactory factory = getConnectionFactory();
94
95 if ( factory == null ) {
96 throw new JMSException( "No ConnectionFactory configured. Cannot create a JMS Session" );
97 }
98
99 if ( isTopic() ) {
100 return createTopicConnection((TopicConnectionFactory) factory);
101 }
102 else {
103 return createQueueConnection((QueueConnectionFactory) factory);
104 }
105 }
106
107
108 public Session createSession(Connection connection) throws JMSException {
109
110 if ( isTopic() ) {
111 TopicConnection topicConnection = (TopicConnection) connection;
112 return topicConnection.createTopicSession( isTransacted(), getAcknowledgeMode() );
113 }
114 else {
115 QueueConnection queueConnection = (QueueConnection) connection;
116 return queueConnection.createQueueSession( isTransacted(), getAcknowledgeMode() );
117 }
118 }
119
120
121
122
123 protected Context createContext() throws NamingException {
124 if ( properties != null ) {
125 return new InitialContext( properties );
126 }
127 else {
128 return new InitialContext();
129 }
130 }
131 }
132