<commit_before>/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2011, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.as.cli;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jboss.as.cli.handlers.ConnectHandler;
import org.jboss.as.cli.handlers.CreateJmsCFHandler;
import org.jboss.as.cli.handlers.CreateJmsQueueHandler;
import org.jboss.as.cli.handlers.CreateJmsResourceHandler;
import org.jboss.as.cli.handlers.CreateJmsTopicHandler;
import org.jboss.as.cli.handlers.DeleteJmsCFHandler;
import org.jboss.as.cli.handlers.DeleteJmsQueueHandler;
import org.jboss.as.cli.handlers.DeleteJmsResourceHandler;
import org.jboss.as.cli.handlers.DeleteJmsTopicHandler;
import org.jboss.as.cli.handlers.PrintWorkingNodeHandler;
import org.jboss.as.cli.handlers.DeployHandler;
import org.jboss.as.cli.handlers.HelpHandler;
import org.jboss.as.cli.handlers.HistoryHandler;
import org.jboss.as.cli.handlers.LsHandler;
import org.jboss.as.cli.handlers.OperationRequestHandler;
import org.jboss.as.cli.handlers.PrefixHandler;
import org.jboss.as.cli.handlers.QuitHandler;
import org.jboss.as.cli.handlers.UndeployHandler;
import org.jboss.as.cli.operation.OperationCandidatesProvider;
import org.jboss.as.cli.operation.OperationRequestAddress;
import org.jboss.as.cli.operation.OperationRequestParser;
import org.jboss.as.cli.operation.PrefixFormatter;
import org.jboss.as.cli.operation.impl.DefaultOperationCandidatesProvider;
import org.jboss.as.cli.operation.impl.DefaultOperationRequestAddress;
import org.jboss.as.cli.operation.impl.DefaultOperationRequestParser;
import org.jboss.as.cli.operation.impl.DefaultPrefixFormatter;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.protocol.StreamUtils;

/**
 *
 * @author <NAME>
 */
public class CommandLineMain {

    private static final CommandRegistry cmdRegistry = new CommandRegistry();
    static {
        cmdRegistry.registerHandler(new HelpHandler(), "help", "h");
        cmdRegistry.registerHandler(new QuitHandler(), "quit", "q");
        cmdRegistry.registerHandler(new ConnectHandler(), "connect");
        cmdRegistry.registerHandler(new PrefixHandler(), "cd", "cn");
        cmdRegistry.registerHandler(new LsHandler(), "ls");
        cmdRegistry.registerHandler(new HistoryHandler(), "history");
        cmdRegistry.registerHandler(new DeployHandler(), "deploy");
        cmdRegistry.registerHandler(new UndeployHandler(), "undeploy");
        cmdRegistry.registerHandler(new PrintWorkingNodeHandler(), "pwd", "pwn");

        cmdRegistry.registerHandler(new CreateJmsQueueHandler(), "create-jms-queue");
        cmdRegistry.registerHandler(new DeleteJmsQueueHandler(), "delete-jms-queue");
        cmdRegistry.registerHandler(new CreateJmsTopicHandler(), "create-jms-topic");
        cmdRegistry.registerHandler(new DeleteJmsTopicHandler(), "delete-jms-topic");
        cmdRegistry.registerHandler(new CreateJmsCFHandler(), "create-jms-cf");
        cmdRegistry.registerHandler(new DeleteJmsCFHandler(), "delete-jms-cf");
        cmdRegistry.registerHandler(new CreateJmsResourceHandler(), false, "create-jms-resource");
        cmdRegistry.registerHandler(new DeleteJmsResourceHandler(), false, "delete-jms-resource");
    }

