private boolean setBTRootFromConfig() { boolean rc = false; // NB: This file name construction only works if this code is run from our plugin's // jar file in the standard elascticsearch directory tree. If you are running this // in some other way, either pass -Dbt.root=<BT root path> to the VM or set the // BT_ROOT environment variable. Environment environment = new Environment(); File myJarPath = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath()); File myConfigFile = new File(String.format("%s/../../%s/bt/%s-config.yml", myJarPath.getParent(), environment.configFile().getName(), PLUGIN_NAME)); logger.info(String.format("Attempting to set BT root directory from the configuration file '%s'.", myConfigFile.getAbsolutePath())); try { YamlSettingsLoader sl = new YamlSettingsLoader(); Map<String, String> settings = sl.load(readFile(myConfigFile)); String btRoot = settings.get(BT_ROOT_PARAM); rc = setBTRoot(btRoot); } catch (IOException e) { logger.error(String.format("Error reading config file %s\n Caused by: %s", myConfigFile.getAbsolutePath(), e.getStackTrace())); } return rc; }
public static Settings getSettingsFromResource(String path) throws IOException { InputStream is = SettingsLoader.class.getResourceAsStream(path); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder out = new StringBuilder(); String line; while((line = reader.readLine()) != null){ out.append("\n").append(line); } reader.close(); Map<String,String> settingsMap = new YamlSettingsLoader().load(out.toString()); return ImmutableSettings.settingsBuilder().put(settingsMap).build(); }