-
Notifications
You must be signed in to change notification settings - Fork 920
/
BrokerService.java
821 lines (663 loc) · 25.4 KB
/
BrokerService.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.broker;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.activemq.ActiveMQConnectionMetaData;
import org.apache.activemq.Service;
import org.apache.activemq.artemis.core.remoting.impl.netty.TransportConstants;
import org.apache.activemq.broker.artemiswrapper.ArtemisBrokerWrapper;
import org.apache.activemq.broker.jmx.BrokerView;
import org.apache.activemq.broker.jmx.ManagementContext;
import org.apache.activemq.broker.region.Destination;
import org.apache.activemq.broker.region.DestinationInterceptor;
import org.apache.activemq.broker.region.policy.PolicyMap;
import org.apache.activemq.broker.region.policy.RegionBrokerProxy;
import org.apache.activemq.broker.scheduler.JobSchedulerStore;
import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.command.BrokerId;
import org.apache.activemq.network.ConnectionFilter;
import org.apache.activemq.network.DiscoveryNetworkConnector;
import org.apache.activemq.network.NetworkConnector;
import org.apache.activemq.network.jms.JmsConnector;
import org.apache.activemq.proxy.ProxyConnector;
import org.apache.activemq.security.MessageAuthorizationPolicy;
import org.apache.activemq.spring.SpringSslContext;
import org.apache.activemq.store.PListStore;
import org.apache.activemq.store.PersistenceAdapter;
import org.apache.activemq.store.PersistenceAdapterFactory;
import org.apache.activemq.thread.TaskRunnerFactory;
import org.apache.activemq.transport.TransportServer;
import org.apache.activemq.usage.SystemUsage;
import org.apache.activemq.util.IOExceptionHandler;
import org.apache.activemq.util.IOHelper;
import org.apache.activemq.util.ServiceStopper;
import org.junit.rules.TemporaryFolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Manages the life-cycle of an ActiveMQ Broker. A BrokerService consists of a
* number of transport connectors, network connectors and a bunch of properties
* which can be used to configure the broker as its lazily created.
*/
public class BrokerService implements Service {
public static final String DEFAULT_PORT = "61616";
public static final AtomicInteger RANDOM_PORT_BASE = new AtomicInteger(51616);
public static final String DEFAULT_BROKER_NAME = "localhost";
public static final String BROKER_VERSION;
public static final int DEFAULT_MAX_FILE_LENGTH = 1024 * 1024 * 32;
public static final long DEFAULT_START_TIMEOUT = 600000L;
public static boolean disableWrapper = false;
private SslContext sslContext;
private static final Logger LOG = LoggerFactory.getLogger(BrokerService.class);
@SuppressWarnings("unused")
private static final long serialVersionUID = 7353129142305630237L;
private String brokerName = DEFAULT_BROKER_NAME;
private Broker broker;
private BrokerId brokerId;
private Throwable startException = null;
private boolean startAsync = false;
public Set<ConnectorInfo> extraConnectors = new HashSet<>();
private List<TransportConnector> transportConnectors = new ArrayList<>();
private File dataDirectoryFile;
private PolicyMap destinationPolicy;
private SystemUsage systemUsage;
private final List<NetworkConnector> networkConnectors = new CopyOnWriteArrayList<>();
private TemporaryFolder tmpfolder;
public static WeakHashMap<Broker, Exception> map = new WeakHashMap<>();
static {
InputStream in;
String version = null;
if ((in = BrokerService.class.getResourceAsStream("/org/apache/activemq/version.txt")) != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
try {
version = reader.readLine();
} catch (Exception e) {
}
}
BROKER_VERSION = version;
}
@Override
public String toString() {
return "BrokerService[" + getBrokerName() + "]" + super.toString();
}
private String getBrokerVersion() {
String version = ActiveMQConnectionMetaData.PROVIDER_VERSION;
if (version == null) {
version = BROKER_VERSION;
}
return version;
}
@Override
public void start() throws Exception {
File targetTmp = new File("./target/tmp");
targetTmp.mkdirs();
tmpfolder = new TemporaryFolder(targetTmp);
tmpfolder.create();
Exception e = new Exception();
startBroker(startAsync);
map.put(broker, e);
}
private void startBroker(boolean async) throws Exception {
if (async) {
new Thread("Broker Starting Thread") {
@Override
public void run() {
try {
doStartBroker();
} catch (Throwable t) {
startException = t;
}
}
}.start();
} else {
doStartBroker();
}
}
private void doStartBroker() throws Exception {
if (startException != null) {
return;
}
broker = getBroker();
brokerId = broker.getBrokerId();
LOG.info("Apache ActiveMQ Artemis Wrapper {} ({}, {}) is starting", new Object[]{getBrokerVersion(), getBrokerName(), brokerId});
try {
broker.start();
} catch (Exception e) {
throw e;
} catch (Throwable t) {
throw new Exception(t);
}
LOG.info("Apache ActiveMQ Artemis Wrapper {} ({}, {}) started", new Object[]{getBrokerVersion(), getBrokerName(), brokerId});
LOG.info("For help or more information please see: http://activemq.apache.org");
}
@Override
public void stop() throws Exception {
System.out.println("broker is: " + broker);
LOG.info("Apache ActiveMQ Artemis{} ({}, {}) is shutting down", new Object[]{getBrokerVersion(), getBrokerName(), brokerId});
if (broker != null) {
broker.stop();
broker = null;
}
tmpfolder.delete();
LOG.info("Apache ActiveMQ Artemis {} ({}, {}) is shutdown", new Object[]{getBrokerVersion(), getBrokerName(), brokerId});
}
// Properties
// -------------------------------------------------------------------------
public Broker getBroker() throws Exception {
if (broker == null) {
broker = createBroker(tmpfolder.getRoot());
}
return broker;
}
public String getBrokerName() {
return brokerName;
}
public void setBrokerName(String brokerName) {
if (brokerName == null) {
throw new NullPointerException("The broker name cannot be null");
}
String str = brokerName.replaceAll("[^a-zA-Z0-9\\.\\_\\-\\:]", "_");
if (!str.equals(brokerName)) {
LOG.error("Broker Name: {} contained illegal characters - replaced with {}", brokerName, str);
}
this.brokerName = str.trim();
}
protected Broker createBroker(File temporaryFile) throws Exception {
broker = createBrokerWrapper(temporaryFile);
return broker;
}
private Broker createBrokerWrapper(File temporaryFile) {
return new ArtemisBrokerWrapper(this, temporaryFile);
}
public void makeSureDestinationExists(ActiveMQDestination activemqDestination) throws Exception {
ArtemisBrokerWrapper hqBroker = (ArtemisBrokerWrapper) this.broker;
//it can be null
if (activemqDestination == null) {
return;
}
if (activemqDestination.isQueue()) {
String qname = activemqDestination.getPhysicalName();
System.out.println("physical name: " + qname);
hqBroker.makeSureQueueExists(qname);
}
}
//below are methods called directly by tests
//we don't actually implement many of these for now,
//just to make test compile pass.
public Broker getRegionBroker() {
try {
return RegionBrokerProxy.newRegionBroker((ArtemisBrokerWrapper) getBroker());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void setPersistenceAdapter(PersistenceAdapter persistenceAdapter) throws IOException {
}
public File getDataDirectoryFile() {
if (dataDirectoryFile == null) {
dataDirectoryFile = new File(IOHelper.getDefaultDataDirectory());
}
return dataDirectoryFile;
}
public File getBrokerDataDirectory() {
String brokerDir = getBrokerName();
return new File(getDataDirectoryFile(), brokerDir);
}
public PersistenceAdapter getPersistenceAdapter() throws IOException {
return null;
}
public void waitUntilStopped() {
}
public boolean waitUntilStarted() {
return true;
}
public void setDestinationPolicy(PolicyMap policyMap) {
this.destinationPolicy = policyMap;
}
public void setDeleteAllMessagesOnStartup(boolean deletePersistentMessagesOnStartup) {
}
public void setUseJmx(boolean useJmx) {
}
public ManagementContext getManagementContext() {
return null;
}
public BrokerView getAdminView() throws Exception {
return null;
}
public List<TransportConnector> getTransportConnectors() {
return transportConnectors;
}
public TransportConnector addConnector(String bindAddress) throws Exception {
return addConnector(new URI(bindAddress));
}
public void setIoExceptionHandler(IOExceptionHandler ioExceptionHandler) {
}
public void setPersistent(boolean persistent) {
}
public boolean isSlave() {
return false;
}
public Destination getDestination(ActiveMQDestination destination) throws Exception {
return null;
}
public void setAllowTempAutoCreationOnSend(boolean allowTempAutoCreationOnSend) {
}
public void setDedicatedTaskRunner(boolean dedicatedTaskRunner) {
}
public void setAdvisorySupport(boolean advisorySupport) {
}
public void setUseShutdownHook(boolean useShutdownHook) {
}
public void deleteAllMessages() throws IOException {
}
public Service[] getServices() {
return null;
}
public void setPopulateUserNameInMBeans(boolean value) {
}
public void setDestinations(ActiveMQDestination[] destinations) {
}
public URI getVmConnectorURI() {
try {
URI substituteUri = new URI("tcp://localhost:61616");
return substituteUri;
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}
public SystemUsage getSystemUsage() {
if (systemUsage == null) {
systemUsage = new SystemUsage();
}
return systemUsage;
}
public synchronized PListStore getTempDataStore() {
return null;
}
public void setJmsBridgeConnectors(JmsConnector[] jmsConnectors) {
}
public void setDestinationInterceptors(DestinationInterceptor[] destinationInterceptors) {
}
public SslContext getSslContext() {
return this.sslContext;
}
public void setDataDirectory(String dataDirectory) {
}
public void setPlugins(BrokerPlugin[] plugins) {
}
public void setKeepDurableSubsActive(boolean keepDurableSubsActive) {
}
public TransportConnector getConnectorByName(String connectorName) {
return null;
}
public TransportConnector addConnector(TransportConnector connector) throws Exception {
return connector;
}
public void setEnableStatistics(boolean enableStatistics) {
}
public void setSystemUsage(SystemUsage memoryManager) {
this.systemUsage = memoryManager;
}
public void setManagementContext(ManagementContext managementContext) {
}
public void setSchedulerDirectoryFile(File schedulerDirectory) {
}
public NetworkConnector addNetworkConnector(String discoveryAddress) throws Exception {
return addNetworkConnector(new URI(discoveryAddress));
}
public NetworkConnector addNetworkConnector(URI discoveryAddress) throws Exception {
NetworkConnector connector = new DiscoveryNetworkConnector(discoveryAddress);
return addNetworkConnector(connector);
}
public List<NetworkConnector> getNetworkConnectors() {
return this.networkConnectors;
}
public void setSchedulerSupport(boolean schedulerSupport) {
}
public void setPopulateJMSXUserID(boolean populateJMSXUserID) {
}
public boolean isUseJmx() {
return false;
}
public boolean isPersistent() {
return false;
}
public TransportConnector getTransportConnectorByScheme(String scheme) {
return null;
}
public TaskRunnerFactory getTaskRunnerFactory() {
return null;
}
public boolean isStarted() {
if (broker == null)
return false;
return !broker.isStopped();
}
public ProxyConnector addProxyConnector(ProxyConnector connector) throws Exception {
return connector;
}
public void setDataDirectoryFile(File dataDirectoryFile) {
this.dataDirectoryFile = dataDirectoryFile;
}
public PolicyMap getDestinationPolicy() {
return this.destinationPolicy;
}
public void setTransportConnectorURIs(String[] transportConnectorURIs) {
}
public boolean isPopulateJMSXUserID() {
return false;
}
public NetworkConnector getNetworkConnectorByName(String connectorName) {
return null;
}
public boolean removeNetworkConnector(NetworkConnector connector) {
return true;
}
public void setTransportConnectors(List<TransportConnector> transportConnectors) throws Exception {
this.transportConnectors = transportConnectors;
for (TransportConnector connector : transportConnectors) {
if (sslContext instanceof SpringSslContext) {
this.extraConnectors.add(new ConnectorInfo(connector.getUri(), (SpringSslContext) sslContext));
} else {
this.extraConnectors.add(new ConnectorInfo(connector.getUri()));
}
}
}
public NetworkConnector addNetworkConnector(NetworkConnector connector) throws Exception {
connector.setBrokerService(this);
System.out.println("------------------------ this broker uri: " + this.getConnectURI());
connector.setLocalUri(this.getConnectURI());
// Set a connection filter so that the connector does not establish loop
// back connections.
connector.setConnectionFilter(new ConnectionFilter() {
@Override
public boolean connectTo(URI location) {
List<TransportConnector> transportConnectors = getTransportConnectors();
for (Iterator<TransportConnector> iter = transportConnectors.iterator(); iter.hasNext(); ) {
try {
TransportConnector tc = iter.next();
if (location.equals(tc.getConnectUri())) {
return false;
}
} catch (Throwable e) {
}
}
return true;
}
});
networkConnectors.add(connector);
return connector;
}
public void setTempDataStore(PListStore tempDataStore) {
}
public void setJobSchedulerStore(JobSchedulerStore jobSchedulerStore) {
}
public ObjectName getBrokerObjectName() throws MalformedObjectNameException {
return null;
}
public TransportConnector addConnector(URI bindAddress) throws Exception {
Integer port = bindAddress.getPort();
String host = bindAddress.getHost();
FakeTransportConnector connector = null;
host = (host == null || host.length() == 0) ? "localhost" : host;
if ("0.0.0.0".equals(host)) {
host = "localhost";
}
if (port == 0) {
//In actual impl in amq5, after connector has been added the socket
//is bound already. This means in case of 0 port uri, the random
//port is available after this call. With artemis wrapper however
//the real binding happens during broker start. To work around this
//we use manually calculated port for that.
port = getPseudoRandomPort();
}
bindAddress = new URI(bindAddress.getScheme(), bindAddress.getUserInfo(), host, port, bindAddress.getPath(), bindAddress.getQuery(), bindAddress.getFragment());
connector = new FakeTransportConnector(bindAddress);
this.transportConnectors.add(connector);
if (sslContext instanceof SpringSslContext) {
this.extraConnectors.add(new ConnectorInfo(bindAddress, (SpringSslContext) sslContext));
} else {
this.extraConnectors.add(new ConnectorInfo(bindAddress));
}
return connector;
}
private static int getPseudoRandomPort() {
int currentRandomPort = RANDOM_PORT_BASE.getAndIncrement();
int maxTry = 20;
while (!checkPort(currentRandomPort)) {
currentRandomPort = RANDOM_PORT_BASE.getAndIncrement();
System.out.println("for port: " + currentRandomPort);
maxTry--;
if (maxTry == 0) {
LOG.error("Too many port used");
break;
}
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
}
}
return currentRandomPort;
}
public static boolean checkPort(final int port) {
ServerSocket ssocket = null;
try {
ssocket = new ServerSocket(port);
} catch (Exception e) {
LOG.info("port " + port + " is being used.");
return false;
} finally {
if (ssocket != null) {
try {
ssocket.close();
} catch (IOException e) {
}
}
}
return true;
}
public void setCacheTempDestinations(boolean cacheTempDestinations) {
}
public void setOfflineDurableSubscriberTimeout(long offlineDurableSubscriberTimeout) {
}
public void setOfflineDurableSubscriberTaskSchedule(long offlineDurableSubscriberTaskSchedule) {
}
public boolean isStopped() {
return broker != null ? broker.isStopped() : true;
}
public void setBrokerId(String brokerId) {
}
public BrokerPlugin[] getPlugins() {
return null;
}
public void stopAllConnectors(ServiceStopper stopper) {
}
public void setMessageAuthorizationPolicy(MessageAuthorizationPolicy messageAuthorizationPolicy) {
}
public void setNetworkConnectorStartAsync(boolean networkConnectorStartAsync) {
}
public boolean isRestartAllowed() {
return true;
}
public void setTaskRunnerFactory(TaskRunnerFactory taskRunnerFactory) {
}
public void start(boolean force) throws Exception {
this.start();
}
public void setMonitorConnectionSplits(boolean monitorConnectionSplits) {
}
public void setUseMirroredQueues(boolean useMirroredQueues) {
}
public File getTmpDataDirectory() {
return null;
}
public boolean isUseShutdownHook() {
return true;
}
public boolean isDeleteAllMessagesOnStartup() {
return false;
}
public void setUseVirtualTopics(boolean useVirtualTopics) {
}
public boolean isUseLoggingForShutdownErrors() {
return true;
}
public TransportConnector addConnector(TransportServer transport) throws Exception {
return null;
}
public synchronized JobSchedulerStore getJobSchedulerStore() {
return null;
}
public boolean removeConnector(TransportConnector connector) throws Exception {
return true;
}
public ConnectionContext getAdminConnectionContext() throws Exception {
return null;
}
public void setUseAuthenticatedPrincipalForJMSXUserID(boolean useAuthenticatedPrincipalForJMSXUserID) {
}
public void setSchedulePeriodForDestinationPurge(int schedulePeriodForDestinationPurge) {
}
public void setMbeanInvocationTimeout(long mbeanInvocationTimeout) {
}
public void setNetworkConnectors(List<?> networkConnectors) throws Exception {
}
public void removeDestination(ActiveMQDestination destination) throws Exception {
}
public void setMaxPurgedDestinationsPerSweep(int maxPurgedDestinationsPerSweep) {
}
public void setBrokerObjectName(ObjectName brokerObjectName) {
}
public Map<String, String> getTransportConnectorURIsAsMap() {
return null;
}
public void setSslContext(SslContext sslContext) {
this.sslContext = sslContext;
}
public void setPersistenceFactory(PersistenceAdapterFactory persistenceFactory) {
}
protected TransportConnector createTransportConnector(URI brokerURI) throws Exception {
return null;
}
public String getDefaultUri() {
return "tcp://localhost:61616";
}
public static boolean checkStopped() {
boolean runningBrokers = false;
for (Map.Entry<Broker, Exception> brokerExceptionEntry : map.entrySet()) {
Broker b = brokerExceptionEntry.getKey();
if (!b.isStopped()) {
try {
b.stop();
} catch (Exception e) {
e.printStackTrace();
}
brokerExceptionEntry.getValue().printStackTrace();
runningBrokers = true;
}
}
map.clear();
return runningBrokers;
}
public URI getConnectURI() {
URI uri = null;
try {
if (this.extraConnectors.size() > 0) {
ConnectorInfo info = extraConnectors.iterator().next();
Integer port = info.uri.getPort();
String schema = info.ssl ? "ssl" : "tcp";
uri = new URI(schema + "://localhost:" + port);
} else {
uri = new URI(this.getDefaultUri());
}
} catch (URISyntaxException e) {
//ignore
}
return uri;
}
public static class ConnectorInfo {
public static final String defaultKeyStore = "server.keystore";
public static final String defaultKeyStorePassword = "password";
public static final String defaultKeyStoreType = "jks";
public static final String defaultTrustStore = "client.keystore";
public static final String defaultTrustStorePassword = "password";
public static final String defaultTrustStoreType = "jks";
public URI uri;
public boolean ssl;
public boolean clientAuth;
public ConnectorInfo(URI bindAddress) throws URISyntaxException {
this(bindAddress, null);
}
//bindAddress must be Artemis compliant, except
//scheme
public ConnectorInfo(URI bindAddress, SpringSslContext context) throws URISyntaxException {
Integer port = bindAddress.getPort();
String host = bindAddress.getHost();
this.ssl = "ssl".equals(bindAddress.getScheme());
host = (host == null || host.length() == 0) ? "localhost" : host;
if ("0.0.0.0".equals(host)) {
host = "localhost";
}
if (port == 0) {
port = getPseudoRandomPort();
}
String query = bindAddress.getQuery();
if (!ssl || query != null && query.contains(TransportConstants.SSL_ENABLED_PROP_NAME)) {
//it means the uri is already configured ssl
uri = new URI("tcp", bindAddress.getUserInfo(), host, port, bindAddress.getPath(), bindAddress.getQuery(), bindAddress.getFragment());
} else {
String baseUri = "tcp://" + host + ":" + port + "?" + TransportConstants.SSL_ENABLED_PROP_NAME + "=true&" + TransportConstants.KEYSTORE_PATH_PROP_NAME + "=" + (context == null ? defaultKeyStore : context.getKeyStore()) + "&" + TransportConstants.KEYSTORE_PASSWORD_PROP_NAME + "=" + (context == null ? defaultKeyStorePassword : context.getKeyStorePassword()) + "&" + TransportConstants.KEYSTORE_TYPE_PROP_NAME + "=" + (context == null ? defaultKeyStoreType : context.getKeyStoreType());
if (clientAuth) {
baseUri = baseUri + "&" + TransportConstants.NEED_CLIENT_AUTH_PROP_NAME + "=true" + "&" + TransportConstants.TRUSTSTORE_PATH_PROP_NAME + "=" + (context == null ? defaultTrustStore : context.getTrustStore()) + "&" + TransportConstants.TRUSTSTORE_PASSWORD_PROP_NAME + "=" + (context == null ? defaultTrustStorePassword : context.getTrustStorePassword()) + "&" + TransportConstants.TRUSTSTORE_TYPE_PROP_NAME + "=" + (context == null ? defaultTrustStoreType : context.getTrustStoreType());
}
uri = new URI(baseUri);
}
System.out.println("server uri:::::::::::: " + uri.toString());
}
@Override
public int hashCode() {
return uri.getPort();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof ConnectorInfo) {
return uri.getPort() == ((ConnectorInfo) obj).uri.getPort();
}
return false;
}
}
}