brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From grk...@apache.org
Subject [06/13] git commit: Use coerce method with TypeToken when setting configuration
Date Sun, 05 Oct 2014 22:44:30 GMT
Use coerce method with TypeToken when setting configuration


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

Branch: refs/heads/master
Commit: 9f1920b87b10fd71f9a7bea380f33795feae3f67
Parents: 8c47cd8
Author: Andrew Kennedy <grkvlt@apache.org>
Authored: Tue Sep 23 17:05:43 2014 +0100
Committer: Andrew Kennedy <grkvlt@apache.org>
Committed: Sun Oct 5 19:45:53 2014 +0100

----------------------------------------------------------------------
 .../brooklyn/entity/basic/EntityConfigMap.java   |  2 +-
 .../brooklyn/entity/basic/EntityFunctions.java   |  2 +-
 .../rebind/BasicLocationRebindSupport.java       |  2 +-
 .../brooklyn/policy/basic/ConfigMapImpl.java     |  2 +-
 .../java/brooklyn/util/flags/TypeCoercions.java  | 19 ++++++++-----------
 .../brooklyn/entity/chef/ChefAttributeFeed.java  |  2 +-
 .../rest/resources/EntityConfigResource.java     |  4 ++--
 .../rest/resources/PolicyConfigResource.java     |  2 +-
 8 files changed, 16 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/9f1920b8/core/src/main/java/brooklyn/entity/basic/EntityConfigMap.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/basic/EntityConfigMap.java b/core/src/main/java/brooklyn/entity/basic/EntityConfigMap.java
