All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.osgl.inject.loader.ConfigurationValueLoader Maven / Gradle / Ivy

There is a newer version: 1.13.2
Show newest version
package org.osgl.inject.loader;

import org.osgl.$;
import org.osgl.inject.BeanSpec;
import org.osgl.inject.InjectException;
import org.osgl.inject.ValueLoader;
import org.osgl.util.S;
import org.osgl.util.StringValueResolver;

/**
 * Load value from configuration source
 */
public abstract class ConfigurationValueLoader extends ValueLoader.Base {

    @Override
    public T get() {
        String confKey = value();
        if (S.isBlank(confKey)) {
            throw new InjectException(("Missing configuration key"));
        }
        Object conf = conf(confKey);
        if (null == conf) {
            return null;
        }
        return cast(conf, spec);
    }

    /**
     * Return whatever configured by `key`
     * @param key the configuration key
     * @return the configured value
     */
    protected abstract Object conf(String key);

    private T cast(Object val, BeanSpec spec) {
        Class type = spec.rawType();
        if (type.isInstance(val)) {
            return (T) val;
        }
        if ($.isSimpleType(type)) {
            StringValueResolver svr = StringValueResolver.predefined(type);
            if (null != svr) {
                return (T) svr.resolve(S.string(val));
            }
        }
        throw new InjectException("Cannot cast value type[%s] to required type[%]", val.getClass(), type);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy