brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [11/21] incubator-brooklyn git commit: a few more goodies, coercing strings and maps to location and other fixes
Date Sat, 15 Nov 2014 00:05:20 GMT
a few more goodies, coercing strings and maps to location and other fixes

where new Object() was used in persistence, it would not serialize nicely, and also it would
not deserialize to the same (NONE instance) value; fix the logic for BasicParameterType defaults
including null, and then when testing discovered problems if we passed Strings in, so fix
that


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

Branch: refs/heads/master
Commit: f90faf4b1a9838b726d1ccec76fc2acb2304df47
Parents: b9c1b6f
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Thu Nov 13 04:50:23 2014 +0000
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Thu Nov 13 23:38:54 2014 +0000

----------------------------------------------------------------------
 .../brooklyn/location/LocationRegistry.java     |  4 +-
 .../entity/basic/BasicParameterType.java        | 22 ++++++---
 .../java/brooklyn/location/basic/Locations.java | 48 ++++++++++++++++++++
 .../software/MachineLifecycleEffectorTasks.java |  3 +-
 4 files changed, 69 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f90faf4b/api/src/main/java/brooklyn/location/LocationRegistry.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/brooklyn/location/LocationRegistry.java b/api/src/main/java/brooklyn/location/LocationRegistry.java
index eea5ce4..cc2c251 100644
--- a/api/src/main/java/brooklyn/location/LocationRegistry.java
+++ b/api/src/main/java/brooklyn/location/LocationRegistry.java
@@ -22,6 +22,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.NoSuchElementException;
 
+import javax.annotation.Nullable;
+
 import brooklyn.util.guava.Maybe;
 
 import com.google.common.annotations.Beta;
