/** * Adds an effector with an explicit body to this entity. */ @Beta public <T> void addEffector(Effector<T> effector, EffectorTaskFactory<T> body) { addEffector(new EffectorAndBody<T>(effector, body)); }
private void addSpecParameters(EntitySpec<?> spec, EntityDynamicType edType) { // if coming from a catalog item, parsed by CAMP, then the spec list of parameters is canonical, // the parent item has had its config keys set as parameters here with those non-inheritable // via type definition removed, so wipe those on the EDT to make sure non-inheritable ones are removed; // OTOH if item is blank, it was set as a java type, not inheriting it, // and the config keys on the dynamic type are the correct ones to use, and usually there is nothing in spec.parameters, // except what is being added programmatically. // (this logic could get confused if catalog item ID referred to some runtime-inherited context, // but those semantics should no longer be used -- https://issues.apache.org/jira/browse/BROOKLYN-445) if (Strings.isNonBlank(spec.getCatalogItemId())) { edType.clearConfigKeys(); } for (SpecParameter<?> param : spec.getParameters()) { edType.addConfigKey(param.getConfigKey()); if (param.getSensor()!=null) edType.addSensor(param.getSensor()); } }
@Override public EntityType getEntityType() { if (entityType==null) return null; return entityType.getSnapshot(); }
@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"); }
@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); }
/** * Adds the given {@link Sensor}s to this entity. */ public void addSensors(Iterable<? extends Sensor<?>> newSensors) { for (Sensor<?> sensor : newSensors) { addSensor(sensor); } }
@Override public void remove(AttributeSensor<?> attribute) { if (LOG.isTraceEnabled()) LOG.trace(""+AbstractEntity.this+" removeAttribute "+attribute); attributesInternal.remove(attribute); entityType.removeSensor(attribute); }
/** * Adds the given {@link ConfigKey} to this entity. */ public void addConfigKeys(Iterable<ConfigKey<?>> newKeys) { for (ConfigKey<?> newKey : newKeys) { addConfigKey(newKey); } }
@Test public void testEffectorRemovedDuringInit() { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .addInitializer(new EntityInitializer() { @Override public void apply(EntityLocal entity) { ((EntityInternal) entity).getMutableEntityType().removeEffector(TestEntity.IDENTITY_EFFECTOR); } })); assertFalse(entity.getMutableEntityType().getEffectors().containsKey(TestEntity.IDENTITY_EFFECTOR.getName())); }
@Override public void apply(EntityLocal entity) { sensor = newSensor(entity); ((EntityInternal) entity).getMutableEntityType().addSensor(sensor); }
/** * Removes the named {@link Sensor} from this entity. */ public boolean removeSensor(Sensor<?> sensor) { return (removeSensor(sensor.getName()) != null); }
@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))); }
builder.effectors.removeAll(definedType.getEffectors().values());
/** * Adds an effector with an explicit body to this entity. */ @Beta public <T> void addEffector(Effector<T> effector, EffectorBody<T> body) { addEffector(effector, new EffectorBodyTaskFactory<T>(body)); }
@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))); }
@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))); }
@Test public void testConfigKeysInheritance() throws Exception { Entity child = setupBasicInheritanceTest(); Assert.assertNotNull(child.getConfig(MyEntityWithPartiallyHeritableConfig.HERITABLE_BY_DEFAULT)); Assert.assertNotNull(child.getConfig(MyEntityWithPartiallyHeritableConfig.ALWAYS_HERITABLE)); Assert.assertNull(child.getConfig(MyEntityWithPartiallyHeritableConfig.NEVER_INHERIT)); // it's reinheritable unless explicitly declared Assert.assertNotNull(child.getConfig(MyEntityWithPartiallyHeritableConfig.NOT_REINHERITABLE)); app.getMutableEntityType().addConfigKey(MyEntityWithPartiallyHeritableConfig.NOT_REINHERITABLE); Assert.assertNull(child.getConfig(MyEntityWithPartiallyHeritableConfig.NOT_REINHERITABLE)); }
spec = b.getTypeRegistry().createSpec(item, null, EntitySpec.class); EntityDynamicType typeMap = BrooklynTypes.getDefinedEntityType(spec.getType()); EntityType type = typeMap.getSnapshot();
@Override public void apply(EntityLocal entity) { ((EntityInternal)entity).getMutableEntityType().addEffector(effector); }
@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)); }