brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [37/45] git commit: add support for EntitySpec.child and .children
Date Fri, 04 Jul 2014 09:51:19 GMT
add support for EntitySpec.child and .children


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

Branch: refs/heads/master
Commit: d709b7e5109cf2b7ba629d6394cbc6173cf98e54
Parents: e96af92
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Thu Jul 3 16:26:35 2014 +0100
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Thu Jul 3 16:26:35 2014 +0100

----------------------------------------------------------------------
 .../brooklyn/entity/proxying/EntitySpec.java    | 23 ++++++++++++++++++--
 .../entity/proxying/InternalEntityFactory.java  |  7 +++++-
 .../brooklyn/entity/basic/EntitySpecTest.java   | 16 ++++++++++++++
 3 files changed, 43 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/d709b7e5/api/src/main/java/brooklyn/entity/proxying/EntitySpec.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/brooklyn/entity/proxying/EntitySpec.java b/api/src/main/java/brooklyn/entity/proxying/EntitySpec.java
index a23975e..3055064 100644
--- a/api/src/main/java/brooklyn/entity/proxying/EntitySpec.java
+++ b/api/src/main/java/brooklyn/entity/proxying/EntitySpec.java
@@ -97,7 +97,8 @@ public class EntitySpec<T extends Entity> implements Serializable
{
                 .policies(spec.getPolicies())
                 .enricherSpecs(spec.getEnricherSpecs())
                 .enrichers(spec.getEnrichers())
-                .addInitializers(spec.getInitializers());
+                .addInitializers(spec.getInitializers())
+                .children(spec.getChildren());
         
         if (spec.getParent() != null) result.parent(spec.getParent());
         if (spec.getImplementation() != null) result.impl(spec.getImplementation());
@@ -123,6 +124,7 @@ public class EntitySpec<T extends Entity> implements Serializable
{
     private final List<Location> locations = Lists.newArrayList();
     private final Set<Class<?>> additionalInterfaces = Sets.newLinkedHashSet();
     private final List<EntityInitializer> entityInitializers = Lists.newArrayList();
+    private final List<EntitySpec<?>> children = Lists.newArrayList();
     private volatile boolean immutable;
     
     public EntitySpec(Class<T> type) {
@@ -174,6 +176,10 @@ public class EntitySpec<T extends Entity> implements Serializable
{
         return entityInitializers;
     }
     
+    public List<EntitySpec<?>> getChildren() {
+        return children;
+    }
+    
     /**
      * @return The entity's parent
      */
@@ -272,7 +278,20 @@ public class EntitySpec<T extends Entity> implements Serializable
{
         }
         return this;
     }
-        
+
+    public EntitySpec<T> children(Iterable<? extends EntitySpec<?>> children)
{
+        checkMutable();
+        Iterables.addAll(this.children, children);
+        return this;
+    }
+
+    /** The supplied class must have a public no-arg constructor. */
+    public EntitySpec<T> child(EntitySpec<?> child) {
+        checkMutable();
+        children.add(child);
+        return this;
+    }
+
     public EntitySpec<T> parent(Entity val) {
         checkMutable();
         parent = checkNotNull(val, "parent");

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/d709b7e5/core/src/main/java/brooklyn/entity/proxying/InternalEntityFactory.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/proxying/InternalEntityFactory.java b/core/src/main/java/brooklyn/entity/proxying/InternalEntityFactory.java
index 67a0208..74b7ad2 100644
--- a/core/src/main/java/brooklyn/entity/proxying/InternalEntityFactory.java
+++ b/core/src/main/java/brooklyn/entity/proxying/InternalEntityFactory.java
@@ -171,7 +171,12 @@ public class InternalEntityFactory {
             for (Map.Entry<ConfigKey<?>, Object> entry : spec.getConfig().entrySet())
{
                 ((EntityLocal)entity).setConfig((ConfigKey)entry.getKey(), entry.getValue());
             }
-            
+
+            for (EntitySpec<?> childSpec : spec.getChildren()) {
+                Entity child = createEntity(childSpec);
+                ((EntityLocal)entity).addChild(child);
+            }
+
             /* Marked transient so that the task is not needlessly kept around at the highest
level.
              * Note that the task is not normally visible in the GUI, because 
              * (a) while it is running, the entity is parentless (and so not in the tree);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/d709b7e5/core/src/test/java/brooklyn/entity/basic/EntitySpecTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/basic/EntitySpecTest.java b/core/src/test/java/brooklyn/entity/basic/EntitySpecTest.java
index ef53cc0..6233350 100644
--- a/core/src/test/java/brooklyn/entity/basic/EntitySpecTest.java
+++ b/core/src/test/java/brooklyn/entity/basic/EntitySpecTest.java
@@ -8,6 +8,7 @@ import org.testng.annotations.Test;
 
 import brooklyn.enricher.basic.AbstractEnricher;
 import brooklyn.entity.BrooklynAppUnitTestSupport;
+import brooklyn.entity.Entity;
 import brooklyn.entity.proxying.EntitySpec;
 import brooklyn.event.basic.BasicConfigKey;
 import brooklyn.location.basic.SimulatedLocation;
@@ -44,6 +45,21 @@ public class EntitySpecTest extends BrooklynAppUnitTestSupport {
     }
 
     @Test
+    public void testAddsChidlren() throws Exception {
+        entity = app.createAndManageChild( EntitySpec.create(TestEntity.class)
+            .displayName("child")
+            .child(EntitySpec.create(TestEntity.class)
+                .displayName("grandchild")) );
+        
+        Entity child = Iterables.getOnlyElement(app.getChildren());
+        assertEquals(child, entity);
+        assertEquals(child.getDisplayName(), "child");
+        Entity grandchild = Iterables.getOnlyElement(child.getChildren());
+        assertEquals(grandchild.getDisplayName(), "grandchild");
+    }
+    
+
+    @Test
     public void testAddsPolicySpec() throws Exception {
         entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)
                 .policy(PolicySpec.create(MyPolicy.class)


Mime
View raw message