You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2021/04/28 23:49:46 UTC

[brooklyn-server] branch master updated: allow search of some entities even when unmanaged, and better error messages

This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git


The following commit(s) were added to refs/heads/master by this push:
     new 94a674b  allow search of some entities even when unmanaged, and better error messages
94a674b is described below

commit 94a674b3031b50d0862213de18fdb9f154e5ab05
Author: Alex Heneveld <al...@cloudsoftcorp.com>
AuthorDate: Thu Apr 29 00:48:57 2021 +0100

    allow search of some entities even when unmanaged, and better error messages
---
 .../brooklyn/spi/dsl/methods/DslComponent.java     | 28 +++++++++++++++++++---
 .../brooklyn/util/core/task/ValueResolver.java     |  3 +++
 2 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java
index 6226415..cf6fa2d 100644
--- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java
+++ b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java
@@ -20,6 +20,7 @@ package org.apache.brooklyn.camp.brooklyn.spi.dsl.methods;
 
 import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonIgnore;
+import java.util.Set;
 import static org.apache.brooklyn.camp.brooklyn.spi.dsl.DslUtils.resolved;
 
 import java.util.Collection;
@@ -50,6 +51,7 @@ import org.apache.brooklyn.core.mgmt.internal.EntityManagerInternal;
 import org.apache.brooklyn.core.sensor.DependentConfiguration;
 import org.apache.brooklyn.core.sensor.Sensors;
 import org.apache.brooklyn.util.JavaGroovyEquivalents;
+import org.apache.brooklyn.util.collections.MutableSet;
 import org.apache.brooklyn.util.core.flags.TypeCoercions;
 import org.apache.brooklyn.util.core.task.DeferredSupplier;
 import org.apache.brooklyn.util.core.task.ImmediateSupplier;
@@ -219,7 +221,7 @@ public class DslComponent extends BrooklynDslDeferredSupplier<Entity> implements
         protected final Scope scope;
         protected final String componentId;
         protected final DeferredSupplier<?> componentIdSupplier;
-        
+
         public EntityInScopeFinder(DslComponent scopeComponent, Scope scope, String componentId, DeferredSupplier<?> componentIdSupplier) {
             this.scopeComponent = scopeComponent;
             this.scope = scope;
@@ -278,8 +280,28 @@ public class DslComponent extends BrooklynDslDeferredSupplier<Entity> implements
                 case PARENT:
                     return Maybe.<Entity>of(entity.getParent());
                 case GLOBAL:
-                    entitiesToSearch = ((EntityManagerInternal)entity.getManagementContext().getEntityManager())
-                        .getAllEntitiesInApplication( entity().getApplication() );
+                    if (Entities.isManaged(entity())) {
+                        // use management context if entity is managed (usual case, more efficient)
+                        entitiesToSearch = ((EntityManagerInternal) entity.getManagementContext().getEntityManager())
+                                .getAllEntitiesInApplication(entity().getApplication());
+                    } else {
+                        // otherwise traverse the application
+                        if (entity()!=null && entity().getApplication()!=null) {
+                            Set<Entity> toVisit = MutableSet.of(entity().getApplication()), visited = MutableSet.of(entity().getApplication());
+                            while (!toVisit.isEmpty()) {
+                                Set<Entity> visiting = MutableSet.copyOf(toVisit);
+                                toVisit.clear();
+                                visiting.forEach(e -> {
+                                    e.getChildren().forEach(ec -> {
+                                        if (visited.add(ec)) toVisit.add(ec);
+                                    });
+                                });
+                            }
+                            entitiesToSearch = visited;
+                        } else {
+                            // nothing to do
+                        }
+                    }
                     break;
                 case ROOT:
                     return Maybe.<Entity>of(entity.getApplication());
diff --git a/core/src/main/java/org/apache/brooklyn/util/core/task/ValueResolver.java b/core/src/main/java/org/apache/brooklyn/util/core/task/ValueResolver.java
index 0502953..842e812 100644
--- a/core/src/main/java/org/apache/brooklyn/util/core/task/ValueResolver.java
+++ b/core/src/main/java/org/apache/brooklyn/util/core/task/ValueResolver.java
@@ -466,6 +466,9 @@ public class ValueResolver<T> implements DeferredSupplier<T>, Iterable<Maybe<Obj
 
                 Maybe<T> result = null;
                 try {
+                    if (exec==null) {
+                        return Maybe.absent("Immediate resolution requested for '"+getDescription()+"' but no execution context available");
+                    }
                     result = exec.getImmediately(v);
 
                     return (result.isPresent())