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

org.sonar.application.es.EsYmlSettings Maven / Gradle / Ivy

/*
 * SonarQube
 * Copyright (C) 2009-2018 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.application.es;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.Map;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import static org.yaml.snakeyaml.DumperOptions.FlowStyle.BLOCK;

public class EsYmlSettings {
  private static final String ELASTICSEARCH_YML_OPTIONS_HEADER = "# This file has been automatically generated by SonarQube during startup.\n" +
    "\n" +
    "# DO NOT EDIT THIS FILE\n" +
    "\n";

  private final Map elasticsearchSettings;

  public EsYmlSettings(Map elasticsearchSettings) {
    this.elasticsearchSettings = elasticsearchSettings;
  }

  public void writeToYmlSettingsFile(File file) {
    DumperOptions dumperOptions = new DumperOptions();
    dumperOptions.setPrettyFlow(true);
    dumperOptions.setDefaultFlowStyle(BLOCK);
    Yaml yaml = new Yaml(dumperOptions);
    String output = ELASTICSEARCH_YML_OPTIONS_HEADER + yaml.dump(elasticsearchSettings);
    try {
      Files.write(file.toPath(), output.getBytes(Charset.forName("UTF-8")));
    } catch (IOException e) {
      throw new IllegalStateException("Cannot write Elasticsearch yml settings file", e);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy