brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [2/3] brooklyn-server git commit: Fix DSL config() to use parameter defaults
Date Mon, 07 Nov 2016 11:41:36 GMT
Fix DSL config() to use parameter defaults

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

Branch: refs/heads/master
Commit: ba62cad9fb3f02256d4c7d40efbd3d31a86b30bc
Parents: ae651a6
Author: Aled Sage <aled.sage@gmail.com>
Authored: Mon Nov 7 10:09:13 2016 +0000
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Mon Nov 7 10:09:13 2016 +0000

----------------------------------------------------------------------
 .../brooklyn/spi/dsl/methods/DslComponent.java  | 10 +++++---
 .../brooklyn/camp/brooklyn/dsl/DslTest.java     | 24 +++++++++++++++++++-
 .../AbstractConfigurationSupportInternal.java   |  3 +++
 .../org/apache/brooklyn/util/guava/Maybe.java   |  9 ++++++++
 4 files changed, 42 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ba62cad9/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java
----------------------------------------------------------------------
diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java
b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java
index 720dfae..5e42fbe 100644
--- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java
+++ b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java
@@ -29,6 +29,7 @@ import org.apache.brooklyn.api.sensor.AttributeSensor;
 import org.apache.brooklyn.api.sensor.Sensor;
 import org.apache.brooklyn.camp.brooklyn.BrooklynCampConstants;
 import org.apache.brooklyn.camp.brooklyn.spi.dsl.BrooklynDslDeferredSupplier;
+import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.core.entity.Entities;
 import org.apache.brooklyn.core.entity.EntityInternal;
@@ -361,8 +362,10 @@ public class DslComponent extends BrooklynDslDeferredSupplier<Entity>
{
             Maybe<Entity> targetEntityMaybe = component.getImmediately();
             if (targetEntityMaybe.isAbsent()) return Maybe.absent("Target entity not available");
             EntityInternal targetEntity = (EntityInternal) targetEntityMaybe.get();
-            
-            return targetEntity.config().getNonBlocking(ConfigKeys.newConfigKey(Object.class,
keyName));
+
+            ConfigKey<?> key = targetEntity.getEntityType().getConfigKey(keyName);
+            Maybe<? extends Object> result = targetEntity.config().getNonBlocking(key
!= null ? key : ConfigKeys.newConfigKey(Object.class, keyName));
+            return Maybe.<Object>cast(result);
         }
 
         @Override
@@ -376,7 +379,8 @@ public class DslComponent extends BrooklynDslDeferredSupplier<Entity>
{
                         @Override
                         public Object call() throws Exception {
                             Entity targetEntity = component.get();
-                            return targetEntity.getConfig(ConfigKeys.newConfigKey(Object.class,
keyName));
+                            ConfigKey<?> key = targetEntity.getEntityType().getConfigKey(keyName);
+                            return targetEntity.getConfig(key != null ? key : ConfigKeys.newConfigKey(Object.class,
keyName));
                         }})
                     .build();
         }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ba62cad9/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/dsl/DslTest.java
----------------------------------------------------------------------
diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/dsl/DslTest.java
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/dsl/DslTest.java
index 69a0aee..b4ff405 100644
--- a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/dsl/DslTest.java
+++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/dsl/DslTest.java
@@ -35,6 +35,7 @@ import org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.BrooklynDslCommon;
 import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.core.entity.EntityInternal;
+import org.apache.brooklyn.core.objs.BasicSpecParameter;
 import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
 import org.apache.brooklyn.core.test.entity.TestApplication;
 import org.apache.brooklyn.core.test.entity.TestEntity;
@@ -52,6 +53,7 @@ import org.testng.annotations.Test;
 import com.google.common.base.Function;
 import com.google.common.base.Functions;
 import com.google.common.base.Supplier;
+import com.google.common.collect.ImmutableList;
 import com.google.common.util.concurrent.ListenableScheduledFuture;
 import com.google.common.util.concurrent.ListeningScheduledExecutorService;
 import com.google.common.util.concurrent.MoreExecutors;
@@ -177,6 +179,21 @@ public class DslTest extends BrooklynAppUnitTestSupport {
     }
     
     @Test
