brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [6/8] git commit: Remove duplication from InternalEntityFactory etc
Date Fri, 01 Aug 2014 20:13:01 GMT
Remove duplication from InternalEntityFactory etc


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

Branch: refs/heads/master
Commit: 519bc9a8de5443f48472bfff597c41d543c95dd6
Parents: c0bd46d
Author: Aled Sage <aled.sage@gmail.com>
Authored: Wed Jul 2 09:34:08 2014 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Mon Jul 14 23:45:19 2014 +0100

----------------------------------------------------------------------
 .../brooklyn/entity/basic/AbstractEntity.java   |   4 +-
 .../entity/proxying/InternalEntityFactory.java  |  89 ++----------
 .../entity/proxying/InternalFactory.java        | 137 +++++++++++++++++++
 .../proxying/InternalLocationFactory.java       |  85 +++---------
 .../entity/proxying/InternalPolicyFactory.java  | 119 ++++------------
 .../location/basic/AbstractLocation.java        |   4 +-
 .../policy/basic/AbstractEntityAdjunct.java     |   4 +-
 7 files changed, 193 insertions(+), 249 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/519bc9a8/core/src/main/java/brooklyn/entity/basic/AbstractEntity.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/basic/AbstractEntity.java b/core/src/main/java/brooklyn/entity/basic/AbstractEntity.java
index 0f291bf..081e0a6 100644
--- a/core/src/main/java/brooklyn/entity/basic/AbstractEntity.java
+++ b/core/src/main/java/brooklyn/entity/basic/AbstractEntity.java
@@ -38,7 +38,7 @@ import brooklyn.entity.Entity;
 import brooklyn.entity.EntityType;
 import brooklyn.entity.Group;
 import brooklyn.entity.proxying.EntitySpec;
-import brooklyn.entity.proxying.InternalEntityFactory;
+import brooklyn.entity.proxying.InternalFactory;
 import brooklyn.entity.rebind.BasicEntityRebindSupport;
 import brooklyn.entity.rebind.RebindManagerImpl;
 import brooklyn.entity.rebind.RebindSupport;
