public AttributeWhenReadyTestWorker(TestApplication parent, AttributeSensor<String> sensor, BrooklynDslDeferredSupplier<?> dsl) { super(parent, dsl, sensor.getType()); this.sensor = 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); }
@Override public T apply(String input) { return TypeCoercions.coerce(Strings.trimEnd(input), (Class<T>) sensor.getType()); }}, SshValueFunctions.stdout()));
@Override public T apply(String input) { return TypeCoercions.coerce(Strings.trimEnd(input), (Class<T>) sensor.getType()); }}, SshValueFunctions.stdout()));
@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); } } };
@SuppressWarnings("unchecked") public <T> T getValue(AttributeSensor<T> sensor) { return (T) TypeCoercions.coerce(getValue(sensor.getNameParts()), sensor.getType()); }
@Override public void onSuccess(WinRmToolResponse val) { for (String pollResponse : val.getStdOut().split("\r\n")) { if (Strings.isNullOrEmpty(pollResponse)) { continue; } String path = pollResponse.substring(0, OUTPUT_COLUMN_WIDTH - 1); // The performance counter output prepends the sensor name with "\\<machinename>" so we need to remove it Matcher machineNameLookbackMatcher = MACHINE_NAME_LOOKBACK_PATTERN.matcher(path); if (!machineNameLookbackMatcher.find()) { continue; } String name = machineNameLookbackMatcher.group(0).trim(); String rawValue = pollResponse.substring(OUTPUT_COLUMN_WIDTH).replaceAll("^\\s+", ""); WindowsPerformanceCounterPollConfig<?> config = getPollConfig(name); Class<?> clazz = config.getSensor().getType(); AttributeSensor<Object> attribute = (AttributeSensor<Object>) Sensors.newSensor(clazz, config.getSensor().getName(), config.getDescription()); try { Object value = TypeCoercions.coerce(rawValue, TypeToken.of(clazz)); entity.sensors().set(attribute, value); } catch (Exception e) { Exceptions.propagateIfFatal(e); if (failedAttributes.add(attribute)) { log.warn("Failed to coerce value '{}' to {} for {} -> {}", new Object[] {rawValue, clazz, entity, attribute}); } else { if (log.isTraceEnabled()) log.trace("Failed (repeatedly) to coerce value '{}' to {} for {} -> {}", new Object[] {rawValue, clazz, entity, attribute}); } } } }
@Override public void onFailure(WinRmToolResponse val) { if (val == null) { log.trace("Windows Performance Counter not executed since there is still now WinRmMachineLocation"); return; } log.error("Windows Performance Counter query did not respond as expected. exitcode={} stdout={} stderr={}", new Object[]{val.getStatusCode(), val.getStdOut(), val.getStdErr()}); for (WindowsPerformanceCounterPollConfig<?> config : polls) { Class<?> clazz = config.getSensor().getType(); AttributeSensor<?> attribute = Sensors.newSensor(clazz, config.getSensor().getName(), config.getDescription()); entity.sensors().set(attribute, null); } }
@Test public void testAttributeWhenReadyEmptyDoesNotBlock() throws Exception { BrooklynDslDeferredSupplier<?> dsl = BrooklynDslCommon.attributeWhenReady(TestApplication.MY_ATTRIBUTE.getName()); Maybe<?> actualValue = execDslRealRealQuick(dsl, TestApplication.MY_ATTRIBUTE.getType(), app); assertTrue(actualValue.isAbsent()); }
@Test public void testAttributeWhenReadyEmptyImmediatelyDoesNotBlock() throws Exception { BrooklynDslDeferredSupplier<?> dsl = BrooklynDslCommon.attributeWhenReady(TestApplication.MY_ATTRIBUTE.getName()); Maybe<?> actualValue = execDslImmediately(dsl, TestApplication.MY_ATTRIBUTE.getType(), app, true); assertTrue(actualValue.isAbsent()); }
public static void configureEnrichers(SubnetTier subnetTier, Entity entity) { for (AttributeSensor sensor : Iterables.filter(entity.getEntityType().getSensors(), AttributeSensor.class)) { if ((DockerUtils.URL_SENSOR_NAMES.contains(sensor.getName()) || sensor.getName().endsWith(".url") || URI.class.isAssignableFrom(sensor.getType())) && !DockerUtils.BLACKLIST_URL_SENSOR_NAMES.contains(sensor.getName())) { AttributeSensor<String> target = DockerUtils.<String>mappedSensor(sensor); entity.enrichers().add(subnetTier.uriTransformingEnricher( EntityAndAttribute.create(entity, sensor), target)); Set<Hint<?>> hints = RendererHints.getHintsFor(sensor); for (Hint<?> hint : hints) { RendererHints.register(target, (NamedActionWithUrl) hint); } LOG.debug("Mapped URL sensor: entity={}, origin={}, mapped={}", new Object[] {entity, sensor.getName(), target.getName()}); } else if (sensor.getName().matches("docker\\.port\\.[0-9]+") || PortAttributeSensorAndConfigKey.class.isAssignableFrom(sensor.getClass())) { AttributeSensor<String> target = DockerUtils.mappedPortSensor(sensor); entity.enrichers().add(subnetTier.hostAndPortTransformingEnricher( EntityAndAttribute.create(entity, sensor), target)); LOG.debug("Mapped port sensor: entity={}, origin={}, mapped={}", new Object[] {entity, sensor.getName(), target.getName()}); } } }
@SuppressWarnings("unchecked") protected void setSensor(Object v) { if (Entities.isNoLongerManaged(entity)) { if (Tasks.isInterrupted()) return; log.warn(""+entity+" is not managed; feed "+this+" setting "+sensor+" to "+v+" at this time is not supported ("+Tasks.current()+")"); } if (v == FeedConfig.UNCHANGED) { // nothing } else if (v == FeedConfig.REMOVE) { ((EntityInternal)entity).removeAttribute(sensor); feed.onRemoveSensor(sensor); } else if (sensor == FeedConfig.NO_SENSOR) { // nothing } else { Object coercedV = TypeCoercions.coerce(v, sensor.getType()); if (suppressDuplicates && Objects.equal(coercedV, entity.getAttribute(sensor))) { // no change; nothing } else { entity.sensors().set(sensor, coercedV); feed.onPublishSensor(sensor, coercedV); } } }