Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle warning regarding double locking #1228

Merged
merged 10 commits into from
Aug 8, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class TransformationRuleEngine implements RuleEngine {
private String ruleDefinitionsFileName;
final List<TransformationRule> rules = new ArrayList<>();

volatile boolean rulesLoaded = false;
private static final TransformationRuleEngine INSTANCE = new TransformationRuleEngine();

@Inject Logger logger;
Expand All @@ -32,14 +32,14 @@ private TransformationRuleEngine() {}
@Override
public void unloadRules() {
rules.clear();
rulesLoaded = false;
}

@Override
public void ensureRulesLoaded() throws RuleLoaderException {
// Double-checked locking - needed to protect from excessive sync locks
if (rules.isEmpty()) {
synchronized (this) {
if (rules.isEmpty()) {
if (!rulesLoaded) {
synchronized (rules) {
if (!rulesLoaded) {
InputStream resourceStream =
getClass()
.getClassLoader()
Expand All @@ -51,7 +51,8 @@ public void ensureRulesLoaded() throws RuleLoaderException {
}
List<TransformationRule> parsedRules =
ruleLoader.loadRules(resourceStream, new TypeReference<>() {});
this.rules.addAll(parsedRules);
rules.addAll(parsedRules);
rulesLoaded = true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class ValidationRuleEngine implements RuleEngine {
private String ruleDefinitionsFileName;
final List<ValidationRule> rules = new ArrayList<>();

volatile boolean rulesLoaded = false;
private static final ValidationRuleEngine INSTANCE = new ValidationRuleEngine();

@Inject Logger logger;
Expand All @@ -32,14 +32,14 @@ private ValidationRuleEngine() {}
@Override
public void unloadRules() {
rules.clear();
rulesLoaded = false;
}

@Override
public void ensureRulesLoaded() throws RuleLoaderException {
// Double-checked locking - needed to protect from excessive sync locks
if (rules.isEmpty()) {
synchronized (this) {
if (rules.isEmpty()) {
if (!rulesLoaded) {
synchronized (rules) {
if (!rulesLoaded) {
InputStream resourceStream =
getClass()
.getClassLoader()
Expand All @@ -51,7 +51,8 @@ public void ensureRulesLoaded() throws RuleLoaderException {
}
List<ValidationRule> parsedRules =
ruleLoader.loadRules(resourceStream, new TypeReference<>() {});
this.rules.addAll(parsedRules);
rules.addAll(parsedRules);
rulesLoaded = true;
}
}
}
Expand Down