Repository: brooklyn-server Updated Branches: refs/heads/master e19137251 -> 770ca3498
BROOKLYN-568: fix NPE on SshFeed rebind Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/2f0bd261 Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/2f0bd261 Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/2f0bd261 Branch: refs/heads/master Commit: 2f0bd261cfab06cb9b26c2242750fd4bfadc3764 Parents: e191372 Author: Aled Sage <aled.s...@gmail.com> Authored: Thu Dec 7 15:54:20 2017 +0000 Committer: Aled Sage <aled.s...@gmail.com> Committed: Thu Dec 7 15:54:20 2017 +0000 ---------------------------------------------------------------------- .../core/sensor/ssh/SshCommandSensor.java | 33 +++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2f0bd261/core/src/main/java/org/apache/brooklyn/core/sensor/ssh/SshCommandSensor.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/sensor/ssh/SshCommandSensor.java b/core/src/main/java/org/apache/brooklyn/core/sensor/ssh/SshCommandSensor.java index a5052df..2fd3f3f 100644 --- a/core/src/main/java/org/apache/brooklyn/core/sensor/ssh/SshCommandSensor.java +++ b/core/src/main/java/org/apache/brooklyn/core/sensor/ssh/SshCommandSensor.java @@ -22,17 +22,6 @@ import java.util.Map; import java.util.concurrent.ExecutionException; import org.apache.brooklyn.api.entity.Entity; -import org.apache.brooklyn.feed.CommandPollConfig; -import org.apache.brooklyn.feed.ssh.SshFeed; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.annotations.Beta; -import com.google.common.base.Function; -import com.google.common.base.Functions; -import com.google.common.base.Preconditions; -import com.google.common.base.Supplier; - import org.apache.brooklyn.api.entity.EntityInitializer; import org.apache.brooklyn.api.entity.EntityLocal; import org.apache.brooklyn.config.ConfigKey; @@ -43,8 +32,8 @@ import org.apache.brooklyn.core.entity.BrooklynConfigKeys; import org.apache.brooklyn.core.entity.EntityInitializers; import org.apache.brooklyn.core.entity.EntityInternal; import org.apache.brooklyn.core.sensor.http.HttpRequestSensor; -import org.apache.brooklyn.feed.AbstractCommandFeed; -import org.apache.brooklyn.feed.ssh.SshPollConfig; +import org.apache.brooklyn.feed.CommandPollConfig; +import org.apache.brooklyn.feed.ssh.SshFeed; import org.apache.brooklyn.feed.ssh.SshValueFunctions; import org.apache.brooklyn.util.collections.MutableMap; import org.apache.brooklyn.util.core.config.ConfigBag; @@ -54,6 +43,15 @@ import org.apache.brooklyn.util.core.task.Tasks; import org.apache.brooklyn.util.exceptions.Exceptions; import org.apache.brooklyn.util.os.Os; import org.apache.brooklyn.util.text.Strings; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.annotations.Beta; +import com.google.common.base.Function; +import com.google.common.base.Functions; +import com.google.common.base.Preconditions; +import com.google.common.base.Supplier; +import com.google.common.collect.ImmutableMap; /** * Configurable {@link EntityInitializer} which adds an SSH sensor feed running the <code>command</code> supplied @@ -104,6 +102,8 @@ public final class SshCommandSensor<T> extends AddSensor<T> { Supplier<Map<String,String>> envSupplier = new Supplier<Map<String,String>>() { @Override public Map<String, String> get() { + if (entity == null) return ImmutableMap.of(); // See BROOKLYN-568 + Map<String, Object> env = MutableMap.copyOf(entity.getConfig(BrooklynConfigKeys.SHELL_ENVIRONMENT)); // Add the shell environment entries from our configuration @@ -125,6 +125,9 @@ public final class SshCommandSensor<T> extends AddSensor<T> { Supplier<String> commandSupplier = new Supplier<String>() { @Override public String get() { + // Note that entity may be null during rebind (e.g. if this SshFeed is orphaned, with no associated entity): + // See https://issues.apache.org/jira/browse/BROOKLYN-568. + // We therefore guard against null in makeCommandExecutingInDirectory. return makeCommandExecutingInDirectory(command, executionDir, entity); } }; @@ -157,14 +160,14 @@ public final class SshCommandSensor<T> extends AddSensor<T> { String execDir = executionDir; if (Strings.isBlank(execDir)) { // default to run dir - execDir = entity.getAttribute(BrooklynConfigKeys.RUN_DIR); + execDir = (entity != null) ? entity.getAttribute(BrooklynConfigKeys.RUN_DIR) : null; // if no run dir, default to home if (Strings.isBlank(execDir)) { execDir = "~"; } } else if (!Os.isAbsolutish(execDir)) { // relative paths taken wrt run dir - String runDir = entity.getAttribute(BrooklynConfigKeys.RUN_DIR); + String runDir = (entity != null) ? entity.getAttribute(BrooklynConfigKeys.RUN_DIR) : null; if (!Strings.isBlank(runDir)) { execDir = Os.mergePaths(runDir, execDir); }