Skip to content

Commit 3dff1b3

Browse files
committed
SimpleAliasRegistry fully synchronizes registerAlias and removeAlias
Issue: SPR-16577 (cherry picked from commit 1b1a69a)
1 parent b1295d0 commit 3dff1b3

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

spring-core/src/main/java/org/springframework/core/SimpleAliasRegistry.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,23 +45,25 @@ public class SimpleAliasRegistry implements AliasRegistry {
4545
public void registerAlias(String name, String alias) {
4646
Assert.hasText(name, "'name' must not be empty");
4747
Assert.hasText(alias, "'alias' must not be empty");
48-
if (alias.equals(name)) {
49-
this.aliasMap.remove(alias);
50-
}
51-
else {
52-
String registeredName = this.aliasMap.get(alias);
53-
if (registeredName != null) {
54-
if (registeredName.equals(name)) {
55-
// An existing alias - no need to re-register
56-
return;
57-
}
58-
if (!allowAliasOverriding()) {
59-
throw new IllegalStateException("Cannot register alias '" + alias + "' for name '" +
60-
name + "': It is already registered for name '" + registeredName + "'.");
48+
synchronized (this.aliasMap) {
49+
if (alias.equals(name)) {
50+
this.aliasMap.remove(alias);
51+
}
52+
else {
53+
String registeredName = this.aliasMap.get(alias);
54+
if (registeredName != null) {
55+
if (registeredName.equals(name)) {
56+
// An existing alias - no need to re-register
57+
return;
58+
}
59+
if (!allowAliasOverriding()) {
60+
throw new IllegalStateException("Cannot register alias '" + alias + "' for name '" +
61+
name + "': It is already registered for name '" + registeredName + "'.");
62+
}
6163
}
64+
checkForAliasCircle(name, alias);
65+
this.aliasMap.put(alias, name);
6266
}
63-
checkForAliasCircle(name, alias);
64-
this.aliasMap.put(alias, name);
6567
}
6668
}
6769

@@ -92,9 +94,11 @@ public boolean hasAlias(String name, String alias) {
9294

9395
@Override
9496
public void removeAlias(String alias) {
95-
String name = this.aliasMap.remove(alias);
96-
if (name == null) {
97-
throw new IllegalStateException("No alias '" + alias + "' registered");
97+
synchronized (this.aliasMap) {
98+
String name = this.aliasMap.remove(alias);
99+
if (name == null) {
100+
throw new IllegalStateException("No alias '" + alias + "' registered");
101+
}
98102
}
99103
}
100104

0 commit comments

Comments
 (0)