|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| package jtr.jms; |
|
8 |
| |
|
9 |
| import java.lang.reflect.*; |
|
10 |
| import java.util.*; |
|
11 |
| import javax.jms.*; |
|
12 |
| import javax.jms.Queue; |
|
13 |
| import jtr.assigner.util.BeanUtilsExtension; |
|
14 |
| |
|
15 |
| import org.apache.commons.beanutils.*; |
|
16 |
| |
|
17 |
| import jtr.config.jms.JMSConfig; |
|
18 |
| import jtr.config.jms.JMSConnection; |
|
19 |
| import jtr.config.jms.JMSConnectionFactory; |
|
20 |
| import jtr.config.jms.JMSDestination; |
|
21 |
| import jtr.config.jms.JMSSession; |
|
22 |
| import jtr.enterprise.LocatorException; |
|
23 |
| import jtr.runners.*; |
|
24 |
| import org.apache.log4j.Logger; |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| public class JMSHelper { |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
14
| public static void inject(IRunner target, JMSConfig jmsConfig) throws JMSInjectionException {
|
|
61 |
14
| try {
|
|
62 |
14
| injectQueues(target, jmsConfig);
|
|
63 |
14
| injectTopics(target, jmsConfig);
|
|
64 |
14
| injectConnectionFactories(target, jmsConfig);
|
|
65 |
| } catch (Exception e) { |
|
66 |
0
| String msg = "Caught an exception while injecting JMS administered object into IRunner " + target.getDefaultName();
|
|
67 |
0
| logger.fatal(msg, e);
|
|
68 |
0
| throw new JMSInjectionException(msg, e);
|
|
69 |
| } |
|
70 |
| } |
|
71 |
| |
|
72 |
14
| protected static void injectQueues(IRunner target, JMSConfig jmsConfig) throws JMSInjectionException, LocatorException, IllegalAccessException, InvocationTargetException {
|
|
73 |
14
| logger.debug("Injecting Queues...");
|
|
74 |
14
| Hashtable ht = target.getEnterprise().toHashtable();
|
|
75 |
14
| Iterator iter = jmsConfig.getQueues().iterator();
|
|
76 |
14
| while (iter.hasNext()) {
|
|
77 |
14
| JMSDestination dest = (JMSDestination) iter.next();
|
|
78 |
14
| Queue queue = (Queue) AdministeredObjectsLocator.getDestination(dest.getJndi(), ht);
|
|
79 |
14
| BeanUtilsExtension.setProperty(target, dest.getProperty(), queue);
|
|
80 |
14
| logger.debug("injected queue " + dest.getJndi());
|
|
81 |
| } |
|
82 |
14
| logger.debug("Injecting Queues... done!");
|
|
83 |
| } |
|
84 |
| |
|
85 |
14
| protected static void injectTopics(IRunner target, JMSConfig jmsConfig) throws JMSInjectionException, LocatorException, IllegalAccessException, InvocationTargetException {
|
|
86 |
14
| logger.debug("Injecting Topics...");
|
|
87 |
14
| Hashtable ht = target.getEnterprise().toHashtable();
|
|
88 |
14
| Iterator iter = jmsConfig.getTopics().iterator();
|
|
89 |
14
| while (iter.hasNext()) {
|
|
90 |
0
| JMSDestination dest = (JMSDestination) iter.next();
|
|
91 |
0
| Topic topic = (Topic) AdministeredObjectsLocator.getDestination(dest.getJndi(), ht);
|
|
92 |
0
| BeanUtilsExtension.setProperty(target, dest.getProperty(), topic);
|
|
93 |
0
| logger.debug("injected topic " + dest.getJndi());
|
|
94 |
| } |
|
95 |
14
| logger.debug("Injecting Topics... done!");
|
|
96 |
| } |
|
97 |
| |
|
98 |
14
| protected static void injectConnectionFactories(IRunner target, JMSConfig jmsConfig) throws LocatorException, InvocationTargetException, IllegalAccessException, IllegalAccessException,
|
|
99 |
| InvocationTargetException, JMSInjectionException, JMSException { |
|
100 |
14
| logger.debug("Injecting ConnectionFactories...");
|
|
101 |
14
| Hashtable ht = target.getEnterprise().toHashtable();
|
|
102 |
14
| Iterator iter = jmsConfig.getConnectionFactories().iterator();
|
|
103 |
14
| while (iter.hasNext()) {
|
|
104 |
14
| JMSConnectionFactory jmscf = (JMSConnectionFactory) iter.next();
|
|
105 |
14
| ConnectionFactory cf = (ConnectionFactory) AdministeredObjectsLocator.getConnectionFactory(jmscf.getJndi(), ht);
|
|
106 |
14
| BeanUtilsExtension.setProperty(target, jmscf.getProperty(), cf);
|
|
107 |
14
| logger.debug("injected connectionFactory " + jmscf.getJndi());
|
|
108 |
| |
|
109 |
14
| injectConnections(target, jmscf, cf, jmsConfig.getUniqueName());
|
|
110 |
| } |
|
111 |
14
| logger.debug("Injecting ConnectionFactories... done!");
|
|
112 |
| } |
|
113 |
| |
|
114 |
14
| protected static void injectConnections(IRunner target, JMSConnectionFactory jmscf, ConnectionFactory cf, String configKey) throws JMSException, JMSInjectionException, InvocationTargetException, IllegalAccessException {
|
|
115 |
14
| logger.debug("Injecting Connections...");
|
|
116 |
14
| Iterator connIter = jmscf.getConnections().iterator();
|
|
117 |
14
| while (connIter.hasNext()) {
|
|
118 |
14
| Connection c = null;
|
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
14
| JMSConnection jmsc = (JMSConnection) connIter.next();
|
|
123 |
14
| if(jmsc.isShared()) {
|
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
0
| synchronized(connectionCache) {
|
|
135 |
0
| if(connectionCache.containsKey(configKey)) {
|
|
136 |
| |
|
137 |
0
| c = connectionCache.get(configKey);
|
|
138 |
0
| injectSessions(target,jmsc,c);
|
|
139 |
| } else { |
|
140 |
| |
|
141 |
0
| c = createConnection(jmsc,cf);
|
|
142 |
0
| setClientId(target,jmsc,c);
|
|
143 |
0
| injectSessions(target,jmsc,c);
|
|
144 |
0
| connectionCache.put(configKey,c);
|
|
145 |
| } |
|
146 |
| } |
|
147 |
| } else { |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
14
| c = createConnection(jmsc,cf);
|
|
152 |
14
| setClientId(target,jmsc,c);
|
|
153 |
14
| injectSessions(target,jmsc,c);
|
|
154 |
| } |
|
155 |
| |
|
156 |
14
| BeanUtilsExtension.setProperty(target, jmsc.getProperty(), c);
|
|
157 |
14
| logger.debug("injected connection " + c);
|
|
158 |
| } |
|
159 |
| } |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
0
| protected static void injectXATopicSessions(IRunner target, JMSConnection jmsc, XATopicConnection xatc) throws JMSException, InvocationTargetException, IllegalAccessException {
|
|
175 |
0
| logger.debug("Injecting XATopicSessions...");
|
|
176 |
0
| Iterator sessionIter = jmsc.getSessions().iterator();
|
|
177 |
0
| while (sessionIter.hasNext()) {
|
|
178 |
0
| JMSSession jmss = (JMSSession) sessionIter.next();
|
|
179 |
0
| TopicSession ts = xatc.createTopicSession(jmss.getTransacted(), jmss.getAckMode());
|
|
180 |
0
| BeanUtilsExtension.setProperty(target, jmss.getProperty(), ts);
|
|
181 |
0
| logger.debug("injected XATopicSession");
|
|
182 |
| } |
|
183 |
0
| logger.debug("Injecting XATopicSessions... done!");
|
|
184 |
| } |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
0
| protected static void injectXAQueueSessions(IRunner target, JMSConnection jmsc, XAQueueConnection xaqc) throws JMSException, InvocationTargetException, IllegalAccessException {
|
|
200 |
0
| logger.debug("Injecting XAQueueSessions...");
|
|
201 |
0
| Iterator sessionIter = jmsc.getSessions().iterator();
|
|
202 |
0
| while (sessionIter.hasNext()) {
|
|
203 |
0
| JMSSession jmss = (JMSSession) sessionIter.next();
|
|
204 |
0
| QueueSession qs = xaqc.createQueueSession(jmss.getTransacted(), jmss.getAckMode());
|
|
205 |
0
| BeanUtilsExtension.setProperty(target, jmss.getProperty(), qs);
|
|
206 |
0
| logger.debug("injected XAQueueSession");
|
|
207 |
| } |
|
208 |
0
| logger.debug("Injecting XAQueueSessions... done!");
|
|
209 |
| } |
|
210 |
| |
|
211 |
14
| protected static void injectQueueSessions(IRunner target, JMSConnection jmsc, QueueConnection qc) throws JMSException, InvocationTargetException, IllegalAccessException {
|
|
212 |
14
| logger.debug("Injecting QueueSessions...");
|
|
213 |
14
| Iterator sessionIter = jmsc.getSessions().iterator();
|
|
214 |
14
| while (sessionIter.hasNext()) {
|
|
215 |
14
| JMSSession jmss = (JMSSession) sessionIter.next();
|
|
216 |
14
| QueueSession qs = qc.createQueueSession(jmss.getTransacted(), jmss.getAckMode());
|
|
217 |
14
| BeanUtilsExtension.setProperty(target, jmss.getProperty(), qs);
|
|
218 |
14
| logger.debug("injected queueSession");
|
|
219 |
| } |
|
220 |
14
| logger.debug("Injecting QueueSessions... done!");
|
|
221 |
| } |
|
222 |
| |
|
223 |
0
| protected static void injectTopicSessions(IRunner target, JMSConnection jmsc, TopicConnection tc) throws JMSException, InvocationTargetException, IllegalAccessException {
|
|
224 |
0
| logger.debug("Injecting TopicSessions...");
|
|
225 |
0
| Iterator sessionIter = jmsc.getSessions().iterator();
|
|
226 |
0
| while (sessionIter.hasNext()) {
|
|
227 |
0
| JMSSession jmss = (JMSSession) sessionIter.next();
|
|
228 |
0
| TopicSession ts = tc.createTopicSession(jmss.getTransacted(), jmss.getAckMode());
|
|
229 |
0
| BeanUtilsExtension.setProperty(target, jmss.getProperty(), ts);
|
|
230 |
0
| logger.debug("injected topicSession");
|
|
231 |
| } |
|
232 |
0
| logger.debug("Injecting TopicSessions... done!");
|
|
233 |
| } |
|
234 |
| |
|
235 |
14
| private static void injectSessions(IRunner target, JMSConnection jmsc, Connection c) throws JMSException, InvocationTargetException, IllegalAccessException, JMSInjectionException {
|
|
236 |
14
| if (JMSConnection.isQueueConnection(jmsc)) {
|
|
237 |
| |
|
238 |
14
| injectQueueSessions(target,jmsc,(QueueConnection)c);
|
|
239 |
| } else { |
|
240 |
0
| if (JMSConnection.isTopicConnection(jmsc)) {
|
|
241 |
| |
|
242 |
0
| injectTopicSessions(target,jmsc,(TopicConnection)c);
|
|
243 |
| } else { |
|
244 |
0
| if (JMSConnection.isXaQueueConnection(jmsc)) {
|
|
245 |
| |
|
246 |
0
| injectXAQueueSessions(target,jmsc,(XAQueueConnection)c);
|
|
247 |
| } else { |
|
248 |
0
| if (JMSConnection.isXaTopicConnection(jmsc)) {
|
|
249 |
| |
|
250 |
0
| injectXATopicSessions(target,jmsc,(XATopicConnection)c);
|
|
251 |
| } else { |
|
252 |
| |
|
253 |
0
| String msg = "Unable to instantiate sessions for a connection of type " + jmsc.getType();
|
|
254 |
0
| logger.error(msg);
|
|
255 |
0
| throw new JMSInjectionException(msg);
|
|
256 |
| } |
|
257 |
| } |
|
258 |
| } |
|
259 |
| } |
|
260 |
| } |
|
261 |
| |
|
262 |
14
| private static void setClientId(IRunner target, JMSConnection jmsc, Connection c) throws JMSException {
|
|
263 |
14
| if (jmsc.getClientId() != null) {
|
|
264 |
| |
|
265 |
| |
|
266 |
14
| logger.debug("Setting clientID " + jmsc.getClientId() + " for the current connection...");
|
|
267 |
14
| if(jmsc.getClientId().equals(DEFAULT_CLIENT_ID))
|
|
268 |
14
| c.setClientID(target.getName());
|
|
269 |
| else |
|
270 |
0
| c.setClientID(jmsc.getClientId());
|
|
271 |
| } |
|
272 |
| } |
|
273 |
| |
|
274 |
14
| private static Connection createConnection(JMSConnection jmsc, ConnectionFactory cf) throws JMSInjectionException, JMSException {
|
|
275 |
14
| Connection c = null;
|
|
276 |
14
| if (JMSConnection.isQueueConnection(jmsc)) {
|
|
277 |
| |
|
278 |
14
| if (jmsc.getUserName() == null && jmsc.getPassword() == null) {
|
|
279 |
14
| c = ((QueueConnectionFactory) cf).createQueueConnection();
|
|
280 |
0
| } else if (jmsc.getUserName() != null && jmsc.getPassword() != null) {
|
|
281 |
0
| c = ((QueueConnectionFactory) cf).createQueueConnection(jmsc.getUserName(), jmsc.getPassword());
|
|
282 |
| } else { |
|
283 |
0
| String msg = "Invalid userName/password null values combination";
|
|
284 |
0
| logger.error(msg);
|
|
285 |
0
| throw new JMSInjectionException(msg);
|
|
286 |
| } |
|
287 |
| } else { |
|
288 |
0
| if (JMSConnection.isTopicConnection(jmsc)) {
|
|
289 |
0
| if (jmsc.getUserName() == null && jmsc.getPassword() == null) {
|
|
290 |
0
| c = ((TopicConnectionFactory) cf).createTopicConnection();
|
|
291 |
0
| } else if (jmsc.getUserName() != null && jmsc.getPassword() != null) {
|
|
292 |
0
| c = ((TopicConnectionFactory) cf).createTopicConnection(jmsc.getUserName(), jmsc.getPassword());
|
|
293 |
| } else { |
|
294 |
0
| String msg = "Invalid userName/password null values combination";
|
|
295 |
0
| logger.error(msg);
|
|
296 |
0
| throw new JMSInjectionException(msg);
|
|
297 |
| } |
|
298 |
| } else { |
|
299 |
0
| if (JMSConnection.isXaQueueConnection(jmsc)) {
|
|
300 |
| |
|
301 |
0
| if (jmsc.getUserName() == null && jmsc.getPassword() == null) {
|
|
302 |
0
| c = ((XAQueueConnectionFactory) cf).createXAQueueConnection();
|
|
303 |
0
| } else if (jmsc.getUserName() != null && jmsc.getPassword() != null) {
|
|
304 |
0
| c = ((XAQueueConnectionFactory) cf).createXAQueueConnection(jmsc.getUserName(), jmsc.getPassword());
|
|
305 |
| } else { |
|
306 |
0
| String msg = "Invalid userName/password null values combination";
|
|
307 |
0
| logger.error(msg);
|
|
308 |
0
| throw new JMSInjectionException(msg);
|
|
309 |
| } |
|
310 |
| } else { |
|
311 |
0
| if (JMSConnection.isXaTopicConnection(jmsc)) {
|
|
312 |
| |
|
313 |
0
| if (jmsc.getUserName() == null && jmsc.getPassword() == null) {
|
|
314 |
0
| c = ((XATopicConnectionFactory) cf).createXATopicConnection();
|
|
315 |
0
| } else if (jmsc.getUserName() != null && jmsc.getPassword() != null) {
|
|
316 |
0
| c = ((XATopicConnectionFactory) cf).createXATopicConnection(jmsc.getUserName(), jmsc.getPassword());
|
|
317 |
| } else { |
|
318 |
0
| String msg = "Invalid userName/password null values combination";
|
|
319 |
0
| logger.error(msg);
|
|
320 |
0
| throw new JMSInjectionException(msg);
|
|
321 |
| } |
|
322 |
| } else { |
|
323 |
| |
|
324 |
0
| String msg = "Unable to instantiate a JMS connection of type " + jmsc.getType();
|
|
325 |
0
| logger.error(msg);
|
|
326 |
0
| throw new JMSInjectionException(msg);
|
|
327 |
| } |
|
328 |
| } |
|
329 |
| } |
|
330 |
| } |
|
331 |
| |
|
332 |
14
| return c;
|
|
333 |
| } |
|
334 |
| |
|
335 |
| private static HashMap<String,Connection> connectionCache = new HashMap<String,Connection>(); |
|
336 |
| private static Logger logger = Logger.getLogger(JMSHelper.class); |
|
337 |
| private static final String DEFAULT_CLIENT_ID = "${runner.id}"; |
|
338 |
| } |