    public static void main(String[] args) throws Exception {

        final jline.ConsoleReader console = initConsoleReader();

        final CommandContextImpl cmdCtx = new CommandContextImpl(console);
        SecurityActions.addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                cmdCtx.disconnectController();
            }
        }));
        console.addCompletor(new CommandCompleter(cmdRegistry, cmdCtx));

        String[] commands = null;
        String fileName = null;
        boolean connect = false;
        for(String arg : args) {
            if(arg.startsWith("controller=")) {
                String value = arg.substring(11);
                String portStr = null;
                int colonIndex = value.indexOf(':');
                if(colonIndex < 0) {
                    // default port
                    cmdCtx.defaultControllerHost = value;
                } else if(colonIndex == 0) {
                    // default host
                    portStr = value.substring(1);
                } else {
                    cmdCtx.defaultControllerHost = value.substring(0, colonIndex);
                    portStr = value.substring(colonIndex + 1);
                }

                if(portStr != null) {
                    int port = -1;
                    try {
                        port = Integer.parseInt(portStr);
                        if(port < 0) {
                            cmdCtx.printLine("The port must be a valid non-negative integer: '" + args + "'");
                        } else {
                            cmdCtx.defaultControllerPort = port;
                        }
                    } catch(NumberFormatException e) {
                        cmdCtx.printLine("The port must be a valid non-negative integer: '" + arg + "'");
                    }
                }
            } else if("--connect".equals(arg)) {
                connect = true;
            } else if(arg.startsWith("file=")) {
                fileName = arg.substring(5);
            } else if(arg.startsWith("commands=")) {
                commands = arg.substring(9).split(";");
            }
        }

        if(connect) {
            cmdCtx.connectController(null, -1);
        } else {
            cmdCtx.printLine("You are disconnected at the moment." +
                " Type 'connect' to connect to the server or" +
                " 'help' for the list of supported commands.");
        }

        if(fileName != null && !fileName.isEmpty()) {
            File f = new File(fileName);
            if(!f.exists()) {
                cmdCtx.printLine("File " + f.getAbsolutePath() + " doesn't exist.");
            } else {
                BufferedReader reader = new BufferedReader(new FileReader(f));
                try {
                    String line = reader.readLine();
                    while(!cmdCtx.terminate && line != null) {
                        processLine(cmdCtx, line.trim());
                        line = reader.readLine();
                    }
                } finally {
                    StreamUtils.safeClose(reader);
                    cmdCtx.terminateSession();
                }
                return;
            }
        }

        if(commands != null) {
            for(int i = 0; i < commands.length && !cmdCtx.terminate; ++i) {
                processLine(cmdCtx, commands[i]);
            }
            cmdCtx.terminateSession();
            return;
        }

        while (!cmdCtx.terminate) {
            String line = console.readLine(cmdCtx.getPrompt()).trim();
            processLine(cmdCtx, line);
        }
        StreamUtils.safeClose(cmdCtx.client);
    }

    protected static void processLine(final CommandContextImpl cmdCtx, String line) {
        if (line.isEmpty()) {
            return;
        }

        if(isOperation(line)) {
            cmdCtx.setArgs(line);<commit_msg>JBAS-9270<commit_after>/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2011, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.as.cli;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jboss.as.cli.handlers.ConnectHandler;
import org.jboss.as.cli.handlers.CreateJmsCFHandler;
import org.jboss.as.cli.handlers.CreateJmsQueueHandler;
import org.jboss.as.cli.handlers.CreateJmsResourceHandler;
import org.jboss.as.cli.handlers.CreateJmsTopicHandler;
import org.jboss.as.cli.handlers.DeleteJmsCFHandler;
import org.jboss.as.cli.handlers.DeleteJmsQueueHandler;
import org.jboss.as.cli.handlers.DeleteJmsResourceHandler;
import org.jboss.as.cli.handlers.DeleteJmsTopicHandler;
import org.jboss.as.cli.handlers.PrintWorkingNodeHandler;
import org.jboss.as.cli.handlers.DeployHandler;
import org.jboss.as.cli.handlers.HelpHandler;
import org.jboss.as.cli.handlers.HistoryHandler;
import org.jboss.as.cli.handlers.LsHandler;
import org.jboss.as.cli.handlers.OperationRequestHandler;
import org.jboss.as.cli.handlers.PrefixHandler;
import org.jboss.as.cli.handlers.QuitHandler;
import org.jboss.as.cli.handlers.UndeployHandler;
import org.jboss.as.cli.operation.OperationCandidatesProvider;
import org.jboss.as.cli.operation.OperationRequestAddress;
import org.jboss.as.cli.operation.OperationRequestParser;
import org.jboss.as.cli.operation.PrefixFormatter;
import org.jboss.as.cli.operation.impl.DefaultOperationCandidatesProvider;
import org.jboss.as.cli.operation.impl.DefaultOperationRequestAddress;
import org.jboss.as.cli.operation.impl.DefaultOperationRequestParser;
import org.jboss.as.cli.operation.impl.DefaultPrefixFormatter;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.protocol.StreamUtils;

/**
 *
 * @author <NAME>
 */
public class CommandLineMain {

    private static final CommandRegistry cmdRegistry = new CommandRegistry();
    static {
        cmdRegistry.registerHandler(new HelpHandler(), "help", "h");
        cmdRegistry.registerHandler(new QuitHandler(), "quit", "q");
        cmdRegistry.registerHandler(new ConnectHandler(), "connect");
        cmdRegistry.registerHandler(new PrefixHandler(), "cd", "cn");
        cmdRegistry.registerHandler(new LsHandler(), "ls");
        cmdRegistry.registerHandler(new HistoryHandler(), "history");
        cmdRegistry.registerHandler(new DeployHandler(), "deploy");
        cmdRegistry.registerHandler(new UndeployHandler(), "undeploy");
        cmdRegistry.registerHandler(new PrintWorkingNodeHandler(), "pwd", "pwn");

        cmdRegistry.registerHandler(new CreateJmsQueueHandler(), "create-jms-queue");
        cmdRegistry.registerHandler(new DeleteJmsQueueHandler(), "delete-jms-queue");
        cmdRegistry.registerHandler(new CreateJmsTopicHandler(), "create-jms-topic");
        cmdRegistry.registerHandler(new DeleteJmsTopicHandler(), "delete-jms-topic");
        cmdRegistry.registerHandler(new CreateJmsCFHandler(), "create-jms-cf");
        cmdRegistry.registerHandler(new DeleteJmsCFHandler(), "delete-jms-cf");
        cmdRegistry.registerHandler(new CreateJmsResourceHandler(), false, "create-jms-resource");
        cmdRegistry.registerHandler(new DeleteJmsResourceHandler(), false, "delete-jms-resource");
    }

    public static void main(String[] args) throws Exception {

        final jline.ConsoleReader console = initConsoleReader();

        final CommandContextImpl cmdCtx = new CommandContextImpl(console);
        SecurityActions.addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                cmdCtx.disconnectController();
            }
        }));
        console.addCompletor(new CommandCompleter(cmdRegistry, cmdCtx));

        String[] commands = null;
        String fileName = null;
        boolean connect = false;
        for(String arg : args) {
            if(arg.startsWith("controller=")) {
                String value = arg.substring(11);
                String portStr = null;
                int colonIndex = value.indexOf(':');
                if(colonIndex < 0) {
                    // default port
                    cmdCtx.defaultControllerHost = value;
                } else if(colonIndex == 0) {
                    // default host
                    portStr = value.substring(1);
                } else {
                    cmdCtx.defaultControllerHost = value.substring(0, colonIndex);
                    portStr = value.substring(colonIndex + 1);
                }

                if(portStr != null) {
                    int port = -1;
                    try {
                        port = Integer.parseInt(portStr);
                        if(port < 0) {
                            cmdCtx.printLine("The port must be a valid non-negative integer: '" + args + "'");
                        } else {
                            cmdCtx.defaultControllerPort = port;
                        }
                    } catch(NumberFormatException e) {
                        cmdCtx.printLine("The port must be a valid non-negative integer: '" + arg + "'");
                    }
                }
            } else if("--connect".equals(arg)) {
                connect = true;
            } else if(arg.startsWith("file=")) {
                fileName = arg.substring(5);
            } else if(arg.startsWith("commands=")) {
                commands = arg.substring(9).split(",+");
            }
        }

        if(connect) {
            cmdCtx.connectController(null, -1);
        } else {
            cmdCtx.printLine("You are disconnected at the moment." +
                " Type 'connect' to connect to the server or" +
                " 'help' for the list of supported commands.");
        }

        if(fileName != null && !fileName.isEmpty()) {
            File f = new File(fileName);
            if(!f.exists()) {
                cmdCtx.printLine("File " + f.getAbsolutePath() + " doesn't exist.");
            } else {
                BufferedReader reader = new BufferedReader(new FileReader(f));
                try {
                    String line = reader.readLine();
                    while(!cmdCtx.terminate && line != null) {
                        processLine(cmdCtx, line.trim());
                        line = reader.readLine();
                    }
                } finally {
                    StreamUtils.safeClose(reader);
                    if(!cmdCtx.terminate) {
                        cmdCtx.terminateSession();
                    }
                    cmdCtx.disconnectController();
                }
                return;
            }
        }

        if(commands != null) {
            for(int i = 0; i < commands.length && !cmdCtx.terminate; ++i) {
                processLine(cmdCtx, commands[i]);
            }
            if(!cmdCtx.terminate) {
                cmdCtx.terminateSession();
            }
            cmdCtx.disconnectController();
            return;
        }

        try {
            while (!cmdCtx.terminate) {
                String line = console.readLine(cmdCtx.getPrompt()).trim();
                processLine(cmdCtx, line);
            }
        } finally {
            cmdCtx.disconnectController();
        }
    }

    protected static void processLine(final CommandContextImpl cmdCtx, String line) {
        if (line.isEmpty()) {
            return;
        }

        if(isOperation(line)) {
            cmdCtx.setArgs(line);
{'fraction_non_alphanumeric': 0.10674769248409104, 'fraction_numerical': 0.003669093619216878, 'mean_word_length': 2.3904761904761904, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3830405', 'n_tokens_mistral': 4788, 'n_tokens_neox': 4618, 'n_words': 1063}