index 3f5884b..4b9654b 100644
--- a/core/src/main/java/brooklyn/entity/basic/EntityConfigMap.java
+++ b/core/src/main/java/brooklyn/entity/basic/EntityConfigMap.java
@@ -194,7 +194,7 @@ public class EntityConfigMap implements ConfigMap {
             val = v;
         } else {
             try {
-                val = TypeCoercions.coerce(v, key.getType());
+                val = TypeCoercions.coerce(v, key.getTypeToken());
             } catch (Exception e) {
                 throw new IllegalArgumentException("Cannot coerce or set "+v+" to "+key,
e);
             }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/9f1920b8/core/src/main/java/brooklyn/entity/basic/EntityFunctions.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/basic/EntityFunctions.java b/core/src/main/java/brooklyn/entity/basic/EntityFunctions.java
index 4b8d8cd..ebd4c39 100644
--- a/core/src/main/java/brooklyn/entity/basic/EntityFunctions.java
+++ b/core/src/main/java/brooklyn/entity/basic/EntityFunctions.java
@@ -92,7 +92,7 @@ public class EntityFunctions {
                     } else if (value==Entities.REMOVE) {
                         ((EntityInternal)input).removeAttribute(sensor);
                     } else {
-                        value = TypeCoercions.coerce(value, sensor.getType());
+                        value = TypeCoercions.coerce(value, sensor.getTypeToken());
                         ((EntityInternal)input).setAttribute(sensor, value);
                     }
                 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/9f1920b8/core/src/main/java/brooklyn/entity/rebind/BasicLocationRebindSupport.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/rebind/BasicLocationRebindSupport.java b/core/src/main/java/brooklyn/entity/rebind/BasicLocationRebindSupport.java
index 69463e2..bc5841e 100644
--- a/core/src/main/java/brooklyn/entity/rebind/BasicLocationRebindSupport.java
+++ b/core/src/main/java/brooklyn/entity/rebind/BasicLocationRebindSupport.java
@@ -88,7 +88,7 @@ public class BasicLocationRebindSupport extends AbstractBrooklynObjectRebindSupp
                 // And use magic of setFieldFromFlag's magic to either set config or field
as appropriate.
                 if (ConfigKey.class.isAssignableFrom(fieldType)) {
                     ConfigKey<?> configKey = (ConfigKey<?>) FlagUtils.getField(location,
field);
-                    value = TypeCoercions.coerce(entry.getValue(), configKey.getType());
+                    value = TypeCoercions.coerce(entry.getValue(), configKey.getTypeToken());
                 } else {
                     value = TypeCoercions.coerce(entry.getValue(), fieldType);
                 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/9f1920b8/core/src/main/java/brooklyn/policy/basic/ConfigMapImpl.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/policy/basic/ConfigMapImpl.java b/core/src/main/java/brooklyn/policy/basic/ConfigMapImpl.java
index d942603..686c4c2 100644
--- a/core/src/main/java/brooklyn/policy/basic/ConfigMapImpl.java
+++ b/core/src/main/java/brooklyn/policy/basic/ConfigMapImpl.java
@@ -146,7 +146,7 @@ public class ConfigMapImpl implements brooklyn.config.ConfigMap {
             val = v;
         } else {
             try {
-                val = TypeCoercions.coerce(v, key.getType());
+                val = TypeCoercions.coerce(v, key.getTypeToken());
             } catch (Exception e) {
                 throw new IllegalArgumentException("Cannot coerce or set "+v+" to "+key,
e);
             }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/9f1920b8/core/src/main/java/brooklyn/util/flags/TypeCoercions.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/util/flags/TypeCoercions.java b/core/src/main/java/brooklyn/util/flags/TypeCoercions.java
index 82a0241..4f3f362 100644
--- a/core/src/main/java/brooklyn/util/flags/TypeCoercions.java
+++ b/core/src/main/java/brooklyn/util/flags/TypeCoercions.java
@@ -43,7 +43,6 @@ import javax.annotation.concurrent.GuardedBy;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.testng.collections.Maps;
 
 import brooklyn.entity.basic.ClosureEntityFactory;
 import brooklyn.entity.basic.ConfigurableEntityFactory;
@@ -63,7 +62,6 @@ import brooklyn.util.text.Strings;
 import brooklyn.util.time.Duration;
 import brooklyn.util.yaml.Yamls;
 
-import com.google.api.client.util.Lists;
 import com.google.common.base.CaseFormat;
 import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
@@ -71,6 +69,8 @@ import com.google.common.base.Predicate;
 import com.google.common.collect.HashBasedTable;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 import com.google.common.collect.Table;
 import com.google.common.net.HostAndPort;
@@ -128,14 +128,13 @@ public class TypeCoercions {
                 if (arguments.length != 1) {
                     throw new IllegalStateException("Unexpected number of parameters in collection
type: " + arguments);
                 }
-                Collection coerced = Lists.newArrayList();
-                TypeToken<?> parameterType = TypeToken.of(arguments[0]);
+                Collection coerced = Lists.newLinkedList();
+                TypeToken<?> listEntryType = TypeToken.of(arguments[0]);
                 for (Object entry : (Iterable<?>) value) {
-                    Maybe<?> result = tryCoerce(entry, parameterType);
-                    coerced.add(result.get());
+                    coerced.add(coerce(entry, listEntryType));
                 }
                 if (Set.class.isAssignableFrom(targetType)) {
-                    return (T) Sets.newHashSet(coerced);
+                    return (T) Sets.newLinkedHashSet(coerced);
                 } else {
                     return (T) coerced;
                 }
@@ -144,13 +143,11 @@ public class TypeCoercions {
                 if (arguments.length != 2) {
                     throw new IllegalStateException("Unexpected number of parameters in map
type: " + arguments);
                 }
-                Map coerced = Maps.newHashMap();
+                Map coerced = Maps.newLinkedHashMap();
                 TypeToken<?> mapKeyType = TypeToken.of(arguments[0]);
                 TypeToken<?> mapValueType = TypeToken.of(arguments[1]);
                 for (Map.Entry entry : ((Map<?,?>) value).entrySet()) {
-                    Maybe<?> keyResult = tryCoerce(entry.getKey(), mapKeyType);
-                    Maybe<?> valueResult = tryCoerce(entry.getValue(), mapValueType);
-                    coerced.put(keyResult.get(), valueResult.get());
+                    coerced.put(coerce(entry.getKey(), mapKeyType),  coerce(entry.getValue(),
mapValueType));
                 }
                 return (T) coerced;
             }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/9f1920b8/software/base/src/main/java/brooklyn/entity/chef/ChefAttributeFeed.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/brooklyn/entity/chef/ChefAttributeFeed.java b/software/base/src/main/java/brooklyn/entity/chef/ChefAttributeFeed.java
index e27b391..d79262a 100644
--- a/software/base/src/main/java/brooklyn/entity/chef/ChefAttributeFeed.java
+++ b/software/base/src/main/java/brooklyn/entity/chef/ChefAttributeFeed.java
@@ -347,7 +347,7 @@ public class ChefAttributeFeed extends AbstractFeed {
                     }
                 }
                 if (elementForSensor != null) {
-                    entity.setAttribute((AttributeSensor)sensor, TypeCoercions.coerce(elementForSensor.getAsString(),
sensor.getType()));
+                    entity.setAttribute((AttributeSensor)sensor, TypeCoercions.coerce(elementForSensor.getAsString(),
sensor.getTypeToken()));
                 } else {
                     log.debug("Entity {}: no Chef attribute matching {}; setting sensor {}
to null", new Object[]{
                             entity.getDisplayName(),

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/9f1920b8/usage/rest-server/src/main/java/brooklyn/rest/resources/EntityConfigResource.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/main/java/brooklyn/rest/resources/EntityConfigResource.java
b/usage/rest-server/src/main/java/brooklyn/rest/resources/EntityConfigResource.java
index 9cf484e..ec09c9a 100644
--- a/usage/rest-server/src/main/java/brooklyn/rest/resources/EntityConfigResource.java
+++ b/usage/rest-server/src/main/java/brooklyn/rest/resources/EntityConfigResource.java
@@ -127,10 +127,10 @@ public class EntityConfigResource extends AbstractBrooklynRestResource
implement
       }
 
       ConfigKey ck = findConfig(entity, configName);
-      ((EntityInternal)entity).setConfig(ck, TypeCoercions.coerce(newValue, ck.getType()));
+      ((EntityInternal) entity).setConfig(ck, TypeCoercions.coerce(newValue, ck.getTypeToken()));
       if (Boolean.TRUE.equals(recurse)) {
           for (Entity e2: Entities.descendants(entity, Predicates.alwaysTrue(), false)) {
-              ((EntityInternal)e2).setConfig(ck, newValue);
+              ((EntityInternal) e2).setConfig(ck, newValue);
           }
       }
   }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/9f1920b8/usage/rest-server/src/main/java/brooklyn/rest/resources/PolicyConfigResource.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/main/java/brooklyn/rest/resources/PolicyConfigResource.java
b/usage/rest-server/src/main/java/brooklyn/rest/resources/PolicyConfigResource.java
index 4f03168..4136daf 100644
--- a/usage/rest-server/src/main/java/brooklyn/rest/resources/PolicyConfigResource.java
+++ b/usage/rest-server/src/main/java/brooklyn/rest/resources/PolicyConfigResource.java
@@ -95,7 +95,7 @@ public class PolicyConfigResource extends AbstractBrooklynRestResource implement
       ConfigKey<?> ck = policy.getPolicyType().getConfigKey(configKeyName);
       if (ck == null) throw WebResourceUtils.notFound("Cannot find config key '%s' in policy
'%s' of entity '%s'", configKeyName, policy, entityToken);
       
-      policy.setConfig((ConfigKey)ck, TypeCoercions.coerce(value, ck.getType()));
+      policy.setConfig((ConfigKey) ck, TypeCoercions.coerce(value, ck.getTypeToken()));
       
       return Response.status(Response.Status.OK).build();
   }


Mime
View raw message