Search in sources :

Example 1 with CommandHandlerException

use of org.apache.samza.sql.client.exceptions.CommandHandlerException in project samza by apache.

the class CliCommandHandler method handleCommand.

/**
 * Handles the given command
 * @param command input {@link CliCommand} to handle
 * @return false if command is to quit, or fatal error happened that Shell should not continue running. True o.w.
 * @throws CommandHandlerException if unrecoverable error happened while handling the input {@link CliCommand}
 */
public boolean handleCommand(CliCommand command) throws CommandHandlerException {
    boolean keepRunning = true;
    if (!command.getCommandType().argsAreOptional() && CliUtil.isNullOrEmpty(command.getParameters())) {
        CliUtil.printCommandUsage(command, writer);
        return true;
    }
    try {
        switch((CliCommandType) command.getCommandType()) {
            case CLEAR:
                commandClear();
                break;
            case DESCRIBE:
                commandDescribe(command);
                break;
            case EXECUTE:
                commandExecuteFile(command);
                break;
            case EXIT:
            case QUIT:
                keepRunning = false;
                break;
            case HELP:
                commandHelp(command);
                break;
            case INSERT_INTO:
                commandInsertInto(command);
                break;
            case LS:
                commandLs(command);
                break;
            case RM:
                commandRm(command);
                break;
            case SELECT:
                commandSelect(command);
                break;
            case SET:
                commandSet(command);
                break;
            case SHOW_FUNCTIONS:
                commandShowFunctions(command);
                break;
            case SHOW_TABLES:
                commandShowTables(command);
                break;
            case STOP:
                commandStop(command);
                break;
            case VERSION:
                commandVersion();
                break;
            case INVALID_COMMAND:
                printHelpMessage();
                break;
            default:
                writer.println("UNDER DEVELOPEMENT. Command:" + command.getCommandType());
                writer.println("Parameters:" + (CliUtil.isNullOrEmpty(command.getParameters()) ? "NULL" : command.getParameters()));
                writer.flush();
        }
    } catch (Exception e) {
        throw new CommandHandlerException(e);
    }
    return keepRunning;
}
Also used : CommandHandlerException(org.apache.samza.sql.client.exceptions.CommandHandlerException) URISyntaxException(java.net.URISyntaxException) CommandHandlerException(org.apache.samza.sql.client.exceptions.CommandHandlerException) ExecutorException(org.apache.samza.sql.client.exceptions.ExecutorException)

Example 2 with CommandHandlerException

use of org.apache.samza.sql.client.exceptions.CommandHandlerException in project samza by apache.

the class CliEnvironment method finishInitialization.

/**
 * Gives CliEnvironment a chance to apply settings, especially during initialization, things like
 * making default values take effect
 */
public void finishInitialization() throws CliException {
    if (executor == null) {
        try {
            createShellExecutor(CliConstants.DEFAULT_EXECUTOR_CLASS);
            activeExecutorClassName = CliConstants.DEFAULT_EXECUTOR_CLASS;
            executorEnvHandler = executor.getEnvironmentVariableHandler();
            if (delayedExecutorVars != null) {
                for (Map.Entry<String, String> entry : delayedExecutorVars.entrySet()) {
                    setEnvironmentVariable(entry.getKey(), entry.getValue());
                }
                delayedExecutorVars = null;
            }
        } catch (ExecutorException | CommandHandlerException e) {
            // we have failed to create even the default executor thus not recoverable
            throw new CliException(e);
        }
    }
    finishInitialization(shellEnvHandler);
    finishInitialization(executorEnvHandler);
}
Also used : CommandHandlerException(org.apache.samza.sql.client.exceptions.CommandHandlerException) ExecutorException(org.apache.samza.sql.client.exceptions.ExecutorException) CliException(org.apache.samza.sql.client.exceptions.CliException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with CommandHandlerException

use of org.apache.samza.sql.client.exceptions.CommandHandlerException in project samza by apache.

the class Main method main.

public static void main(String[] args) {
    // Get configuration file path
    String configFilePath = null;
    for (int i = 0; i < args.length; ++i) {
        switch(args[i]) {
            case "-conf":
                if (i + 1 < args.length) {
                    configFilePath = args[i + 1];
                    i++;
                }
                break;
            default:
                LOG.warn("Unknown parameter {}", args[i]);
                break;
        }
    }
    CliEnvironment environment = new CliEnvironment();
    StringBuilder messageBuilder = new StringBuilder();
    if (!CliUtil.isNullOrEmpty(configFilePath)) {
        LOG.info("Configuration file path is: {}", configFilePath);
        try {
            FileReader fileReader = new FileReader(configFilePath);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.startsWith("#") || line.startsWith("[")) {
                    continue;
                }
                String[] strs = line.split("=");
                if (strs.length != 2) {
                    continue;
                }
                String key = strs[0].trim().toLowerCase();
                String value = strs[1].trim();
                try {
                    LOG.info("Configuration: setting {} = {}", key, value);
                    int result = environment.setEnvironmentVariable(key, value);
                    if (result == -1) {
                        // CliEnvironment doesn't recognize the key.
                        LOG.warn("Unknowing shell environment variable: {}", key);
                    } else if (result == -2) {
                        // Invalid value
                        LOG.warn("Unknowing shell environment value: {}", value);
                    }
                } catch (ExecutorException e) {
                    messageBuilder.append("Warning: Failed to create executor: ").append(value).append('\n');
                    messageBuilder.append("Warning: Using default executor " + CliConstants.DEFAULT_EXECUTOR_CLASS);
                    LOG.error("Failed to create user specified executor {}", value, e);
                } catch (CommandHandlerException e) {
                    messageBuilder.append("Warning: Failed to create CommandHandler: ").append(value).append('\n');
                    LOG.error("Failed to create user specified CommandHandler {}", value, e);
                }
            }
        } catch (IOException e) {
            LOG.error("Error in opening and reading the configuration file {}", e.toString());
        }
    }
    environment.finishInitialization();
    CliShell shell;
    try {
        shell = new CliShell(environment);
    } catch (ExecutorException e) {
        System.out.println("Unable to initialize executor. Shell must exit. ");
        LOG.error("Unable to initialize executor.", e);
        return;
    }
    shell.open(messageBuilder.toString());
}
Also used : CommandHandlerException(org.apache.samza.sql.client.exceptions.CommandHandlerException) ExecutorException(org.apache.samza.sql.client.exceptions.ExecutorException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException)

Aggregations

CommandHandlerException (org.apache.samza.sql.client.exceptions.CommandHandlerException)3 ExecutorException (org.apache.samza.sql.client.exceptions.ExecutorException)3 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CliException (org.apache.samza.sql.client.exceptions.CliException)1