@@ -268,7 +268,7 @@ public abstract class AbstractEntity implements EntityLocal, EntityInternal
{
         // TODO Don't let `this` reference escape during construction
         entityType = new EntityDynamicType(this);
         
-        _legacyConstruction = !InternalEntityFactory.FactoryConstructionTracker.isConstructing();
+        _legacyConstruction = !InternalFactory.FactoryConstructionTracker.isConstructing();
         
         if (_legacyConstruction) {
             LOG.warn("Deprecated use of old-style entity construction for "+getClass().getName()+";
instead use EntityManager().createEntity(spec)");

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/519bc9a8/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 df73423..e572ce9 100644
--- a/core/src/main/java/brooklyn/entity/proxying/InternalEntityFactory.java
+++ b/core/src/main/java/brooklyn/entity/proxying/InternalEntityFactory.java
@@ -49,7 +49,6 @@ import brooklyn.util.javalang.AggregateClassLoader;
 import brooklyn.util.javalang.Reflections;
 import brooklyn.util.task.Tasks;
 
-import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableMap;
 
 /**
@@ -60,67 +59,29 @@ import com.google.common.collect.ImmutableMap;
  * 
  * @author aled
  */
-public class InternalEntityFactory {
+public class InternalEntityFactory extends InternalFactory {
 
     private static final Logger log = LoggerFactory.getLogger(InternalEntityFactory.class);
     
-    private final ManagementContextInternal managementContext;
     private final EntityTypeRegistry entityTypeRegistry;
     private final InternalPolicyFactory policyFactory;
     
     /**
-     * For tracking if AbstractEntity constructor has been called by framework, or in legacy
way (i.e. directly).
-     * 
-     * To be deleted once we delete support for constructing entities directly (and expecting
configure() to be
-     * called inside the constructor, etc).
-     * 
-     * @author aled
-     */
-    public static class FactoryConstructionTracker {
-        private static ThreadLocal<Boolean> constructing = new ThreadLocal<Boolean>();
-        
-        public static boolean isConstructing() {
-            return (constructing.get() == Boolean.TRUE);
-        }
-        
-        static void reset() {
-            constructing.set(Boolean.FALSE);
-        }
-        
-        static void setConstructing() {
-            constructing.set(Boolean.TRUE);
-        }
-    }
-
-    /**
      * Returns true if this is a "new-style" entity (i.e. where not expected to call the
constructor to instantiate it).
      * That means it is an entity with a no-arg constructor.
      * @param managementContext
      * @param clazz
      */
     public static boolean isNewStyleEntity(ManagementContext managementContext, Class<?>
clazz) {
-        try {
-            return isNewStyleEntity(clazz);
-        } catch (IllegalArgumentException e) {
-            return false;
-        }
+        return InternalFactory.isNewStyle(managementContext, clazz);
     }
     
     public static boolean isNewStyleEntity(Class<?> clazz) {
-        if (!Entity.class.isAssignableFrom(clazz)) {
-            throw new IllegalArgumentException("Class "+clazz+" is not an entity");
-        }
-        
-        try {
-            clazz.getConstructor(new Class[0]);
-            return true;
-        } catch (NoSuchMethodException e) {
-            return false;
-        }
+        return InternalFactory.isNewStyle(clazz);
     }
     
     public InternalEntityFactory(ManagementContextInternal managementContext, EntityTypeRegistry
entityTypeRegistry, InternalPolicyFactory policyFactory) {
-        this.managementContext = checkNotNull(managementContext, "managementContext");
+        super(managementContext);
         this.entityTypeRegistry = checkNotNull(entityTypeRegistry, "entityTypeRegistry");
         this.policyFactory = checkNotNull(policyFactory, "policyFactory");
     }
@@ -322,52 +283,22 @@ public class InternalEntityFactory {
      * Constructs an entity (if new-style, calls no-arg constructor; if old-style, uses spec
to pass in config).
      */
     public <T extends Entity> T constructEntity(Class<? extends T> clazz, EntitySpec<T>
spec) {
-        try {
-            FactoryConstructionTracker.setConstructing();
-            try {
-                if (isNewStyleEntity(clazz)) {
-                    return clazz.newInstance();
-                } else {
-                    return constructOldStyle(clazz, MutableMap.copyOf(spec.getFlags()));
-                }
-            } finally {
-                FactoryConstructionTracker.reset();
-            }
-        } catch (Exception e) {
-            throw Exceptions.propagate(e);
-         }
-     }
+        return super.construct(clazz, spec.getFlags());
+    }
 
     /**
      * Constructs a new-style entity (fails if no no-arg constructor).
      */
     public <T extends Entity> T constructEntity(Class<T> clazz) {
-        try {
-            FactoryConstructionTracker.setConstructing();
-            try {
-                if (isNewStyleEntity(clazz)) {
-                    return clazz.newInstance();
-                } else {
-                    throw new IllegalStateException("Entity class "+clazz+" must have a no-arg
constructor");
-                }
-            } finally {
-                FactoryConstructionTracker.reset();
-            }
-        } catch (Exception e) {
-            throw Exceptions.propagate(e);
-        }
+        return super.constructNewStyle(clazz);
     }
     
-    private <T extends Entity> T constructOldStyle(Class<? extends T> clazz,
Map<String,?> flags) throws InstantiationException, IllegalAccessException, InvocationTargetException
{
+    @Override
+    protected <T> T constructOldStyle(Class<T> clazz, Map<String,?> flags)
throws InstantiationException, IllegalAccessException, InvocationTargetException {
         if (flags.containsKey("parent") || flags.containsKey("owner")) {
             throw new IllegalArgumentException("Spec's flags must not contain parent or owner;
use spec.parent() instead for "+clazz);
         }
-        Optional<? extends T> v = Reflections.invokeConstructorWithArgs(clazz, new
Object[] {MutableMap.copyOf(flags)}, true);
-        if (v.isPresent()) {
-            return v.get();
-        } else {
-            throw new IllegalStateException("No valid constructor defined for "+clazz+" (expected
no-arg or single java.util.Map argument)");
-        }
+        return super.constructOldStyle(clazz, flags);
     }
     
     private <T extends Entity> Class<? extends T> getImplementedBy(EntitySpec<T>
spec) {

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/519bc9a8/core/src/main/java/brooklyn/entity/proxying/InternalFactory.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/proxying/InternalFactory.java b/core/src/main/java/brooklyn/entity/proxying/InternalFactory.java
new file mode 100644
index 0000000..e024872
--- /dev/null
+++ b/core/src/main/java/brooklyn/entity/proxying/InternalFactory.java
@@ -0,0 +1,137 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package brooklyn.entity.proxying;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.Map;
+
+import brooklyn.management.ManagementContext;
+import brooklyn.management.internal.ManagementContextInternal;
+import brooklyn.util.collections.MutableMap;
+import brooklyn.util.exceptions.Exceptions;
+import brooklyn.util.javalang.Reflections;
+
+import com.google.common.base.Optional;
+
+/**
+ */
+public class InternalFactory {
+
+    protected final ManagementContextInternal managementContext;
+
+    /**
+     * For tracking if constructor has been called by framework, or in legacy way (i.e. directly).
+     * 
+     * To be deleted once we delete support for constructing directly (and expecting configure()
to be
+     * called inside the constructor, etc).
+     * 
+     * @author aled
+     */
+    public static class FactoryConstructionTracker {
+        private static ThreadLocal<Boolean> constructing = new ThreadLocal<Boolean>();
+        
+        public static boolean isConstructing() {
+            return (constructing.get() == Boolean.TRUE);
+        }
+        
+        static void reset() {
+            constructing.set(Boolean.FALSE);
+        }
+        
+        static void setConstructing() {
+            constructing.set(Boolean.TRUE);
+        }
+    }
+
+    /**
+     * Returns true if this is a "new-style" policy (i.e. where not expected to call the
constructor to instantiate it).
+     * 
+     * @param managementContext
+     * @param clazz
+     */
+    public static boolean isNewStyle(ManagementContext managementContext, Class<?>
clazz) {
+        try {
+            return isNewStyle(clazz);
+        } catch (IllegalArgumentException e) {
+            return false;
+        }
+    }
+    
+    public static boolean isNewStyle(Class<?> clazz) {
+        try {
+            clazz.getConstructor(new Class[0]);
+            return true;
+        } catch (NoSuchMethodException e) {
+            return false;
+        }
+    }
+    
+    public InternalFactory(ManagementContextInternal managementContext) {
+        this.managementContext = checkNotNull(managementContext, "managementContext");
+    }
+
+    protected <T> T construct(Class<? extends T> clazz, Map<String, ?>
constructorFlags) {
+        try {
+            if (isNewStyle(clazz)) {
+                return constructNewStyle(clazz);
+            } else {
+                return constructOldStyle(clazz, MutableMap.copyOf(constructorFlags));
+            }
+        } catch (Exception e) {
+            throw Exceptions.propagate(e);
+         }
+     }
+
+    /**
+     * Constructs a new instance (fails if no no-arg constructor).
+     */
+    protected <T> T constructNewStyle(Class<T> clazz) {
+        if (!isNewStyle(clazz)) {
+            throw new IllegalStateException("Class "+clazz+" must have a no-arg constructor");
+        }
+        
+        try {
+            FactoryConstructionTracker.setConstructing();
+            try {
+                return clazz.newInstance();
+            } finally {
+                FactoryConstructionTracker.reset();
+            }
+        } catch (Exception e) {
+            throw Exceptions.propagate(e);
+        }
+    }
+    
+    protected <T> T constructOldStyle(Class<T> clazz, Map<String,?> flags)
throws InstantiationException, IllegalAccessException, InvocationTargetException {
+        FactoryConstructionTracker.setConstructing();
+        Optional<T> v;
+        try {
+            v = Reflections.invokeConstructorWithArgs(clazz, new Object[] {MutableMap.copyOf(flags)},
true);
+        } finally {
+            FactoryConstructionTracker.reset();
+        }
+        if (v.isPresent()) {
+            return v.get();
+        } else {
+            throw new IllegalStateException("No valid constructor defined for "+clazz+" (expected
no-arg or single java.util.Map argument)");
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/519bc9a8/core/src/main/java/brooklyn/entity/proxying/InternalLocationFactory.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/proxying/InternalLocationFactory.java b/core/src/main/java/brooklyn/entity/proxying/InternalLocationFactory.java
index 61715e7..ffcfac2 100644
--- a/core/src/main/java/brooklyn/entity/proxying/InternalLocationFactory.java
+++ b/core/src/main/java/brooklyn/entity/proxying/InternalLocationFactory.java
@@ -18,8 +18,6 @@
  */
 package brooklyn.entity.proxying;
 
-import static com.google.common.base.Preconditions.checkNotNull;
-
 import java.lang.reflect.InvocationTargetException;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -32,13 +30,10 @@ import brooklyn.location.basic.LocationInternal;
 import brooklyn.management.ManagementContext;
 import brooklyn.management.internal.LocalLocationManager;
 import brooklyn.management.internal.ManagementContextInternal;
-import brooklyn.util.collections.MutableMap;
 import brooklyn.util.config.ConfigBag;
 import brooklyn.util.exceptions.Exceptions;
 import brooklyn.util.flags.FlagUtils;
-import brooklyn.util.javalang.Reflections;
 
-import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableMap;
 
 /**
@@ -49,33 +44,7 @@ import com.google.common.collect.ImmutableMap;
  * 
  * @author aled
  */
-public class InternalLocationFactory {
-
-    private final ManagementContextInternal managementContext;
-
-    /**
-     * For tracking if AbstractLocation constructor has been called by framework, or in legacy
way (i.e. directly).
-     * 
-     * To be deleted once we delete support for constructing locations directly (and expecting
configure() to be
-     * called inside the constructor, etc).
-     * 
-     * @author aled
-     */
-    public static class FactoryConstructionTracker {
-        private static ThreadLocal<Boolean> constructing = new ThreadLocal<Boolean>();
-        
-        public static boolean isConstructing() {
-            return (constructing.get() == Boolean.TRUE);
-        }
-        
-        static void reset() {
-            constructing.set(Boolean.FALSE);
-        }
-        
-        static void setConstructing() {
-            constructing.set(Boolean.TRUE);
-        }
-    }
+public class InternalLocationFactory extends InternalFactory {
 
     /**
      * Returns true if this is a "new-style" location (i.e. where not expected to call the
constructor to instantiate it).
@@ -96,16 +65,11 @@ public class InternalLocationFactory {
             throw new IllegalArgumentException("Class "+clazz+" is not an location");
         }
         
-        try {
-            clazz.getConstructor(new Class[0]);
-            return true;
-        } catch (NoSuchMethodException e) {
-            return false;
-        }
+        return InternalFactory.isNewStyle(clazz);
     }
     
     public InternalLocationFactory(ManagementContextInternal managementContext) {
-        this.managementContext = checkNotNull(managementContext, "managementContext");
+        super(managementContext);
     }
 
     @SuppressWarnings({ "unchecked", "rawtypes" })
@@ -123,12 +87,7 @@ public class InternalLocationFactory {
         try {
             Class<? extends T> clazz = spec.getType();
             
-            T loc;
-            if (isNewStyleLocation(clazz)) {
-                loc = constructLocation(clazz);
-            } else {
-                loc = constructOldStyle(clazz, MutableMap.copyOf(spec.getFlags()));
-            }
+            T loc = constructLocation(clazz, spec);
 
             if (spec.getId() != null) {
                 FlagUtils.setFieldsFromFlags(ImmutableMap.of("id", spec.getId()), loc);
@@ -163,34 +122,24 @@ public class InternalLocationFactory {
     }
     
     /**
-     * Constructs a new-style location (fails if no no-arg constructor).
+     * Constructs an entity (if new-style, calls no-arg constructor; if old-style, uses spec
to pass in config).
+     */
+    public <T extends Location> T constructLocation(Class<? extends T> clazz,
LocationSpec<T> spec) {
+        return super.construct(clazz, spec.getFlags());
+    }
+
+    /**
+     * Constructs a new-style entity (fails if no no-arg constructor).
      */
     public <T extends Location> T constructLocation(Class<T> clazz) {
-        try {
-            FactoryConstructionTracker.setConstructing();
-            try {
-                if (isNewStyleLocation(clazz)) {
-                    return clazz.newInstance();
-                } else {
-                    throw new IllegalStateException("Location class "+clazz+" must have a
no-arg constructor");
-                }
-            } finally {
-                FactoryConstructionTracker.reset();
-            }
-        } catch (Exception e) {
-            throw Exceptions.propagate(e);
-        }
+        return super.constructNewStyle(clazz);
     }
     
-    private <T extends Location> T constructOldStyle(Class<? extends T> clazz,
Map<String,?> flags) throws InstantiationException, IllegalAccessException, InvocationTargetException
{
-        if (flags.containsKey("parentLocation")) {
+    @Override
+    protected <T> T constructOldStyle(Class<T> clazz, Map<String,?> flags)
throws InstantiationException, IllegalAccessException, InvocationTargetException {
+        if (flags.containsKey("parent") || flags.containsKey("owner")) {
             throw new IllegalArgumentException("Spec's flags must not contain parent or owner;
use spec.parent() instead for "+clazz);
         }
-        Optional<? extends T> v = Reflections.invokeConstructorWithArgs(clazz, new
Object[] {MutableMap.copyOf(flags)}, true);
-        if (v.isPresent()) {
-            return v.get();
-        } else {
-            throw new IllegalStateException("No valid constructor defined for "+clazz+" (expected
no-arg or single java.util.Map argument)");
-        }
+        return super.constructOldStyle(clazz, flags);
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/519bc9a8/core/src/main/java/brooklyn/entity/proxying/InternalPolicyFactory.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/proxying/InternalPolicyFactory.java b/core/src/main/java/brooklyn/entity/proxying/InternalPolicyFactory.java
index 378d161..5480823 100644
--- a/core/src/main/java/brooklyn/entity/proxying/InternalPolicyFactory.java
+++ b/core/src/main/java/brooklyn/entity/proxying/InternalPolicyFactory.java
@@ -18,9 +18,6 @@
  */
 package brooklyn.entity.proxying;
 
-import static com.google.common.base.Preconditions.checkNotNull;
-
-import java.lang.reflect.InvocationTargetException;
 import java.util.Map;
 
 import brooklyn.config.ConfigKey;
@@ -35,9 +32,6 @@ import brooklyn.policy.basic.AbstractPolicy;
 import brooklyn.util.collections.MutableMap;
 import brooklyn.util.config.ConfigBag;
 import brooklyn.util.exceptions.Exceptions;
-import brooklyn.util.javalang.Reflections;
-
-import com.google.common.base.Optional;
 
 /**
  * Creates policies of required types.
@@ -50,33 +44,7 @@ import com.google.common.base.Optional;
  * 
  * @author aled
  */
-public class InternalPolicyFactory {
-
-    private final ManagementContextInternal managementContext;
-
-    /**
-     * For tracking if AbstractPolicy constructor has been called by framework, or in legacy
way (i.e. directly).
-     * 
-     * To be deleted once we delete support for constructing policies directly (and expecting
configure() to be
-     * called inside the constructor, etc).
-     * 
-     * @author aled
-     */
-    public static class FactoryConstructionTracker {
-        private static ThreadLocal<Boolean> constructing = new ThreadLocal<Boolean>();
-        
-        public static boolean isConstructing() {
-            return (constructing.get() == Boolean.TRUE);
-        }
-        
-        static void reset() {
-            constructing.set(Boolean.FALSE);
-        }
-        
-        static void setConstructing() {
-            constructing.set(Boolean.TRUE);
-        }
-    }
+public class InternalPolicyFactory extends InternalFactory {
 
     /**
      * Returns true if this is a "new-style" policy (i.e. where not expected to call the
constructor to instantiate it).
@@ -96,13 +64,8 @@ public class InternalPolicyFactory {
         if (!Policy.class.isAssignableFrom(clazz)) {
             throw new IllegalArgumentException("Class "+clazz+" is not a policy");
         }
-        
-        try {
-            clazz.getConstructor(new Class[0]);
-            return true;
-        } catch (NoSuchMethodException e) {
-            return false;
-        }
+
+        return InternalFactory.isNewStyle(clazz);
     }
     
     public static boolean isNewStyleEnricher(Class<?> clazz) {
@@ -110,16 +73,11 @@ public class InternalPolicyFactory {
             throw new IllegalArgumentException("Class "+clazz+" is not an enricher");
         }
         
-        try {
-            clazz.getConstructor(new Class[0]);
-            return true;
-        } catch (NoSuchMethodException e) {
-            return false;
-        }
+        return InternalFactory.isNewStyle(clazz);
     }
     
     public InternalPolicyFactory(ManagementContextInternal managementContext) {
-        this.managementContext = checkNotNull(managementContext, "managementContext");
+        super(managementContext);
     }
 
     @SuppressWarnings({ "unchecked", "rawtypes" })
@@ -131,12 +89,7 @@ public class InternalPolicyFactory {
         try {
             Class<? extends T> clazz = spec.getType();
             
-            T pol;
-            if (isNewStylePolicy(clazz)) {
-                pol = constructPolicy(clazz);
-            } else {
-                pol = constructOldStyle(clazz, MutableMap.copyOf(spec.getFlags()));
-            }
+            T pol = constructPolicy(clazz, spec);
 
             if (spec.getDisplayName()!=null)
                 ((AbstractPolicy)pol).setName(spec.getDisplayName());
@@ -171,12 +124,7 @@ public class InternalPolicyFactory {
         try {
             Class<? extends T> clazz = spec.getType();
             
-            T enricher;
-            if (isNewStyleEnricher(clazz)) {
-                enricher = constructEnricher(clazz);
-            } else {
-                enricher = constructOldStyle(clazz, MutableMap.copyOf(spec.getFlags()));
-            }
+            T enricher = constructEnricher(clazz, spec);
             
             if (spec.getDisplayName()!=null)
                 ((AbstractEnricher)enricher).setName(spec.getDisplayName());
@@ -203,51 +151,30 @@ public class InternalPolicyFactory {
     }
     
     /**
+     * Constructs a policy (if new-style, calls no-arg constructor; if old-style, uses spec
to pass in config).
+     */
+    public <T extends Policy> T constructPolicy(Class<? extends T> clazz, PolicySpec<T>
spec) {
+        return super.construct(clazz, spec.getFlags());
+    }
+
+    /**
+     * Constructs an enricher (if new-style, calls no-arg constructor; if old-style, uses
spec to pass in config).
+     */
+    public <T extends Enricher> T constructEnricher(Class<? extends T> clazz,
EnricherSpec<T> spec) {
+        return super.construct(clazz, spec.getFlags());
+    }
+
+    /**
      * Constructs a new-style policy (fails if no no-arg constructor).
      */
     public <T extends Policy> T constructPolicy(Class<T> clazz) {
-        try {
-            FactoryConstructionTracker.setConstructing();
-            try {
-                if (isNewStylePolicy(clazz)) {
-                    return clazz.newInstance();
-                } else {
-                    throw new IllegalStateException("Policy class "+clazz+" must have a no-arg
constructor");
-                }
-            } finally {
-                FactoryConstructionTracker.reset();
-            }
-        } catch (Exception e) {
-            throw Exceptions.propagate(e);
-        }
+        return super.constructNewStyle(clazz);
     }
     
     /**
      * Constructs a new-style enricher (fails if no no-arg constructor).
      */
     public <T extends Enricher> T constructEnricher(Class<T> clazz) {
-        try {
-            FactoryConstructionTracker.setConstructing();
-            try {
-                if (isNewStyleEnricher(clazz)) {
-                    return clazz.newInstance();
-                } else {
-                    throw new IllegalStateException("Enricher class "+clazz+" must have a
no-arg constructor");
-                }
-            } finally {
-                FactoryConstructionTracker.reset();
-            }
-        } catch (Exception e) {
-            throw Exceptions.propagate(e);
-        }
-    }
-    
-    private <T> T constructOldStyle(Class<T> clazz, Map<String,?> flags)
throws InstantiationException, IllegalAccessException, InvocationTargetException {
-        Optional<T> v = Reflections.invokeConstructorWithArgs(clazz, new Object[] {MutableMap.copyOf(flags)},
true);
-        if (v.isPresent()) {
-            return v.get();
-        } else {
-            throw new IllegalStateException("No valid constructor defined for "+clazz+" (expected
no-arg or single java.util.Map argument)");
-        }
+        return super.constructNewStyle(clazz);
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/519bc9a8/core/src/main/java/brooklyn/location/basic/AbstractLocation.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/location/basic/AbstractLocation.java b/core/src/main/java/brooklyn/location/basic/AbstractLocation.java
index d468622..ebb5909 100644
--- a/core/src/main/java/brooklyn/location/basic/AbstractLocation.java
+++ b/core/src/main/java/brooklyn/location/basic/AbstractLocation.java
@@ -35,7 +35,7 @@ import org.slf4j.LoggerFactory;
 import brooklyn.config.ConfigKey;
 import brooklyn.config.ConfigKey.HasConfigKey;
 import brooklyn.entity.basic.EntityDynamicType;
-import brooklyn.entity.proxying.InternalLocationFactory;
+import brooklyn.entity.proxying.InternalFactory;
 import brooklyn.entity.rebind.BasicLocationRebindSupport;
 import brooklyn.entity.rebind.RebindManagerImpl;
 import brooklyn.entity.rebind.RebindSupport;
@@ -144,7 +144,7 @@ public abstract class AbstractLocation implements LocationInternal, HasHostGeoIn
      */
     public AbstractLocation(Map properties) {
         inConstruction = true;
-        _legacyConstruction = !InternalLocationFactory.FactoryConstructionTracker.isConstructing();
+        _legacyConstruction = !InternalFactory.FactoryConstructionTracker.isConstructing();
         if (!_legacyConstruction && properties!=null && !properties.isEmpty())
{
             LOG.warn("Forcing use of deprecated old-style location construction for "+getClass().getName()+"
because properties were specified ("+properties+")");
             _legacyConstruction = true;

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/519bc9a8/core/src/main/java/brooklyn/policy/basic/AbstractEntityAdjunct.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/policy/basic/AbstractEntityAdjunct.java b/core/src/main/java/brooklyn/policy/basic/AbstractEntityAdjunct.java
index a5d324f..3760933 100644
--- a/core/src/main/java/brooklyn/policy/basic/AbstractEntityAdjunct.java
+++ b/core/src/main/java/brooklyn/policy/basic/AbstractEntityAdjunct.java
@@ -37,7 +37,7 @@ import brooklyn.entity.Entity;
 import brooklyn.entity.Group;
 import brooklyn.entity.basic.EntityInternal;
 import brooklyn.entity.basic.EntityLocal;
-import brooklyn.entity.proxying.InternalPolicyFactory;
+import brooklyn.entity.proxying.InternalFactory;
 import brooklyn.entity.rebind.RebindManagerImpl;
 import brooklyn.entity.trait.Configurable;
 import brooklyn.event.AttributeSensor;
@@ -106,7 +106,7 @@ public abstract class AbstractEntityAdjunct implements EntityAdjunct,
Configurab
     
     public AbstractEntityAdjunct(@SuppressWarnings("rawtypes") Map flags) {
         inConstruction = true;
-        _legacyConstruction = !InternalPolicyFactory.FactoryConstructionTracker.isConstructing();
+        _legacyConstruction = !InternalFactory.FactoryConstructionTracker.isConstructing();
         _legacyNoConstructionInit = (flags != null) && Boolean.TRUE.equals(flags.get("noConstructionInit"));
         
         if (!_legacyConstruction && flags!=null && !flags.isEmpty()) {


Mime
View raw message