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 return ( Topic )getHermes().getDestination( name, Domain.TOPIC );
131 }
132
133 /***
134 * return queue by name
135 *
136 * @return Queue
137 * @throws JMSException
138 * , NamingException
139 */
140 public Queue getQueue( String name ) throws JMSException, NamingException
141 {
142 return ( Queue )getHermes().getDestination( name, Domain.QUEUE );
143 }
144
145 /***
146 *
147 * @return Session
148 * @throws JMSException
149 */
150 public Session getSession() throws JMSException
151 {
152 if( session == null )
153 {
154 return session = getConnection().createSession( false, Session.AUTO_ACKNOWLEDGE );
155 }
156 return session;
157 }
158
159 /***
160 * closes sessions and connections
161 */
162 public void closeAll()
163 {
164 try
165 {
166 if( session != null )
167 session.close();
168 if( connection != null )
169 connection.close();
170 }
171 catch( JMSException e )
172 {
173 SoapUI.logError( e );
174 }
175 }
176
177 }