From 39b50f63c76099f3cf9bcd5d18b192110da6ae00 Mon Sep 17 00:00:00 2001 From: Amnon Heiman Date: Tue, 3 Nov 2015 14:54:29 +0200 Subject: [PATCH] Adding a configuration object for the jmx proxy This patch adds a configuration object to the jmx proxy that support both the system/command line based properties and it accept a yaml configuration file. The later options allows the jmx to read scylla configuration file and connect to it based on this configuration. The configuration file reader uses a yaml parser that was added to the pom.xml If no configuration file is found in the command line, it would look for SCYLLA_CONF then SCYLLA_HOME then for relative 'conf' directory Signed-off-by: Amnon Heiman need merge apiconfig --- pom.xml | 5 + .../com/cloudius/urchin/api/APIConfig.java | 115 ++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 src/main/java/com/cloudius/urchin/api/APIConfig.java diff --git a/pom.xml b/pom.xml index 33040f8..a56fa42 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,11 @@ + + org.yaml + snakeyaml + 1.16 + org.glassfish.jersey.core jersey-common diff --git a/src/main/java/com/cloudius/urchin/api/APIConfig.java b/src/main/java/com/cloudius/urchin/api/APIConfig.java new file mode 100644 index 0000000..04879fb --- /dev/null +++ b/src/main/java/com/cloudius/urchin/api/APIConfig.java @@ -0,0 +1,115 @@ +package com.cloudius.urchin.api; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.util.Map; + +import org.yaml.snakeyaml.Yaml; + +/* + * Copyright (C) 2015 ScyllaDB + */ + +/* + * This file is part of Scylla. + * + * Scylla is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Scylla 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Scylla. If not, see . + */ + +public class APIConfig { + static String address = "localhost"; + static String port = "10000"; + + public static String getAddress() { + return address; + } + + public static String getPort() { + return port; + } + + public static String getBaseUrl() { + return "http://" + address + ":" + + port; + } + + public static void readFile(String name) { + System.out.println("Using config file: " + name); + InputStream input; + try { + input = new FileInputStream(new File(name)); + Yaml yaml = new Yaml(); + @SuppressWarnings("unchecked") + Map map = (Map) yaml.load(input); + if (map.containsKey("listen_address")) { + address = (String) map.get("listen_address"); + } + if (map.containsKey("api_address")) { + address = (String) map.get("api_address"); + } + if (map.containsKey("api_port")) { + port = (String) map.get("api_port").toString(); + } + } catch (FileNotFoundException e) { + System.err.println("fail reading from config file: " + name); + System.exit(-1); + } + } + + public static boolean fileExists(String name) { + File varTmpDir = new File(name); + return varTmpDir.exists(); + } + + public static boolean loadIfExists(String path, String name) { + if (path == null) { + return false; + } + if (!fileExists(path + name)) { + return false; + } + readFile(path + name); + return true; + } + /** + * setConfig load the JMX proxy configuration + * The configuration hierarchy is as follow: + * Command line argument takes precedence over everything + * Then configuration file in the command line (command line + * argument can replace specific values in it. + * Then SCYLLA_CONF/scylla.yaml + * Then SCYLLA_HOME/conf/scylla.yaml + * Then conf/scylla.yaml + * Then the default values + * With file configuration, to make it clearer what is been used, only + * one file will be chosen with the highest precedence + */ + public static void setConfig() { + if (!System.getProperty("apiconfig","").equals("")) { + readFile(System.getProperty("apiconfig")); + } else if (!loadIfExists(System.getenv("SCYLLA_CONF"), "/scylla.yaml") && + !loadIfExists(System.getenv("SCYLLA_HOME"), "/conf/scylla.yaml")) { + loadIfExists("", "conf/scylla.yaml"); + } + + if (!System.getProperty("apiaddress", "").equals("")) { + address = System.getProperty("apiaddress"); + } + if (!System.getProperty("apiport", "").equals("")) { + port = System.getProperty("apiport", "10000"); + } + } +}