/** blocks until the given task completes, submitting if necessary, returning the result of that task; * optional contextMessage is available in status if this is running in a task */ @SuppressWarnings("unchecked") public static <T> T waitForTask(Task<T> t, Entity context, String contextMessage) throws InterruptedException { try { return (T) Tasks.resolveValue(t, Object.class, ((EntityInternal)context).getExecutionContext(), contextMessage); } catch (ExecutionException e) { throw Throwables.propagate(e); } }
@Override public Boolean call() throws Exception { Task<Entity> task = dslComponent.newTask(); result.set(Tasks.resolveValue(task, Entity.class, executionContext, "Finding entity " + targetId)); return true; } };
private void assertResolvesValue(Object actual, Class<?> type, Object expected) throws Exception { Object result = Tasks.resolveValue(actual, type, executionContext); assertEquals(result, expected); }
@Override public Object apply(Object input) { if (input instanceof DeferredSupplier<?>) { try { return Tasks.resolveValue(input, Object.class, getExecutionContext()); } catch (Exception e) { throw Exceptions.propagate(e); } } return input; }
protected Object resolveValue(Object v, ExecutionContext exec) throws ExecutionException, InterruptedException { if (v instanceof Collection || v instanceof Map) { return Tasks.resolveDeepValue(v, Object.class, exec, "config "+name); } else { return Tasks.resolveValue(v, getType(), exec, "config "+name); } }
@Override public T get() { try { TypeToken<T> type = new TypeToken<T>(sensor.getType()) {}; return Tasks.resolveValue(task, (Class<T>) type.getRawType(), ((EntityInternal) entity).getExecutionContext(), "attributeSupplierWhenReady"); } catch (Exception e) { throw Exceptions.propagate(e); } } };
public String getArgs(){ List<Object> args = getEntity().getConfig(VanillaJavaApp.ARGS); StringBuilder sb = new StringBuilder(); Iterator<Object> it = args.iterator(); while (it.hasNext()) { Object argO = it.next(); try { String arg = Tasks.resolveValue(argO, String.class, getEntity().getExecutionContext()); BashStringEscapes.assertValidForDoubleQuotingInBash(arg); sb.append(format("\"%s\"",arg)); } catch (Exception e) { throw Exceptions.propagate(e); } if (it.hasNext()) { sb.append(" "); } } return sb.toString(); }
@Test public void testFormatStringWithConfig() throws Exception { app(); Object y1 = parseDslExpression("$brooklyn:formatString(\"%s-%s\", config(\"dest\"), $brooklyn:config(\"dest2\"))"); Assert.assertEquals(y1.toString(), "$brooklyn:formatString(\"%s-%s\", config(\"dest\"), config(\"dest2\"))"); String y2 = Tasks.resolveValue(y1, String.class, ((EntityInternal) find("two")).getExecutionContext()); Assert.assertEquals(y2.toString(), "1-2"); String y3 = Tasks.resolveValue(y1, String.class, ((EntityInternal) find("one")).getExecutionContext()); Assert.assertEquals(y3.toString(), "1-1"); }
@Test public void testConfigEvaluation() throws Exception { app(); Object y1 = parseDslExpression("$brooklyn:config(\"dest\")"); String y2 = Tasks.resolveValue(y1, String.class, ((EntityInternal) find("two")).getExecutionContext()); Assert.assertEquals(y2.toString(), "1"); }
private <T> T resolve(ConfigKey<T> key, Object value) { Object transformed = transform(key, value); Object result; if (transformed instanceof DeferredSupplier) { ExecutionContext exec = mgmt.getServerExecutionContext(); try { result = Tasks.resolveValue(transformed, key.getType(), exec); } catch (ExecutionException | InterruptedException e) { throw Exceptions.propagate(e); } } else { result = transformed; } return TypeCoercions.coerce(result, key.getTypeToken()); }
@Test public void testCampYamlParserHandlesExternalisedConfig() throws Exception { CampYamlParser parser = mgmt().getScratchpad().get(CampYamlParser.YAML_PARSER_KEY); DeferredSupplier<?> supplier = (DeferredSupplier<?>) parser.parse("$brooklyn:external(\"myprovider\", \"mykey\")"); ExecutionContext exec = mgmt().getServerExecutionContext(); String result = Tasks.resolveValue(supplier, String.class, exec); assertEquals(result, "myval"); }
@Test public void testEntityReferenceAndAttributeWhenReady() throws Exception { app(); find("one").sensors().set(Attributes.ADDRESS, "1"); find("two").sensors().set(Attributes.ADDRESS, "2"); Object y1 = parseDslExpression("$brooklyn:formatString(\"%s-%s\", " + "parent().attributeWhenReady(\"host.address\"), " + "$brooklyn:attributeWhenReady(\"host.address\"))"); Assert.assertEquals(y1.toString(), "$brooklyn:formatString(\"%s-%s\", " + "parent().attributeWhenReady(\"host.address\"), " + "attributeWhenReady(\"host.address\"))"); String y2 = Tasks.resolveValue(y1, String.class, ((EntityInternal) find("two")).getExecutionContext()); Assert.assertEquals(y2.toString(), "1-2"); Object z1 = parseDslExpression("$brooklyn:formatString(\"%s-%s\", " + "entity(\"one\").descendant(\"two\").attributeWhenReady(\"host.address\"), " + "component(\"two\").entity(entityId()).attributeWhenReady(\"host.address\"))"); Assert.assertEquals(z1.toString(), "$brooklyn:formatString(\"%s-%s\", " + "entity(\"one\").descendant(\"two\").attributeWhenReady(\"host.address\"), " + "entity(entityId()).attributeWhenReady(\"host.address\"))"); String z2 = Tasks.resolveValue(z1, String.class, ((EntityInternal) find("one")).getExecutionContext()); Assert.assertEquals(z2.toString(), "2-1"); }
protected void runExternalisedConfigGetters(BrooklynProperties props, String property, String expectedVal, boolean testSubMap) throws Exception { ExecutionContext exec = mgmt().getServerExecutionContext(); String val1 = props.getConfig(ConfigKeys.newStringConfigKey(property)); assertEquals(val1, expectedVal); DeferredSupplier<?> val2 = (DeferredSupplier<?>) props.getConfigRaw(ConfigKeys.newStringConfigKey(property)).get(); assertEquals(Tasks.resolveValue(val2, String.class, exec), expectedVal); DeferredSupplier<?> val3 = (DeferredSupplier<?>) props.getConfigLocalRaw(ConfigKeys.newStringConfigKey(property)).get(); assertEquals(Tasks.resolveValue(val3, String.class, exec), expectedVal); DeferredSupplier<?> val4 = (DeferredSupplier<?>) props.getAllConfigLocalRaw().get(ConfigKeys.newStringConfigKey(property)); assertEquals(Tasks.resolveValue(val4, String.class, exec), expectedVal); String val5 = props.getFirst(property); assertTrue(val5.startsWith("$brooklyn:external"), "val="+val5); if (testSubMap) { BrooklynProperties submap = props.submap(ConfigPredicates.nameEqualTo(property)); runExternalisedConfigGetters(submap, property, expectedVal, false); } } }