brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [3/5] brooklyn-server git commit: allow strings or sensors or deferred values to be supplied as arguments to $brooklyn:sensor(...)
Date Fri, 26 Aug 2016 10:55:00 GMT
allow strings or sensors or deferred values to be supplied as arguments to $brooklyn:sensor(...)


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

Branch: refs/heads/master
Commit: 1a930db915e9bdc6416365fca4eebbe023ce6804
Parents: 2ab4e5c
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Thu Aug 25 23:07:10 2016 +0100
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Thu Aug 25 23:07:10 2016 +0100

----------------------------------------------------------------------
 .../spi/dsl/methods/BrooklynDslCommon.java      |  2 +-
 .../brooklyn/spi/dsl/methods/DslComponent.java  | 63 +++++++++++++-------
 2 files changed, 42 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/1a930db9/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
----------------------------------------------------------------------
diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
index 84441db..71bba2b 100644
--- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
+++ b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
@@ -130,7 +130,7 @@ public class BrooklynDslCommon {
 
     /** Returns a {@link Sensor}, looking up the sensor on the context if available and using
that,
      * or else defining an untyped (Object) sensor */
-    public static BrooklynDslDeferredSupplier<Sensor<?>> sensor(String sensorName)
{
+    public static BrooklynDslDeferredSupplier<Sensor<?>> sensor(Object sensorName)
{
         return new DslComponent(Scope.THIS, "").sensor(sensorName);
     }
     

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/1a930db9/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 bc5817e..a29ce25 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
@@ -20,18 +20,10 @@ package org.apache.brooklyn.camp.brooklyn.spi.dsl.methods;
 
 import java.util.NoSuchElementException;
 import java.util.concurrent.Callable;
-
-import com.google.common.base.CaseFormat;
-import com.google.common.base.Converter;
-import com.google.common.base.Objects;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Predicate;
-import com.google.common.base.Predicates;
-import com.google.common.collect.Iterables;
-import com.google.common.util.concurrent.Callables;
+import java.util.concurrent.ExecutionException;
 
 import org.apache.brooklyn.api.entity.Entity;
+import org.apache.brooklyn.api.mgmt.ExecutionContext;
 import org.apache.brooklyn.api.mgmt.Task;
 import org.apache.brooklyn.api.sensor.AttributeSensor;
 import org.apache.brooklyn.api.sensor.Sensor;
@@ -50,6 +42,16 @@ import org.apache.brooklyn.util.core.task.Tasks;
 import org.apache.brooklyn.util.guava.Maybe;
 import org.apache.brooklyn.util.text.StringEscapes.JavaStringEscapes;
 
+import com.google.common.base.CaseFormat;
+import com.google.common.base.Converter;
+import com.google.common.base.Objects;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
+import com.google.common.collect.Iterables;
+import com.google.common.util.concurrent.Callables;
+
 public class DslComponent extends BrooklynDslDeferredSupplier<Entity> {
 
     private static final long serialVersionUID = -7715984495268724954L;
@@ -210,6 +212,7 @@ public class DslComponent extends BrooklynDslDeferredSupplier<Entity>
{
         return new EntityId(this);
     }
     protected static class EntityId extends BrooklynDslDeferredSupplier<Object> {
+        private static final long serialVersionUID = -419427634694971033L;
         private final DslComponent component;
 
         public EntityId(DslComponent component) {
@@ -327,17 +330,17 @@ public class DslComponent extends BrooklynDslDeferredSupplier<Entity>
{
         }
     }
     
-    public BrooklynDslDeferredSupplier<Sensor<?>> sensor(final String sensorName)
{
-        return new DslSensorSupplier(this, sensorName);
+    public BrooklynDslDeferredSupplier<Sensor<?>> sensor(final Object sensorIndicator)
{
+        return new DslSensorSupplier(this, sensorIndicator);
     }
     protected final static class DslSensorSupplier extends BrooklynDslDeferredSupplier<Sensor<?>>
{
         private final DslComponent component;
-        private final String sensorName;
+        private final Object sensorName;
         private static final long serialVersionUID = -4735177561947722511L;
 
-        public DslSensorSupplier(DslComponent component, String sensorName) {
+        public DslSensorSupplier(DslComponent component, Object sensorIndicator) {
             this.component = Preconditions.checkNotNull(component);
-            this.sensorName = sensorName;
+            this.sensorName = sensorIndicator;
         }
 
         @Override
@@ -345,13 +348,26 @@ public class DslComponent extends BrooklynDslDeferredSupplier<Entity>
{
             return Tasks.<Sensor<?>>builder().displayName("looking up sensor
for "+sensorName).dynamic(false).body(new Callable<Sensor<?>>() {
                 @Override
                 public Sensor<?> call() throws Exception {
-                    Entity targetEntity = component.get();
-                    Sensor<?> result = null;
-                    if (targetEntity!=null) {
-                        result = targetEntity.getEntityType().getSensor(sensorName);
+                    return resolve(sensorName, false);
+                }
+                
+                public Sensor<?> resolve(Object si, boolean resolved) throws ExecutionException,
InterruptedException {
+                    if (si instanceof Sensor) return (Sensor<?>)si;
+                    if (si instanceof String) {
+                        Entity targetEntity = component.get();
+                        Sensor<?> result = null;
+                        if (targetEntity!=null) {
+                            result = targetEntity.getEntityType().getSensor((String)si);
+                        }
+                        if (result!=null) return result;
+                        return Sensors.newSensor(Object.class, (String)si);
+                    }
+                    if (!resolved) {
+                        // attempt to resolve, and recurse
+                        final ExecutionContext executionContext = ((EntityInternal)entity()).getExecutionContext();
+                        return resolve(Tasks.resolveDeepValue(si, Object.class, executionContext),
true);
                     }
-                    if (result!=null) return result;
-                    return Sensors.newSensor(Object.class, sensorName);
+                    throw new IllegalStateException("Cannot resolve '"+sensorName+"' as a
sensor");
                 }
             }).build();
         }
@@ -373,7 +389,10 @@ public class DslComponent extends BrooklynDslDeferredSupplier<Entity>
{
         @Override
         public String toString() {
             return (component.scope==Scope.THIS ? "" : component.toString()+".") + 
-                "sensor("+JavaStringEscapes.wrapJavaString(sensorName)+")";
+                "sensor("+
+                    (sensorName instanceof String ? JavaStringEscapes.wrapJavaString((String)sensorName)
:
+                        sensorName instanceof Sensor ? JavaStringEscapes.wrapJavaString(((Sensor<?>)sensorName).getName())
:
+                        sensorName)+")";
         }
     }
 


Mime
View raw message