Search in sources :

Example 6 with ConfigValue

use of com.typesafe.config.ConfigValue in project config by typesafehub.

the class SerializedConfigValue method writeValueData.

private static void writeValueData(DataOutput out, ConfigValue value) throws IOException {
    SerializedValueType st = SerializedValueType.forValue(value);
    out.writeByte(st.ordinal());
    switch(st) {
        case BOOLEAN:
            out.writeBoolean(((ConfigBoolean) value).unwrapped());
            break;
        case NULL:
            break;
        case INT:
            // saving numbers as both string and binary is redundant but easy
            out.writeInt(((ConfigInt) value).unwrapped());
            out.writeUTF(((ConfigNumber) value).transformToString());
            break;
        case LONG:
            out.writeLong(((ConfigLong) value).unwrapped());
            out.writeUTF(((ConfigNumber) value).transformToString());
            break;
        case DOUBLE:
            out.writeDouble(((ConfigDouble) value).unwrapped());
            out.writeUTF(((ConfigNumber) value).transformToString());
            break;
        case STRING:
            out.writeUTF(((ConfigString) value).unwrapped());
            break;
        case LIST:
            ConfigList list = (ConfigList) value;
            out.writeInt(list.size());
            for (ConfigValue v : list) {
                writeValue(out, v, (SimpleConfigOrigin) list.origin());
            }
            break;
        case OBJECT:
            ConfigObject obj = (ConfigObject) value;
            out.writeInt(obj.size());
            for (Map.Entry<String, ConfigValue> e : obj.entrySet()) {
                out.writeUTF(e.getKey());
                writeValue(out, e.getValue(), (SimpleConfigOrigin) obj.origin());
            }
            break;
    }
}
Also used : ConfigValue(com.typesafe.config.ConfigValue) ConfigObject(com.typesafe.config.ConfigObject) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) Map(java.util.Map) ConfigList(com.typesafe.config.ConfigList)

Example 7 with ConfigValue

use of com.typesafe.config.ConfigValue in project helios by spotify.

the class TemporaryJobs method job.

private TemporaryJobBuilder job(final Job.Builder jobBuilder) {
    final TemporaryJobBuilder builder = new TemporaryJobBuilder(deployer, jobPrefixFile.prefix(), prober, env, reportWriter.get(), jobBuilder);
    if (config.hasPath("env")) {
        final Config env = config.getConfig("env");
        for (final Entry<String, ConfigValue> entry : env.entrySet()) {
            builder.env(entry.getKey(), entry.getValue().unwrapped());
        }
    }
    if (config.hasPath("version")) {
        builder.version(config.getString("version"));
    }
    if (config.hasPath("image")) {
        builder.image(config.getString("image"));
    }
    if (config.hasPath("command")) {
        builder.command(getListByKey("command", config));
    }
    if (config.hasPath("host")) {
        builder.host(config.getString("host"));
    }
    if (config.hasPath("deploy")) {
        builder.deploy(getListByKey("deploy", config));
    }
    if (config.hasPath("imageInfoFile")) {
        builder.imageFromInfoFile(config.getString("imageInfoFile"));
    }
    if (config.hasPath("registrationDomain")) {
        builder.registrationDomain(config.getString("registrationDomain"));
    }
    // port and expires intentionally left out -- since expires is a specific point in time, I
    // cannot imagine a config-file use for it, additionally for ports, I'm thinking that port
    // allocations are not likely to be common -- but PR's welcome if I'm wrong. - drewc@spotify.com
    builder.hostFilter(defaultHostFilter);
    return builder;
}
Also used : ConfigValue(com.typesafe.config.ConfigValue) Config(com.typesafe.config.Config)

Example 8 with ConfigValue

use of com.typesafe.config.ConfigValue in project config by typesafehub.

the class ConfigBeanImpl method createInternal.

/**
     * This is public ONLY for use by the "config" package, DO NOT USE this ABI
     * may change.
     * @param <T> type of the bean
     * @param config config to use
     * @param clazz class of the bean
     * @return the bean instance
     */
