Search in sources :

Example 1 with SafeConstructor

use of org.yaml.snakeyaml.constructor.SafeConstructor in project storm by apache.

the class Utils method findAndReadConfigFile.

public static Map<String, Object> findAndReadConfigFile(String name, boolean mustExist) {
    InputStream in = null;
    boolean confFileEmpty = false;
    try {
        in = getConfigFileInputStream(name);
        if (null != in) {
            Yaml yaml = new Yaml(new SafeConstructor());
            @SuppressWarnings("unchecked") Map<String, Object> ret = (Map<String, Object>) yaml.load(new InputStreamReader(in));
            if (null != ret) {
                return new HashMap<>(ret);
            } else {
                confFileEmpty = true;
            }
        }
        if (mustExist) {
            if (confFileEmpty)
                throw new RuntimeException("Config file " + name + " doesn't have any valid storm configs");
            else
                throw new RuntimeException("Could not find config file on classpath " + name);
        } else {
            return new HashMap<>();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (null != in) {
            try {
                in.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ClassLoaderObjectInputStream(org.apache.commons.io.input.ClassLoaderObjectInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Yaml(org.yaml.snakeyaml.Yaml) SafeConstructor(org.yaml.snakeyaml.constructor.SafeConstructor) ComponentObject(org.apache.storm.generated.ComponentObject) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 2 with SafeConstructor

use of org.yaml.snakeyaml.constructor.SafeConstructor in project camel by apache.

the class SnakeYAMLDataFormat method defaultConstructor.

// ***************************
// Defaults
// ***************************
private BaseConstructor defaultConstructor(CamelContext context) {
    ClassLoader yamlClassLoader = this.classLoader;
    Collection<TypeFilter> yamlTypeFilters = this.typeFilters;
    if (yamlClassLoader == null && useApplicationContextClassLoader) {
        yamlClassLoader = context.getApplicationContextClassLoader();
    }
    if (allowAnyType) {
        yamlTypeFilters = Collections.singletonList(TypeFilters.allowAll());
    }
    BaseConstructor yamlConstructor;
    if (yamlTypeFilters != null) {
        yamlConstructor = yamlClassLoader != null ? typeFilterConstructor(yamlClassLoader, yamlTypeFilters) : typeFilterConstructor(yamlTypeFilters);
    } else {
        yamlConstructor = new SafeConstructor();
    }
    if (typeDescriptions != null && yamlConstructor instanceof Constructor) {
        for (TypeDescription typeDescription : typeDescriptions) {
            ((Constructor) yamlConstructor).addTypeDescription(typeDescription);
        }
    }
    return yamlConstructor;
}
Also used : BaseConstructor(org.yaml.snakeyaml.constructor.BaseConstructor) BaseConstructor(org.yaml.snakeyaml.constructor.BaseConstructor) SafeConstructor(org.yaml.snakeyaml.constructor.SafeConstructor) CustomClassLoaderConstructor(org.yaml.snakeyaml.constructor.CustomClassLoaderConstructor) Constructor(org.yaml.snakeyaml.constructor.Constructor) SafeConstructor(org.yaml.snakeyaml.constructor.SafeConstructor) TypeDescription(org.yaml.snakeyaml.TypeDescription)

Example 3 with SafeConstructor

use of org.yaml.snakeyaml.constructor.SafeConstructor in project jstorm by alibaba.

the class LoadConf method findAndReadYaml.

/**
     * @param name
     * @param mustExist   -- if this is true, the file must exist, otherwise throw exception
     * @param canMultiple -- if this is false and there is multiple conf, it will throw exception
     * @return
     */
public static Map findAndReadYaml(String name, boolean mustExist, boolean canMultiple) {
    InputStream in = null;
    boolean confFileEmpty = false;
    try {
        in = getConfigFileInputStream(name, canMultiple);
        if (null != in) {
            Yaml yaml = new Yaml(new SafeConstructor());
            Map ret = (Map) yaml.load(new InputStreamReader(in));
            if (null != ret) {
                return new HashMap(ret);
            } else {
                confFileEmpty = true;
            }
        }
        if (mustExist) {
            if (confFileEmpty)
                throw new RuntimeException("Config file " + name + " doesn't have any valid storm configs");
            else
                throw new RuntimeException("Could not find config file on classpath " + name);
        } else {
            return new HashMap();
        }
    } catch (IOException e) {
        StringBuilder sb = new StringBuilder();
        sb.append("Invalid configuration ").append(name).append(":").append(e.getMessage());
        throw new RuntimeException(sb.toString(), e);
    } finally {
        if (null != in) {
            try {
                in.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SafeConstructor(org.yaml.snakeyaml.constructor.SafeConstructor) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) Yaml(org.yaml.snakeyaml.Yaml)

Aggregations

SafeConstructor (org.yaml.snakeyaml.constructor.SafeConstructor)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Yaml (org.yaml.snakeyaml.Yaml)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 TreeMap (java.util.TreeMap)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)1 ClassLoaderObjectInputStream (org.apache.commons.io.input.ClassLoaderObjectInputStream)1 ComponentObject (org.apache.storm.generated.ComponentObject)1 TypeDescription (org.yaml.snakeyaml.TypeDescription)1 BaseConstructor (org.yaml.snakeyaml.constructor.BaseConstructor)1 Constructor (org.yaml.snakeyaml.constructor.Constructor)1 CustomClassLoaderConstructor (org.yaml.snakeyaml.constructor.CustomClassLoaderConstructor)1