@@ -98,7 +100,7 @@ public interface LocationRegistry {
     
     /** As {@link #resolve(String, Boolean, Map)}, but unwrapped
      * @throws NoSuchElementException if the spec cannot be resolved */
-    public Location resolve(String spec, Map locationFlags);
+    public Location resolve(String spec, @Nullable Map locationFlags);
     
     /** as {@link #resolve(String)} but returning null (never throwing)
      * @deprecated since 0.7.0 use {@link #resolve(String, Boolean, Map)} */

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f90faf4b/core/src/main/java/brooklyn/entity/basic/BasicParameterType.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/basic/BasicParameterType.java b/core/src/main/java/brooklyn/entity/basic/BasicParameterType.java
index 81c5d24..76846f5 100644
--- a/core/src/main/java/brooklyn/entity/basic/BasicParameterType.java
+++ b/core/src/main/java/brooklyn/entity/basic/BasicParameterType.java
@@ -31,12 +31,14 @@ public class BasicParameterType<T> implements ParameterType<T>
{
     private String name;
     private Class<T> type;
     private String description;
-    private T defaultValue = (T) NONE;
+    private Boolean hasDefaultValue = null;
+    private T defaultValue = null;
 
     public BasicParameterType() {
         this(Collections.emptyMap());
     }
     
+    @SuppressWarnings("unchecked")
     public BasicParameterType(Map<?, ?> arguments) {
         if (arguments.containsKey("name")) name = (String) arguments.get("name");
         if (arguments.containsKey("type")) type = (Class<T>) arguments.get("type");
@@ -45,22 +47,29 @@ public class BasicParameterType<T> implements ParameterType<T>
{
     }
 
     public BasicParameterType(String name, Class<T> type) {
-        this(name, type, null, (T) NONE);
+        this(name, type, null, null, false);
     }
     
     public BasicParameterType(String name, Class<T> type, String description) {
-        this(name, type, description, (T) NONE);
+        this(name, type, description, null, false);
     }
     
     public BasicParameterType(String name, Class<T> type, String description, T defaultValue)
{
+        this(name, type, description, defaultValue, true);
+    }
+    
+    public BasicParameterType(String name, Class<T> type, String description, T defaultValue,
boolean hasDefaultValue) {
         this.name = name;
         this.type = type;
         this.description = description;
         this.defaultValue = defaultValue;
+        if (defaultValue!=null && !defaultValue.getClass().equals(Object.class))
{
+            // if default value is null (or is an Object, which is ambiguous on resolution
to to rebind), 
+            // don't bother to set this as it creates noise in the persistence files
+            this.hasDefaultValue = hasDefaultValue;
+        }
     }
 
-    private static Object NONE = new Object();
-    
     @Override
     public String getName() { return name; }
 
@@ -79,7 +88,8 @@ public class BasicParameterType<T> implements ParameterType<T>
{
     }
 
     public boolean hasDefaultValue() {
-        return defaultValue != NONE;
+        // a new Object() was previously used to indicate no default value, but that doesn't
work well across serialization boundaries!
+        return hasDefaultValue!=null ? hasDefaultValue : defaultValue!=null && !defaultValue.getClass().equals(Object.class);
     }
     
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f90faf4b/core/src/main/java/brooklyn/location/basic/Locations.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/location/basic/Locations.java b/core/src/main/java/brooklyn/location/basic/Locations.java
index a096168..c74db74 100644
--- a/core/src/main/java/brooklyn/location/basic/Locations.java
+++ b/core/src/main/java/brooklyn/location/basic/Locations.java
@@ -20,7 +20,9 @@ package brooklyn.location.basic;
 
 import java.io.Serializable;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -32,7 +34,9 @@ import brooklyn.location.LocationSpec;
 import brooklyn.location.MachineLocation;
 import brooklyn.management.LocationManager;
 import brooklyn.management.ManagementContext;
+import brooklyn.util.collections.MutableList;
 import brooklyn.util.guava.Maybe;
+import brooklyn.util.yaml.Yamls;
 
 import com.google.common.collect.ImmutableList;
 
@@ -105,4 +109,48 @@ public class Locations {
             managementContext.getLocationManager().manage(loc);
         }
     }
+
+    public static Location coerce(ManagementContext mgmt, Object rawO) {
+        if (rawO instanceof Location)
+            return (Location)rawO;
+        
+        Object raw = rawO;
+        if (raw instanceof String)
+            raw = Yamls.parseAll((String)raw).iterator().next();
+        
+        String name;
+        Map<?, ?> flags = null;
+        if (raw instanceof Map) {
+            // for yaml, take the key, and merge with locationFlags
+            Map<?,?> tm = ((Map<?,?>)raw);
+            if (tm.size()!=1) {
+                throw new IllegalArgumentException("Location "+rawO+" is invalid; maps must
have only one key, being the location spec string");
+            }
+            name = (String) tm.keySet().iterator().next();
+            flags = (Map<?, ?>) tm.values().iterator().next();
+            
+        } else if (raw instanceof String) {
+            name = (String)raw;
+            
+        } else {
+            throw new IllegalArgumentException("Location "+rawO+" is invalid; can only parse
strings or maps");
+        }
+        return mgmt.getLocationRegistry().resolve(name, flags);
+    }
+    
+    public static Collection<? extends Location> coerceToCollection(ManagementContext
mgmt, Object rawO) {
+        Object raw = rawO;
+        if (raw instanceof Collection) {
+            List<Location> result = MutableList.<Location>of();
+            for (Object o: (Collection<?>)raw)
+                result.add(coerce(mgmt, o));
+            return result;
+        }
+        if (raw instanceof String) {
+            raw = Yamls.parseAll((String)raw).iterator().next();
+            if (raw instanceof Collection)
+                return coerceToCollection(mgmt, raw);
+        }
+        return Collections.singletonList( coerce(mgmt, raw) );
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f90faf4b/software/base/src/main/java/brooklyn/entity/software/MachineLifecycleEffectorTasks.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/brooklyn/entity/software/MachineLifecycleEffectorTasks.java
b/software/base/src/main/java/brooklyn/entity/software/MachineLifecycleEffectorTasks.java
index 3aac2a7..65b0428 100644
--- a/software/base/src/main/java/brooklyn/entity/software/MachineLifecycleEffectorTasks.java
+++ b/software/base/src/main/java/brooklyn/entity/software/MachineLifecycleEffectorTasks.java
@@ -152,7 +152,8 @@ public abstract class MachineLifecycleEffectorTasks {
         return new EffectorBody<Void>() {
             @Override
             public Void call(ConfigBag parameters) {
-                Collection<? extends Location> locations = parameters.get(LOCATIONS);
+                Object locationsRaw = parameters.getStringKey(LOCATIONS.getName());
+                Collection<? extends Location> locations = Locations.coerceToCollection(entity().getManagementContext(),
locationsRaw);
                 if (locations==null) {
                     // null/empty will mean to inherit from parent
                     locations = Collections.emptyList();


Mime
View raw message