http://git-wip-us.apache.org/repos/asf/cassandra/blob/38027382/src/java/org/apache/cassandra/cli/CliCommandHelp.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cli/CliCommandHelp.java b/src/java/org/apache/cassandra/cli/CliCommandHelp.java deleted file mode 100644 index dce8d60..0000000 --- a/src/java/org/apache/cassandra/cli/CliCommandHelp.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.cassandra.cli; - -public class CliCommandHelp -{ - public String name; - public String help; -} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/cassandra/blob/38027382/src/java/org/apache/cassandra/cli/CliCompiler.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cli/CliCompiler.java b/src/java/org/apache/cassandra/cli/CliCompiler.java deleted file mode 100644 index 6b84be1..0000000 --- a/src/java/org/apache/cassandra/cli/CliCompiler.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.cassandra.cli; - -import java.util.List; - -import org.antlr.runtime.ANTLRStringStream; -import org.antlr.runtime.CharStream; -import org.antlr.runtime.CommonTokenStream; -import org.antlr.runtime.tree.Tree; -import org.apache.cassandra.thrift.CfDef; -import org.apache.cassandra.thrift.KsDef; - - -public class CliCompiler -{ - - // ANTLR does not provide case-insensitive tokenization support - // out of the box. So we override the LA (lookahead) function - // of the ANTLRStringStream class. Note: This doesn't change the - // token text-- but just relaxes the matching rules to match - // in upper case. [Logic borrowed from Hive code.] - // - // Also see discussion on this topic in: - // http://www.antlr.org/wiki/pages/viewpage.action?pageId=1782. - public static class ANTLRNoCaseStringStream extends ANTLRStringStream - { - public ANTLRNoCaseStringStream(String input) - { - super(input); - } - - public int LA(int i) - { - int returnChar = super.LA(i); - if (returnChar == CharStream.EOF) - { - return returnChar; - } - else if (returnChar == 0) - { - return returnChar; - } - - return Character.toUpperCase((char)returnChar); - } - } - - public static Tree compileQuery(String query) - { - Tree queryTree; - - try - { - ANTLRStringStream input = new ANTLRNoCaseStringStream(query); - - CliLexer lexer = new CliLexer(input); - CommonTokenStream tokens = new CommonTokenStream(lexer); - - CliParser parser = new CliParser(tokens); - - // start parsing... - queryTree = (Tree)(parser.root().getTree()); - - // semantic analysis if any... - // [tbd] - - } - catch(Exception e) - { - // if there was an exception we don't want to process request any further - throw new RuntimeException(e.getMessage(), e); - } - - return queryTree; - } - /* - * NODE_COLUMN_ACCESS related functions. - */ - - public static String getColumnFamily(Tree astNode, Iterable<CfDef> cfDefs) - { - return getColumnFamily(CliUtils.unescapeSQLString(astNode.getChild(0).getText()), cfDefs); - } - - public static String getColumnFamily(String cfName, Iterable<CfDef> cfDefs) - { - int matches = 0; - String lastMatchedName = ""; - - for (CfDef cfDef : cfDefs) - { - if (cfDef.name.equals(cfName)) - { - return cfName; - } - else if (cfDef.name.toUpperCase().equals(cfName.toUpperCase())) - { - lastMatchedName = cfDef.name; - matches++; - } - } - - if (matches > 1 || matches == 0) - throw new RuntimeException(cfName + " not found in current keyspace."); - - return lastMatchedName; - } - - public static String getKeySpace(Tree statement, List<KsDef> keyspaces) - { - return getKeySpace(CliUtils.unescapeSQLString(statement.getChild(0).getText()), keyspaces); - } - - public static String getKeySpace(String ksName, List<KsDef> keyspaces) - { - int matches = 0; - String lastMatchedName = ""; - - for (KsDef ksDef : keyspaces) - { - if (ksDef.name.equals(ksName)) - { - return ksName; - } - else if (ksDef.name.toUpperCase().equals(ksName.toUpperCase())) - { - lastMatchedName = ksDef.name; - matches++; - } - } - - if (matches > 1 || matches == 0) - throw new RuntimeException("Keyspace '" + ksName + "' not found."); - - return lastMatchedName; - } - - public static String getKey(Tree astNode) - { - return CliUtils.unescapeSQLString(astNode.getChild(1).getText()); - } - - public static int numColumnSpecifiers(Tree astNode) - { - // Skip over keyspace, column family and rowKey - return astNode.getChildCount() - 2; - } - - // Returns the pos'th (0-based index) column specifier in the astNode - public static String getColumn(Tree astNode, int pos) - { - // Skip over keyspace, column family and rowKey - return CliUtils.unescapeSQLString(astNode.getChild(pos + 2).getText()); - } - -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/38027382/src/java/org/apache/cassandra/cli/CliCompleter.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cli/CliCompleter.java b/src/java/org/apache/cassandra/cli/CliCompleter.java deleted file mode 100644 index a95de54..0000000 --- a/src/java/org/apache/cassandra/cli/CliCompleter.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.cassandra.cli; - -import jline.SimpleCompletor; - -public class CliCompleter extends SimpleCompletor -{ - private static final String[] commands = { - "connect", - "describe keyspace", - "exit", - "help", - "quit", - "show cluster name", - "show keyspaces", - "show schema", - "show api version", - "create keyspace", - "create column family", - "drop keyspace", - "drop column family", - "rename keyspace", - "rename column family", - "consistencylevel", - - "help connect", - "help describe keyspace", - "help exit", - "help help", - "help quit", - "help show cluster name", - "help show keyspaces", - "help show schema", - "help show api version", - "help create keyspace", - "help create column family", - "help drop keyspace", - "help drop column family", - "help rename keyspace", - "help rename column family", - "help get", - "help set", - "help del", - "help count", - "help list", - "help truncate", - "help consistencylevel" - }; - private static final String[] keyspaceCommands = { - "get", - "set", - "count", - "del", - "list", - "truncate", - "incr", - "decr" - }; - - public CliCompleter() - { - super(commands); - } - - String[] getKeyspaceCommands() - { - return keyspaceCommands; - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/38027382/src/java/org/apache/cassandra/cli/CliMain.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cli/CliMain.java b/src/java/org/apache/cassandra/cli/CliMain.java deleted file mode 100644 index 7cfc77b..0000000 --- a/src/java/org/apache/cassandra/cli/CliMain.java +++ /dev/null @@ -1,405 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.cassandra.cli; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.nio.charset.CharacterCodingException; -import java.util.*; - -import org.apache.cassandra.auth.IAuthenticator; -import org.apache.cassandra.io.util.FileUtils; -import org.apache.cassandra.thrift.*; -import org.apache.cassandra.utils.FBUtilities; -import org.apache.thrift.TException; -import org.apache.thrift.protocol.TBinaryProtocol; -import org.apache.thrift.transport.TTransport; -import jline.ConsoleReader; -import jline.History; - -/** - * Cassandra Command Line Interface (CLI) Main - */ -public class CliMain -{ - public final static String OLD_HISTORYFILE = ".cassandra.history"; - public final static String HISTORYFILE = "cli.history"; - - private static TTransport transport = null; - private static Cassandra.Client thriftClient = null; - public static final CliSessionState sessionState = new CliSessionState(); - private static CliClient cliClient; - private static final CliCompleter completer = new CliCompleter(); - private static int lineNumber = 1; - - /** - * Establish a thrift connection to cassandra instance - * - * @param server - hostname or IP of the server - * @param port - Thrift port number - */ - public static void connect(String server, int port) - { - if (transport != null) - transport.close(); - - try - { - transport = sessionState.transportFactory.openTransport(server, port); - } - catch (Exception e) - { - e.printStackTrace(sessionState.err); - - String error = (e.getCause() == null) ? e.getMessage() : e.getCause().getMessage(); - throw new RuntimeException("Exception connecting to " + server + "/" + port + ". Reason: " + error + "."); - } - - TBinaryProtocol binaryProtocol = new TBinaryProtocol(transport, true, true); - thriftClient = new Cassandra.Client(binaryProtocol); - cliClient = new CliClient(sessionState, thriftClient); - - if ((sessionState.username != null) && (sessionState.password != null)) - { - // Authenticate - Map<String, String> credentials = new HashMap<String, String>(); - credentials.put(IAuthenticator.USERNAME_KEY, sessionState.username); - credentials.put(IAuthenticator.PASSWORD_KEY, sessionState.password); - AuthenticationRequest authRequest = new AuthenticationRequest(credentials); - try - { - thriftClient.login(authRequest); - cliClient.setUsername(sessionState.username); - } - catch (AuthenticationException e) - { - thriftClient = null; - sessionState.err.println("Exception during authentication to the cassandra node, " + - "Verify the keyspace exists, and that you are using the correct credentials."); - return; - } - catch (AuthorizationException e) - { - thriftClient = null; - sessionState.err.println("You are not authorized to use keyspace: " + sessionState.keyspace); - return; - } - catch (TException e) - { - thriftClient = null; - sessionState.err.println("Login failure. Did you specify 'keyspace', 'username' and 'password'?"); - return; - } - } - - if (sessionState.keyspace != null) - { - try - { - sessionState.keyspace = CliCompiler.getKeySpace(sessionState.keyspace, thriftClient.describe_keyspaces());; - thriftClient.set_keyspace(sessionState.keyspace); - cliClient.setKeySpace(sessionState.keyspace); - updateCompletor(CliUtils.getCfNamesByKeySpace(cliClient.getKSMetaData(sessionState.keyspace))); - } - catch (InvalidRequestException | NotFoundException e) - { - sessionState.err.println("Keyspace " + sessionState.keyspace + " not found"); - return; - } - catch (TException e) - { - sessionState.err.println("Did you specify 'keyspace'?"); - return; - } - } - - // Lookup the cluster name, this is to make it clear which cluster the user is connected to - String clusterName; - - try - { - clusterName = thriftClient.describe_cluster_name(); - } - catch (Exception e) - { - sessionState.err.println("Exception retrieving information about the cassandra node, check you have connected to the thrift port."); - - e.printStackTrace(sessionState.err); - - return; - } - - sessionState.out.printf("Connected to: \"%s\" on %s/%d%n", clusterName, server, port); - } - - /** - * Disconnect thrift connection to cassandra instance - */ - public static void disconnect() - { - if (transport != null) - { - transport.close(); - transport = null; - } - } - - /** - * Checks whether the thrift client is connected. - * @return boolean - true when connected, false otherwise - */ - public static boolean isConnected() - { - if (thriftClient == null) - { - sessionState.out.println("Not connected to a cassandra instance."); - return false; - } - return true; - } - - public static void updateCompletor(Set<String> candidates) - { - Set<String> actions = new HashSet<String>(); - for (String cf : candidates) - { - for (String cmd : completer.getKeyspaceCommands()) - actions.add(String.format("%s %s", cmd, cf)); - } - - String[] strs = Arrays.copyOf(actions.toArray(), actions.toArray().length, String[].class); - - completer.setCandidateStrings(strs); - } - - public static void processStatement(String query) throws CharacterCodingException, TException, NotFoundException, InvalidRequestException, NoSuchFieldException, UnavailableException, IllegalAccessException, InstantiationException - { - cliClient.executeCLIStatement(query); - } - - public static void processStatementInteractive(String query) - { - try - { - cliClient.executeCLIStatement(query); - } - catch (Exception e) - { - String errorTemplate = sessionState.inFileMode() ? "Line " + lineNumber + " => " : ""; - - Throwable exception = (e.getCause() == null) ? e : e.getCause(); - String message = (exception instanceof InvalidRequestException) ? ((InvalidRequestException) exception).getWhy() : e.getMessage(); - - sessionState.err.println(errorTemplate + message); - - if (sessionState.debug || !(e instanceof RuntimeException)) - e.printStackTrace(sessionState.err); - - if (sessionState.batch || sessionState.inFileMode()) - { - System.exit(4); - } - } - finally - { - lineNumber++; - } - } - - public static void main(String args[]) throws IOException - { - // process command line arguments - CliOptions cliOptions = new CliOptions(); - cliOptions.processArgs(sessionState, args); - - // connect to cassandra server if host argument specified. - if (sessionState.hostName != null) - { - try - { - connect(sessionState.hostName, sessionState.thriftPort); - } - catch (RuntimeException e) - { - sessionState.err.println(e.getMessage()); - } - } - - if ( cliClient == null ) - { - // Connection parameter was either invalid or not present. - // User must connect explicitly using the "connect" CLI statement. - cliClient = new CliClient(sessionState, null); - } - - // load statements from file and process them - if (sessionState.inFileMode()) - { - BufferedReader reader = null; - - try - { - reader = new BufferedReader(new FileReader(sessionState.filename)); - evaluateFileStatements(reader); - } - catch (IOException e) - { - sessionState.err.println(e.getMessage()); - System.exit(1); - } - finally - { - FileUtils.closeQuietly(reader); - } - - return; - } - - ConsoleReader reader = new ConsoleReader(); - - if (!sessionState.batch) - { - reader.addCompletor(completer); - reader.setBellEnabled(false); - File historyFile = handleHistoryFiles(); - - try - { - History history = new History(historyFile); - reader.setHistory(history); - } - catch (IOException exp) - { - sessionState.err.printf("Unable to open %s for writing", historyFile.getAbsolutePath()); - } - } - else if (!sessionState.verbose) // if in batch mode but no verbose flag - { - sessionState.out.close(); - } - - cliClient.printBanner(); - - String prompt; - String line = ""; - String currentStatement = ""; - boolean inCompoundStatement = false; - - while (line != null) - { - prompt = (inCompoundStatement) ? "...\t" : getPrompt(cliClient); - - try - { - line = reader.readLine(prompt); - } - catch (IOException e) - { - // retry on I/O Exception - } - - if (line == null) - return; - - line = line.trim(); - - // skipping empty and comment lines - if (line.isEmpty() || line.startsWith("--")) - continue; - - currentStatement += line; - - if (line.endsWith(";") || line.equals("?")) - { - processStatementInteractive(currentStatement); - currentStatement = ""; - inCompoundStatement = false; - } - else - { - currentStatement += " "; // ready for new line - inCompoundStatement = true; - } - } - } - - private static File handleHistoryFiles() - { - File outputDir = FBUtilities.getToolsOutputDirectory(); - File historyFile = new File(outputDir, HISTORYFILE); - File oldHistoryFile = new File(System.getProperty("user.home"), OLD_HISTORYFILE); - if(oldHistoryFile.exists()) - FileUtils.renameWithConfirm(oldHistoryFile, historyFile); - - return historyFile; - } - - private static void evaluateFileStatements(BufferedReader reader) throws IOException - { - String line; - String currentStatement = ""; - - boolean commentedBlock = false; - - while ((line = reader.readLine()) != null) - { - line = line.trim(); - - // skipping empty and comment lines - if (line.isEmpty() || line.startsWith("--")) - continue; - - if (line.startsWith("/*")) - commentedBlock = true; - - if (line.startsWith("*/") || line.endsWith("*/")) - { - commentedBlock = false; - continue; - } - - if (commentedBlock) // skip commented lines - continue; - - currentStatement += line; - - if (line.endsWith(";")) - { - processStatementInteractive(currentStatement); - currentStatement = ""; - } - else - { - currentStatement += " "; // ready for new line - } - } - } - - /** - * Returns prompt for current connection - * @param client - currently connected client - * @return String - prompt with username and keyspace (if any) - */ - private static String getPrompt(CliClient client) - { - return "[" + client.getUsername() + "@" + client.getKeySpace() + "] "; - } - -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/38027382/src/java/org/apache/cassandra/cli/CliOptions.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cli/CliOptions.java b/src/java/org/apache/cassandra/cli/CliOptions.java deleted file mode 100644 index 7894bf9..0000000 --- a/src/java/org/apache/cassandra/cli/CliOptions.java +++ /dev/null @@ -1,319 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.cassandra.cli; - -import java.util.HashMap; -import java.util.Map; - -import com.google.common.base.Joiner; -import org.apache.commons.cli.*; - -import org.apache.cassandra.config.EncryptionOptions; -import org.apache.cassandra.thrift.ITransportFactory; -import org.apache.cassandra.thrift.SSLTransportFactory; - -/** - * - * Used to process, and act upon the arguments passed to the CLI. - * - */ -public class CliOptions -{ - private static final CLIOptions options; // Info about command line options - - // Name of the command line tool (used for error messages) - private static final String TOOL_NAME = "cassandra-cli"; - - // Command line options - private static final String HOST_OPTION = "host"; - private static final String PORT_OPTION = "port"; - private static final String TRANSPORT_FACTORY = "transport-factory"; - private static final String DEBUG_OPTION = "debug"; - private static final String USERNAME_OPTION = "username"; - private static final String PASSWORD_OPTION = "password"; - private static final String KEYSPACE_OPTION = "keyspace"; - private static final String BATCH_OPTION = "batch"; - private static final String HELP_OPTION = "help"; - private static final String FILE_OPTION = "file"; - private static final String JMX_PORT_OPTION = "jmxport"; - private static final String JMX_USERNAME_OPTION = "jmxusername"; - private static final String JMX_PASSWORD_OPTION = "jmxpassword"; - private static final String VERBOSE_OPTION = "verbose"; - - private static final String SSL_TRUSTSTORE = "truststore"; - private static final String SSL_TRUSTSTORE_PW = "truststore-password"; - private static final String SSL_PROTOCOL = "ssl-protocol"; - private static final String SSL_ALGORITHM = "ssl-alg"; - private static final String SSL_STORE_TYPE = "store-type"; - private static final String SSL_CIPHER_SUITES = "ssl-ciphers"; - - // Default values for optional command line arguments - private static final String DEFAULT_HOST = "127.0.0.1"; - private static final int DEFAULT_THRIFT_PORT = 9160; - - // Register the command line options and their properties (such as - // whether they take an extra argument, etc. - static - { - options = new CLIOptions(); - - options.addOption("h", HOST_OPTION, "HOSTNAME", "cassandra server's host name"); - options.addOption("p", PORT_OPTION, "PORT", "cassandra server's thrift port"); - options.addOption("u", USERNAME_OPTION, "USERNAME", "user name for cassandra authentication"); - options.addOption("pw", PASSWORD_OPTION, "PASSWORD", "password for cassandra authentication"); - options.addOption("k", KEYSPACE_OPTION, "KEYSPACE", "cassandra keyspace user is authenticated against"); - options.addOption("f", FILE_OPTION, "FILENAME", "load statements from the specific file"); - options.addOption(null, JMX_PORT_OPTION, "JMX-PORT", "JMX service port"); - options.addOption(null, JMX_USERNAME_OPTION, "JMX-USERNAME", "JMX service username"); - options.addOption(null, JMX_PASSWORD_OPTION, "JMX-PASSWORD", "JMX service password"); - options.addOption("tf", TRANSPORT_FACTORY, "TRANSPORT-FACTORY", "Fully-qualified ITransportFactory class name for creating a connection to cassandra"); - - // ssl connection-related options - options.addOption("ts", SSL_TRUSTSTORE, "TRUSTSTORE", "SSL: full path to truststore"); - options.addOption("tspw", SSL_TRUSTSTORE_PW, "TRUSTSTORE-PASSWORD", "SSL: password of the truststore"); - options.addOption("prtcl", SSL_PROTOCOL, "PROTOCOL", "SSL: connections protocol to use (default: TLS)"); - options.addOption("alg", SSL_ALGORITHM, "ALGORITHM", "SSL: algorithm (default: SunX509)"); - options.addOption("st", SSL_STORE_TYPE, "STORE-TYPE", "SSL: type of store"); - options.addOption("ciphers", SSL_CIPHER_SUITES, "CIPHER-SUITES", "SSL: comma-separated list of encryption suites to use"); - - // options without argument - options.addOption("B", BATCH_OPTION, "enabled batch mode (suppress output; errors are fatal)"); - options.addOption(null, DEBUG_OPTION, "display stack-traces (NOTE: We print strack-traces in the places where it makes sense even without --debug)"); - options.addOption("?", HELP_OPTION, "usage help"); - options.addOption("v", VERBOSE_OPTION, "verbose output when using batch mode"); - } - - private static void printUsage() - { - new HelpFormatter().printHelp(TOOL_NAME, options); - } - - public void processArgs(CliSessionState css, String[] args) - { - CommandLineParser parser = new GnuParser(); - - try - { - CommandLine cmd = parser.parse(options, args, false); - - if (cmd.hasOption(HOST_OPTION)) - { - css.hostName = cmd.getOptionValue(HOST_OPTION); - } - else - { - css.hostName = DEFAULT_HOST; - } - - if (cmd.hasOption(DEBUG_OPTION)) - { - css.debug = true; - } - - // Look for optional args. - if (cmd.hasOption(PORT_OPTION)) - { - css.thriftPort = Integer.parseInt(cmd.getOptionValue(PORT_OPTION)); - } - else - { - css.thriftPort = DEFAULT_THRIFT_PORT; - } - - // Look for authentication credentials (username and password) - if (cmd.hasOption(USERNAME_OPTION)) - { - css.username = cmd.getOptionValue(USERNAME_OPTION); - } - - if (cmd.hasOption(PASSWORD_OPTION)) - { - css.password = cmd.getOptionValue(PASSWORD_OPTION); - } - - // Look for keyspace - if (cmd.hasOption(KEYSPACE_OPTION)) - { - css.keyspace = cmd.getOptionValue(KEYSPACE_OPTION); - } - - if (cmd.hasOption(BATCH_OPTION)) - { - css.batch = true; - } - - if (cmd.hasOption(FILE_OPTION)) - { - css.filename = cmd.getOptionValue(FILE_OPTION); - } - - if (cmd.hasOption(JMX_PORT_OPTION)) - { - css.jmxPort = Integer.parseInt(cmd.getOptionValue(JMX_PORT_OPTION)); - } - - if (cmd.hasOption(JMX_USERNAME_OPTION)) - { - css.jmxUsername = cmd.getOptionValue(JMX_USERNAME_OPTION); - } - - if (cmd.hasOption(JMX_PASSWORD_OPTION)) - { - css.jmxPassword = cmd.getOptionValue(JMX_PASSWORD_OPTION); - } - - if (cmd.hasOption(HELP_OPTION)) - { - printUsage(); - System.exit(1); - } - - if (cmd.hasOption(VERBOSE_OPTION)) - { - css.verbose = true; - } - - if(cmd.hasOption(SSL_TRUSTSTORE)) - { - css.encOptions.truststore = cmd.getOptionValue(SSL_TRUSTSTORE); - } - - if(cmd.hasOption(SSL_TRUSTSTORE_PW)) - { - css.encOptions.truststore_password = cmd.getOptionValue(SSL_TRUSTSTORE_PW); - } - - if(cmd.hasOption(SSL_PROTOCOL)) - { - css.encOptions.protocol = cmd.getOptionValue(SSL_PROTOCOL); - } - - if(cmd.hasOption(SSL_ALGORITHM)) - { - css.encOptions.algorithm = cmd.getOptionValue(SSL_ALGORITHM); - } - - if(cmd.hasOption(SSL_STORE_TYPE)) - { - css.encOptions.store_type = cmd.getOptionValue(SSL_STORE_TYPE); - } - - if(cmd.hasOption(SSL_CIPHER_SUITES)) - { - css.encOptions.cipher_suites = cmd.getOptionValue(SSL_CIPHER_SUITES).split(","); - } - - if (cmd.hasOption(TRANSPORT_FACTORY)) - { - css.transportFactory = validateAndSetTransportFactory(cmd.getOptionValue(TRANSPORT_FACTORY)); - configureTransportFactory(css.transportFactory, css.encOptions); - } - - // Abort if there are any unrecognized arguments left - if (cmd.getArgs().length > 0) - { - System.err.printf("Unknown argument: %s%n", cmd.getArgs()[0]); - System.err.println(); - printUsage(); - System.exit(1); - } - } - catch (ParseException e) - { - System.err.println(e.getMessage()); - System.err.println(); - printUsage(); - System.exit(1); - } - } - - private static class CLIOptions extends Options - { - /** - * Add option with argument and argument name - * @param opt shortcut for option name - * @param longOpt complete option name - * @param argName argument name - * @param description description of the option - * @return updated Options object - */ - public Options addOption(String opt, String longOpt, String argName, String description) - { - Option option = new Option(opt, longOpt, true, description); - option.setArgName(argName); - - return addOption(option); - } - - /** - * Add option without argument - * @param opt shortcut for option name - * @param longOpt complete option name - * @param description description of the option - * @return updated Options object - */ - public Options addOption(String opt, String longOpt, String description) - { - return addOption(new Option(opt, longOpt, false, description)); - } - } - - private static ITransportFactory validateAndSetTransportFactory(String transportFactory) - { - try - { - Class<?> factory = Class.forName(transportFactory); - if (!ITransportFactory.class.isAssignableFrom(factory)) - throw new IllegalArgumentException(String.format("transport factory '%s' " + - "not derived from ITransportFactory", transportFactory)); - return (ITransportFactory) factory.newInstance(); - } - catch (Exception e) - { - throw new IllegalArgumentException(String.format("Cannot create a transport factory '%s'.", transportFactory), e); - } - } - - private static void configureTransportFactory(ITransportFactory transportFactory, EncryptionOptions encOptions) - { - Map<String, String> options = new HashMap<>(); - // If the supplied factory supports the same set of options as our SSL impl, set those - if (transportFactory.supportedOptions().contains(SSLTransportFactory.TRUSTSTORE)) - options.put(SSLTransportFactory.TRUSTSTORE, encOptions.truststore); - if (transportFactory.supportedOptions().contains(SSLTransportFactory.TRUSTSTORE_PASSWORD)) - options.put(SSLTransportFactory.TRUSTSTORE_PASSWORD, encOptions.truststore_password); - if (transportFactory.supportedOptions().contains(SSLTransportFactory.PROTOCOL)) - options.put(SSLTransportFactory.PROTOCOL, encOptions.protocol); - if (transportFactory.supportedOptions().contains(SSLTransportFactory.CIPHER_SUITES)) - options.put(SSLTransportFactory.CIPHER_SUITES, Joiner.on(',').join(encOptions.cipher_suites)); - - if (transportFactory.supportedOptions().contains(SSLTransportFactory.KEYSTORE) - && encOptions.require_client_auth) - options.put(SSLTransportFactory.KEYSTORE, encOptions.keystore); - if (transportFactory.supportedOptions().contains(SSLTransportFactory.KEYSTORE_PASSWORD) - && encOptions.require_client_auth) - options.put(SSLTransportFactory.KEYSTORE_PASSWORD, encOptions.keystore_password); - - // Now check if any of the factory's supported options are set as system properties - for (String optionKey : transportFactory.supportedOptions()) - if (System.getProperty(optionKey) != null) - options.put(optionKey, System.getProperty(optionKey)); - - transportFactory.setOptions(options); - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/38027382/src/java/org/apache/cassandra/cli/CliSessionState.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cli/CliSessionState.java b/src/java/org/apache/cassandra/cli/CliSessionState.java deleted file mode 100644 index f0de713..0000000 --- a/src/java/org/apache/cassandra/cli/CliSessionState.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.cassandra.cli; - -import java.io.InputStream; -import java.io.PrintStream; - -import org.apache.cassandra.config.EncryptionOptions; -import org.apache.cassandra.config.EncryptionOptions.ClientEncryptionOptions; -import org.apache.cassandra.thrift.ITransportFactory; -import org.apache.cassandra.thrift.TFramedTransportFactory; -import org.apache.cassandra.tools.NodeProbe; - -/** - * Used to hold the state for the CLI. - */ -public class CliSessionState -{ - - public String hostName; // cassandra server name - public int thriftPort; // cassandra server's thrift port - public boolean debug = false; // print stack traces when errors occur in the CLI - public String username; // cassandra login name (if password-based authenticator is used) - public String password; // cassandra login password (if password-based authenticator is used) - public String keyspace; // cassandra keyspace user is authenticating - public boolean batch = false; // enable/disable batch processing mode - public String filename = ""; // file to read commands from - public int jmxPort = 7199;// JMX service port - public String jmxUsername; // JMX service username - public String jmxPassword; // JMX service password - public boolean verbose = false; // verbose output - public ITransportFactory transportFactory = new TFramedTransportFactory(); - public EncryptionOptions encOptions = new ClientEncryptionOptions(); - - /* - * Streams to read/write from - */ - public InputStream in; - public PrintStream out; - public PrintStream err; - - public CliSessionState() - { - in = System.in; - out = System.out; - err = System.err; - } - - public void setOut(PrintStream newOut) - { - this.out = newOut; - } - - public void setErr(PrintStream newErr) - { - this.err = newErr; - } - - public boolean inFileMode() - { - return !this.filename.isEmpty(); - } - - public NodeProbe getNodeProbe() - { - try - { - return jmxUsername != null && jmxPassword != null - ? new NodeProbe(hostName, jmxPort, jmxUsername, jmxPassword) - : new NodeProbe(hostName, jmxPort); - } - catch (Exception e) - { - err.printf("WARNING: Could not connect to the JMX on %s:%d - some information won't be shown.%n%n", hostName, jmxPort); - } - - return null; - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/38027382/src/java/org/apache/cassandra/cli/CliUserHelp.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cli/CliUserHelp.java b/src/java/org/apache/cassandra/cli/CliUserHelp.java deleted file mode 100644 index aa4192c..0000000 --- a/src/java/org/apache/cassandra/cli/CliUserHelp.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.cassandra.cli; - -import java.util.List; - -public class CliUserHelp -{ - public String banner; - - public String help; - - public List<CliCommandHelp> commands; -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/38027382/src/java/org/apache/cassandra/cli/CliUtils.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cli/CliUtils.java b/src/java/org/apache/cassandra/cli/CliUtils.java deleted file mode 100644 index d0c8e37..0000000 --- a/src/java/org/apache/cassandra/cli/CliUtils.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.cassandra.cli; - -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Set; - -import org.apache.cassandra.thrift.CfDef; -import org.apache.cassandra.thrift.IndexOperator; -import org.apache.cassandra.thrift.KsDef; -import org.apache.commons.lang3.StringEscapeUtils; - -public class CliUtils -{ - /** - * Strips leading and trailing "'" characters, and handles - * and escaped characters such as \n, \r, etc. - * @param b - string to unescape - * @return String - unexspaced string - */ - public static String unescapeSQLString(String b) - { - if (b.charAt(0) == '\'' && b.charAt(b.length()-1) == '\'') - b = b.substring(1, b.length()-1); - return StringEscapeUtils.unescapeJava(b); - } - - public static String escapeSQLString(String b) - { - // single quotes are not escaped in java, need to be for cli - return StringEscapeUtils.escapeJava(b).replace("\'", "\\'"); - } - - public static String maybeEscapeName(String name) - { - return Character.isLetter(name.charAt(0)) ? name : "\'" + name + "\'"; - } - - /** - * Returns IndexOperator from string representation - * @param operator - string representing IndexOperator (=, >=, >, <, <=) - * @return IndexOperator - enum value of IndexOperator or null if not found - */ - public static IndexOperator getIndexOperator(String operator) - { - if (operator.equals("=")) - { - return IndexOperator.EQ; - } - else if (operator.equals(">=")) - { - return IndexOperator.GTE; - } - else if (operator.equals(">")) - { - return IndexOperator.GT; - } - else if (operator.equals("<")) - { - return IndexOperator.LT; - } - else if (operator.equals("<=")) - { - return IndexOperator.LTE; - } - - return null; - } - - /** - * Returns set of column family names in specified keySpace. - * @param keySpace - keyspace definition to get column family names from. - * @return Set - column family names - */ - public static Set<String> getCfNamesByKeySpace(KsDef keySpace) - { - Set<String> names = new LinkedHashSet<String>(); - - for (CfDef cfDef : keySpace.getCf_defs()) - { - names.add(cfDef.getName()); - } - - return names; - } - - /** - * Parse the statement from cli and return KsDef - * - * @param keyspaceName - name of the keyspace to lookup - * @param keyspaces - List of known keyspaces - * - * @return metadata about keyspace or null - */ - public static KsDef getKeySpaceDef(String keyspaceName, List<KsDef> keyspaces) - { - keyspaceName = keyspaceName.toUpperCase(); - - for (KsDef ksDef : keyspaces) - { - if (ksDef.name.toUpperCase().equals(keyspaceName)) - return ksDef; - } - - return null; - } - - public static String quote(String str) - { - return String.format("'%s'", str); - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/38027382/src/java/org/apache/cassandra/cli/transport/FramedTransportFactory.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cli/transport/FramedTransportFactory.java b/src/java/org/apache/cassandra/cli/transport/FramedTransportFactory.java deleted file mode 100644 index dc7ef81..0000000 --- a/src/java/org/apache/cassandra/cli/transport/FramedTransportFactory.java +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.cassandra.cli.transport; - -import org.apache.thrift.transport.TFramedTransport; -import org.apache.thrift.transport.TTransport; -import org.apache.thrift.transport.TTransportFactory; - -public class FramedTransportFactory extends TTransportFactory -{ - public static final int DEFAULT_MAX_FRAME_SIZE = 15 * 1024 * 1024; // 15 MiB - - public TTransport getTransport(TTransport base) - { - return new TFramedTransport(base, DEFAULT_MAX_FRAME_SIZE); - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/38027382/src/java/org/apache/cassandra/cli/transport/SSLTransportFactory.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cli/transport/SSLTransportFactory.java b/src/java/org/apache/cassandra/cli/transport/SSLTransportFactory.java deleted file mode 100644 index 4aa9fc1..0000000 --- a/src/java/org/apache/cassandra/cli/transport/SSLTransportFactory.java +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.cassandra.cli.transport; - -import org.apache.cassandra.cli.CliMain; -import org.apache.cassandra.cli.CliSessionState; -import org.apache.thrift.transport.TSSLTransportFactory; -import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters; -import org.apache.thrift.transport.TTransport; -import org.apache.thrift.transport.TTransportException; -import org.apache.thrift.transport.TTransportFactory; - -public class SSLTransportFactory extends TTransportFactory -{ - private static final int SOCKET_TIMEOUT = 0; - - public TTransport getTransport(TTransport trans) - { - final CliSessionState sessionState = CliMain.sessionState; - try - { - TSSLTransportParameters params = new TSSLTransportParameters(sessionState.encOptions.protocol, sessionState.encOptions.cipher_suites); - params.setTrustStore(sessionState.encOptions.truststore, sessionState.encOptions.truststore_password); - trans = TSSLTransportFactory.getClientSocket(sessionState.hostName, sessionState.thriftPort, SOCKET_TIMEOUT, params); - return new FramedTransportFactory().getTransport(trans); - } - catch (TTransportException e) - { - throw new RuntimeException("Failed to create a client SSL connection.", e); - } - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/38027382/src/java/org/apache/cassandra/db/DefsTables.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/db/DefsTables.java b/src/java/org/apache/cassandra/db/DefsTables.java index f1fe0bf..14c5241 100644 --- a/src/java/org/apache/cassandra/db/DefsTables.java +++ b/src/java/org/apache/cassandra/db/DefsTables.java @@ -48,68 +48,6 @@ import org.apache.cassandra.utils.ByteBufferUtil; * SCHEMA_{KEYSPACES, COLUMNFAMILIES, COLUMNS}_CF are used to store Keyspace/ColumnFamily attributes to make schema * load/distribution easy, it replaces old mechanism when local migrations where serialized, stored in system.Migrations * and used for schema distribution. - * - * SCHEMA_KEYSPACES_CF layout: - * - * <key (AsciiType)> - * ascii => json_serialized_value - * ... - * </key> - * - * Where <key> is a name of keyspace e.g. "ks". - * - * SCHEMA_COLUMNFAMILIES_CF layout: - * - * <key (AsciiType)> - * composite(ascii, ascii) => json_serialized_value - * </key> - * - * Where <key> is a name of keyspace e.g. "ks"., first component of the column name is name of the ColumnFamily, last - * component is the name of the ColumnFamily attribute. - * - * SCHEMA_COLUMNS_CF layout: - * - * <key (AsciiType)> - * composite(ascii, ascii, ascii) => json_serialized value - * </key> - * - * Where <key> is a name of keyspace e.g. "ks". - * - * Cell names where made composite to support 3-level nesting which represents following structure: - * "ColumnFamily name":"column name":"column attribute" => "value" - * - * Example of schema (using CLI): - * - * schema_keyspaces - * ---------------- - * RowKey: ks - * => (column=durable_writes, value=true, timestamp=1327061028312185000) - * => (column=name, value="ks", timestamp=1327061028312185000) - * => (column=replication_factor, value=0, timestamp=1327061028312185000) - * => (column=strategy_class, value="org.apache.cassandra.locator.NetworkTopologyStrategy", timestamp=1327061028312185000) - * => (column=strategy_options, value={"datacenter1":"1"}, timestamp=1327061028312185000) - * - * schema_columnfamilies - * --------------------- - * RowKey: ks - * => (column=cf:bloom_filter_fp_chance, value=0.0, timestamp=1327061105833119000) - * => (column=cf:caching, value="NONE", timestamp=1327061105833119000) - * => (column=cf:column_type, value="Standard", timestamp=1327061105833119000) - * => (column=cf:comment, value="ColumnFamily", timestamp=1327061105833119000) - * => (column=cf:default_validation_class, value="org.apache.cassandra.db.marshal.BytesType", timestamp=1327061105833119000) - * => (column=cf:gc_grace_seconds, value=864000, timestamp=1327061105833119000) - * => (column=cf:id, value=1000, timestamp=1327061105833119000) - * => (column=cf:key_alias, value="S0VZ", timestamp=1327061105833119000) - * ... part of the output omitted. - * - * schema_columns - * -------------- - * RowKey: ks - * => (column=cf:c:index_name, value=null, timestamp=1327061105833119000) - * => (column=cf:c:index_options, value=null, timestamp=1327061105833119000) - * => (column=cf:c:index_type, value=null, timestamp=1327061105833119000) - * => (column=cf:c:name, value="aGVsbG8=", timestamp=1327061105833119000) - * => (column=cf:c:validation_class, value="org.apache.cassandra.db.marshal.AsciiType", timestamp=1327061105833119000) */ public class DefsTables { http://git-wip-us.apache.org/repos/asf/cassandra/blob/38027382/src/java/org/apache/cassandra/thrift/SSLTransportFactory.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/thrift/SSLTransportFactory.java b/src/java/org/apache/cassandra/thrift/SSLTransportFactory.java index b23d28c..497cd5c 100644 --- a/src/java/org/apache/cassandra/thrift/SSLTransportFactory.java +++ b/src/java/org/apache/cassandra/thrift/SSLTransportFactory.java @@ -18,7 +18,8 @@ package org.apache.cassandra.thrift; import com.google.common.collect.Sets; -import org.apache.cassandra.cli.transport.FramedTransportFactory; + +import org.apache.thrift.transport.TFramedTransport; import org.apache.thrift.transport.TSSLTransportFactory; import org.apache.thrift.transport.TTransport; @@ -27,6 +28,8 @@ import java.util.Set; public class SSLTransportFactory implements ITransportFactory { + public static final int DEFAULT_MAX_FRAME_SIZE = 15 * 1024 * 1024; // 15 MiB + public static final String TRUSTSTORE = "enc.truststore"; public static final String TRUSTSTORE_PASSWORD = "enc.truststore.password"; public static final String KEYSTORE = "enc.keystore"; @@ -57,7 +60,7 @@ public class SSLTransportFactory implements ITransportFactory if (null != keystore) params.setKeyStore(keystore, keystorePassword); TTransport trans = TSSLTransportFactory.getClientSocket(host, port, SOCKET_TIMEOUT, params); - return new FramedTransportFactory().getTransport(trans); + return new TFramedTransport(trans, DEFAULT_MAX_FRAME_SIZE); } @Override
