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);
}
}
}
}
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;
}
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);
}
}
}
}
Aggregations