@Override public void apply(EntityLocal entity) { ((EntityInternal)entity).getMutableEntityType().addEffector(effector); }
@Override public void apply(EntityLocal entity) { sensor = newSensor(entity); ((EntityInternal) entity).getMutableEntityType().addSensor(sensor); }
/** Attaches lifecycle effectors (start, restart, stop) to the given entity post-creation. */ public void attachLifecycleEffectors(Entity entity) { ((EntityInternal) entity).getMutableEntityType().addEffector(newStartEffector()); ((EntityInternal) entity).getMutableEntityType().addEffector(newRestartEffector()); ((EntityInternal) entity).getMutableEntityType().addEffector(newStopEffector()); }
@Override public void apply(EntityLocal entity) { ((EntityInternal) entity).getMutableEntityType().addEffector(EffectorTaskTest.DOUBLE_1); } }));
@Override public void apply(EntityLocal entity) { ((EntityInternal)entity).getMutableEntityType().addEffector(EffectorTaskTest.DOUBLE_1); }});
@Override public void apply(EntityLocal entity) { ((EntityInternal) entity).getMutableEntityType().removeEffector(TestEntity.IDENTITY_EFFECTOR); } }));
@Test public void testAddConfigKey() throws Exception{ entity.getMutableEntityType().addConfigKey(NEW_CONFIG); assertEquals(entity.getEntityType().getConfigKeys(), ImmutableSet.builder().addAll(DEFAULT_CONFIG_KEYS).add(NEW_CONFIG).build()); assertEventuallyListenerEventsEqual(ImmutableList.of(BasicSensorEvent.ofUnchecked(AbstractEntity.CONFIG_KEY_ADDED, entity, NEW_CONFIG))); }
@Test public void testAddSensors() throws Exception{ entity.getMutableEntityType().addSensor(TEST_SENSOR); assertEquals(entity.getEntityType().getSensors(), ImmutableSet.builder().addAll(DEFAULT_SENSORS).add(TEST_SENSOR).build()); assertEventuallyListenerEventsEqual(ImmutableList.of(BasicSensorEvent.ofUnchecked(SENSOR_ADDED, entity, TEST_SENSOR))); }
@Override @SuppressWarnings("unchecked") protected void addCustoms(RebindContext rebindContext, EntityMemento memento) { for (ConfigKey<?> key : memento.getDynamicConfigKeys()) { entity.getMutableEntityType().addConfigKey(key); } for (Effector<?> eff : memento.getEffectors()) { entity.getMutableEntityType().addEffector(eff); } for (Map.Entry<AttributeSensor<?>, Object> entry : memento.getAttributes().entrySet()) { try { AttributeSensor<?> key = entry.getKey(); Object value = entry.getValue(); @SuppressWarnings("unused") // just to ensure we can load the declared type? or maybe not needed Class<?> type = (key.getType() != null) ? key.getType() : rebindContext.loadClass(key.getTypeName()); entity.sensors().setWithoutPublishing((AttributeSensor<Object>)key, value); } catch (Exception e) { LOG.warn("Error adding custom sensor "+entry+" when rebinding "+entity+" (rethrowing): "+e); throw Exceptions.propagate(e); } } setParent(rebindContext, memento); addChildren(rebindContext, memento); addMembers(rebindContext, memento); addLocations(rebindContext, memento); }
@Test public void testEffectorBodyAddedImplicitlyButBodylessSignatureInvoked() throws Exception { EntityInternal doubler = app.createAndManageChild(EntitySpec.create(TestEntity.class)); // add it doubler.getMutableEntityType().addEffector(DOUBLE_1); // invoke it, but using something with equivalent name (and signature -- though only name is used currently) // ensures that the call picks up the body by looking in the actual entity Assert.assertEquals(doubler.invoke(DOUBLE_BODYLESS, MutableMap.of("numberToDouble", 3)).get(), (Integer)6); }
@Test public void testEffectorBodyAdded() throws Exception { EntityInternal doubler = app.createAndManageChild(EntitySpec.create(TestEntity.class)); // not yet present Assert.assertNull( doubler.getEffector("double") ); // add it doubler.getMutableEntityType().addEffector(DOUBLE_BODYLESS, new EffectorBody<Integer>() { @Override public Integer call(ConfigBag parameters) { int input = (Integer)parameters.getStringKey("numberToDouble"); return queue(times(input, 2)).getUnchecked(); } }); // now it is present Assert.assertNotNull( doubler.getEffector("double") ); Assert.assertEquals(doubler.invoke(DOUBLE_BODYLESS, MutableMap.of("numberToDouble", 3)).get(), (Integer)6); }
@Test public void testRemoveConfigKey() throws Exception { entity.getMutableEntityType().removeConfigKey(ConfigEntityForTesting.INSTALL_UNIQUE_LABEL); assertEquals(entity.getEntityType().getConfigKeys(), MutableSet.builder().addAll(DEFAULT_CONFIG_KEYS).remove(ConfigEntityForTesting.INSTALL_UNIQUE_LABEL).build().asUnmodifiable()); assertEventuallyListenerEventsEqual(ImmutableList.of( BasicSensorEvent.ofUnchecked(AbstractEntity.CONFIG_KEY_REMOVED, entity, ConfigEntityForTesting.INSTALL_UNIQUE_LABEL))); }
@Override public void apply(@SuppressWarnings("deprecation") org.apache.brooklyn.api.entity.EntityLocal entity) { Effector<String> eff = Effectors.effector(String.class, EFFECTOR_SAY_HELLO).parameter(String.class, "name").impl( new EffectorBody<String>() { @Override public String call(ConfigBag parameters) { Object name = parameters.getStringKey("name"); entity().sensors().set(Sensors.newStringSensor(SENSOR_LAST_HELLO), Strings.toString(name)); return helloWord()+" "+name; } }).build(); ((EntityInternal)entity).getMutableEntityType().addEffector(eff); ((EntityInternal)entity).getMutableEntityType().addSensor(Sensors.newStringSensor(SENSOR_HELLO_DEFINED)); AttributeSensor<String> emitted = Sensors.newStringSensor(SENSOR_HELLO_DEFINED_EMITTED); ((EntityInternal)entity).getMutableEntityType().addSensor(emitted); entity.sensors().set(emitted, "1"); }
@Test public void testRemoveSensor() throws Exception { entity.getMutableEntityType().removeSensor(SENSOR_ADDED); assertEquals(entity.getEntityType().getSensors(), MutableSet.builder().addAll(DEFAULT_SENSORS).remove(SENSOR_ADDED).build().asUnmodifiable()); assertEventuallyListenerEventsEqual(ImmutableList.of( BasicSensorEvent.ofUnchecked(SENSOR_REMOVED, entity, SENSOR_ADDED))); }
@Override public void apply(@SuppressWarnings("deprecation") final org.apache.brooklyn.api.entity.EntityLocal entity) { final AttributeSensor<ReleaseableLatch> sensor = Sensors.newSensor(ReleaseableLatch.class, sensorName); ((EntityInternal) entity).getMutableEntityType().addSensor(sensor); final Task<ReleaseableLatch> resolveValueTask = DependentConfiguration.maxConcurrency(maxConcurrency); class SetValue implements Runnable { @Override public void run() { ReleaseableLatch releaseableLatch = resolveValueTask.getUnchecked(); log.debug(this+" setting sensor "+sensor+" to "+releaseableLatch+" on "+entity); entity.sensors().set(sensor, releaseableLatch); } } Task<ReleaseableLatch> setValueTask = Tasks.<ReleaseableLatch>builder().displayName("Setting " + sensor + " on " + entity).body(new SetValue()).build(); Entities.submit(entity, Tasks.sequential("Resolving and setting " + sensor + " on " + entity, resolveValueTask, setValueTask)); }
private void execRelease(final JcloudsLocation loc, final JcloudsSshMachineLocation machine) throws Exception { EffectorBody<Void> effectorBody = new EffectorBody<Void>() { @Override public Void call(ConfigBag parameters) { loc.releasePortForwarding(machine); return null; } }; Effector<Void> effector = new EffectorAndBody<Void>("myeffector", Void.class, ImmutableList.<ParameterType<?>>of(), "", new EffectorBodyTaskFactory<Void>(effectorBody)); EntityInternal entity = (EntityInternal) app.createAndManageChild(EntitySpec.create(BasicEntity.class)); entity.getMutableEntityType().addEffector(effector); entity.invoke(effector, ImmutableMap.<String, Object>of()).get(); } }
@Override public void apply(EntityLocal entity) { Maybe<Effector<?>> effectorMaybe = entity.getEntityType().getEffectorByName(effector.getName()); if (!effectorMaybe.isAbsentOrNull()) { Effector<?> original = Effectors.effector(effectorMaybe.get()).name(ORIGINAL_PREFIX + effector.getName()).build(); ((EntityInternal) entity).getMutableEntityType().addEffector(original); } super.apply(entity); }
@Test public void testRemoveSensors() throws Exception { entity.getMutableEntityType().removeSensor(SENSOR_ADDED.getName()); entity.getMutableEntityType().removeSensor(POLICY_ADDED.getName()); assertEquals(entity.getEntityType().getSensors(), MutableSet.builder().addAll(DEFAULT_SENSORS).remove(SENSOR_ADDED).remove(POLICY_ADDED).build().asUnmodifiable()); final RecordingSensorEventListener<?> listener = this.listener; Asserts.succeedsEventually(new Runnable() { @Override public void run() { assertEquals(Iterables.size(listener.getEvents()), 2); } }); assertEventuallyListenerEventsEqual(ImmutableList.of( BasicSensorEvent.ofUnchecked(SENSOR_REMOVED, entity, SENSOR_ADDED), BasicSensorEvent.ofUnchecked(SENSOR_REMOVED, entity, POLICY_ADDED))); }