@Override public Entity call() { return propagator.getConfig(Propagator.PRODUCER); }}).get(); Assert.assertEquals(producer, childEntity);
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public void onEvent(SensorEvent<Object> event) { // propagate upwards Sensor<?> sourceSensor = event.getSensor(); Sensor<?> destinationSensor = getDestinationSensor(sourceSensor); if (!sensorFilter.apply(sourceSensor)) { return; // ignoring excluded sensor } if (LOG.isTraceEnabled()) LOG.trace("enricher {} got {}, propagating via {}{}", new Object[] {this, event, entity, (sourceSensor == destinationSensor ? "" : " (as "+destinationSensor+")")}); emit((Sensor)destinationSensor, event.getValue()); }
/** useful once sensors are added to emit all values */ public void emitAllAttributes() { emitAllAttributes(false); }
super.setEntity(entity); producer = getConfig(PRODUCER); sensorMapping = resolveSensorMappings(getConfig(SENSOR_MAPPING)); propagatingAllBut = resolveSensorCollection(getConfig(PROPAGATING_ALL_BUT)); propagatingAll = Boolean.TRUE.equals(getConfig(PROPAGATING_ALL)) || propagatingAllBut.size() > 0; Collection<Sensor<?>> propagating = resolveSensorCollection(getConfig(PROPAGATING)); } else if (propagating.size() > 0) { throw new IllegalStateException("Propagator enricher "+this+" must not have "+PROPAGATING.getName()+", when publishing to own entity (to avoid infinite loop)"); } else if (filterForKeyEqualsValue(sensorMapping).size() > 0) { Map<? extends Sensor<?>, ? extends Sensor<?>> selfPublishingSensors = filterForKeyEqualsValue(sensorMapping); throw new IllegalStateException("Propagator enricher "+this+" must not publish to same sensor in config "+SENSOR_MAPPING.getName()+" ("+selfPublishingSensors.keySet()+"), when publishing to own entity (to avoid infinite loop)"); "Nothing to propagate; detected: propagatingAll (%s, excluding %s), sensorMapping (%s)", propagatingAll, getConfig(PROPAGATING_ALL_BUT), sensorMapping); subscriptions().subscribe(producer, null, this); highlightTriggers("Listening for all sensors on "+producer); } else { for (Sensor<?> sensor : sensorMapping.keySet()) { subscriptions().subscribe(producer, sensor, this); highlightTriggers("Listening for "+sensorMapping.keySet()+" sensors on "+producer); } else { highlightTriggers(sensorMapping.keySet(), producer); emitAllAttributes();
@SuppressWarnings({ "rawtypes", "unchecked" }) public void emitAllAttributes(boolean includeNullValues) { Iterable<? extends Sensor<?>> sensorsToPopulate = propagatingAll ? Iterables.filter(producer.getEntityType().getSensors(), sensorFilter) : sensorMapping.keySet(); for (Sensor<?> s : sensorsToPopulate) { if (s instanceof AttributeSensor) { AttributeSensor destinationSensor = (AttributeSensor<?>) getDestinationSensor(s); Object v = producer.getAttribute((AttributeSensor<?>)s); // TODO we should keep a timestamp for the source sensor and echo it // (this pretends timestamps are current, which probably isn't the case when we are propagating) if (v != null || includeNullValues) entity.sensors().set(destinationSensor, v); } } }
}}).get(); Assert.assertEquals(producer, childEntity); Asserts.assertTrue(Boolean.valueOf(propagator.getConfig(Propagator.PROPAGATING_ALL)), "Expected Propagator.PROPAGATING_ALL to be true"); ((TestEntity)childEntity).sensors().set(TestEntity.NAME, "New Name"); Asserts.eventually(Entities.attributeSupplier(parentEntity, TestEntity.NAME), Predicates.<String>equalTo("New Name"));