brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [2/6] incubator-brooklyn git commit: Adds regexReplacement to BrooklynDslCommon
Date Wed, 28 Oct 2015 12:05:17 GMT
Adds regexReplacement to BrooklynDslCommon


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

Branch: refs/heads/master
Commit: c76db103547530315b34e7e9bf0830318fb50818
Parents: d53bd18
Author: Martin Harris <github@nakomis.com>
Authored: Fri Sep 18 13:11:37 2015 +0100
Committer: Martin Harris <github@nakomis.com>
Committed: Thu Oct 15 15:40:41 2015 +0100

----------------------------------------------------------------------
 .../core/sensor/DependentConfiguration.java     | 71 +++++++++++++++++++-
 .../brooklyn/enricher/stock/Transformer.java    |  1 +
 .../core/entity/DependentConfigurationTest.java | 25 +++++++
 .../spi/dsl/methods/BrooklynDslCommon.java      | 48 +++++++++++++
 4 files changed, 144 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c76db103/core/src/main/java/org/apache/brooklyn/core/sensor/DependentConfiguration.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/sensor/DependentConfiguration.java
b/core/src/main/java/org/apache/brooklyn/core/sensor/DependentConfiguration.java
index ddca05c..2bc5b6e 100644
--- a/core/src/main/java/org/apache/brooklyn/core/sensor/DependentConfiguration.java
+++ b/core/src/main/java/org/apache/brooklyn/core/sensor/DependentConfiguration.java
@@ -180,7 +180,7 @@ public class DependentConfiguration {
     public static <T> T waitInTaskForAttributeReady(final Entity source, final AttributeSensor<T>
sensor, Predicate<? super T> ready, List<AttributeAndSensorCondition<?>>
abortConditions, String blockingDetails) {
         return new WaitInTaskForAttributeReady<T,T>(source, sensor, ready, abortConditions,
blockingDetails).call();
     }
-    
+
     protected static class WaitInTaskForAttributeReady<T,V> implements Callable<V>
{
 
         /* This is a change since before Oct 2014. Previously it would continue to poll,
@@ -483,6 +483,75 @@ public class DependentConfiguration {
             taskArgs);
     }
 
+    public static Task<Function<String, String>> regexReplacement(Object pattern,
Object replacement) {
+        List<TaskAdaptable<Object>> taskArgs = getTaskAdaptable(pattern, replacement);
+        Function<List<Object>, Function<String, String>> transformer =
new RegexTransformer(pattern, replacement);
+        return transformMultiple(
+                MutableMap.of("displayName", String.format("creating regex replacement function
(%s:%s)", pattern, replacement)),
+                transformer,
+                taskArgs
+        );
+    }
+
+    private static List<TaskAdaptable<Object>> getTaskAdaptable(Object... args){
+        List<TaskAdaptable<Object>> taskArgs = Lists.newArrayList();
+        for (Object arg: args) {
+            if (arg instanceof TaskAdaptable) {
+                taskArgs.add((TaskAdaptable<Object>)arg);
+            } else if (arg instanceof TaskFactory) {
+                taskArgs.add(((TaskFactory<TaskAdaptable<Object>>)arg).newTask());
+            }
+        }
+        return taskArgs;
+    }
+
+    public static class RegexTransformer  implements Function<List<Object>, Function<String,
String>> {
+
+        private final Object pattern;
+        private final Object replacement;
+
+        public RegexTransformer(Object pattern, Object replacement){
+            this.pattern = pattern;
+            this.replacement = replacement;
+        }
+
+        @Override
+        public Function<String, String> apply(List<Object> input) {
+            Iterator<?> taskArgsIterator = input.iterator();
+            return new RegexReplacer(resolveArgument(pattern, taskArgsIterator), resolveArgument(replacement,
taskArgsIterator));
+        }
+
+        private String resolveArgument(Object argument, Iterator<?> taskArgsIterator)
{
+            Object resolvedArgument;
+            if (argument instanceof TaskAdaptable || argument instanceof TaskFactory) {
+                resolvedArgument = taskArgsIterator.next();
+            } else if (argument instanceof DeferredSupplier) {
+                resolvedArgument = ((DeferredSupplier<?>) argument).get();
+            } else {
+                resolvedArgument = argument;
+            }
+            return String.valueOf(resolvedArgument);
+        }
+
+    }
+
+    public static class RegexReplacer implements Function<String, String> {
+
+        private final String pattern;
+        private final String replacement;
+
+        public RegexReplacer(String pattern, String replacement) {
+            this.pattern = pattern;
+            this.replacement = replacement;
+        }
+
+        @Nullable
+        @Override
+        public String apply(@Nullable String s) {
+            return Strings.replaceAllRegex(s, pattern, replacement);
+        }
+    }
+
     /** returns a task for parallel execution returning a list of values for the given sensor
for the given entity list, 
      * optionally when the values satisfy a given readiness predicate (defaulting to groovy
truth if not supplied) */
     public static <T> Task<List<T>> listAttributesWhenReady(AttributeSensor<T>
sensor, Iterable<Entity> entities) {

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c76db103/core/src/main/java/org/apache/brooklyn/enricher/stock/Transformer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/enricher/stock/Transformer.java b/core/src/main/java/org/apache/brooklyn/enricher/stock/Transformer.java
index ef23ab4..815b07d 100644
--- a/core/src/main/java/org/apache/brooklyn/enricher/stock/Transformer.java
+++ b/core/src/main/java/org/apache/brooklyn/enricher/stock/Transformer.java
@@ -45,6 +45,7 @@ public class Transformer<T,U> extends AbstractTransformer<T,U>
{
     public static ConfigKey<Function<?, ?>> TRANSFORMATION_FROM_EVENT = ConfigKeys.newConfigKey(new
TypeToken<Function<?, ?>>() {}, "enricher.transformation.fromevent");
     
     public Transformer() {
+        System.out.println("Creating");
     }
 
     /** returns a function for transformation, for immediate use only (not for caching, as
it may change) */

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c76db103/core/src/test/java/org/apache/brooklyn/core/entity/DependentConfigurationTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/entity/DependentConfigurationTest.java
b/core/src/test/java/org/apache/brooklyn/core/entity/DependentConfigurationTest.java
index acd3a7d..43c37e7 100644
--- a/core/src/test/java/org/apache/brooklyn/core/entity/DependentConfigurationTest.java
+++ b/core/src/test/java/org/apache/brooklyn/core/entity/DependentConfigurationTest.java
@@ -31,11 +31,14 @@ import java.util.concurrent.atomic.AtomicReference;
 
 import org.apache.brooklyn.api.entity.EntitySpec;
 import org.apache.brooklyn.api.mgmt.Task;
+import org.apache.brooklyn.api.sensor.AttributeSensor;
+import org.apache.brooklyn.api.sensor.Sensor;
 import org.apache.brooklyn.core.entity.Attributes;
 import org.apache.brooklyn.core.entity.Entities;
 import org.apache.brooklyn.core.entity.lifecycle.Lifecycle;
 import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic;
 import org.apache.brooklyn.core.sensor.DependentConfiguration;
+import org.apache.brooklyn.core.sensor.Sensors;
 import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
 import org.apache.brooklyn.core.test.entity.TestEntity;
 import org.apache.brooklyn.test.Asserts;
@@ -110,6 +113,28 @@ public class DependentConfigurationTest extends BrooklynAppUnitTestSupport
{
     }
 
     @Test
+    public void testRegexReplacementWithStrings() throws Exception {
+        Task<Function<String, String>> task = DependentConfiguration.regexReplacement("foo",
"bar");
+        submit(task);
+        Function<String, String> regexReplacer = task.get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
+        Assert.assertEquals(regexReplacer.apply("somefootext"), "somebartext");
+    }
+
+    @Test
+    public void testRegexReplacementWithAttributeWhenReady() throws Exception {
+        AttributeSensor<Object> replacementSensor = Sensors.newSensor(Object.class,
"test.replacement");
+        DependentConfiguration.attributeWhenReady(entity, TestEntity.NAME);
+        Task<String> pattern = DependentConfiguration.attributeWhenReady(entity, TestEntity.NAME);
+        Task<Object> replacement = DependentConfiguration.attributeWhenReady(entity,
replacementSensor);
+        Task<Function<String, String>> task = DependentConfiguration.regexReplacement(pattern,
replacement);
+        submit(task);
+        entity.sensors().set(TestEntity.NAME, "foo");
+        entity.sensors().set(replacementSensor, "bar");
+        Function<String, String> regexReplacer = task.get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
+        Assert.assertEquals(regexReplacer.apply("somefootext"), "somebartext");
+    }
+
+    @Test
     public void testAttributeWhenReady() throws Exception {
         final Task<String> t = submit(DependentConfiguration.attributeWhenReady(entity,
TestEntity.NAME));
         assertNotDoneContinually(t);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c76db103/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
index 92dc14e..5139b06 100644
--- a/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
+++ b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
@@ -23,6 +23,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.Callable;
 
+import javax.annotation.Nullable;
+
 import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.mgmt.Task;
 import org.apache.brooklyn.api.mgmt.TaskAdaptable;
@@ -191,6 +193,31 @@ public class BrooklynDslCommon {
         }
     }
 
+
+    public static Object regexReplacement(final Object pattern, final Object replacement)
{
+        if (DslUtils.resolved(pattern, replacement)) {
+            return new RegexReplacer(String.valueOf(pattern), String.valueOf(replacement));
+        } else {
+            return new DslRegexReplacer(pattern, replacement);
+        }
+    }
+
+    public static class RegexReplacer implements Function<String, String> {
+        private final String pattern;
+        private final String replacement;
+
+        public RegexReplacer(String pattern, String replacement) {
+            this.pattern = pattern;
+            this.replacement = replacement;
+        }
+
+        @Nullable
+        @Override
+        public String apply(@Nullable String s) {
+            return Strings.replaceAllRegex(s, pattern, replacement);
+        }
+    }
+
     /**
      * Deferred execution of String formatting.
      *
@@ -221,6 +248,27 @@ public class BrooklynDslCommon {
         }
     }
 
+    protected static class DslRegexReplacer extends BrooklynDslDeferredSupplier<Function<String,
String>> {
+
+        private Object pattern;
+        private Object replacement;
+
+        public DslRegexReplacer(Object pattern, Object replacement) {
+            this.pattern = pattern;
+            this.replacement = replacement;
+        }
+
+        @Override
+        public Task<Function<String, String>> newTask() {
+            return DependentConfiguration.regexReplacement(pattern, replacement);
+        }
+
+        @Override
+        public String toString() {
+            return String.format("$brooklyn:regexReplace(%s:%s)", pattern, replacement);
+        }
+    }
+
     /** @deprecated since 0.7.0; use {@link DslFormatString} */
     @SuppressWarnings("serial")
     @Deprecated


Mime
View raw message