brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [2/5] brooklyn-server git commit: support more ManagementContext.lookup including predicates, adjuncts, lookupAll
Date Fri, 10 Nov 2017 10:58:21 GMT
support more ManagementContext.lookup including predicates, adjuncts, lookupAll


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/2900156d
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/2900156d
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/2900156d

Branch: refs/heads/master
Commit: 2900156d8988a16e2c7de8847de648ceaa62cfae
Parents: a70a553
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Thu Nov 2 16:51:06 2017 +0000
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Thu Nov 2 16:51:20 2017 +0000

----------------------------------------------------------------------
 .../brooklyn/api/mgmt/ManagementContext.java    | 13 +++--
 .../internal/AbstractManagementContext.java     | 50 +++++++++++++++++++-
 .../NonDeploymentManagementContext.java         | 13 +++++
 .../brooklyn/enricher/stock/EnrichersTest.java  | 11 +++++
 4 files changed, 82 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2900156d/api/src/main/java/org/apache/brooklyn/api/mgmt/ManagementContext.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/org/apache/brooklyn/api/mgmt/ManagementContext.java b/api/src/main/java/org/apache/brooklyn/api/mgmt/ManagementContext.java
index 4ad9718..b82c884 100644
--- a/api/src/main/java/org/apache/brooklyn/api/mgmt/ManagementContext.java
+++ b/api/src/main/java/org/apache/brooklyn/api/mgmt/ManagementContext.java
@@ -41,6 +41,7 @@ import org.apache.brooklyn.config.StringConfigMap;
 import org.apache.brooklyn.util.guava.Maybe;
 
 import com.google.common.annotations.Beta;
+import com.google.common.base.Predicate;
 
 /**
  * This is the entry point for accessing and interacting with a realm of applications and
their entities in Brooklyn.
@@ -309,8 +310,14 @@ public interface ManagementContext {
     /** As {@link #lookup(String, Class)} but not constraining the return type */
     public BrooklynObject lookup(String id);
     
-    /** Finds an entity with the given ID known at this management context */
-    // TODO in future support policies etc
+    /** As {@link #lookup(Predicate)} comparing the ID of the object with the given string
*/
     public <T extends BrooklynObject> T lookup(String id, Class<T> type); 
 
-}
\ No newline at end of file
+    /** Finds a {@link BrooklynObject} known in this management context 
+     * satisfying the given predicate, or null */
+    public <T extends BrooklynObject> T lookup(Predicate<? super T> filter);
+    
+    /** As {@link #lookup(Predicate)} but returning all such instances */
+    public <T extends BrooklynObject> Collection<T> lookupAll(Predicate<?
super T> filter);
+    
+}

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2900156d/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/AbstractManagementContext.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/AbstractManagementContext.java
b/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/AbstractManagementContext.java
index 3cdd944..c0e3d7c 100644
--- a/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/AbstractManagementContext.java
+++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/AbstractManagementContext.java
@@ -25,6 +25,7 @@ import java.lang.reflect.InvocationTargetException;
 import java.net.URI;
 import java.net.URL;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
@@ -88,6 +89,7 @@ import org.slf4j.LoggerFactory;
 import com.google.common.base.Function;
 import com.google.common.base.Objects;
 import com.google.common.base.Preconditions;
+import com.google.common.base.Predicate;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
@@ -536,8 +538,52 @@ public abstract class AbstractManagementContext implements ManagementContextInte
         result = getLocationManager().getLocation(id);
         if (result!=null && type.isInstance(result)) return (T)result;
 
-        // TODO policies, enrichers, feeds; bundles?
-        return null;
+        return lookup((o) -> { return type.isInstance(o) && Objects.equal(id,
o.getId()); });
+    }
+    
+    @Override
+    public <T extends BrooklynObject> T lookup(Predicate<? super T> filter) {
+        Collection<T> list = lookupAll(filter, true);
+        if (list.isEmpty()) return null;
+        return list.iterator().next();
+    }
+
+    @Override
+    public <T extends BrooklynObject> Collection<T> lookupAll(Predicate<?
super T> filter) {
+        return lookupAll(filter, false);
+    }
+    
+    @SuppressWarnings("unchecked")
+    private <T extends BrooklynObject> Collection<T> lookupAll(Predicate<?
super T> filter, boolean justOne) {
+        List<T> result = MutableList.of();
+
+        final class Scanner {
+            public boolean scan(Iterable<? extends BrooklynObject> items) {
+                for (BrooklynObject i: items) {
+                    try {
+                        if (filter.apply((T)i)) {
+                            result.add((T)i);
+                            if (justOne) return true;
+                        }
+                    } catch (Exception exc) {
+                        Exceptions.propagateIfFatal(exc);
+                        // just assume filter isn't for this type, class cast
+                        return false;
+                    }
+                }
+                return false;
+            }
+        }
+        Scanner scanner = new Scanner();
+        if (scanner.scan( getEntityManager().getEntities() ) && justOne) return result;
+        if (scanner.scan( getLocationManager().getLocations() ) && justOne) return
result;
+        for (Entity e: getEntityManager().getEntities()) {
+            if (scanner.scan( e.policies() ) && justOne) return result;
+            if (scanner.scan( e.enrichers() ) && justOne) return result;
+            if (scanner.scan( ((EntityInternal)e).feeds() ) && justOne) return result;
+        }
+        
+        return result;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2900156d/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/NonDeploymentManagementContext.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/NonDeploymentManagementContext.java
b/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/NonDeploymentManagementContext.java
index 8f65e62..9a01419 100644
--- a/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/NonDeploymentManagementContext.java
+++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/NonDeploymentManagementContext.java
@@ -77,6 +77,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.base.MoreObjects;
+import com.google.common.base.Predicate;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 
@@ -504,6 +505,18 @@ public class NonDeploymentManagementContext implements ManagementContextInternal
     }
 
     @Override
+    public <T extends BrooklynObject> T lookup(Predicate<? super T> filter) {
+        checkInitialManagementContextReal();
+        return initialManagementContext.lookup(filter);
+    }
+
+    @Override
+    public <T extends BrooklynObject> Collection<T> lookupAll(Predicate<?
super T> filter) {
+        checkInitialManagementContextReal();
+        return initialManagementContext.lookupAll(filter);
+    }
+    
+    @Override
     public List<Throwable> errors() {
         checkInitialManagementContextReal();
         return initialManagementContext.errors();

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2900156d/core/src/test/java/org/apache/brooklyn/enricher/stock/EnrichersTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/enricher/stock/EnrichersTest.java b/core/src/test/java/org/apache/brooklyn/enricher/stock/EnrichersTest.java
index 19503e7..6b7f50e 100644
--- a/core/src/test/java/org/apache/brooklyn/enricher/stock/EnrichersTest.java
+++ b/core/src/test/java/org/apache/brooklyn/enricher/stock/EnrichersTest.java
@@ -85,6 +85,17 @@ public class EnrichersTest extends BrooklynAppUnitTestSupport {
     }
     
     @Test
+    public void testLookup() {
+        Enricher enr = entity.enrichers().add(Enrichers.builder()
+                .combining(NUM1, NUM2)
+                .publishing(NUM3)
+                .computingSum()
+                .build());
+        
+        Assert.assertEquals(mgmt.lookup(enr.getId()), enr);
+    }
+    
+    @Test
     public void testAdding() {
         Enricher enr = entity.enrichers().add(Enrichers.builder()
                 .combining(NUM1, NUM2)


Mime
View raw message