Skip to content

Commit 3bb0397

Browse files
committed
Add locking on cachedTask for DSL effector
1 parent 8efc762 commit 3bb0397

1 file changed

Lines changed: 25 additions & 10 deletions

File tree

  • camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods

camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -539,12 +539,14 @@ protected static class ExecuteEffector extends BrooklynDslDeferredSupplier<Objec
539539
private final Map<String, ?> args;
540540
private final List<? extends Object> argList;
541541
private Task<?> cachedTask;
542+
542543
public ExecuteEffector(DslComponent component, String effectorName, Map<String, ?> args) {
543544
this.component = Preconditions.checkNotNull(component);
544545
this.effectorName = effectorName;
545546
this.args = args;
546547
this.argList = null;
547548
}
549+
548550
public ExecuteEffector(DslComponent component, String effectorName, List<? extends Object> args) {
549551
this.component = Preconditions.checkNotNull(component);
550552
this.effectorName = effectorName;
@@ -560,20 +562,26 @@ public Task<Object> newTask() {
560562
if (targetEffector.isAbsentOrNull()) {
561563
throw new IllegalArgumentException("Effector " + effectorName + " not found on entity: " + targetEntity);
562564
}
563-
if (cachedTask == null) {
564-
if (argList == null) {
565-
cachedTask = Entities.invokeEffector(targetEntity, targetEntity, targetEffector.get(), args);
566-
} else {
567-
cachedTask = invokeWithDeferredArgs(targetEntity, targetEffector.get(), argList);
565+
synchronized (this) {
566+
if (cachedTask == null) {
567+
if (argList == null) {
568+
cachedTask = Entities.invokeEffector(targetEntity, targetEntity, targetEffector.get(), args);
569+
} else {
570+
cachedTask = invokeWithDeferredArgs(targetEntity, targetEffector.get(), argList);
571+
}
568572
}
569573
}
570574
return (Task<Object>) cachedTask;
571575
}
576+
572577
private Task<Object> invokeWithDeferredArgs(final Entity targetEntity, final Effector<?> targetEffector, final List<? extends Object> args) {
573578
List<TaskAdaptable<Object>> taskArgs = Lists.newArrayList();
574579
for (Object arg : args) {
575-
if (arg instanceof TaskAdaptable) taskArgs.add((TaskAdaptable<Object>) arg);
576-
else if (arg instanceof TaskFactory) taskArgs.add(((TaskFactory<TaskAdaptable<Object>>) arg).newTask());
580+
if (arg instanceof TaskAdaptable) {
581+
taskArgs.add((TaskAdaptable<Object>) arg);
582+
} else if (arg instanceof TaskFactory) {
583+
taskArgs.add(((TaskFactory<TaskAdaptable<Object>>) arg).newTask());
584+
}
577585
}
578586

579587
return DependentConfiguration.transformMultiple(
@@ -585,20 +593,26 @@ public Object apply(List<Object> input) {
585593
Object[] vv = new Object[args.size()];
586594
int i=0;
587595
for (Object arg : args) {
588-
if (arg instanceof TaskAdaptable || arg instanceof TaskFactory) vv[i] = tri.next();
589-
else if (arg instanceof DeferredSupplier) vv[i] = ((DeferredSupplier<?>) arg).get();
590-
else vv[i] = arg;
596+
if (arg instanceof TaskAdaptable || arg instanceof TaskFactory) {
597+
vv[i] = tri.next();
598+
} else if (arg instanceof DeferredSupplier) {
599+
vv[i] = ((DeferredSupplier<?>) arg).get();
600+
} else {
601+
vv[i] = arg;
602+
}
591603
i++;
592604
}
593605
return Entities.invokeEffectorWithArgs(targetEntity, targetEntity, targetEffector, vv);
594606
}
595607
},
596608
taskArgs);
597609
}
610+
598611
@Override
599612
public int hashCode() {
600613
return Objects.hashCode(component, effectorName);
601614
}
615+
602616
@Override
603617
public boolean equals(Object obj) {
604618
if (this == obj) return true;
@@ -607,6 +621,7 @@ public boolean equals(Object obj) {
607621
return Objects.equal(this.component, that.component) &&
608622
Objects.equal(this.effectorName, that.effectorName);
609623
}
624+
610625
@Override
611626
public String toString() {
612627
return (component.scope==Scope.THIS ? "" : component.toString()+".") +

0 commit comments

Comments
 (0)