Skip to content

Commit 084502a

Browse files
authored
Make brokerName immutable in RegionBroker (#1917)
The brokerName should come from BrokerService and should only be configured on first creation. This update changes RegionBroker so that it gets the name from the broker service during construction and verifies that it is not null. The other benefit of this is that BrokerService always validates the name has valid characters. This change also cleans up the name regex to get rid of unnecessary escapes and also adds some regex tests.
1 parent 3218266 commit 084502a

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

activemq-broker/src/main/java/org/apache/activemq/broker/BrokerService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,8 @@ public String getBrokerName() {
10551055
}
10561056

10571057

1058-
private static final String brokerNameReplacedCharsRegExp = "[^a-zA-Z0-9\\.\\_\\-\\:]";
1058+
// Matches a single character that is invalid in a broker name
1059+
private static final String INVALID_BROKER_NAME_CHAR_REG_EXP = "[^a-zA-Z0-9._\\-:]";
10591060

10601061
/**
10611062
* Sets the name of this broker; which must be unique in the network.
@@ -1064,9 +1065,9 @@ public void setBrokerName(String brokerName) {
10641065
if (brokerName == null) {
10651066
throw new NullPointerException("The broker name cannot be null");
10661067
}
1067-
String str = brokerName.replaceAll(brokerNameReplacedCharsRegExp, "_");
1068+
String str = brokerName.replaceAll(INVALID_BROKER_NAME_CHAR_REG_EXP, "_");
10681069
if (!str.equals(brokerName)) {
1069-
LOG.error("Broker Name: {} contained illegal characters matching regExp: {} - replaced with {}", brokerName, brokerNameReplacedCharsRegExp, str);
1070+
LOG.error("Broker Name: {} contained illegal characters matching regExp: {} - replaced with {}", brokerName, INVALID_BROKER_NAME_CHAR_REG_EXP, str);
10701071
}
10711072
this.brokerName = str.trim();
10721073
}
@@ -2408,7 +2409,6 @@ protected Broker createRegionBroker(DestinationInterceptor destinationIntercepto
24082409
}
24092410
destinationFactory.setRegionBroker(regionBroker);
24102411
regionBroker.setKeepDurableSubsActive(keepDurableSubsActive);
2411-
regionBroker.setBrokerName(getBrokerName());
24122412
regionBroker.getDestinationStatistics().setEnabled(enableStatistics);
24132413
regionBroker.setAllowTempAutoCreationOnSend(isAllowTempAutoCreationOnSend());
24142414
if (brokerId != null) {

activemq-broker/src/main/java/org/apache/activemq/broker/region/RegionBroker.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.List;
2525
import java.util.Locale;
2626
import java.util.Map;
27+
import java.util.Objects;
2728
import java.util.Set;
2829
import java.util.concurrent.ConcurrentHashMap;
2930
import java.util.concurrent.CopyOnWriteArrayList;
@@ -104,7 +105,7 @@ public class RegionBroker extends EmptyBroker {
104105

105106
private final LongSequenceGenerator sequenceGenerator = new LongSequenceGenerator();
106107
private BrokerId brokerId;
107-
private String brokerName;
108+
private final String brokerName;
108109
private final Map<String, ConnectionContext> clientIdSet = new HashMap<String, ConnectionContext>();
109110
private final DestinationInterceptor destinationInterceptor;
110111
private ConnectionContext adminConnectionContext;
@@ -138,7 +139,8 @@ public void run() {
138139

139140
public RegionBroker(BrokerService brokerService, TaskRunnerFactory taskRunnerFactory, SystemUsage memoryManager, DestinationFactory destinationFactory,
140141
DestinationInterceptor destinationInterceptor, Scheduler scheduler, ThreadPoolExecutor executor) throws IOException {
141-
this.brokerService = brokerService;
142+
this.brokerService = Objects.requireNonNull(brokerService);
143+
this.brokerName = Objects.requireNonNull(brokerService.getBrokerName(), "The broker name cannot be null");
142144
this.executor = executor;
143145
this.scheduler = scheduler;
144146
if (destinationFactory == null) {
@@ -564,20 +566,9 @@ public void setBrokerId(BrokerId brokerId) {
564566

565567
@Override
566568
public String getBrokerName() {
567-
if (brokerName == null) {
568-
try {
569-
brokerName = InetAddressUtil.getLocalHostName().toLowerCase(Locale.ENGLISH);
570-
} catch (Exception e) {
571-
brokerName = "localhost";
572-
}
573-
}
574569
return brokerName;
575570
}
576571

577-
public void setBrokerName(String brokerName) {
578-
this.brokerName = brokerName;
579-
}
580-
581572
public DestinationStatistics getDestinationStatistics() {
582573
return destinationStatistics;
583574
}

activemq-unit-tests/src/test/java/org/apache/activemq/broker/BrokerServiceTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,44 @@ public void testSystemUsage() {
9595
assertEquals( 1024L * 1024 * 1024 * 100, service.getSystemUsage().getStoreUsage().getLimit() );
9696
}
9797

98+
public void testSetBrokerNameInvalidChars() {
99+
final BrokerService brokerService = new BrokerService();
100+
101+
// All valid
102+
brokerService.setBrokerName("valid");
103+
assertEquals("valid", brokerService.getBrokerName());
104+
brokerService.setBrokerName("valid123");
105+
assertEquals("valid123", brokerService.getBrokerName());
106+
brokerService.setBrokerName("this_is_valid");
107+
assertEquals("this_is_valid", brokerService.getBrokerName());
108+
brokerService.setBrokerName("this_123_valid");
109+
assertEquals("this_123_valid", brokerService.getBrokerName());
110+
brokerService.setBrokerName("valid-name123");
111+
assertEquals("valid-name123", brokerService.getBrokerName());
112+
brokerService.setBrokerName("1235.6789");
113+
assertEquals("1235.6789", brokerService.getBrokerName());
114+
brokerService.setBrokerName("valid:123");
115+
assertEquals("valid:123", brokerService.getBrokerName());
116+
117+
// Test invalid names
118+
brokerService.setBrokerName("abc?bad");
119+
assertEquals("abc_bad", brokerService.getBrokerName());
120+
brokerService.setBrokerName("#");
121+
assertEquals("_", brokerService.getBrokerName());
122+
brokerService.setBrokerName("?");
123+
assertEquals("_", brokerService.getBrokerName());
124+
brokerService.setBrokerName("invalid%");
125+
assertEquals("invalid_", brokerService.getBrokerName());
126+
brokerService.setBrokerName("\\");
127+
assertEquals("_", brokerService.getBrokerName());
128+
brokerService.setBrokerName("<>");
129+
assertEquals("__", brokerService.getBrokerName());
130+
brokerService.setBrokerName("abc=");
131+
assertEquals("abc_", brokerService.getBrokerName());
132+
brokerService.setBrokerName("name:abc=?bad");
133+
assertEquals("name:abc__bad", brokerService.getBrokerName());
134+
}
135+
98136
/** // AMQ-9239 FIXME: byte-buddy module opens
99137
@Test
100138
public void testLargeFileSystem() throws Exception {

0 commit comments

Comments
 (0)