You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by al...@apache.org on 2014/07/09 23:46:17 UTC

[04/50] git commit: put more design, and comments, around order in which children are added vs parent set and initializers run

put more design, and comments, around order in which children are added vs parent set and initializers run


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

Branch: refs/heads/master
Commit: 19c0a2b69f3ca2192f27eecd2c0b959c61d6e83e
Parents: 19ae27b
Author: Alex Heneveld <al...@cloudsoftcorp.com>
Authored: Fri Jul 4 13:22:03 2014 +0100
Committer: Aled Sage <al...@gmail.com>
Committed: Wed Jul 9 22:34:42 2014 +0100

----------------------------------------------------------------------
 .../entity/proxying/InternalEntityFactory.java  | 36 +++++++++++++++-----
 1 file changed, 28 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/19c0a2b6/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 10b1aba..92a9845 100644
--- a/core/src/main/java/brooklyn/entity/proxying/InternalEntityFactory.java
+++ b/core/src/main/java/brooklyn/entity/proxying/InternalEntityFactory.java
@@ -97,7 +97,6 @@ public class InternalEntityFactory {
      * @param managementContext
      * @param clazz
      */
-    @SuppressWarnings({ "unchecked", "rawtypes" })
     public static boolean isNewStyleEntity(ManagementContext managementContext, Class<?> clazz) {
         try {
             return isNewStyleEntity(clazz);
@@ -190,9 +189,22 @@ public class InternalEntityFactory {
                 ((EntityLocal)entity).setConfig((ConfigKey)entry.getKey(), entry.getValue());
             }
 
-            for (EntitySpec<?> childSpec : spec.getChildren()) {
-                Entity child = createEntity(childSpec);
-                entity.addChild(child);
+            /* Order is important here. Changed Jul 2014 when supporting children in spec.
+             * (Previously parent was set *after* running initializers, and there were no children.)
+             * <p>
+             * We may want access to the parent when running initializers (or in children initializers).
+             * <p>
+             * Currently initializers will be run before children are added.
+             * on the basis that it is more likely children will want to assume parent is initialized,
+             * than initializers will want access to children.
+             * <p>
+             * If this is not good enough we could do an additional pass where all children are built up
+             * before running any initializers.
+             */
+            Entity parent = spec.getParent();
+            if (parent != null) {
+                parent = (parent instanceof AbstractEntity) ? ((AbstractEntity)parent).getProxyIfAvailable() : parent;
+                entity.setParent(parent);
             }
 
             /* Marked transient so that the task is not needlessly kept around at the highest level.
@@ -239,11 +251,19 @@ public class InternalEntityFactory {
                 }
             }).build()).get();
             
-            Entity parent = spec.getParent();
-            if (parent != null) {
-                parent = (parent instanceof AbstractEntity) ? ((AbstractEntity)parent).getProxyIfAvailable() : parent;
-                entity.setParent(parent);
+            for (EntitySpec<?> childSpec : spec.getChildren()) {
+                if (childSpec.getParent()!=null) {
+                    if (!childSpec.getParent().equals(entity)) {
+                        throw new IllegalStateException("Spec "+childSpec+" has parent "+childSpec.getParent()+" defined, "
+                            + "but it is defined as a child of "+entity);
+                    }
+                    log.warn("Child spec "+childSpec+" is already set with parent "+entity+"; how did this happen?!");
+                }
+                childSpec.parent(entity);
+                Entity child = createEntity(childSpec);
+                entity.addChild(child);
             }
+
             return entity;
             
         } catch (Exception e) {