package ch.uzh.ifi.ddis.ifp.esper.cassandra;
/*
* #%L
* Cassandra for Esper
* %%
* Copyright (C) 2013 University of Zurich
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* <p>
* Helper class that manages parsing the configuration JSON file.
* </p>
* <p>
* <strong>Note: This class may be subject to change without
* notification.</strong>
* </p>
*
* @author Thomas Scharrenbach
* @version 0.1.3
* @since 0.0.2
*
*/
class CassandraConfiguration {
//
//
//
private static final Logger _log = LoggerFactory
.getLogger(CassandraConfiguration.class);
public static final String CASSANDRA_HOST = "host";
public static final String CASSANDRA_KEYSPACE = "keyspace";
public static final String CASSANDRA_TABLE = "table";
//
//
//
/**
* <p>
* Creates a new {@link CassandraConfiguration} from a file or resource.
* </p>
* <p>
* This method first tests whether the configuration parameter is a file. If
* this fails, it tests whether the parameter references a resource. If this
* fails, the method tries to treat the string as a JSON content.
* </p>
*
* @param configuration
* String that either references a file, a resource or a JSON
* string.
* @return new {@link CassandraConfiguration}
* @throws IOException
* if the string is a file or a resource but an
* {@link IOException} occurs when reading the file or resource.
* @throws ParseException
* if the string is neither a file nor a resource but a JSON
* string and the parsing fails.
* @throws RuntimeException
* if the parameter is neither a file nor a resource nor a JSON
* string.
*/
public static CassandraConfiguration create(String configuration)
throws IOException, ParseException {
final CassandraConfiguration result = new CassandraConfiguration();
final JSONParser parser = new JSONParser();
try {
_log.info("Started checking whether configuration is a URI...");
final File file = new File(configuration);
if (file.exists()) {
_log.info("Configuration is a file.");
result.configure((JSONObject) parser
.parse(new FileReader(file)));
return result;
} else {
_log.info("Configuration is not a file.");
}
_log.info("Started checking whether configuration is a resource...");
InputStream stream = null;
try {
stream = CassandraConfiguration.class
.getResourceAsStream(configuration);
_log.info("Configuration is a resource.");
} catch (Exception e) {
_log.info("Configuration is not a resource.");
} finally {
if (stream != null) {
result.configure((JSONObject) parser
.parse(new BufferedReader(new InputStreamReader(
stream))));
return result;
}
}
_log.info("Started checking whether configuration is a JSON string...");
JSONObject configObject = null;
try {
configObject = (JSONObject) parser.parse(configuration);
} catch (Exception e) {
_log.info("Configuration is not a JSON string.");
} finally {
if (configObject != null) {
_log.info("Configuration is a JSON string.");
result.configure(configObject);
return result;
}
}
throw new RuntimeException(
"Configuration is neither a file nor a resource nor a JSON string");
} catch (ParseException e) {
throw e;
} catch (IOException e) {
throw e;
}
}
//
//
//
private String _cassandraHost;
private String _keyspace;
private String _table;
//
//
//
private CassandraConfiguration() {
}
//
//
//
/**
* Extracts all parameters from the cassandra configuration.
*
* @param configuration
*/
public CassandraConfiguration configure(JSONObject cassandraConfig)
throws IllegalArgumentException {
_log.debug("Cassandra configuration: {}", cassandraConfig);
try {
return handleHost(cassandraConfig).handleKeyspace(cassandraConfig)
.handleTable(cassandraConfig);
} catch (IllegalArgumentException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
protected CassandraConfiguration handleHost(JSONObject cassandraConfig)
throws IllegalArgumentException {
Object tmp = null;
//
try {
tmp = cassandraConfig.get(CASSANDRA_HOST).toString();
_cassandraHost = tmp.toString();
} catch (Exception e) {
throw new IllegalArgumentException(String.format(
"Illegal argument for %s with value %s !", CASSANDRA_HOST,
tmp), e);
}
return this;
}
protected CassandraConfiguration handleKeyspace(JSONObject cassandraConfig) {
Object tmp = null;
//
try {
tmp = cassandraConfig.get(CASSANDRA_KEYSPACE).toString();
_keyspace = tmp.toString();
} catch (Exception e) {
throw new IllegalArgumentException(String.format(
"Illegal argument for %s with value %s !",
CASSANDRA_KEYSPACE, tmp), e);
}
return this;
}
protected CassandraConfiguration handleTable(JSONObject cassandraConfig) {
Object tmp = null;
//
try {
tmp = cassandraConfig.get(CASSANDRA_TABLE).toString();
_table = tmp.toString();
} catch (Exception e) {
throw new IllegalArgumentException(String.format(
"Illegal argument for %s with value %s !", CASSANDRA_TABLE,
tmp), e);
}
return this;
}
//
//
//
public String getHost() {
return _cassandraHost;
}
public String getKeyspace() {
return _keyspace;
}
public String getTable() {
return _table;
}
}