Search in sources :

Example 91 with Yaml

use of org.yaml.snakeyaml.Yaml in project incubator-heron by apache.

the class ConfigUtilsTests method testStateManagerFileOverrides.

@Test
@SuppressWarnings("unchecked")
public void testStateManagerFileOverrides() throws IOException {
    final Properties overrideProperties = createOverrideProperties(Pair.create("heron.statemgr.connection.string", "zookeeper:2181"), Pair.create("heron.kubernetes.scheduler.uri", "http://127.0.0.1:8001"));
    final String overridesPath = ConfigUtils.createOverrideConfiguration(overrideProperties);
    // write default state manager config
    final Path stateManagerPath = Files.createTempFile("statemgr-", ".yaml");
    stateManagerPath.toFile().deleteOnExit();
    try (Writer writer = Files.newBufferedWriter(stateManagerPath)) {
        final Map<String, String> config = new HashMap<>();
        config.put("heron.statemgr.connection.string", "<host>:<port>");
        new Yaml().dump(config, writer);
    }
    // apply the overrides
    ConfigUtils.applyOverridesToStateManagerConfig(Paths.get(overridesPath), stateManagerPath);
    try (Reader reader = Files.newBufferedReader(stateManagerPath)) {
        final Map<String, Object> stateManagerWithOverrides = (Map<String, Object>) new Yaml().loadAs(reader, Map.class);
        assertEquals(stateManagerWithOverrides.size(), 1);
        assertEquals(stateManagerWithOverrides.get("heron.statemgr.connection.string"), "zookeeper:2181");
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) Reader(java.io.Reader) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map) Writer(java.io.Writer) Yaml(org.yaml.snakeyaml.Yaml) Test(org.junit.Test)

Example 92 with Yaml

use of org.yaml.snakeyaml.Yaml in project incubator-heron by apache.

the class ConfigUtilsTests method testApplyOverrides.

@Test
@SuppressWarnings("unchecked")
public void testApplyOverrides() throws IOException {
    final Properties overrideProperties = createOverrideProperties(Pair.create("heron.statemgr.connection.string", "zookeeper:2181"), Pair.create("heron.kubernetes.scheduler.uri", "http://127.0.0.1:8001"));
    final String overridesPath = ConfigUtils.createOverrideConfiguration(overrideProperties);
    final Map<String, String> overrides = createOverrides(Pair.create("my.override.key", "my.override.value"));
    ConfigUtils.applyOverrides(Paths.get(overridesPath), overrides);
    final Map<String, String> combinedOverrides = new HashMap<>();
    combinedOverrides.putAll(overrides);
    for (String key : overrideProperties.stringPropertyNames()) {
        combinedOverrides.put(key, overrideProperties.getProperty(key));
    }
    try (Reader reader = Files.newBufferedReader(Paths.get(overridesPath))) {
        final Map<String, Object> newOverrides = (Map<String, Object>) new Yaml().loadAs(reader, Map.class);
        assertEquals(newOverrides, combinedOverrides);
    }
}
Also used : HashMap(java.util.HashMap) Reader(java.io.Reader) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map) Yaml(org.yaml.snakeyaml.Yaml) Test(org.junit.Test)

Example 93 with Yaml

use of org.yaml.snakeyaml.Yaml in project incubator-heron by apache.

the class ConfigUtils method newYaml.

private static Yaml newYaml() {
    final DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
    options.setPrettyFlow(true);
    return new Yaml(options);
}
Also used : DumperOptions(org.yaml.snakeyaml.DumperOptions) Yaml(org.yaml.snakeyaml.Yaml)

Example 94 with Yaml

use of org.yaml.snakeyaml.Yaml in project serverless by bluenimble.

the class SpecUtils method y2j.

