-
Notifications
You must be signed in to change notification settings - Fork 33
/
DirectConnectionFactory.java
564 lines (509 loc) · 23.5 KB
/
DirectConnectionFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
* Copyright (c) 2000, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2022 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.messaging.jms.ra;
import java.util.Properties;
import java.util.logging.Logger;
import jakarta.jms.Connection;
import jakarta.jms.JMSContext;
import jakarta.jms.JMSException;
import jakarta.jms.JMSSecurityException;
import jakarta.jms.QueueConnection;
import jakarta.jms.TopicConnection;
import javax.naming.NamingException;
import javax.naming.Reference;
import jakarta.resource.ResourceException;
import javax.transaction.xa.XAResource;
import com.sun.messaging.jmq.jmsclient.ContainerType;
import com.sun.messaging.jmq.jmsclient.JMSContextImpl;
import com.sun.messaging.jmq.jmsservice.JMSService;
import com.sun.messaging.jmq.jmsservice.JMSServiceException;
import com.sun.messaging.jmq.jmsservice.JMSServiceReply;
/**
* DirectConnectionFactory encapsulates JMS ConnectionFactory behavior for MQ DIRECT mode operation.
*/
public class DirectConnectionFactory extends ConnectionCreator implements jakarta.jms.ConnectionFactory, jakarta.jms.QueueConnectionFactory,
jakarta.jms.TopicConnectionFactory, jakarta.resource.Referenceable, java.io.Serializable {
/**
* Configuration properties of the Direct ConnectionFactory
*/
private Properties configuration = null;
/**
* ResourceAdapter for this Direct ConnectionFactory
*/
private ResourceAdapter ra = null;
/**
* ManagedConnectionFactory for this DirectConnectionFactory
*/
private ManagedConnectionFactory mcf = null;
/**
* Flags whether this DirectConnectionFactory is being used in the Application Client Container
*/
private boolean inACC = false;
/**
* Flags whether this DirectConnectionFactory is being used in a clustered instance or not
*/
private boolean inClusteredContainer = false;
/**
* The RA Namespace to use for this DirectConnectionFactory in a clustered environment
*/
private String raNameSpace = null;
/**
* JMSService for the Direct ConnectionFactory
*/
private transient JMSService jmsservice = null;
/** The ConnectionManager instance */
private jakarta.resource.spi.ConnectionManager cm = null;
/**
* Logging
*/
private static final String _className = "com.sun.messaging.jms.ra.DirectConnectionFactory";
private static final String _lgrNameOutboundConnection = "javax.resourceadapter.mqjmsra.outbound.connection";
private static final String _lgrNameJMSConnectionFactory = "jakarta.jms.ConnectionFactory.mqjmsra";
private static final Logger _loggerOC = Logger.getLogger(_lgrNameOutboundConnection);
private static final Logger _loggerJF = Logger.getLogger(_lgrNameJMSConnectionFactory);
private static final String _lgrMIDPrefix = "MQJMSRA_DCF";
private static final String _lgrMID_INF = _lgrMIDPrefix + "1101: ";
private static final String _lgrMID_EXC = _lgrMIDPrefix + "4001: ";
private static boolean _disableConnectionManagement;
// private static String dcm;
static {
// dcm = System.getProperty("imq.jmsra.direct.disableCM", "false");
_disableConnectionManagement = Boolean.getBoolean("imq.jmsra.direct.disableCM");
}
/**
* Construct a new instance of DirectConnectionFactory with a JMSService object and configuration properties
*
* @param jmsservice The JMSService to use to create JMS objects
* @param props The Properties to use to configure this ConnectionFactory
*
*/
public DirectConnectionFactory(JMSService jmsservice, Properties props) {
Object params[] = new Object[2];
params[0] = jmsservice;
params[1] = props;
_loggerOC.entering(_className, "constructor(jmsservice, props)", params);
this.jmsservice = jmsservice;
this.configuration = props;
_loggerOC.exiting(_className, "constructor(jmsservice, props):config=" + toString());
}
/**
* Construct a new instance of DirectConnectionFactory with a JMSService object, configuration properties and a
* ConnectionManager
*/
public DirectConnectionFactory(ManagedConnectionFactory mcf, jakarta.resource.spi.ConnectionManager cm) {
Object params[] = new Object[2];
params[0] = mcf;
params[1] = cm;
_loggerOC.entering(_className, "constructor(mcf, cm)", params);
this.mcf = mcf;
this.cm = cm;
this.ra = (ResourceAdapter) mcf.getResourceAdapter();
this.jmsservice = ra._getJMSService();
this.inACC = ra.getInAppClientContainer();
this.inClusteredContainer = ra.getInClusteredContainer();
this.raNameSpace = ra._getRAUID();
_loggerOC.exiting(_className, "constructor(mcf, cm):config=" + toString());
}
/////////////////////////////////////////////////////////////////////////
// Methods implementing jakarta.resource.Referenceable
/////////////////////////////////////////////////////////////////////////
/**
* Set the Reference for this ConnectionFactory
*
* @param ref The Reference to set
*/
@Override
public void setReference(Reference ref) {
_loggerJF.fine(_lgrMID_INF + "setReference():" + "Reference=" + ref);
}
/**
* Returns the Reference for this ConnectionFactory
*
* @return The Reference
*
* @throws NamingException If the Reference is unavailable
*/
@Override
public Reference getReference() throws NamingException {
_loggerJF.fine(_lgrMID_INF + "getReference():");
// MQJMSRA doesn't create Reference objects;
// throw the correct Exception
throw new NamingException("MQRA:DCF:getReference:NOT Supported");
}
/////////////////////////////////////////////////////////////////////////
// end jakarta.resource.Referenceable
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
// Methods that implement jakarta.jms.ConnectionFactory
/////////////////////////////////////////////////////////////////////////
/**
* Creates a DIRECT Connection with the default user identity.
*
* @return The JMS Connection object to use
*
* @throws JMSSecurityException if the default user identity does not authenthicate successfully with the JMS server
* @throws JMSException if any JMS server error occurred
*/
@Override
public Connection createConnection() throws JMSException {
_loggerJF.fine(_lgrMID_INF + "createConnection():");
String username, password = null;
if (this.mcf == null || mcf.getUserName() == null) {
username = "guest";
} else {
username = mcf.getUserName();
}
if (this.mcf == null || mcf.getPassword() == null) {
password = "guest";
} else {
password = mcf.getPassword();
}
return this.createConnection(username, password);
}
/**
* Creates a DIRECT Connection with a specified user identity.
*
* @param username The username that should be used to authenticate the creation of this JMS Connection
* @param password The password that should be used to authenticate the creation of this JMS Connection
*
* @return The JMS Connection object to use
*
* @throws JMSSecurityException if the specified user identity does not authenticate successfully with the JMS server
* @throws JMSException if any JMS server error occurred
*/
@Override
public Connection createConnection(String username, String password) throws JMSException {
_loggerJF.fine(_lgrMID_INF + "createConnection(u,p):username=" + username);
if (!this._disableConnectionManagement) {
return this._allocateConnection(username, password);
}
long connectionId = _createConnectionId(username, password);
DirectConnection dc = new DirectConnection(this, this.jmsservice, connectionId, this.inACC);
return dc;
}
@Override
public JMSContext createContext() {
return new JMSContextImpl(this, getContainerType());
}
@Override
public JMSContext createContext(String userName, String password) {
return new JMSContextImpl(this, getContainerType(), userName, password);
}
@Override
public JMSContext createContext(String userName, String password, int sessionMode) {
return new JMSContextImpl(this, getContainerType(), userName, password, sessionMode);
}
@Override
public JMSContext createContext(int sessionMode) {
return new JMSContextImpl(this, getContainerType(), sessionMode);
}
private ContainerType getContainerType() {
if (ra.getInAppClientContainer()) {
return ContainerType.JavaEE_ACC;
} else {
return ContainerType.JavaEE_Web_or_EJB;
}
}
/////////////////////////////////////////////////////////////////////////
// end jakarta.jms.ConnectionFactory
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
// Methods that implement jakarta.jms.QueueConnectionFactory
/////////////////////////////////////////////////////////////////////////
/**
* Creates a DIRECT QueueConnection with the default user identity.
*
* @return The JMS QueueConnection object to use
*
* @throws JMSSecurityException if the default user identity does not authenthicate successfully with the JMS server
* @throws JMSException if any JMS server error occurred
*/
@Override
public QueueConnection createQueueConnection() throws JMSException {
_loggerJF.fine(_lgrMID_INF + "createQueueConnection():");
String username, password = null;
if (this.mcf == null || mcf.getUserName() == null) {
username = "guest";
} else {
username = mcf.getUserName();
}
if (this.mcf == null || mcf.getPassword() == null) {
password = "guest";
} else {
password = mcf.getPassword();
}
return this.createQueueConnection(username, password);
}
/**
* Creates a DIRECT QueueConnection with a specified user identity.
*
* @param username The username that should be used to authenticate the creation of this JMS QueueConnection
* @param password The password that should be used to authenticate the creation of this JMS QueueConnection
*
* @return The JMS QueueConnection object to use
*
* @throws JMSSecurityException if the specified user identity does not authenticate successfully with the JMS server
* @throws JMSException if any JMS server error occurred
*/
@Override
public QueueConnection createQueueConnection(String username, String password) throws JMSException {
_loggerJF.fine(_lgrMID_INF + "createQueueConnection(u,p):username=" + username);
if (!this._disableConnectionManagement) {
return (QueueConnection) this._allocateQueueConnection(username, password);
}
long connectionId = _createConnectionId(username, password);
DirectConnection dc = new DirectQueueConnection(this, this.jmsservice, connectionId, this.inACC);
return dc;
}
/////////////////////////////////////////////////////////////////////////
// end jakarta.jms.QueueConnectionFactory
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
// Methods that implement jakarta.jms.TopicConnectionFactory
/////////////////////////////////////////////////////////////////////////
/**
* Creates a DIRECT TopicConnection with the default user identity.
*
* @return The JMS TopicConnection object to use
*
* @throws JMSSecurityException if the default user identity does not authenthicate successfully with the JMS server
* @throws JMSException if any JMS server error occurred
*/
@Override
public TopicConnection createTopicConnection() throws JMSException {
_loggerJF.fine(_lgrMID_INF + "createTopicConnection():");
String username, password = null;
if (this.mcf == null || mcf.getUserName() == null) {
username = "guest";
} else {
username = mcf.getUserName();
}
if (this.mcf == null || mcf.getPassword() == null) {
password = "guest";
} else {
password = mcf.getPassword();
}
return this.createTopicConnection(username, password);
}
/**
* Creates a DIRECT TopicConnection with a specified user identity.
*
* @param username The username that should be used to authenticate the creation of this JMS TopicConnection
* @param password The password that should be used to authenticate the creation of this JMS TopicConnection
*
* @return The JMS TopicConnection object to use
*
* @throws JMSSecurityException if the specified user identity does not authenticate successfully with the JMS server
* @throws JMSException if any JMS server error occurred
*/
@Override
public TopicConnection createTopicConnection(String username, String password) throws JMSException {
_loggerJF.fine(_lgrMID_INF + "createTopicConnection(u,p):username=" + username);
if (!this._disableConnectionManagement) {
return (TopicConnection) this._allocateTopicConnection(username, password);
}
long connectionId = _createConnectionId(username, password);
DirectConnection dc = new DirectTopicConnection(this, this.jmsservice, connectionId, this.inACC);
return dc;
}
/////////////////////////////////////////////////////////////////////////
// end jakarta.jms.TopicConnectionFactory
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
// MQ methods
/////////////////////////////////////////////////////////////////////////
/**
* Create a connection with the jmsservice and return a connectionId. Used by the methods implementing
* jakarta.jms.Connection, jakarta.jms.QueueConnection, and jakarta.jms.TopicConnection
*
* @param username The username that should be used to authenticate the creation of the connection with the jmsservice
* @param password The password that should be used to authenticate the creation of the connection with the jmsservice
*
* @return The connectionId to be used by the DirectConnection object that is returned by the JMS API method
*
* @throws JMSSecurityException if the specified user identity does not authenticate successfully with the JMS server
* @throws JMSException if any JMS server error occurred
*/
private long _createConnectionId(String username, String password) throws JMSException {
JMSServiceReply reply;
long connectionId = 0L;
if (this.jmsservice == null) {
if (this.ra != null) {
this.jmsservice = this.ra._getJMSService();
}
if (this.jmsservice == null) {
// Ultimate fallback :)
this.jmsservice = ResourceAdapter._getRAJMSService();
}
}
assert (this.jmsservice != null);
try {
reply = jmsservice.createConnection(username, password);
try {
connectionId = reply.getJMQConnectionID();
} catch (NoSuchFieldException nsfe) {
String exerrmsg = _lgrMID_EXC + "JMSServiceException:Missing JMQConnectionID";
JMSException jmse = new JMSException(exerrmsg);
jmse.initCause(nsfe);
_loggerJF.severe(exerrmsg);
throw jmse;
}
} catch (JMSServiceException jse) {
JMSServiceReply.Status status = jse.getJMSServiceReply().getStatus();
JMSException jmsse;
String failure_cause;
boolean security_exception = true;
switch (status) {
case INVALID_LOGIN:
failure_cause = "authentication failure.";
break;
case FORBIDDEN:
failure_cause = "authorization failure.";
break;
default:
failure_cause = "unkown JMSService server error.";
security_exception = false;
}
String exerrmsg = "createConnection on JMSService:" + jmsservice.getJMSServiceID() + " failed for username:" + username + " due to "
+ failure_cause;
_loggerJF.severe(exerrmsg);
jmsse = (security_exception ? new JMSSecurityException(exerrmsg, jse.getJMSServiceReply().getErrorCode())
: new JMSException(exerrmsg, jse.getJMSServiceReply().getErrorCode()));
jmsse.initCause(jse);
throw jmsse;
}
return connectionId;
}
/**
* Set the ResourceAdapter object for this DirectConnectionFactory
*/
public void setResourceAdapter(ResourceAdapter ra) {
this.ra = ra;
}
/**
* Returns the configuration of this DirectConnectionFactory
*
* @return The configuration properties of this DirectConnectionFactory
*/
public Properties _getConfiguration() {
return configuration;
}
/**
* Set a configuration property value for this DirectConnectionFactory
*
* @param name The name of the property to set
* @param value The value of the property to set
*
* @throws IllegalArgumentException If the property name is invalid
*
*/
public void setConfigurationProperty(String name, String value) {
}
/**
* Returns whether this DirectConnectionFactory is active in an Application Server cluster or not.
*
* @return {@code true} if it is active in a cluster; {@code false} otherwise
*/
protected boolean isRAClustered() {
return this.inClusteredContainer;
}
/**
* Returns the name space assigned by the RA. Each instance of the RA sets a 'namespace within which JMS client
* identifiers are required to be unique. The 'namespace' is acquired by the RA from the MQ broker.
*
* @return The RA name space
*/
protected String getRANamespace() {
return this.raNameSpace;
}
@Override
protected Connection _createConnection(String username, String password) throws JMSException {
long connectionId = _createConnectionId(username, password);
DirectConnection dc = new DirectConnection(this, this.jmsservice, connectionId, this.inACC);
return dc;
}
@Override
protected Connection _createQueueConnection(String username, String password) throws JMSException {
long connectionId = _createConnectionId(username, password);
DirectConnection dc = new DirectQueueConnection(this, this.jmsservice, connectionId, this.inACC);
return dc;
}
@Override
protected Connection _createTopicConnection(String username, String password) throws JMSException {
long connectionId = _createConnectionId(username, password);
DirectConnection dc = new DirectTopicConnection(this, this.jmsservice, connectionId, this.inACC);
return dc;
}
@Override
protected XAResource _createXAResource(ManagedConnection mc, Object conn) throws JMSException {
return null;
}
private Connection _allocateConnection(String username, String password) throws JMSException {
com.sun.messaging.jms.ra.ConnectionRequestInfo.ConnectionType connectionType = com.sun.messaging.jms.ra.ConnectionRequestInfo.ConnectionType.UNIFIED_CONNECTION;
jakarta.resource.spi.ConnectionRequestInfo crinfo = new com.sun.messaging.jms.ra.ConnectionRequestInfo(mcf, username, password, connectionType);
// System.out.println("MQRA:CFA:createConnection:allocating connection");
DirectConnection dc;
try {
dc = (DirectConnection) this.cm.allocateConnection(mcf, crinfo);
return dc;
} catch (jakarta.resource.spi.SecurityException se) {
// XXX:Fix codes
throw new com.sun.messaging.jms.JMSSecurityException("MQRA:DCF:allocation failure:createConnection:" + se.getMessage(), se.getErrorCode(), se);
} catch (ResourceException re) {
// XXX:Fix codes
throw new com.sun.messaging.jms.JMSException("MQRA:DCF:allocation failure:createConnection:" + re.getMessage(), re.getErrorCode(), re);
}
}
private Connection _allocateQueueConnection(String username, String password) throws JMSException {
com.sun.messaging.jms.ra.ConnectionRequestInfo.ConnectionType connectionType = com.sun.messaging.jms.ra.ConnectionRequestInfo.ConnectionType.QUEUE_CONNECTION;
jakarta.resource.spi.ConnectionRequestInfo crinfo = new com.sun.messaging.jms.ra.ConnectionRequestInfo(mcf, username, password, connectionType);
// System.out.println("MQRA:CFA:createConnection:allocating connection");
DirectConnection dc;
try {
dc = (DirectConnection) this.cm.allocateConnection(mcf, crinfo);
return dc;
} catch (jakarta.resource.spi.SecurityException se) {
// XXX:Fix codes
throw new com.sun.messaging.jms.JMSSecurityException("MQRA:DCF:allocation failure:createConnection:" + se.getMessage(), se.getErrorCode(), se);
} catch (ResourceException re) {
// XXX:Fix codes
throw new com.sun.messaging.jms.JMSException("MQRA:DCF:allocation failure:createConnection:" + re.getMessage(), re.getErrorCode(), re);
}
}
private Connection _allocateTopicConnection(String username, String password) throws JMSException {
com.sun.messaging.jms.ra.ConnectionRequestInfo.ConnectionType connectionType = com.sun.messaging.jms.ra.ConnectionRequestInfo.ConnectionType.TOPIC_CONNECTION;
jakarta.resource.spi.ConnectionRequestInfo crinfo = new com.sun.messaging.jms.ra.ConnectionRequestInfo(mcf, username, password, connectionType);
// System.out.println("MQRA:CFA:createConnection:allocating connection");
DirectConnection dc;
try {
dc = (DirectConnection) this.cm.allocateConnection(mcf, crinfo);
return dc;
} catch (jakarta.resource.spi.SecurityException se) {
// XXX:Fix codes
throw new com.sun.messaging.jms.JMSSecurityException("MQRA:DCF:allocation failure:createConnection:" + se.getMessage(), se.getErrorCode(), se);
} catch (ResourceException re) {
// XXX:Fix codes
throw new com.sun.messaging.jms.JMSException("MQRA:DCF:allocation failure:createConnection:" + re.getMessage(), re.getErrorCode(), re);
}
}
ManagedConnectionFactory getMCF() {
return mcf;
}
void setMCF(ManagedConnectionFactory mcf) {
this.mcf = mcf;
}
}