Java Code Examples for org.apache.brooklyn.config.ConfigKey#getDefaultValue()

The following examples show how to use org.apache.brooklyn.config.ConfigKey#getDefaultValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConfigSummary.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
public ConfigSummary(ConfigKey<?> config, String label, Double priority, Boolean pinned, Map<String, URI> links) {
    this.name = config.getName();
    this.description = config.getDescription();
    this.reconfigurable = config.isReconfigurable();

    /* Use String, to guarantee it is serializable; otherwise get:
     *   No serializer found for class org.apache.brooklyn.policy.autoscaling.AutoScalerPolicy$3 and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.ArrayList[9]->org.apache.brooklyn.rest.domain.PolicyConfigSummary["defaultValue"])
     *   at org.codehaus.jackson.map.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:52)
     */
    this.label = label;
    this.priority = priority;
    this.pinned = pinned;
    this.constraints = ConstraintSerialization.INSTANCE.toJsonList(config);
    if (config.getType().isEnum()) {
        this.type = Enum.class.getName();
        this.defaultValue = config.getDefaultValue() instanceof Enum? ((Enum<?>) config.getDefaultValue()).name() : 
            Jsonya.convertToJsonPrimitive(config.getDefaultValue());
        this.possibleValues = FluentIterable
                .from(Arrays.asList((Enum<?>[])(config.getType().getEnumConstants())))
                .transform(new Function<Enum<?>, Map<String, String>>() {
                    @Nullable
                    @Override
                    public Map<String, String> apply(@Nullable Enum<?> input) {
                        return ImmutableMap.of(
                                "value", input != null ? input.name() : null,
                               "description", input != null ? input.toString() : null);
                    }})
                .toList();
    } else {
        this.type = config.getTypeName();
        this.defaultValue = Jsonya.convertToJsonPrimitive(config.getDefaultValue());
        this.possibleValues = null;
    }
    this.links = (links == null) ? ImmutableMap.<String, URI>of() : ImmutableMap.copyOf(links);
}
 
Example 2
Source File: ShellAbstractTool.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
public 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) && map.get(key) != null) {
        return TypeCoercions.coerce(map.get(key), keyC.getTypeToken());
    } else {
        return keyC.getDefaultValue();
    }
}
 
Example 3
Source File: ExecWithLoggingHelpers.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example 4
Source File: EntityInitializers.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the value for key from configBag.
 * <p>
 * If key is an instance of {@link ConfigKeySelfExtracting} and executionContext is
 * not null then its value will be retrieved per the key's implementation of
 * {@link ConfigKeySelfExtracting#extractValue extractValue}. Otherwise, the value
 * will be retrieved from configBag directly.
 */
public static <T> T resolve(ConfigBag configBag, ConfigKey<T> key, ExecutionContext executionContext) {
    if (key instanceof ConfigKeySelfExtracting && executionContext != null) {
        ConfigKeySelfExtracting<T> ckse = ((ConfigKeySelfExtracting<T>) key);
        T result = ckse.extractValue(configBag.getAllConfigAsConfigKeyMap(), executionContext);
        return (result != null || configBag.containsKey(key.getName())) ? result : key.getDefaultValue();
    }
    return configBag.get(key);
}
 
Example 5
Source File: BasicEntityMemento.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private static boolean isAnonymous(ConfigKey<?> key) {
    Preconditions.checkNotNull(key, "key");
    if (!Object.class.equals(key.getType())) return false;
    if (!Strings.isBlank(key.getDescription())) return false;
    if (key.getDefaultValue()!=null) return false;
    // inheritance, constraints, reconfigurability could also be checked - but above should catch any such
    // (not even sure we need this method at all - see caller)
    return true;
}
 
Example 6
Source File: BrooklynPropertiesImpl.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T getConfig(ConfigKey<T> key, T defaultValue) {
    // TODO does not support MapConfigKey etc where entries use subkey notation; for now, access using submap
    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());
}
 
Example 7
Source File: ConfigUtils.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** 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());
}
 
Example 8
Source File: AutoScalerPolicy.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
private <T> void setOrDefault(ConfigKey<T> key, T val) {
    if (val==null) val = key.getDefaultValue();
    config().set(key, val);
}
 
Example 9
Source File: Effectors.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
public static <V> ParameterType<V> asParameterType(ConfigKey<V> key) {
    return key.hasDefaultValue()
        ? new BasicParameterType<V>(key.getName(), key.getTypeToken(), key.getDescription(), key.getDefaultValue())
        : new BasicParameterType<V>(key.getName(), key.getTypeToken(), key.getDescription());
}