protected static <T> T getMandatoryVal(Map<String,?> map, ConfigKey<T> keyC) { String key = keyC.getName(); checkArgument(map.containsKey(key), "must contain key '"+keyC+"'"); return TypeCoercions.coerce(map.get(key), keyC.getTypeToken()); }
/** returns the value of the key if specified, otherwise defaultValue */ protected static <T> T getOptionalVal(Map<String,?> map, ConfigKey<T> keyC, T defaultValue) { String key = keyC.getName(); if (map.containsKey(key)) { return TypeCoercions.coerce(map.get(key), keyC.getTypeToken()); } else { return defaultValue; } }
public BasicConfigKeyOverwriting(ConfigKey<T> key, T defaultValue) { super(checkNotNull(key.getTypeToken(), "type"), checkNotNull(key.getName(), "name"), key.getDescription(), defaultValue); parentKey = key; }
/** @deprecated since 0.6.0; use {@link ConfigKeys#newConfigKeyWithDefault(ConfigKey, Object)} */ public BasicConfigKey(ConfigKey<T> key, T defaultValue) { log.warn("deprecated use of BasicConfigKey(exendedKey) constructor, for "+key+" ("+defaultValue+")"); this.description = key.getDescription(); this.name = checkNotNull(key.getName(), "name"); this.typeToken = checkNotNull(key.getTypeToken(), "type"); this.type = checkNotNull(key.getType(), "type"); this.defaultValue = defaultValue; this.reconfigurable = false; }
/** removes the given prefix from the key for configuration purposes; logs warning and does nothing if there is no such prefix. * prefix will typically end with a ".". * this is useful for configuration purposes when a subsystem uses a short-name config (e.g. "user") * but in entity config or at the root (brooklyn.properties) there are longer names (e.g. "brooklyn.ssh.config.user"), * and we wish to convert from the longer names to the short-name. */ public static <T> ConfigKey<T> unprefixedKey(String prefix, ConfigKey<T> key) { String newName = key.getName(); if (newName.startsWith(prefix)) newName = newName.substring(prefix.length()); else log.warn("Cannot remove prefix "+prefix+" from key "+key+" (ignoring)"); return new BasicConfigKey<T>(key.getTypeToken(), newName, key.getDescription(), key.getDefaultValue()); }
protected static <T> T getOptionalVal(Map<String,?> map, ConfigKey<T> keyC) { if (keyC==null) return null; String key = keyC.getName(); if (map!=null && map.containsKey(key)) { return TypeCoercions.coerce(map.get(key), keyC.getTypeToken()); } else { return keyC.getDefaultValue(); } }
public static <T> ConfigKey<T> newConfigKeyRenamed(String newName, ConfigKey<T> key) { return new BasicConfigKey<T>(key.getTypeToken(), newName, key.getDescription(), key.getDefaultValue()); }
protected <T> T get(ConfigKey<T> key, boolean remove) { // TODO for now, no evaluation -- closure content / smart (self-extracting) keys are NOT supported // (need a clean way to inject that behaviour, as well as desired TypeCoercions) Object value; if (config.containsKey(key.getName())) value = getStringKey(key.getName(), remove); else value = key.getDefaultValue(); return TypeCoercions.coerce(value, key.getTypeToken()); }
public AttributeSensorAndConfigKey(AttributeSensorAndConfigKey<ConfigType,SensorType> orig, ConfigType defaultValue) { super(orig.getTypeToken(), orig.getName(), orig.getDescription()); configKey = ConfigKeys.newConfigKeyWithDefault(orig.configKey, TypeCoercions.coerce(defaultValue, orig.configKey.getTypeToken())); }
@Override public <T> T getConfig(ConfigKey<T> key, T defaultValue) { if (!containsKey(key.getName())) { if (defaultValue!=null) return defaultValue; return key.getDefaultValue(); } Object value = get(key.getName()); if (value==null) return null; // no evaluation / key extraction here return TypeCoercions.coerce(value, key.getTypeToken()); }
@SuppressWarnings("unchecked") @Override public <T> T getConfig(ConfigKey<T> key, T defaultValue) { // FIXME What about inherited task in config?! // alex says: think that should work, no? // FIXME What if someone calls getConfig on a task, before setting parent app? // alex says: not supported (throw exception, or return the task) // In case this entity class has overridden the given key (e.g. to set default), then retrieve this entity's key // TODO If ask for a config value that's not in our configKeys, should we really continue with rest of method and return key.getDefaultValue? // e.g. SshBasedJavaAppSetup calls setAttribute(JMX_USER), which calls getConfig(JMX_USER) // but that example doesn't have a default... ConfigKey<T> ownKey = adjunct!=null ? (ConfigKey<T>)elvis(adjunct.getAdjunctType().getConfigKey(key.getName()), key) : key; // Don't use groovy truth: if the set value is e.g. 0, then would ignore set value and return default! if (ownKey instanceof ConfigKeySelfExtracting) { if (((ConfigKeySelfExtracting<T>)ownKey).isSet(ownConfig)) { // FIXME Should we support config from futures? How to get execution context before setEntity? EntityLocal entity = adjunct.entity; ExecutionContext exec = (entity != null) ? ((EntityInternal)entity).getExecutionContext() : null; return ((ConfigKeySelfExtracting<T>)ownKey).extractValue(ownConfig, exec); } } else { LOG.warn("Config key {} of {} is not a ConfigKeySelfExtracting; cannot retrieve value; returning default", ownKey, this); } return TypeCoercions.coerce((defaultValue != null) ? defaultValue : ownKey.getDefaultValue(), key.getTypeToken()); }
@SuppressWarnings("unchecked") public <T> T getConfig(ConfigKey<T> key, T defaultValue) { // FIXME What about inherited task in config?! // alex says: think that should work, no? // FIXME What if someone calls getConfig on a task, before setting parent app? // alex says: not supported (throw exception, or return the task) // In case this entity class has overridden the given key (e.g. to set default), then retrieve this entity's key // TODO If ask for a config value that's not in our configKeys, should we really continue with rest of method and return key.getDefaultValue? // e.g. SshBasedJavaAppSetup calls setAttribute(JMX_USER), which calls getConfig(JMX_USER) // but that example doesn't have a default... ConfigKey<T> ownKey = entity!=null ? (ConfigKey<T>)elvis(entity.getEntityType().getConfigKey(key.getName()), key) : key; ExecutionContext exec = entity.getExecutionContext(); // Don't use groovy truth: if the set value is e.g. 0, then would ignore set value and return default! if (ownKey instanceof ConfigKeySelfExtracting) { if (((ConfigKeySelfExtracting<T>)ownKey).isSet(ownConfig)) { return ((ConfigKeySelfExtracting<T>)ownKey).extractValue(ownConfig, exec); } else if (((ConfigKeySelfExtracting<T>)ownKey).isSet(inheritedConfig)) { return ((ConfigKeySelfExtracting<T>)ownKey).extractValue(inheritedConfig, exec); } } else { LOG.warn("Config key {} of {} is not a ConfigKeySelfExtracting; cannot retrieve value; returning default", ownKey, this); } return TypeCoercions.coerce((defaultValue != null) ? defaultValue : ownKey.getDefaultValue(), key.getTypeToken()); }