public static <T> T createInternal(Config config, Class<T> clazz) {
    if (((SimpleConfig) config).root().resolveStatus() != ResolveStatus.RESOLVED)
        throw new ConfigException.NotResolved("need to Config#resolve() a config before using it to initialize a bean, see the API docs for Config#resolve()");
    Map<String, AbstractConfigValue> configProps = new HashMap<String, AbstractConfigValue>();
    Map<String, String> originalNames = new HashMap<String, String>();
    for (Map.Entry<String, ConfigValue> configProp : config.root().entrySet()) {
        String originalName = configProp.getKey();
        String camelName = ConfigImplUtil.toCamelCase(originalName);
        // the camel one wins
        if (originalNames.containsKey(camelName) && !originalName.equals(camelName)) {
        // if we aren't a camel name to start with, we lose.
        // if we are or we are the first matching key, we win.
        } else {
            configProps.put(camelName, (AbstractConfigValue) configProp.getValue());
            originalNames.put(camelName, originalName);
        }
    }
    BeanInfo beanInfo = null;
    try {
        beanInfo = Introspector.getBeanInfo(clazz);
    } catch (IntrospectionException e) {
        throw new ConfigException.BadBean("Could not get bean information for class " + clazz.getName(), e);
    }
    try {
        List<PropertyDescriptor> beanProps = new ArrayList<PropertyDescriptor>();
        for (PropertyDescriptor beanProp : beanInfo.getPropertyDescriptors()) {
            if (beanProp.getReadMethod() == null || beanProp.getWriteMethod() == null) {
                continue;
            }
            beanProps.add(beanProp);
        }
        // Try to throw all validation issues at once (this does not comprehensively
        // find every issue, but it should find common ones).
        List<ConfigException.ValidationProblem> problems = new ArrayList<ConfigException.ValidationProblem>();
        for (PropertyDescriptor beanProp : beanProps) {
            Method setter = beanProp.getWriteMethod();
            Class<?> parameterClass = setter.getParameterTypes()[0];
            ConfigValueType expectedType = getValueTypeOrNull(parameterClass);
            if (expectedType != null) {
                String name = originalNames.get(beanProp.getName());
                if (name == null)
                    name = beanProp.getName();
                Path path = Path.newKey(name);
                AbstractConfigValue configValue = configProps.get(beanProp.getName());
                if (configValue != null) {
                    SimpleConfig.checkValid(path, expectedType, configValue, problems);
                } else {
                    if (!isOptionalProperty(clazz, beanProp)) {
                        SimpleConfig.addMissing(problems, expectedType, path, config.origin());
                    }
                }
            }
        }
        if (!problems.isEmpty()) {
            throw new ConfigException.ValidationFailed(problems);
        }
        // Fill in the bean instance
        T bean = clazz.newInstance();
        for (PropertyDescriptor beanProp : beanProps) {
            Method setter = beanProp.getWriteMethod();
            Type parameterType = setter.getGenericParameterTypes()[0];
            Class<?> parameterClass = setter.getParameterTypes()[0];
            String configPropName = originalNames.get(beanProp.getName());
            // Is the property key missing in the config?
            if (configPropName == null) {
                // If so, continue if the field is marked as @{link Optional}
                if (isOptionalProperty(clazz, beanProp)) {
                    continue;
                }
                // Otherwise, raise a {@link Missing} exception right here
                throw new ConfigException.Missing(beanProp.getName());
            }
            Object unwrapped = getValue(clazz, parameterType, parameterClass, config, configPropName);
            setter.invoke(bean, unwrapped);
        }
        return bean;
    } catch (InstantiationException e) {
        throw new ConfigException.BadBean(clazz.getName() + " needs a public no-args constructor to be used as a bean", e);
    } catch (IllegalAccessException e) {
        throw new ConfigException.BadBean(clazz.getName() + " getters and setters are not accessible, they must be for use as a bean", e);
    } catch (InvocationTargetException e) {
        throw new ConfigException.BadBean("Calling bean method on " + clazz.getName() + " caused an exception", e);
    }
}
Also used : ConfigValue(com.typesafe.config.ConfigValue) HashMap(java.util.HashMap) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) ArrayList(java.util.ArrayList) ConfigException(com.typesafe.config.ConfigException) ConfigValueType(com.typesafe.config.ConfigValueType) PropertyDescriptor(java.beans.PropertyDescriptor) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConfigValueType(com.typesafe.config.ConfigValueType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ConfigObject(com.typesafe.config.ConfigObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with ConfigValue

use of com.typesafe.config.ConfigValue in project config by typesafehub.

the class SimpleConfig method hasPathPeek.

private ConfigValue hasPathPeek(String pathExpression) {
    Path path = Path.newPath(pathExpression);
    ConfigValue peeked;
    try {
        peeked = object.peekPath(path);
    } catch (ConfigException.NotResolved e) {
        throw ConfigImpl.improveNotResolved(path, e);
    }
    return peeked;
}
Also used : ConfigValue(com.typesafe.config.ConfigValue) ConfigException(com.typesafe.config.ConfigException)

Example 10 with ConfigValue

use of com.typesafe.config.ConfigValue in project config by typesafehub.

the class SimpleConfig method findPaths.

private static void findPaths(Set<Map.Entry<String, ConfigValue>> entries, Path parent, AbstractConfigObject obj) {
    for (Map.Entry<String, ConfigValue> entry : obj.entrySet()) {
        String elem = entry.getKey();
        ConfigValue v = entry.getValue();
        Path path = Path.newKey(elem);
        if (parent != null)
            path = path.prepend(parent);
        if (v instanceof AbstractConfigObject) {
            findPaths(entries, path, (AbstractConfigObject) v);
        } else if (v instanceof ConfigNull) {
        // nothing; nulls are conceptually not in a Config
        } else {
            entries.add(new AbstractMap.SimpleImmutableEntry<String, ConfigValue>(path.render(), v));
        }
    }
}
Also used : ConfigValue(com.typesafe.config.ConfigValue) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map)

Aggregations

ConfigValue (com.typesafe.config.ConfigValue)16 ConfigException (com.typesafe.config.ConfigException)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 ConfigObject (com.typesafe.config.ConfigObject)3 Config (com.typesafe.config.Config)2 ConfigList (com.typesafe.config.ConfigList)2 ParaObject (com.erudika.para.core.ParaObject)1 ConfigValueType (com.typesafe.config.ConfigValueType)1 BeanInfo (java.beans.BeanInfo)1 IntrospectionException (java.beans.IntrospectionException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 AbstractMap (java.util.AbstractMap)1 EnumMap (java.util.EnumMap)1 List (java.util.List)1