public static void y2j(File fileOrFolder, boolean deleteSource) throws Exception {
    if (!fileOrFolder.exists()) {
        return;
    }
    if (fileOrFolder.isDirectory()) {
        File[] files = fileOrFolder.listFiles(new FileFilter() {

            @Override
            public boolean accept(File file) {
                return file.isDirectory() || file.getName().endsWith(".yaml");
            }
        });
        if (files == null || files.length == 0) {
            return;
        }
        for (File f : files) {
            y2j(f, deleteSource);
        }
    } else {
        File jsonFile = new File(fileOrFolder.getParentFile(), fileOrFolder.getName().substring(0, fileOrFolder.getName().lastIndexOf(Lang.DOT)) + ".json");
        Yaml yaml = new Yaml();
        InputStream is = null;
        try {
            is = new FileInputStream(fileOrFolder);
            @SuppressWarnings("unchecked") JsonObject o = new JsonObject(yaml.loadAs(is, Map.class), true);
            Json.store(o, jsonFile);
        } finally {
            IOUtils.closeQuietly(is);
        }
        if (deleteSource) {
            FileUtils.delete(fileOrFolder);
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonObject(com.bluenimble.platform.json.JsonObject) FileFilter(java.io.FileFilter) File(java.io.File) Map(java.util.Map) Yaml(org.yaml.snakeyaml.Yaml) FileInputStream(java.io.FileInputStream)

Example 95 with Yaml

use of org.yaml.snakeyaml.Yaml in project serverless by bluenimble.

the class SpecUtils method j2y.

@SuppressWarnings("unchecked")
public static void j2y(File fileOrFolder, boolean deleteSource) throws Exception {
    if (!fileOrFolder.exists()) {
        return;
    }
    if (fileOrFolder.isDirectory()) {
        File[] files = fileOrFolder.listFiles(new FileFilter() {

            @Override
            public boolean accept(File file) {
                return file.isDirectory() || file.getName().endsWith(".json");
            }
        });
        if (files == null || files.length == 0) {
            return;
        }
        for (File f : files) {
            j2y(f, deleteSource);
        }
    } else {
        File ymlFile = new File(fileOrFolder.getParentFile(), fileOrFolder.getName().substring(0, fileOrFolder.getName().lastIndexOf(Lang.DOT)) + ".yaml");
        OutputStream out = null;
        try {
            out = new FileOutputStream(ymlFile);
            new YamlOutputStreamPrinter(out).print(Json.load(fileOrFolder));
        } finally {
            IOUtils.closeQuietly(out);
        }
        // validate
        Yaml yaml = new Yaml();
        InputStream is = null;
        try {
            is = new FileInputStream(ymlFile);
            new JsonObject(yaml.loadAs(is, Map.class), true);
        } finally {
            IOUtils.closeQuietly(is);
        }
        if (deleteSource) {
            FileUtils.delete(fileOrFolder);
        }
    }
}
Also used : YamlOutputStreamPrinter(com.bluenimble.platform.json.printers.YamlOutputStreamPrinter) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) JsonObject(com.bluenimble.platform.json.JsonObject) FileFilter(java.io.FileFilter) File(java.io.File) Map(java.util.Map) Yaml(org.yaml.snakeyaml.Yaml) FileInputStream(java.io.FileInputStream)

Aggregations

Yaml (org.yaml.snakeyaml.Yaml)276 Map (java.util.Map)104 HashMap (java.util.HashMap)85 IOException (java.io.IOException)58 FileInputStream (java.io.FileInputStream)49 InputStream (java.io.InputStream)49 File (java.io.File)43 DumperOptions (org.yaml.snakeyaml.DumperOptions)42 Constructor (org.yaml.snakeyaml.constructor.Constructor)30 Test (org.junit.Test)26 ArrayList (java.util.ArrayList)25 FileNotFoundException (java.io.FileNotFoundException)22 SafeConstructor (org.yaml.snakeyaml.constructor.SafeConstructor)22 List (java.util.List)21 Writer (java.io.Writer)18 Path (java.nio.file.Path)17 LinkedHashMap (java.util.LinkedHashMap)17 Reader (java.io.Reader)16 Properties (java.util.Properties)14 InputStreamReader (java.io.InputStreamReader)13