use of org.yaml.snakeyaml.Yaml in project nifi-minifi by apache.
the class SchemaLoader method loadYamlAsMap.
public static Map<String, Object> loadYamlAsMap(InputStream sourceStream) throws IOException, SchemaLoaderException {
try {
Yaml yaml = new Yaml();
// Parse the YAML file
final Object loadedObject = yaml.load(sourceStream);
// Verify the parsed object is a Map structure
if (loadedObject instanceof Map) {
return (Map<String, Object>) loadedObject;
} else {
throw new SchemaLoaderException("Provided YAML configuration is not a Map");
}
} catch (YAMLException e) {
throw new IOException(e);
} finally {
sourceStream.close();
}
}
use of org.yaml.snakeyaml.Yaml in project nifi-minifi by apache.
the class PullHttpChangeIngestor method run.
@Override
public void run() {
try {
logger.debug("Attempting to pull new config");
HttpUrl.Builder builder = new HttpUrl.Builder().host(hostReference.get()).port(portReference.get()).encodedPath(pathReference.get());
String query = queryReference.get();
if (!StringUtil.isNullOrEmpty(query)) {
builder = builder.encodedQuery(query);
}
final HttpUrl url = builder.scheme(connectionScheme).build();
final Request.Builder requestBuilder = new Request.Builder().get().url(url);
if (useEtag) {
requestBuilder.addHeader("If-None-Match", lastEtag);
}
final Request request = requestBuilder.build();
final OkHttpClient httpClient = httpClientReference.get();
final Call call = httpClient.newCall(request);
final Response response = call.execute();
logger.debug("Response received: {}", response.toString());
int code = response.code();
if (code == NOT_MODIFIED_STATUS_CODE) {
return;
}
if (code >= 400) {
throw new IOException("Got response code " + code + " while trying to pull configuration: " + response.body().string());
}
ResponseBody body = response.body();
if (body == null) {
logger.warn("No body returned when pulling a new configuration");
return;
}
ByteBuffer bodyByteBuffer = ByteBuffer.wrap(body.bytes());
ByteBuffer readOnlyNewConfig = null;
// checking if some parts of the configuration must be preserved
if (overrideSecurity) {
readOnlyNewConfig = bodyByteBuffer.asReadOnlyBuffer();
} else {
logger.debug("Preserving previous security properties...");
// get the current security properties from the current configuration file
final File configFile = new File(properties.get().getProperty(RunMiNiFi.MINIFI_CONFIG_FILE_KEY));
ConvertableSchema<ConfigSchema> configSchema = SchemaLoader.loadConvertableSchemaFromYaml(new FileInputStream(configFile));
ConfigSchema currentSchema = configSchema.convert();
SecurityPropertiesSchema secProps = currentSchema.getSecurityProperties();
// override the security properties in the pulled configuration with the previous properties
configSchema = SchemaLoader.loadConvertableSchemaFromYaml(new ByteBufferInputStream(bodyByteBuffer.duplicate()));
ConfigSchema newSchema = configSchema.convert();
newSchema.setSecurityProperties(secProps);
// return the updated configuration preserving the previous security configuration
readOnlyNewConfig = ByteBuffer.wrap(new Yaml().dump(newSchema.toMap()).getBytes()).asReadOnlyBuffer();
}
if (differentiator.isNew(readOnlyNewConfig)) {
logger.debug("New change received, notifying listener");
configurationChangeNotifier.notifyListeners(readOnlyNewConfig);
logger.debug("Listeners notified");
} else {
logger.debug("Pulled config same as currently running.");
}
if (useEtag) {
lastEtag = (new StringBuilder("\"")).append(response.header("ETag").trim()).append("\"").toString();
}
} catch (Exception e) {
logger.warn("Hit an exception while trying to pull", e);
}
}
use of org.yaml.snakeyaml.Yaml in project mxgwd by kamax-io.
the class YamlConfigLoader method loadFromFile.
public static Config loadFromFile(String path) throws IOException {
Representer rep = new Representer();
rep.getPropertyUtils().setAllowReadOnlyProperties(true);
rep.getPropertyUtils().setSkipMissingProperties(true);
Yaml yaml = new Yaml(new Constructor(Config.class), rep);
Object o = yaml.load(new FileInputStream(path));
return GsonUtil.get().fromJson(GsonUtil.get().toJson(o), Config.class);
}
use of org.yaml.snakeyaml.Yaml in project rxlib by RockyLOMO.
the class App method readSettings.
@SneakyThrows
public static Map<String, Object> readSettings(String yamlFile, boolean isResource) {
Map<String, Object> result = null;
Yaml yaml = new Yaml(new SafeConstructor());
for (Object data : yaml.loadAll(isResource ? getClassLoader().getResourceAsStream(yamlFile) : new FileInputStream(yamlFile))) {
Map<String, Object> map = (Map<String, Object>) data;
if (result == null) {
result = map;
continue;
}
result.putAll(map);
}
if (result == null) {
result = new HashMap<>();
}
return result;
}
use of org.yaml.snakeyaml.Yaml in project watchdog by isdream.
the class ConfigureTest method main.
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Configure config = new Configure();
config.setKind(Constants.YAML_POD);
Map<String, String> properties = new HashMap<String, String>();
properties.put(KubernetesListener.ALL_NAMESPACE, KubernetesListener.ALL_NAMESPACE);
properties.put("test", "test");
config.setProperties(properties);
// List<Object> handlers = new ArrayList<Object>();
// KubernetesNamespaceFilterHandler knf = new KubernetesNamespaceFilterHandler();
// KubernetesKindAnalyzerHandler kka = new KubernetesKindAnalyzerHandler();
// DefaultConsoleHandler dch = new DefaultConsoleHandler();
// handlers.add(KubernetesNamespaceFilterHandler.class.getName());
// handlers.add(KubernetesKindAnalyzerHandler.class.getName());
// handlers.add(DefaultConsoleHandler.class.getName());
//
// config.setHandlers(handlers);
Yaml yaml = new Yaml();
System.out.println(yaml.dump(config));
}
Aggregations