<S> ValueResolver<S> cloneReplacingValueAndType(Object newValue, Class<S> superType) { // superType expected to be either type or Object.class if (!superType.isAssignableFrom(type)) { throw new IllegalStateException("superType must be assignable from " + type); } ValueResolver<S> result = new ValueResolver<S>(newValue, superType) .context(exec).description(description) .embedResolutionInTask(embedResolutionInTask) .deep(forceDeep) .timeout(timeout) .immediately(immediately) .recursive(recursive); if (returnDefaultOnGet) { if (!superType.isInstance(defaultValue)) { throw new IllegalStateException("Existing default value " + defaultValue + " not compatible with new type " + superType); } @SuppressWarnings("unchecked") S typedDefaultValue = (S)defaultValue; result.defaultValue(typedDefaultValue); } if (swallowExceptions) result.swallowExceptions(); return result; }
@Test public void testMultipleReferencesJava() throws Exception { final Entity app = createAndStartApplication(loadYaml("test-referencing-entities.yaml")); waitForApplicationTasks(app); Entity root1 = Tasks.resolving(new DslComponent(Scope.ROOT, "xxx").newTask(), Entity.class).context(app).embedResolutionInTask(true).get(); Assert.assertEquals(root1, app); Entity c1 = Tasks.resolving(new DslComponent(Scope.GLOBAL, "c1").newTask(), Entity.class).context(app).embedResolutionInTask(true).get(); Assert.assertEquals(c1, Iterables.getOnlyElement(Entities.descendantsAndSelf(app, EntityPredicates.displayNameEqualTo("child 1")))); Entity e1 = Tasks.resolving(new DslComponent(Scope.PARENT, "xxx").newTask(), Entity.class).context(c1).embedResolutionInTask(true).get(); Assert.assertEquals(e1, Iterables.getOnlyElement(Entities.descendantsAndSelf(app, EntityPredicates.displayNameEqualTo("entity 1")))); Entity root2 = Tasks.resolving(new DslComponent(Scope.ROOT, "xxx").newTask(), Entity.class).context(c1).embedResolutionInTask(true).get(); Assert.assertEquals(root2, app); Entity c1a = Tasks.resolving(BrooklynDslCommon.descendant("c1").newTask(), Entity.class).context(e1).embedResolutionInTask(true).get(); Assert.assertEquals(c1a, c1); Entity e1a = Tasks.resolving(BrooklynDslCommon.ancestor("e1").newTask(), Entity.class).context(c1).embedResolutionInTask(true).get(); Assert.assertEquals(e1a, e1); try { Tasks.resolving(BrooklynDslCommon.ancestor("c1").newTask(), Entity.class).context(e1).embedResolutionInTask(true).get(); Assert.fail("Should not have found c1 as ancestor of e1"); } catch (Exception e) { /* expected */ } }
private Maybe<Entity> getResolvedConfigInTask(final Entity entity, final ConfigKey<Entity> key) { return Tasks.resolving(Tasks.<Entity>builder().body( Functionals.callable(Suppliers.compose(EntityFunctions.config(key), Suppliers.ofInstance(entity))) ).build()) .as(Entity.class) .context(entity).embedResolutionInTask(true) .getMaybe(); }