/
ActiveJmsResourceAdapter.java
executable file
·2388 lines (2120 loc) · 102 KB
/
ActiveJmsResourceAdapter.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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
*
* 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.enterprise.connectors.jms.system;
import com.sun.appserv.connectors.internal.api.ConnectorConstants;
import com.sun.appserv.connectors.internal.api.ConnectorRuntime;
import com.sun.appserv.connectors.internal.api.ConnectorRuntimeException;
import com.sun.appserv.connectors.internal.api.ConnectorsUtil;
import com.sun.appserv.server.util.Version;
import com.sun.enterprise.config.serverbeans.AdminService;
import com.sun.enterprise.config.serverbeans.Applications;
import com.sun.enterprise.config.serverbeans.AvailabilityService;
import com.sun.enterprise.config.serverbeans.Clusters;
import com.sun.enterprise.config.serverbeans.Domain;
import com.sun.enterprise.config.serverbeans.JavaConfig;
import com.sun.enterprise.config.serverbeans.JmxConnector;
import com.sun.enterprise.config.serverbeans.Server;
import com.sun.enterprise.config.serverbeans.Servers;
import com.sun.enterprise.connectors.inbound.ActiveInboundResourceAdapterImpl;
import com.sun.enterprise.connectors.jms.JMSLoggerInfo;
import com.sun.enterprise.connectors.jms.config.JmsAvailability;
import com.sun.enterprise.connectors.jms.config.JmsHost;
import com.sun.enterprise.connectors.jms.config.JmsService;
import com.sun.enterprise.connectors.jms.inflow.MdbContainerProps;
import com.sun.enterprise.connectors.jms.util.JmsRaUtil;
import com.sun.enterprise.connectors.service.ConnectorAdminServiceUtils;
import com.sun.enterprise.connectors.util.ResourcesUtil;
import com.sun.enterprise.connectors.util.SetMethodAction;
import com.sun.enterprise.deployment.Application;
import com.sun.enterprise.deployment.ApplicationClientDescriptor;
import com.sun.enterprise.deployment.ConnectorConfigProperty;
import com.sun.enterprise.deployment.ConnectorDescriptor;
import com.sun.enterprise.deployment.EjbBundleDescriptor;
import com.sun.enterprise.deployment.EjbDescriptor;
import com.sun.enterprise.deployment.EjbMessageBeanDescriptor;
import com.sun.enterprise.deployment.EnvironmentProperty;
import com.sun.enterprise.deployment.JMSDestinationDefinitionDescriptor;
import com.sun.enterprise.deployment.MessageDestinationDescriptor;
import com.sun.enterprise.deployment.WebBundleDescriptor;
import com.sun.enterprise.deployment.runtime.BeanPoolDescriptor;
import com.sun.enterprise.util.SystemPropertyConstants;
import com.sun.enterprise.util.i18n.StringManager;
import com.sun.enterprise.v3.services.impl.DummyNetworkListener;
import com.sun.enterprise.v3.services.impl.GrizzlyService;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Provider;
import jakarta.inject.Singleton;
import jakarta.resource.spi.ActivationSpec;
import jakarta.resource.spi.BootstrapContext;
import jakarta.resource.spi.ManagedConnectionFactory;
import jakarta.resource.spi.ResourceAdapterInternalException;
import java.lang.reflect.Method;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SocketChannel;
import java.rmi.Naming;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import org.glassfish.admin.mbeanserver.JMXStartupService;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.api.logging.LogHelper;
import org.glassfish.api.naming.GlassfishNamingManager;
import org.glassfish.api.naming.SimpleJndiName;
import org.glassfish.connectors.config.AdminObjectResource;
import org.glassfish.connectors.config.ConnectorConnectionPool;
import org.glassfish.connectors.config.ConnectorResource;
import org.glassfish.connectors.config.ResourceAdapterConfig;
import org.glassfish.deployment.common.Descriptor;
import org.glassfish.deployment.common.JavaEEResourceType;
import org.glassfish.deployment.common.ModuleDescriptor;
import org.glassfish.grizzly.config.dom.NetworkListener;
import org.glassfish.hk2.api.MultiException;
import org.glassfish.hk2.api.PostConstruct;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.internal.api.Globals;
import org.glassfish.internal.api.ServerContext;
import org.glassfish.internal.data.ApplicationInfo;
import org.glassfish.internal.data.ApplicationRegistry;
import org.glassfish.internal.grizzly.LazyServiceInitializer;
import org.glassfish.server.ServerEnvironmentImpl;
import org.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.config.types.Property;
import static com.sun.enterprise.connectors.service.ConnectorAdminServiceUtils.getReservePrefixedJNDINameForDescriptor;
/**
* Represents an active JMS resource adapter. This does
* additional configuration to ManagedConnectionFactory
* and ResourceAdapter java beans.
*
* XXX: For code management reasons, think about splitting this
* to a preHawk and postHawk RA (with postHawk RA extending preHawk RA).
*
* @author Satish Kumar
*/
@Service
@Singleton
@Named(ActiveJmsResourceAdapter.JMS_SERVICE)
public class ActiveJmsResourceAdapter extends ActiveInboundResourceAdapterImpl implements LazyServiceInitializer, PostConstruct {
private static final Logger _logger = JMSLoggerInfo.getLogger();
private final String SETTER = "setProperty";
private static final String SEPARATOR = "#";
private static final String MQ_PASS_FILE_PREFIX = "asmq";
private static final String MQ_PASS_FILE_KEY = "imq.imqcmd.password=";
//RA Javabean properties.
public static final String CONNECTION_URL = "ConnectionURL";
private final String RECONNECTENABLED = "ReconnectEnabled";
private final String RECONNECTINTERVAL = "ReconnectInterval";
private final String RECONNECTATTEMPTS = "ReconnectAttempts";
private static final String GROUPNAME = "GroupName";
private static final String CLUSTERCONTAINER = "InClusteredContainer";
//Lifecycle RA JavaBean properties
public static final String BROKERTYPE="BrokerType";
private static final String BROKERINSTANCENAME="BrokerInstanceName";
private static final String BROKERBINDADDRESS="BrokerBindAddress";
private static final String BROKERPORT="BrokerPort";
private static final String BROKERARGS="BrokerArgs";
private static final String BROKERHOMEDIR="BrokerHomeDir";
private static final String BROKERLIBDIR ="BrokerLibDir";
private static final String BROKERVARDIR="BrokerVarDir";
private static final String BROKERJAVADIR="BrokerJavaDir";
private static final String BROKERSTARTTIMEOUT="BrokerStartTimeOut";
public static final String ADMINUSERNAME="AdminUsername";
public static final String ADMINPASSWORD="AdminPassword";
private static final String ADMINPASSFILE="AdminPassFile";
private static final String USERNAME="UserName";
private static final String PASSWORD="Password";
private static final String MQ_PORTMAPPER_BIND = "doBind";//"imq.portmapper.bind";
private static final String MASTERBROKER="MasterBroker";
//JMX properties
private static final String JMXSERVICEURL="JMXServiceURL";
private static final String JMXSERVICEURLLIST="JMXServiceURLList";
private static final String JMXCONNECTORENV="JMXConnectorEnv";
private static final String USEJNDIRMISERVICEURL="useJNDIRMIServiceURL";
private static final String RMIREGISTRYPORT="RmiRegistryPort";
private static final String USEEXTERNALRMIREGISTRY="startRMIRegistry";
private static final int DEFAULTRMIREGISTRYPORT =7776;
private static final int BROKERRMIPORTOFFSET=100;
private static final String SSLJMXCONNECTOR="SslJMXConnector";
//Availability properties
private static final String CONVENTIONAL_CLUSTER__OF_PEER_BROKERS_DB_PREFIX= "imq.cluster.sharecc.persist.jdbc.";
private static final String ENHANCED_CLUSTER_DB_PREFIX= "imq.persist.jdbc.";
private static final String HAREQUIRED = "HARequired";
private static final String CLUSTERID = "ClusterId";
private static final String BROKERID = "BrokerId";
private static final String PINGINTERVAL = "PingInterval";
private static final String DBTYPE = "DBType";
private static final String DBTYPE_HADB="hadb";
private static final String BROKERENABLEHA = "BrokerEnableHA";
private static final String DB_HADB_PROPS = "DBProps";
//properties within DB_PROPS
private static final String DB_HADB_USER = "hadb.user";
private static final String DB_HADB_PASSWORD = "hadb.password";
private static final String DB_HADB_DRIVERCLASS = "hadb.driverClass";
private static final String DS_HADB_PROPS = "DSProps";
//properties within DS_PROPS
private static final String DS_HADB_SERVERLIST = "hadb.serverList";
//Not used now.
private final String CONTAINER = "InAppClientContainer";
//Activation config properties of MQ resource adapter.
public static final String DESTINATION = "destination";
public static final String DESTINATION_TYPE = "destinationType";
private static String SUBSCRIPTION_NAME = "SubscriptionName";
private static String CLIENT_ID = "ClientID";
public static final String PHYSICAL_DESTINATION = "Name";
private static String MAXPOOLSIZE = "EndpointPoolMaxSize";
private static String MINPOOLSIZE = "EndpointPoolSteadySize";
private static String RESIZECOUNT = "EndpointPoolResizeCount";
private static String RESIZETIMEOUT = "EndpointPoolResizeTimeout";
private static String REDELIVERYCOUNT = "EndpointExceptionRedeliveryAttempts";
private static String LOWERCASE_REDELIVERYCOUNT = "endpointExceptionRedeliveryAttempts";
public static final String ADDRESSLIST = "AddressList";
private static String ADRLIST_BEHAVIOUR = "AddressListBehavior";
private static String ADRLIST_ITERATIONS = "AddressListIterations";
private static final String MDBIDENTIFIER = "MdbName";
public static final String JMS_SERVICE = "mq-service";
//MCF properties
private static final String MCFADDRESSLIST = "MessageServiceAddressList";
//private static final String XA_JOIN_ALLOWED= "imq.jmsra.isXAJoinAllowed";
private final StringManager sm = StringManager.getManager(ActiveJmsResourceAdapter.class);
private MQAddressList urlList;
private String addressList;
private String brkrPort;
private boolean doBind;
//Properties in domain.xml for HADB JDBC connection pool (for HA)
private static final String DUSERNAME = "User";
private static final String DPASSWORD = "Password";
private static final String DSERVERLIST = "ServerList";
private static final String HADB_CONNECTION_URL_PREFIX = "jdbc:sun:hadb:";
//Lifecycle properties
public static final String EMBEDDED="EMBEDDED";
public static final String LOCAL="LOCAL";
public static final String REMOTE="REMOTE";
public static final String DIRECT="DIRECT";
private final String DEFAULT_STORE_POOL_JNDI_NAME = "jdbc/hastore";
// Both the properties below are hacks. These will be changed later on.
private static String MQRmiPort = System.getProperty("com.sun.enterprise.connectors.system.MQRmiPort");
private static final String DASRMIPORT = "31099";
private static final String REVERT_TO_EMBEDDED_PROPERTY = "com.sun.enterprise.connectors.system.RevertToEmbedded";
private static final String BROKER_RMI_PORT = "com.sun.enterprise.connectors.system.mq.rmiport";
private static final String DEFAULT_SERVER = "server";
private static final String DEFAULT_MQ_INSTANCE = "imqbroker";
public static final String MQ_DIR_NAME = "imq";
public static final String GRIZZLY_PROXY_PREFIX = "JMS_PROXY_";
private enum ClusterMode {
ENHANCED, CONVENTIONAL_WITH_MASTER_BROKER, CONVENTIONAL_OF_PEER_BROKERS;
}
private Properties dbProps;
private String brokerInstanceName;
private boolean grizzlyListenerInit;
private final Set<String> grizzlyListeners = new HashSet<>();
@Inject
private ConnectorRuntime connectorRuntime;
@Inject
private GlassfishNamingManager nm;
@Inject
private Provider<JMSConfigListener> jmsConfigListenerProvider;
@Inject
private Provider<ServerEnvironmentImpl> serverEnvironmentImplProvider;
@Inject @Named(ServerEnvironment.DEFAULT_INSTANCE_NAME)
private Provider<AdminService> adminServiceProvider;
@Inject
private Provider<Servers> serversProvider;
@Inject
private Provider<ServerContext> serverContextProvider;
@Inject
private Provider<ConnectorRuntime> connectorRuntimeProvider;
@Inject
private ServiceLocator habitat;
@Inject
private ApplicationRegistry appRegistry;
/**
* Constructor for an active Jms Adapter.
*
*/
public ActiveJmsResourceAdapter() {
super();
}
@Override
public void postConstruct() {
/*
* If any special handling is required for the system resource
* adapter, then ActiveResourceAdapter implementation for that
* RA should implement additional functionality by extending
* ActiveInboundResourceAdapter or ActiveOutboundResourceAdapter.
* For example ActiveJmsResourceAdapter extends
* ActiveInboundResourceAdapter.
*/
// if (moduleName.equals(ConnectorConstants.DEFAULT_JMS_ADAPTER)) {
// Upgrade jms resource adapter, if necessary before starting the RA.
try {
JMSConfigListener jmsConfigListener = jmsConfigListenerProvider.get();
jmsConfigListener.setActiveResourceAdapter(this);
JmsRaUtil raUtil = new JmsRaUtil();
raUtil.upgradeIfNecessary();
} catch (Throwable t) {
// t.printStackTrace();
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "Cannot upgrade jmsra" + t.getMessage());
}
}
}
/**
* Loads RA configuration for MQ Resource adapter.
*
* @throws ConnectorRuntimeException in case of an exception.
*/
@Override
protected void loadRAConfiguration() throws ConnectorRuntimeException{
if (connectorRuntime.isServer()) {
// Check whether MQ has started up or not.
try {
setLifecycleProperties();
} catch (Exception e) {
throw new ConnectorRuntimeException(e.getMessage(), e);
}
setMdbContainerProperties();
setJmsServiceProperties(null);
setClusterRABeanProperties();
setAvailabilityProperties();
} else {
setAppClientRABeanProperties();
}
super.loadRAConfiguration();
postRAConfiguration();
}
@Override
public void destroy() {
try {
JmsService jmsService = getJmsService();
if (connectorRuntime.isServer() && grizzlyListenerInit && jmsService != null
&& EMBEDDED.equalsIgnoreCase(jmsService.getType())) {
GrizzlyService grizzlyService = null;
try {
grizzlyService = Globals.get(GrizzlyService.class);
} catch (MultiException rle) {
// if GrizzlyService was shut down already, skip removing the proxy.
}
if (grizzlyService != null) {
synchronized (grizzlyListeners) {
if (!grizzlyListeners.isEmpty()) {
String[] listeners = grizzlyListeners.toArray(new String[grizzlyListeners.size()]);
for (String listenerName : listeners) {
try {
grizzlyService.removeNetworkProxy(listenerName);
grizzlyListeners.remove(listenerName);
} catch (Exception e) {
LogHelper.log(_logger, Level.WARNING,
JMSLoggerInfo.SHUTDOWN_FAIL_GRIZZLY, e, listenerName);
}
}
}
}
}
grizzlyListenerInit = false;
}
} catch (Throwable th) {
if (_logger.isLoggable(Level.WARNING)) {
_logger.log(Level.WARNING, JMSLoggerInfo.SHUTDOWN_FAIL_JMSRA,
new Object[]{th.getMessage()});
}
throw new RuntimeException(th);
}
super.destroy();
}
public Set<String> getGrizzlyListeners() {
return grizzlyListeners;
}
/**
* Start Grizzly based JMS lazy listener, which is going to initialize
* JMS container on first request.
*/
public void initializeLazyListener(JmsService jmsService) {
if (jmsService != null) {
if (EMBEDDED.equalsIgnoreCase(jmsService.getType()) && !grizzlyListenerInit) {
GrizzlyService grizzlyService = Globals.get(GrizzlyService.class);
if (grizzlyService != null) {
List<JmsHost> jmsHosts = jmsService.getJmsHost();
for (JmsHost oneHost : jmsHosts) {
if (Boolean.parseBoolean(oneHost.getLazyInit()) && !doBind) {
String jmsHost = null;
if (oneHost.getHost() != null && "localhost".equals(oneHost.getHost())) {
jmsHost = "0.0.0.0";
} else {
jmsHost = oneHost.getHost();
}
NetworkListener dummy = new DummyNetworkListener();
dummy.setPort(oneHost.getPort());
dummy.setAddress(jmsHost);
dummy.setType("proxy");
dummy.setProtocol(JMS_SERVICE);
dummy.setTransport("tcp");
String name = GRIZZLY_PROXY_PREFIX + oneHost.getName();
dummy.setName(name);
synchronized (grizzlyListeners) {
grizzlyService.createNetworkProxy(dummy);
grizzlyListeners.add(name);
}
grizzlyListenerInit = true;
}
}
}
}
}
}
@Override
protected void startResourceAdapter(BootstrapContext bootstrapContext) throws ResourceAdapterInternalException {
try {
if (this.moduleName_.equals(ConnectorConstants.DEFAULT_JMS_ADAPTER)) {
if (connectorRuntime.isServer()) {
Domain domain = Globals.get(Domain.class);
ServerContext serverContext = Globals.get(ServerContext.class);
Server server = domain.getServerNamed(serverContext.getInstanceName());
JmsService jmsService = server.getConfig().getExtensionByType(JmsService.class);
initializeLazyListener(jmsService);
}
PrivilegedExceptionAction<Void> action = () -> {
resourceadapter_.start(bootStrapContextImpl);
return null;
};
AccessController.doPrivileged(action);
} else {
resourceadapter_.start(bootStrapContextImpl);
}
} catch (PrivilegedActionException ex) {
throw new ResourceAdapterInternalException(ex);
}
}
/**
* This is a HACK to remove the connection URL
* in the case of PE LOCAL/EMBEDDED before setting the properties
* to the RA. If this was not done, MQ RA incorrectly assumed
* that the passed in connection URL is one additional
* URL, apart from the default URL derived from brokerhost:brokerport
* and reported a PE connection url limitation.
*/
@Override
protected Set mergeRAConfiguration(ResourceAdapterConfig raConfig, List<Property> raConfigProps) {
// private void hackMergedProps(Set mergedProps) {
if (!(connectorRuntime.isServer())) {
return super.mergeRAConfiguration(raConfig, raConfigProps);
}
Set<ConnectorConfigProperty> mergedProps = super.mergeRAConfiguration(raConfig, raConfigProps);
String brokerType = null;
for (ConnectorConfigProperty element : mergedProps) {
if (element.getName().equals(ActiveJmsResourceAdapter.BROKERTYPE)) {
brokerType = element.getValue();
}
}
boolean cluster = false;
try {
cluster = isClustered();
} catch (Exception e) {
e.printStackTrace();
}
// hack is required only for nonclustered nonremote brokers.
if (!cluster && brokerType != null) {
if (brokerType.equals(ActiveJmsResourceAdapter.LOCAL)
|| brokerType.equals(ActiveJmsResourceAdapter.EMBEDDED)
|| brokerType.equals(ActiveJmsResourceAdapter.DIRECT)) {
for (Iterator<ConnectorConfigProperty> iter = mergedProps.iterator(); iter.hasNext();) {
ConnectorConfigProperty element = iter.next();
if (element.getName().equals(ActiveJmsResourceAdapter.CONNECTION_URL)) {
iter.remove();
}
}
}
}
return mergedProps;
}
//Overriding ActiveResourceAdapterImpl.setup() as a work around for
//this condition - connectionDefs_.length != 1
//Need to remove this once the original problem is fixed
@Override
public void setup() throws ConnectorRuntimeException {
//TODO NEED TO REMOVE ONCE THE ActiveResourceAdapterImpl.setup() is fixed
if (connectionDefs_ == null) {
throw new ConnectorRuntimeException("No Connection Defs defined in the RA.xml");
}
if (isServer() && !isSystemRar(moduleName_)) {
createAllConnectorResources();
}
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "Completed Active Resource adapter setup", moduleName_);
}
}
/**
* Set Availability related properties
* If JMS availability true set availability properties
* read configured pool information and set.
*/
private void setAvailabilityProperties() throws ConnectorRuntimeException {
if(!isClustered()) {
return;
}
try {
Domain domain = Globals.get(Domain.class);
ServerContext serverContext = Globals.get(ServerContext.class);
Server server = domain.getServerNamed(serverContext.getInstanceName());
JmsService jmsService = server.getConfig().getExtensionByType(JmsService.class);
if (jmsService.getType().equals(REMOTE)) {
// If REMOTE, the broker cluster instances already have
// been configured with the right properties.
return;
}
AvailabilityService as = server.getConfig().getAvailabilityService();
if (as == null) {
if (_logger.isLoggable(Level.FINE)) {
logFine("Availability Service is null. Not setting AvailabilityProperties.");
}
return;
}
boolean useMasterBroker = true;
if (as.getExtensionByType(JmsAvailability.class) != null
&& !MASTERBROKER.equalsIgnoreCase(as.getExtensionByType(JmsAvailability.class).getConfigStoreType())) {
useMasterBroker = false;
}
// jmsService.getUseMasterBroker() != null ?
// Boolean.valueOf(jmsService.getUseMasterBroker()) :true;
boolean isJmsAvailabilityEnabled = this.isJMSAvailabilityOn(as);
if (_logger.isLoggable(Level.FINE)) {
logFine("Setting AvailabilityProperties .. ");
}
if (!useMasterBroker || isJmsAvailabilityEnabled) {
// For conventional cluster of peer brokers and Enhanced Broker Cluster.
ConnectorDescriptor cd = getDescriptor();
String clusterName = getMQClusterName();
ConnectorConfigProperty envProp1 = new ConnectorConfigProperty(
CLUSTERID, clusterName, "Cluster Id", String.class.getName());
setProperty(cd, envProp1);
if (brokerInstanceName == null) {
brokerInstanceName = getBrokerInstanceName(jmsService);
}
ConnectorConfigProperty envProp2 = new ConnectorConfigProperty(
BROKERID, brokerInstanceName, "Broker Id", String.class.getName());
setProperty(cd, envProp2);
// Only if JMS availability is true - Enhanced Broker Cluster only.
if (isJmsAvailabilityEnabled) {
// Set HARequired as true - irrespective of whether it is REMOTE or
// LOCAL
ConnectorConfigProperty envProp3 = new ConnectorConfigProperty(
HAREQUIRED, "true", "HA Required", String.class.getName());
setProperty(cd, envProp3);
/*
* The broker has a property to control whether
* it starts in HA mode or not and that's represented on
* the RA by BrokerEnableHA.
* On the MQ Client connection side it is HARequired -
* this does not control the broker, it just is a client
* side requirement.
* So for AS EE, if BrokerType is LOCAL or EMBEDDED,
* and AS HA is enabled for JMS then both these must be
* set to true.
*/
ConnectorConfigProperty envProp4 = new ConnectorConfigProperty(
BROKERENABLEHA, "true", "BrokerEnableHA flag", "java.lang.Boolean");
setProperty(cd, envProp4);
String nodeHostName = domain.getNodeNamed(server.getNodeRef()).getNodeHost();
if (nodeHostName != null) {
ConnectorConfigProperty envProp5 = new ConnectorConfigProperty(
BROKERBINDADDRESS, nodeHostName, "Broker Bind Address", String.class.getName());
setProperty(cd, envProp5);
}
loadDBProperties(as.getExtensionByType(JmsAvailability.class), ClusterMode.ENHANCED);
} else {
// Conventional cluster of peer brokers
JmsAvailability jmsAvailability = as.getExtensionByType(JmsAvailability.class);
if ("jdbc".equals(jmsAvailability.getMessageStoreType())) {
loadDBProperties(jmsAvailability, ClusterMode.ENHANCED);
}
loadDBProperties(jmsAvailability, ClusterMode.CONVENTIONAL_OF_PEER_BROKERS);
}
} else {
// Conventional cluster with master broker.
if ("jdbc".equals(as.getExtensionByType(JmsAvailability.class).getMessageStoreType())) {
loadDBProperties(as.getExtensionByType(JmsAvailability.class),
ClusterMode.CONVENTIONAL_WITH_MASTER_BROKER);
}
}
} catch (Exception e) {
ConnectorRuntimeException crex = new ConnectorRuntimeException(e.getMessage());
throw (ConnectorRuntimeException)crex.initCause(e);
}
}
private void loadDBProperties(JmsAvailability jmsAvailability, ClusterMode clusterMode) {
String prefix = null;
if (ClusterMode.CONVENTIONAL_WITH_MASTER_BROKER == clusterMode) {
prefix = ENHANCED_CLUSTER_DB_PREFIX;
} else if (ClusterMode.CONVENTIONAL_OF_PEER_BROKERS == clusterMode) {
prefix = CONVENTIONAL_CLUSTER__OF_PEER_BROKERS_DB_PREFIX;
} else if (ClusterMode.ENHANCED == clusterMode) {
prefix = ENHANCED_CLUSTER_DB_PREFIX;
} else {
if (_logger.isLoggable(Level.FINE)) {
logFine("Unknown cluster mode: " + clusterMode.name() + ", imq DB properties are not set.");
}
return;
}
if (dbProps == null) {
dbProps = new Properties();
}
dbProps.setProperty("imq.cluster.clusterid", getMQClusterName());
dbProps.setProperty("imq.persist.store", jmsAvailability.getMessageStoreType());
if (ClusterMode.CONVENTIONAL_WITH_MASTER_BROKER == clusterMode) {
dbProps.setProperty("imq.cluster.nomasterbroker", "false");
} else {
dbProps.setProperty("imq.cluster.nomasterbroker", "true");
}
if (Boolean.valueOf(jmsAvailability.getAvailabilityEnabled()) == true
|| "jdbc".equals(jmsAvailability.getMessageStoreType())) {
dbProps.setProperty("imq.brokerid", getBrokerInstanceName(getJmsService()));
}
String dbVendor = jmsAvailability.getDbVendor();
String dbuser = jmsAvailability.getDbUsername();
String dbPassword = jmsAvailability.getDbPassword();
String dbJdbcUrl = jmsAvailability.getDbUrl();
dbProps.setProperty(prefix + "dbVendor", dbVendor);
String fullprefix = prefix + dbVendor + ".";
if (dbuser != null) {
dbProps.setProperty(fullprefix + "user", dbuser);
}
if (dbPassword != null) {
dbProps.setProperty(fullprefix + "password", dbPassword);
}
List dbprops = jmsAvailability.getProperty();
String propertyPrefix = fullprefix + "property.";
if (dbJdbcUrl != null) {
if ("derby".equals(dbVendor)) {
dbProps.setProperty(fullprefix + "opendburl", dbJdbcUrl);
} else {
dbProps.setProperty(propertyPrefix + "url", dbJdbcUrl);
}
}
for (Object obj : dbprops) {
Property prop = (Property) obj;
String key = prop.getName();
String value = prop.getValue();
// don't set a prefix if the property name is already prefixed with "imq."
if (key.startsWith("imq.")) {
dbProps.setProperty(key, value);
} else {
dbProps.setProperty(propertyPrefix + key, value);
}
}
}
/*
* Gets all the other [apart from serverlist] DataSource properties from
* the HADB JDBC connection pool.
*
private Properties getDSPropertiesFromThePool(JdbcConnectionPool jdbcConPool) {
Properties p = new Properties();
List elemProp = jdbcConPool.getProperty();
Set<String> excludeList = new HashSet<String>();
excludeList.add(DUSERNAME);
excludeList.add(DPASSWORD);
excludeList.add(DSERVERLIST);
for(Property e: elemProp) {
String propName = e.getAttributeValue("name");
if (!excludeList.contains(propName)) {
p.setProperty(propName, e.getAttributeValue("value"));
}
}
logFine("Additional DataSource properties from pool "
+ jdbcConPool.getName() + " are " + p);
return p;
}*/
/**
* Method to perform any post RA configuration action by derivative subclasses.
* For example, this method is used by <code>ActiveJMSResourceAdapter</code>
* to set unsupported javabean property types on its RA JavaBean runtime
* instance.
* @throws ConnectorRuntimeException
*/
protected void postRAConfiguration() throws ConnectorRuntimeException {
//Set all non-supported javabean property types in the JavaBean
try {
if (dbProps == null) {
dbProps = new Properties();
}
dbProps.setProperty("imq.cluster.dynamicChangeMasterBrokerEnabled", "true");
Method mthds = this.resourceadapter_.getClass().getMethod("setBrokerProps", Properties.class);
if (_logger.isLoggable(Level.FINE)) {
logFine("Setting property:" + DB_HADB_PROPS + "=" + dbProps.toString());
}
mthds.invoke(this.resourceadapter_, new Object[]{dbProps});
} catch (Exception e) {
ConnectorRuntimeException crex = new ConnectorRuntimeException(e.getMessage());
throw (ConnectorRuntimeException) crex.initCause(e);
}
}
//todo: enable this method
/* private String getPropertyFromPool(JdbcConnectionPool jdbcConPool,
String poolPropertyName) {
String poolPropertyValue = null;
if(jdbcConPool == null) {
return null;
}
ElementProperty[] props = jdbcConPool.getElementProperty();
for (int i = 0; i < props.length; i++) {
String name = props[i].getAttributeValue("name");
String value = props[i].getAttributeValue("value");
if (name.equalsIgnoreCase(poolPropertyName)) {
//if (name.equalsIgnoreCase("username")) {
poolPropertyValue = value;
}
}
logFine("ActiveJMSResourceAdapter :: got property " + poolPropertyName
+ "="+ poolPropertyValue);
return poolPropertyValue;
} */
private boolean isJMSAvailabilityOn(AvailabilityService as) {
if (as == null) {
return false;
}
JmsAvailability ja = as.getExtensionByType(JmsAvailability.class);
boolean jmsAvailability = false;
/*
* JMS Availability should be false if its not present in
* domain.xml,
*/
if (ja != null) {
jmsAvailability = Boolean.parseBoolean(ja.getAvailabilityEnabled());
}
_logger.log(Level.FINE, "JMS availability :: " + jmsAvailability);
return jmsAvailability;
}
/**
* Set MQ4.0 RA lifecycle properties
*/
private void setLifecycleProperties() throws Exception, ConnectorRuntimeException {
//ConfigContext ctx = ApplicationServer.getServerContext().getConfigContext();
//If PE:
//EMBEDDED/LOCAL goto jms-service, get defaultjmshost info and set
//accordingly
//if EE:
//EMBEDDED/LOCAL get this instance and cluster name, search for a
//jms-host wth this this name in jms-service gets its proeprties
//and set
//@siva As of now use default JMS host. As soon as changes for modifying EE
//cluster to LOCAL is brought in, change this to use system properties
//for EE to get port, host, adminusername, adminpassword.
//JmsService jmsService = ServerBeansFactory.getJmsServiceBean(ctx);
String defaultJmsHost = getJmsService().getDefaultJmsHost();
if (_logger.isLoggable(Level.FINE)) {
logFine("Default JMS Host :: " + defaultJmsHost);
}
JmsHost jmsHost = getJmsHost();
if (jmsHost != null) {//todo: && jmsHost.isEnabled()) {
JavaConfig javaConfig = Globals.get(JavaConfig.class);
String java_home = javaConfig.getJavaHome();
//Get broker type from JMS Service.
// String brokerType = jmsService.getType();
/*
* XXX: adjust the brokertype for the new DIRECT mode in 4.1
* uncomment the line below once we have an MQ integration
* that has DIRECT mode support
*/
String brokerType = adjustForDirectMode(getJmsService().getType());
String brokerPort = jmsHost.getPort();
brkrPort = brokerPort;
String adminUserName = jmsHost.getAdminUserName();
String adminPassword = JmsRaUtil.getUnAliasedPwd(jmsHost.getAdminPassword());
List jmsHostProps= getJmsService().getProperty();
String username = null;
String password = null;
if (jmsHostProps != null) {
for (Object jmsHostProp : jmsHostProps) {
Property jmsProp = (Property) jmsHostProp;
String propName = jmsProp.getName();
String propValue = jmsProp.getValue();
if ("user-name".equals(propName)) {
username = propValue;
} else if ("password".equals(propName)) {
password = propValue;
}
// Add more properties as and when you want.
}
}
if (_logger.isLoggable(Level.FINE)) {
logFine("Broker UserName = " + username);
}
createMQVarDirectoryIfNecessary();
String brokerVarDir = getMQVarDir();
String tmpString = getJmsService().getStartArgs();
if (tmpString == null) {
tmpString = "";
}
String brokerArgs = tmpString;
//XX: Extract the information from the optional properties.
List jmsProperties = getJmsService().getProperty();
List jmsHostProperties = jmsHost.getProperty();
Properties jmsServiceProp = listToProperties(jmsProperties);
Properties jmsHostProp = listToProperties(jmsHostProperties);
jmsServiceProp.putAll(jmsHostProp);
if (jmsServiceProp.size() > 0) {
if (dbProps == null) {
dbProps = new Properties();
}
dbProps.putAll(jmsServiceProp);
}
String brokerHomeDir = getBrokerHomeDir();
String brokerLibDir = getBrokerLibDir();
if (brokerInstanceName == null) {
brokerInstanceName = getBrokerInstanceName(getJmsService());
}
long brokerTimeOut = getBrokerTimeOut(getJmsService());
ConnectorDescriptor cd = getDescriptor();
ConnectorConfigProperty envProp1 = new ConnectorConfigProperty (
BROKERTYPE, brokerType, "Broker Type", String.class.getName());
setProperty(cd, envProp1);
ConnectorConfigProperty envProp2 = new ConnectorConfigProperty (
BROKERINSTANCENAME, brokerInstanceName ,
"Broker Instance Name", String.class.getName());
setProperty(cd, envProp2);
ConnectorConfigProperty envProp3 = new ConnectorConfigProperty (
BROKERPORT , brokerPort ,
"Broker Port", "java.lang.Integer");
setProperty(cd, envProp3);
ConnectorConfigProperty envProp4 = new ConnectorConfigProperty (
BROKERARGS , brokerArgs ,
"Broker Args", String.class.getName());
setProperty(cd, envProp4);
ConnectorConfigProperty envProp5 = new ConnectorConfigProperty (
BROKERHOMEDIR , brokerHomeDir ,
"Broker Home Dir", String.class.getName());
setProperty(cd, envProp5);
ConnectorConfigProperty envProp14 = new ConnectorConfigProperty (
BROKERLIBDIR , brokerLibDir ,
"Broker Lib Dir", String.class.getName());
setProperty(cd, envProp14);
ConnectorConfigProperty envProp6 = new ConnectorConfigProperty (
BROKERJAVADIR , java_home ,
"Broker Java Dir", String.class.getName());
setProperty(cd, envProp6);
ConnectorConfigProperty envProp7 = new ConnectorConfigProperty (
BROKERVARDIR , brokerVarDir ,
"Broker Var Dir", String.class.getName());
setProperty(cd, envProp7);
ConnectorConfigProperty envProp8 = new ConnectorConfigProperty (
BROKERSTARTTIMEOUT , "" + brokerTimeOut ,
"Broker Start Timeout", "java.lang.Integer");
setProperty(cd, envProp8);
ConnectorConfigProperty envProp9 = new ConnectorConfigProperty (
ADMINUSERNAME , adminUserName,
"Broker admin username", String.class.getName());
setProperty(cd, envProp9);
ConnectorConfigProperty envProp10 = new ConnectorConfigProperty (
ADMINPASSWORD , adminPassword ,
"Broker admin password", String.class.getName());
setProperty(cd, envProp10);
ConnectorConfigProperty envProp11 = new ConnectorConfigProperty (
USERNAME , username,
"Broker username", String.class.getName());
setProperty(cd, envProp11);
ConnectorConfigProperty envProp12 = new ConnectorConfigProperty (
PASSWORD , password,
"Broker password", String.class.getName());
setProperty(cd, envProp12);
}
}
private Properties listToProperties(List<Property> props) {
Properties properties = new Properties();
if (props != null) {
for (Property prop : props) {
String key = prop.getName();
String value = prop.getValue();
properties.setProperty(key, value);
}
}
return properties;
}
private String adjustForDirectMode(String brokerType) {
if (!isClustered() && brokerType.equals(EMBEDDED)) {
String revertToEmbedded = System.getProperty(REVERT_TO_EMBEDDED_PROPERTY);
if ((revertToEmbedded != null) && (revertToEmbedded.equals("true"))){
return EMBEDDED;
}
return DIRECT;
}
return brokerType;
}
private long getBrokerTimeOut(JmsService jmsService) {
//@@remove
long defaultTimeout = 30 * 1000; //30 seconds
long timeout = defaultTimeout;
String specifiedTimeOut = jmsService.getInitTimeoutInSeconds();
if (specifiedTimeOut != null) {
timeout = Integer.parseInt(specifiedTimeOut) * 1000L;
}
return timeout;
}
public static String getBrokerInstanceName(JmsService js) {
ServerEnvironmentImpl serverenv = Globals.get(ServerEnvironmentImpl.class);
Domain domain = Globals.get(Domain.class);
String asInstance = serverenv.getInstanceName();
String domainName = null;
if (isClustered()) {
Server server = domain.getServerNamed(asInstance);
domainName = server.getCluster().getName();
} else {
domainName = serverenv.getDomainName();
}
String s = getBrokerInstanceName(domainName, asInstance, js);
if (_logger.isLoggable(Level.FINE)) {
logFine("Got broker Instancename as " + s);
}
String converted = convertStringToValidMQIdentifier(s);
if (_logger.isLoggable(Level.FINE)) {
logFine("converted instance name " + converted);
}
return converted;
}
@Override