Merge "JMX proxy to support configuration file" from Amnon

"Scylla's configuration can change the listening address and port of the API,
the jmx proxy need to use this same configuration.

This series adds the ability to add a path to a yaml configuration file and the
jmx proxy would read its configuration from there.  Configuration from system
properties/command line is still supported and the configuration hirarchy is as
follow from highest to lowest:
* command line
* configuration file according to the hirarchy:
  - command line
  - SCYLLA_CONF
  - SCYLLA_HOME
  - relative conf directory
* default values

The configuration definition was moved to a configuraion class that responsible
for getting the information from the command line and the configuration file."
This commit is contained in:
Pekka Enberg 2015-11-11 09:08:08 +02:00
commit 81aa2a8279
4 changed files with 124 additions and 4 deletions

View File

@ -15,6 +15,11 @@
</properties>
<dependencies>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>

View File

@ -74,8 +74,7 @@ public class APIClient {
.getLogger(APIClient.class.getName());
public static String getBaseUrl() {
return "http://" + System.getProperty("apiaddress", "localhost") + ":"
+ System.getProperty("apiport", "10000");
return APIConfig.getBaseUrl();
}
public Invocation.Builder get(String path, MultivaluedMap<String, String> queryParams) {

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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<String, Object> map = (Map<String, Object>) 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");
}
}
}

View File

@ -3,7 +3,7 @@
*/
package com.cloudius.urchin.main;
import com.cloudius.urchin.api.APIClient;
import com.cloudius.urchin.api.APIConfig;
import org.apache.cassandra.db.ColumnFamilyStore;
import org.apache.cassandra.db.commitlog.CommitLog;
@ -19,7 +19,8 @@ import org.apache.cassandra.service.StorageService;
public class Main {
public static void main(String[] args) throws Exception {
System.out.println("Connecting to " + APIClient.getBaseUrl());
APIConfig.setConfig();
System.out.println("Connecting to " + APIConfig.getBaseUrl());
System.out.println("Starting the JMX server");
StorageService.getInstance();
StorageProxy.getInstance();