+    public void testConfigUsesParameterDefaultValue() throws Exception {
+        final ConfigKey<String> configKey = ConfigKeys.newStringConfigKey("testConfig");
+        ConfigKey<String> configParam = ConfigKeys.newStringConfigKey("testParam",
"myDescription", "myDefaultConfigValue");
+        BrooklynDslDeferredSupplier<?> dsl = BrooklynDslCommon.config(configKey.getName());
+        Supplier<ConfigValuePair> valueSupplier = new Supplier<ConfigValuePair>()
{
+            @Override public ConfigValuePair get() {
+                return new ConfigValuePair(BrooklynDslCommon.config("testParam"), "myDefaultConfigValue");
+            }
+        };
+        new ConfigTestWorker(app, configKey, valueSupplier, dsl)
+                .childSpec(EntitySpec.create(TestEntity.class).parameters(ImmutableList.of(new
BasicSpecParameter<String>("myLabel", true, configParam))))
+                .run();
+    }
+    
+    @Test
     @SuppressWarnings({ "unchecked", "rawtypes" })
     public void testConfigImmediatelyDoesNotBlock() throws Exception {
         ConfigKey<String> configKey = ConfigKeys.newStringConfigKey("testConfig");
@@ -236,7 +253,7 @@ public class DslTest extends BrooklynAppUnitTestSupport {
         protected final TestApplication parent;
         protected final BrooklynDslDeferredSupplier<?> dsl;
         protected final Class<?> type;
-        protected EntitySpec<TestEntity> childSpec = EntitySpec.create(TestEntity.class);
+        protected EntitySpec<? extends TestEntity> childSpec = EntitySpec.create(TestEntity.class);
         protected int resolverIterations = MANY_RESOLVER_ITERATIONS;
         protected boolean satisfiedAsynchronously = false;
         private boolean wrapInTaskForImmediately = true;
@@ -247,6 +264,11 @@ public class DslTest extends BrooklynAppUnitTestSupport {
             this.type = checkNotNull(type, "type");
         }
 
+        public DslTestWorker childSpec(EntitySpec<? extends TestEntity> val) {
+            childSpec = val;
+            return this;
+        }
+        
         public DslTestWorker resolverIterations(int val) {
             resolverIterations = val;
             return this;

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ba62cad9/core/src/main/java/org/apache/brooklyn/core/objs/AbstractConfigurationSupportInternal.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/objs/AbstractConfigurationSupportInternal.java
b/core/src/main/java/org/apache/brooklyn/core/objs/AbstractConfigurationSupportInternal.java
index 3465ecf..57730b6 100644
--- a/core/src/main/java/org/apache/brooklyn/core/objs/AbstractConfigurationSupportInternal.java
+++ b/core/src/main/java/org/apache/brooklyn/core/objs/AbstractConfigurationSupportInternal.java
@@ -131,6 +131,9 @@ public abstract class AbstractConfigurationSupportInternal implements
BrooklynOb
      * See {@link #getNonBlockingResolvingStructuredKey(ConfigKey)}.
      */
     protected <T> Maybe<T> getNonBlockingResolvingSimple(ConfigKey<T> key)
{
+        // TODO See AbstractConfigMapImpl.getConfigImpl, for how it looks up the "container"
of the
+        // key, so that it gets the right context entity etc.
+        
         // getRaw returns Maybe(val) if the key was explicitly set (where val can be null)
         // or Absent if the config key was unset.
         Object unresolved = getRaw(key).or(key.getDefaultValue());

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ba62cad9/utils/common/src/main/java/org/apache/brooklyn/util/guava/Maybe.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/guava/Maybe.java b/utils/common/src/main/java/org/apache/brooklyn/util/guava/Maybe.java
index 37aa7c9..5270595 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/util/guava/Maybe.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/util/guava/Maybe.java
@@ -135,6 +135,15 @@ public abstract class Maybe<T> implements Serializable, Supplier<T>
{
         return ofAllowingNull(value);
     }
     
+    /**
+     * Casts the given value to the desired type. This is valid because {@link Maybe} is
immutable,
+     * so things like {@code Maybe<Object>} is a super-type of {@code Maybe<String>}.
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> Maybe<T> cast(Maybe<? extends T> value) {
+        return (Maybe<T>) value;
+    }
+    
     /** Converts the given {@link Maybe} to {@link Optional}, failing if this {@link Maybe}
contains null. */
     public Optional<T> toOptional() {
         if (isPresent()) return Optional.of(get());


Mime
View raw message