1
2
3
4
5
6
7
8
9
10
11
12 package com.eviware.soapui.impl.wsdl.submit.transports.jms;
13
14 import hermes.Domain;
15 import hermes.Hermes;
16
17 import javax.jms.Connection;
18 import javax.jms.ConnectionFactory;
19 import javax.jms.JMSException;
20 import javax.jms.Queue;
21 import javax.jms.Session;
22 import javax.jms.Topic;
23 import javax.naming.NamingException;
24
25 import com.eviware.soapui.SoapUI;
26 import com.eviware.soapui.support.StringUtils;
27
28 /***
29 * class that holds jms connections and sessions
30 *
31 * @author nebojsa.tasic
32 *
33 */
34 public class JMSConnectionHolder
35 {
36 private ConnectionFactory connectionFactory = null;
37 private Connection connection = null;
38 private Session session = null;
39
40 private JMSEndpoint jmsEndpoint;
41 private Hermes hermes;
42 private String clientID;
43
44 /***
45 *
46 * @param jmsEndpoint
47 * @param hermes
48 * @param createQueueConnection
49 * @param createTopicConnection
50 * @param clientID
51 * @param username
52 * @param password
53 * @throws JMSException
54 */
55 public JMSConnectionHolder( JMSEndpoint jmsEndpoint, Hermes hermes, boolean isTopicDomain, String clientID,
56 String username, String password ) throws JMSException
57 {
58 try
59 {
60 this.jmsEndpoint = jmsEndpoint;
61 this.hermes = hermes;
62 this.clientID = clientID;
63
64 connectionFactory = ( ConnectionFactory )hermes.getConnectionFactory();
65 connection = createConnection( connectionFactory, isTopicDomain ? Domain.TOPIC : Domain.QUEUE, clientID,
66 username, password );
67 connection.start();
68
69 }
70 catch( Throwable t )
71 {
72 SoapUI.logError( t );
73
74 if( connection != null )
75 connection.close();
76
77 throw new JMSException( t.getMessage() );
78
79 }
80 }
81
82 private Connection createConnection( ConnectionFactory connectionFactory, Domain domain, String clientId,
83 String username, String password ) throws JMSException
84 {
85 Connection connection = StringUtils.hasContent( username ) ? ( ( ConnectionFactory )connectionFactory )
86 .createConnection( username, password ) : ( ( ConnectionFactory )connectionFactory ).createConnection();
87
88 if( !StringUtils.isNullOrEmpty( clientId ) && domain.equals( Domain.TOPIC ) )
89 connection.setClientID( clientId );
90
91 return connection;
92
93 }
94
95 public ConnectionFactory getConnectionFactory()
96 {
97 return connectionFactory;
98 }
99
100 public Connection getConnection()
101 {
102 return connection;
103 }
104
105 public String getClientID()
106 {
107 return clientID;
108 }
109
110 public Hermes getHermes()
111
112 {
113 return hermes;
114 }
115
116 public JMSEndpoint getJmsEndpoint()
117 {
118 return jmsEndpoint;
119 }
120
121 /***
122 * return topic by name
123 *
124 * @return Queue
125 * @throws JMSException
126 * , NamingException
127 */
128 public Topic getTopic( String name ) throws JMSException, NamingException
129 {
130 if (name == null || name.isEmpty()) {
131 return getSession().createTemporaryTopic();
132 }
133 else {
134 return ( Topic )getHermes().getDestination( name, Domain.TOPIC );
135 }
136 }
137
138 /***
139 * return queue by name
140 *
141 * @return Queue
142 * @throws JMSException
143 * , NamingException
144 */
145 public Queue getQueue( String name ) throws JMSException, NamingException
146 {
147 if (name == null || name.isEmpty()) {
148 return getSession().createTemporaryQueue();
149 }
150 else {
151 return ( Queue )getHermes().getDestination( name, Domain.QUEUE );
152 }
153 }
154
155 /***
156 *
157 * @return Session
158 * @throws JMSException
159 */
160 public Session getSession() throws JMSException
161 {
162 if( session == null )
163 {
164 return session = getConnection().createSession( false, Session.AUTO_ACKNOWLEDGE );
165 }
166 return session;
167 }
168
169 /***
170 * closes sessions and connections
171 */
172 public void closeAll()
173 {
174 try
175 {
176 if( session != null )
177 session.close();
178 if( connection != null )
179 {
180 connection.close();
181 connection = null;
182 }
183 }
184 catch( JMSException e )
185 {
186 SoapUI.logError( e );
187 }
188 }
189
190 }