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 2015/09/23 12:51:54 UTC

[02/10] incubator-brooklyn git commit: Create entity.enrichers() and entity.policies()

Create entity.enrichers() and entity.policies()

- deprecate methods Entity.*enricher*() and Entity.*policy*(),
  instead to use those on EnricherSupport / PolicySupport.
- added to EntityInternal.enrichers()/policies() the methods
  removeAllEnrichers() and removeAllPolicies().
- deprecate those last methods of EntityLocal
- deprecate EntityLocal


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

Branch: refs/heads/master
Commit: e48bdaa73ab7cd85ac3b1aa42b6f1637c6dab0f9
Parents: 3e46621
Author: Aled Sage <al...@gmail.com>
Authored: Mon Sep 21 08:51:18 2015 +0100
Committer: Aled Sage <al...@gmail.com>
Committed: Wed Sep 23 10:32:55 2015 +0100

----------------------------------------------------------------------
 .../org/apache/brooklyn/api/entity/Entity.java  |  78 +++++-
 .../apache/brooklyn/api/entity/EntityLocal.java |   8 +
 .../brooklyn/core/entity/AbstractEntity.java    | 255 ++++++++++++++-----
 .../brooklyn/core/entity/EntityInternal.java    |  27 ++
 4 files changed, 305 insertions(+), 63 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e48bdaa7/api/src/main/java/org/apache/brooklyn/api/entity/Entity.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/org/apache/brooklyn/api/entity/Entity.java b/api/src/main/java/org/apache/brooklyn/api/entity/Entity.java
index 8388d28..4df913b 100644
--- a/api/src/main/java/org/apache/brooklyn/api/entity/Entity.java
+++ b/api/src/main/java/org/apache/brooklyn/api/entity/Entity.java
@@ -160,12 +160,18 @@ public interface Entity extends BrooklynObject {
     
     /**
      * @return an immutable thread-safe view of the policies.
+     * 
+     * @deprecated since 0.9.0; see {@link PolicySupport#getPolicies()}
      */
+    @Deprecated
     Collection<Policy> getPolicies();
     
     /**
      * @return an immutable thread-safe view of the enrichers.
+     * 
+     * @deprecated since 0.9.0; see {@link EnricherSupport#getEnrichers()}
      */
+    @Deprecated
     Collection<Enricher> getEnrichers();
     
     /**
@@ -240,34 +246,52 @@ public interface Entity extends BrooklynObject {
     
     /**
      * Adds the given policy to this entity. Also calls policy.setEntity if available.
+     * 
+     * @deprecated since 0.9.0; see {@link PolicySupport#addPolicy(Policy)}
      */
+    @Deprecated
     void addPolicy(Policy policy);
     
     /**
      * Adds the given policy to this entity. Also calls policy.setEntity if available.
+     * 
+     * @deprecated since 0.9.0; see {@link PolicySupport#addPolicy(PolicySpec)}
      */
+    @Deprecated
     <T extends Policy> T addPolicy(PolicySpec<T> enricher);
     
     /**
      * Removes the given policy from this entity. 
      * @return True if the policy existed at this entity; false otherwise
+     * 
+     * @deprecated since 0.9.0; see {@link PolicySupport#removePolicy(Policy)}
      */
+    @Deprecated
     boolean removePolicy(Policy policy);
     
     /**
      * Adds the given enricher to this entity. Also calls enricher.setEntity if available.
+     * 
+     * @deprecated since 0.9.0; see {@link EnricherSupport#addEnricher(Enricher)}
      */
+    @Deprecated
     void addEnricher(Enricher enricher);
     
     /**
      * Adds the given enricher to this entity. Also calls enricher.setEntity if available.
+     * 
+     * @deprecated since 0.9.0; see {@link EnricherSupport#addEnricher(EnricherSpec)}
      */
+    @Deprecated
     <T extends Enricher> T addEnricher(EnricherSpec<T> enricher);
     
     /**
      * Removes the given enricher from this entity. 
      * @return True if the policy enricher at this entity; false otherwise
+     * 
+     * @deprecated since 0.9.0; see {@link EnricherSupport#removeEnricher(Enricher)}
      */
+    @Deprecated
     boolean removeEnricher(Enricher enricher);
     
     /**
@@ -278,7 +302,11 @@ public interface Entity extends BrooklynObject {
     SensorSupport sensors();
 
     SubscriptionSupport subscriptions();
-    
+
+    PolicySupport policies();
+
+    EnricherSupport enrichers();
+
     @Beta
     public interface SensorSupport {
 
@@ -364,4 +392,52 @@ public interface Entity extends BrooklynObject {
         @Beta
         boolean unsubscribe(Entity producer, SubscriptionHandle handle);
     }
+
+    @Beta
+    public interface PolicySupport {
+        /**
+         * @return an immutable thread-safe view of the policies.
+         */
+        Collection<Policy> getPolicies();
+        
+        /**
+         * Adds the given policy to this entity. Also calls policy.setEntity if available.
+         */
+        void addPolicy(Policy policy);
+        
+        /**
+         * Adds the given policy to this entity. Also calls policy.setEntity if available.
+         */
+        <T extends Policy> T addPolicy(PolicySpec<T> enricher);
+        
+        /**
+         * Removes the given policy from this entity. 
+         * @return True if the policy existed at this entity; false otherwise
+         */
+        boolean removePolicy(Policy policy);
+    }
+    
+    @Beta
+    public interface EnricherSupport {
+        /**
+         * @return an immutable thread-safe view of the enrichers.
+         */
+        Collection<Enricher> getEnrichers();
+        
+        /**
+         * Adds the given enricher to this entity. Also calls enricher.setEntity if available.
+         */
+        void addEnricher(Enricher enricher);
+        
+        /**
+         * Adds the given enricher to this entity. Also calls enricher.setEntity if available.
+         */
+        <T extends Enricher> T addEnricher(EnricherSpec<T> enricher);
+        
+        /**
+         * Removes the given enricher from this entity. 
+         * @return True if the policy enricher at this entity; false otherwise
+         */
+        boolean removeEnricher(Enricher enricher);
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e48bdaa7/api/src/main/java/org/apache/brooklyn/api/entity/EntityLocal.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/org/apache/brooklyn/api/entity/EntityLocal.java b/api/src/main/java/org/apache/brooklyn/api/entity/EntityLocal.java
index c0b7a42..0ecc73a 100644
--- a/api/src/main/java/org/apache/brooklyn/api/entity/EntityLocal.java
+++ b/api/src/main/java/org/apache/brooklyn/api/entity/EntityLocal.java
@@ -43,6 +43,8 @@ import com.google.common.base.Function;
  * of sub-types.
  * FIXME Add {@link setAttribute(AttributeSensorAndConfigKey<?,T>)} back in if/when move it back,
  * or if we extract an interface for AttributeSensorAndConfigKey.
+ * 
+ * @deprecated since 0.9.0; use {@link Entity} or {@link org.apache.brooklyn.core.entity.EntityInternal}
  */
 public interface EntityLocal extends Entity {
     
@@ -166,13 +168,19 @@ public interface EntityLocal extends Entity {
     /**
      * Removes all policy from this entity. 
      * @return True if any policies existed at this entity; false otherwise
+     * 
+     * @deprecated since 0.9.0; see {@link PolicySupportInternal#removeAllPolicies()}, e.g. {@code ((EntityInternal)entity).policies().removeAllPolicies()}
      */
+    @Deprecated
     boolean removeAllPolicies();
     
     /**
      * Removes all enricher from this entity.
      * Use with caution as some entities automatically register enrichers; this will remove those enrichers as well.
      * @return True if any enrichers existed at this entity; false otherwise
+     * 
+     * @deprecated since 0.9.0; see {@link EnricherSupportInternal#removeAllEnrichers()}, e.g. {@code ((EntityInternal)entity).enrichers().removeAllEnrichers()}
      */
+    @Deprecated
     boolean removeAllEnrichers();
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e48bdaa7/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java b/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java
index 7a626a4..13079d3 100644
--- a/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java
+++ b/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java
@@ -198,8 +198,8 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E
     private Reference<String> iconUrl = new BasicReference<String>();
 
     Map<String,Object> presentationAttributes = Maps.newLinkedHashMap();
-    Collection<AbstractPolicy> policies = Lists.newCopyOnWriteArrayList();
-    Collection<AbstractEnricher> enrichers = Lists.newCopyOnWriteArrayList();
+    private Collection<AbstractPolicy> policiesInternal = Lists.newCopyOnWriteArrayList();
+    private Collection<AbstractEnricher> enrichersInternal = Lists.newCopyOnWriteArrayList();
     Collection<Feed> feeds = Lists.newCopyOnWriteArrayList();
 
     // FIXME we do not currently support changing parents, but to implement a cluster that can shrink we need to support at least
@@ -221,7 +221,11 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E
     private final BasicSensorSupport sensors = new BasicSensorSupport();
 
     private final BasicSubscriptionSupport subscriptions = new BasicSubscriptionSupport();
-    
+
+    private final BasicPolicySupport policies = new BasicPolicySupport();
+
+    private final BasicEnricherSupport enrichers = new BasicEnricherSupport();
+
     /**
      * The config values of this entity. Updating this map should be done
      * via getConfig/setConfig.
@@ -1522,82 +1526,212 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E
     
     // -------- POLICIES --------------------
 
+    @Override 
+    @Beta
+    // the concrete type rather than an interface is returned because Groovy subclasses
+    // complain (incorrectly) if we return PolicySupportInternal
+    // TODO revert to PolicySupportInternal when groovy subclasses work without this (eg new groovy version)
+    public BasicPolicySupport policies() {
+        return policies;
+    }
+
+    @Override 
+    @Beta
+    // the concrete type rather than an interface is returned because Groovy subclasses
+    // complain (incorrectly) if we return EnricherSupportInternal
+    // TODO revert to EnricherSupportInternal when groovy subclasses work without this (eg new groovy version)
+    public BasicEnricherSupport enrichers() {
+        return enrichers;
+    }
+
+    /**
+     * Direct use of this class is strongly discouraged. It will become private in a future release,
+     * once {@link #policies()} is reverted to return {@link {PolicySupportInternal} instead of
+     * {@link BasicPolicySupport}.
+     */
+    @Beta
+    // TODO revert to private when config() is reverted to return SensorSupportInternal
+    public class BasicPolicySupport implements PolicySupportInternal {
+        
+        @Override
+        public Collection<Policy> getPolicies() {
+            return ImmutableList.<Policy>copyOf(policiesInternal);
+        }
+
+        @Override
+        public void addPolicy(Policy policy) {
+            Policy old = findApparentlyEqualAndWarnIfNotSameUniqueTag(policiesInternal, policy);
+            if (old!=null) {
+                LOG.debug("Removing "+old+" when adding "+policy+" to "+AbstractEntity.this);
+                removePolicy(old);
+            }
+            
+            CatalogUtils.setCatalogItemIdOnAddition(AbstractEntity.this, policy);
+            policiesInternal.add((AbstractPolicy)policy);
+            ((AbstractPolicy)policy).setEntity(AbstractEntity.this);
+            
+            getManagementSupport().getEntityChangeListener().onPolicyAdded(policy);
+            emit(AbstractEntity.POLICY_ADDED, new PolicyDescriptor(policy));
+        }
+
+        @Override
+        public <T extends Policy> T addPolicy(PolicySpec<T> spec) {
+            T policy = getManagementContext().getEntityManager().createPolicy(spec);
+            addPolicy(policy);
+            return policy;
+        }
+        
+        @Override
+        public boolean removePolicy(Policy policy) {
+            ((AbstractPolicy)policy).destroy();
+            boolean changed = policiesInternal.remove(policy);
+            
+            if (changed) {
+                getManagementSupport().getEntityChangeListener().onPolicyRemoved(policy);
+                emit(AbstractEntity.POLICY_REMOVED, new PolicyDescriptor(policy));
+            }
+            return changed;
+        }
+        
+        @Override
+        public boolean removeAllPolicies() {
+            boolean changed = false;
+            for (Policy policy : policiesInternal) {
+                removePolicy(policy);
+                changed = true;
+            }
+            return changed;
+        }
+    }
+
+    /**
+     * Direct use of this class is strongly discouraged. It will become private in a future release,
+     * once {@link #enrichers()} is reverted to return {@link EnricherSupportInternal} instead of
+     * {@link BasicEnricherSupport}.
+     */
+    @Beta
+    // TODO revert to private when config() is reverted to return SensorSupportInternal
+    public class BasicEnricherSupport implements EnricherSupportInternal {
+        @Override
+        public Collection<Enricher> getEnrichers() {
+            return ImmutableList.<Enricher>copyOf(enrichersInternal);
+        }
+
+        @Override
+        public <T extends Enricher> T addEnricher(EnricherSpec<T> spec) {
+            T enricher = getManagementContext().getEntityManager().createEnricher(spec);
+            addEnricher(enricher);
+            return enricher;
+        }
+
+        @Override
+        public void addEnricher(Enricher enricher) {
+            Enricher old = findApparentlyEqualAndWarnIfNotSameUniqueTag(enrichersInternal, enricher);
+            if (old!=null) {
+                LOG.debug("Removing "+old+" when adding "+enricher+" to "+AbstractEntity.this);
+                removeEnricher(old);
+            }
+            
+            CatalogUtils.setCatalogItemIdOnAddition(AbstractEntity.this, enricher);
+            enrichersInternal.add((AbstractEnricher) enricher);
+            ((AbstractEnricher)enricher).setEntity(AbstractEntity.this);
+            
+            getManagementSupport().getEntityChangeListener().onEnricherAdded(enricher);
+            // TODO Could add equivalent of AbstractEntity.POLICY_ADDED for enrichers; no use-case for that yet
+        }
+        
+        @Override
+        public boolean removeEnricher(Enricher enricher) {
+            ((AbstractEnricher)enricher).destroy();
+            boolean changed = enrichersInternal.remove(enricher);
+            
+            if (changed) {
+                getManagementSupport().getEntityChangeListener().onEnricherRemoved(enricher);
+            }
+            return changed;
+
+        }
+
+        @Override
+        public boolean removeAllEnrichers() {
+            boolean changed = false;
+            for (AbstractEnricher enricher : enrichersInternal) {
+                changed = removeEnricher(enricher) || changed;
+            }
+            return changed;
+        }
+    }
+    
+    /**
+     * @deprecated since 0.9.0; see {@link BasicPolicySupport#getPolicies()}; e.g. {@code policies().getPolicies()}
+     */
     @Override
+    @Deprecated
     public Collection<Policy> getPolicies() {
-        return ImmutableList.<Policy>copyOf(policies);
+        return policies().getPolicies();
     }
 
+    /**
+     * @deprecated since 0.9.0; see {@link BasicPolicySupport#addPolicy(Policy)}; e.g. {@code policies().addPolicy(policy)}
+     */
     @Override
+    @Deprecated
     public void addPolicy(Policy policy) {
-        Policy old = findApparentlyEqualAndWarnIfNotSameUniqueTag(policies, policy);
-        if (old!=null) {
-            LOG.debug("Removing "+old+" when adding "+policy+" to "+this);
-            removePolicy(old);
-        }
-        
-        CatalogUtils.setCatalogItemIdOnAddition(this, policy);
-        policies.add((AbstractPolicy)policy);
-        ((AbstractPolicy)policy).setEntity(this);
-        
-        getManagementSupport().getEntityChangeListener().onPolicyAdded(policy);
-        emit(AbstractEntity.POLICY_ADDED, new PolicyDescriptor(policy));
+        policies().addPolicy(policy);
     }
 
+    /**
+     * @deprecated since 0.9.0; see {@link BasicPolicySupport#addPolicy(PolicySpec)}; e.g. {@code policies().addPolicy(spec)}
+     */
     @Override
+    @Deprecated
     public <T extends Policy> T addPolicy(PolicySpec<T> spec) {
-        T policy = getManagementContext().getEntityManager().createPolicy(spec);
-        addPolicy(policy);
-        return policy;
+        return policies().addPolicy(spec);
     }
 
+    /**
+     * @deprecated since 0.9.0; see {@link BasicEnricherSupport#; e.g. {@code enrichers().addEnricher(spec)}
+     */
     @Override
+    @Deprecated
     public <T extends Enricher> T addEnricher(EnricherSpec<T> spec) {
-        T enricher = getManagementContext().getEntityManager().createEnricher(spec);
-        addEnricher(enricher);
-        return enricher;
+        return enrichers().addEnricher(spec);
     }
 
+    /**
+     * @deprecated since 0.9.0; see {@link BasicPolicySupport#removePolicy(Policy)}; e.g. {@code policies().removePolicy(policy)}
+     */
     @Override
+    @Deprecated
     public boolean removePolicy(Policy policy) {
-        ((AbstractPolicy)policy).destroy();
-        boolean changed = policies.remove(policy);
-        
-        if (changed) {
-            getManagementSupport().getEntityChangeListener().onPolicyRemoved(policy);
-            emit(AbstractEntity.POLICY_REMOVED, new PolicyDescriptor(policy));
-        }
-        return changed;
+        return policies().removePolicy(policy);
     }
     
+    /**
+     * @deprecated since 0.9.0; see {@link BasicPolicySupport#removeAllPolicies()}; e.g. {@code policies().removeAllPolicies()}
+     */
     @Override
+    @Deprecated
     public boolean removeAllPolicies() {
-        boolean changed = false;
-        for (Policy policy : policies) {
-            removePolicy(policy);
-            changed = true;
-        }
-        return changed;
+        return policies().removeAllPolicies();
     }
     
+    /**
+     * @deprecated since 0.9.0; see {@link BasicEnricherSupport#getEnrichers()}; e.g. {@code enrichers().getEnrichers()}
+     */
     @Override
+    @Deprecated
     public Collection<Enricher> getEnrichers() {
-        return ImmutableList.<Enricher>copyOf(enrichers);
+        return enrichers().getEnrichers();
     }
 
+    /**
+     * @deprecated since 0.9.0; see {@link BasicEnricherSupport#addEnricher(Enricher)}; e.g. {@code enrichers().addEnricher(enricher)}
+     */
     @Override
+    @Deprecated
     public void addEnricher(Enricher enricher) {
-        Enricher old = findApparentlyEqualAndWarnIfNotSameUniqueTag(enrichers, enricher);
-        if (old!=null) {
-            LOG.debug("Removing "+old+" when adding "+enricher+" to "+this);
-            removeEnricher(old);
-        }
-        
-        CatalogUtils.setCatalogItemIdOnAddition(this, enricher);
-        enrichers.add((AbstractEnricher) enricher);
-        ((AbstractEnricher)enricher).setEntity(this);
-        
-        getManagementSupport().getEntityChangeListener().onEnricherAdded(enricher);
-        // TODO Could add equivalent of AbstractEntity.POLICY_ADDED for enrichers; no use-case for that yet
+        enrichers().addEnricher(enricher);
     }
     
     private <T extends EntityAdjunct> T findApparentlyEqualAndWarnIfNotSameUniqueTag(Collection<? extends T> items, T newItem) {
@@ -1677,25 +1811,22 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E
         return null;
     }
 
+    /**
+     * @deprecated since 0.9.0; see {@link BasicEnricherSupport#removeEnricher(Enricher)}; e.g. {@code enrichers().removeEnricher(enricher)}
+     */
     @Override
+    @Deprecated
     public boolean removeEnricher(Enricher enricher) {
-        ((AbstractEnricher)enricher).destroy();
-        boolean changed = enrichers.remove(enricher);
-        
-        if (changed) {
-            getManagementSupport().getEntityChangeListener().onEnricherRemoved(enricher);
-        }
-        return changed;
-
+        return enrichers().removeEnricher(enricher);
     }
 
+    /**
+     * @deprecated since 0.9.0; see {@link BasicEnricherSupport#removeAllEnrichers()}; e.g. {@code enrichers().removeAllEnrichers()}
+     */
     @Override
+    @Deprecated
     public boolean removeAllEnrichers() {
-        boolean changed = false;
-        for (AbstractEnricher enricher : enrichers) {
-            changed = removeEnricher(enricher) || changed;
-        }
-        return changed;
+        return enrichers().removeAllEnrichers();
     }
     
     // -------- FEEDS --------------------

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e48bdaa7/core/src/main/java/org/apache/brooklyn/core/entity/EntityInternal.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/entity/EntityInternal.java b/core/src/main/java/org/apache/brooklyn/core/entity/EntityInternal.java
index 1ebcaf1..04fa88b 100644
--- a/core/src/main/java/org/apache/brooklyn/core/entity/EntityInternal.java
+++ b/core/src/main/java/org/apache/brooklyn/core/entity/EntityInternal.java
@@ -192,10 +192,18 @@ public interface EntityInternal extends BrooklynObjectInternal, EntityLocal, Reb
      */
     void requestPersist();
     
+    @Override
     SensorSupportInternal sensors();
 
+    @Override
     SubscriptionSupportInternal subscriptions();
 
+    @Override
+    PolicySupportInternal policies();
+
+    @Override
+    EnricherSupportInternal enrichers();
+
     @Beta
     public interface SensorSupportInternal extends Entity.SensorSupport {
         /**
@@ -237,4 +245,23 @@ public interface EntityInternal extends BrooklynObjectInternal, EntityLocal, Reb
     public interface SubscriptionSupportInternal extends Entity.SubscriptionSupport {
         SubscriptionContext getSubscriptionContext();
     }
+    
+    @Beta
+    public interface PolicySupportInternal extends Entity.PolicySupport {
+        /**
+         * Removes all policy from this entity. 
+         * @return True if any policies existed at this entity; false otherwise
+         */
+        boolean removeAllPolicies();
+    }
+    
+    @Beta
+    public interface EnricherSupportInternal extends Entity.EnricherSupport {
+        /**
+         * Removes all enricher from this entity.
+         * Use with caution as some entities automatically register enrichers; this will remove those enrichers as well.
+         * @return True if any enrichers existed at this entity; false otherwise
+         */
+        boolean removeAllEnrichers();
+    }
 }