<commit_before>/*
 * #%L
 * Bitrepository Integrity Service
 * %%
 * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark
 * %%
 * This program 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 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 Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */

package org.bitrepository.commandline;

import org.apache.commons.cli.Option;
import org.apache.commons.cli.ParseException;
import org.bitrepository.bitrepositoryelements.ChecksumDataForFileTYPE;
import org.bitrepository.bitrepositoryelements.ChecksumSpecTYPE;
import org.bitrepository.client.exceptions.InvalidChecksumException;
import org.bitrepository.commandline.output.DefaultOutputHandler;
import org.bitrepository.commandline.output.OutputHandler;
import org.bitrepository.commandline.utils.ChecksumExtractionUtils;
import org.bitrepository.commandline.utils.CommandLineArgumentsHandler;
import org.bitrepository.common.settings.Settings;
import org.bitrepository.common.utils.Base16Utils;
import org.bitrepository.common.utils.CalendarUtils;
import org.bitrepository.common.utils.ChecksumUtils;
import org.bitrepository.common.utils.FileIDValidator;
import org.bitrepository.common.utils.SettingsUtils;
import org.bitrepository.protocol.FileExchange;
import org.bitrepository.protocol.ProtocolComponentFactory;
import org.bitrepository.protocol.messagebus.MessageBus;
import org.bitrepository.protocol.messagebus.MessageBusManager;
import org.bitrepository.protocol.security.SecurityManager;

import javax.jms.JMSException;
import java.io.File;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * Defines the common functionality for command-line-clients.
 */
public abstract class CommandLineClient {
    private final String componentID;
    protected final OutputHandler output = new DefaultOutputHandler(getClass());
    protected final Settings settings;
    protected final SecurityManager securityManager;
    protected final CommandLineArgumentsHandler cmdHandler;
    private final FileIDValidator fileIDValidator;

    /**
     * Runs a specific command-line-client operation.
     * Handles also the closing of connections and deals with exceptions.
     *
     * @throws Exception if an error occurs
     */
    public void runCommand() throws Exception {
        try {
            try {
                performOperation();
            } catch (InvalidChecksumException | IllegalArgumentException ie) {
                System.out.println(ie.getMessage());
                System.exit(Constants.EXIT_OPERATION_FAILURE);
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(Constants.EXIT_OPERATION_FAILURE);
            }
        } finally {
            shutdown();
        }
    }

    /**
     * @param args The generic command line arguments for defining the operation.
     */
    protected CommandLineClient(String... args) {
        cmdHandler = new CommandLineArgumentsHandler();
        createOptionsForCmdArgumentHandler();
        try {
            cmdHandler.parseArguments(args);
        } catch (ParseException pe) {
            output.error(cmdHandler.listArguments() + "Missing argument: " + pe.getMessage());
            throw new IllegalArgumentException("Missing arguments", pe);
        }
        if (cmdHandler.hasOption(Constants.VERBOSITY_ARG)) {
            output.setVerbosity(true);
        }
        settings = cmdHandler.loadSettings();
        componentID = settings.getComponentID();
        securityManager = cmdHandler.loadSecurityManager(settings);
        fileIDValidator = new FileIDValidator(settings);

        try {
            validateArguments();<commit_msg>Added a shutdown to the message bus when hitting an exception. Also removed a redundant try block.<commit_after>/*
 * #%L
 * Bitrepository Integrity Service
 * %%
 * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark
 * %%
 * This program 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 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 Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */

package org.bitrepository.commandline;

import org.apache.commons.cli.Option;
import org.apache.commons.cli.ParseException;
import org.bitrepository.bitrepositoryelements.ChecksumDataForFileTYPE;
import org.bitrepository.bitrepositoryelements.ChecksumSpecTYPE;
import org.bitrepository.client.exceptions.InvalidChecksumException;
import org.bitrepository.commandline.output.DefaultOutputHandler;
import org.bitrepository.commandline.output.OutputHandler;
import org.bitrepository.commandline.utils.ChecksumExtractionUtils;
import org.bitrepository.commandline.utils.CommandLineArgumentsHandler;
import org.bitrepository.common.settings.Settings;
import org.bitrepository.common.utils.Base16Utils;
import org.bitrepository.common.utils.CalendarUtils;
import org.bitrepository.common.utils.ChecksumUtils;
import org.bitrepository.common.utils.FileIDValidator;
import org.bitrepository.common.utils.SettingsUtils;
import org.bitrepository.protocol.FileExchange;
import org.bitrepository.protocol.ProtocolComponentFactory;
import org.bitrepository.protocol.messagebus.MessageBus;
import org.bitrepository.protocol.messagebus.MessageBusManager;
import org.bitrepository.protocol.security.SecurityManager;

import javax.jms.JMSException;
import java.io.File;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * Defines the common functionality for command-line-clients.
 */
public abstract class CommandLineClient {
    private final String componentID;
    protected final OutputHandler output = new DefaultOutputHandler(getClass());
    protected final Settings settings;
    protected final SecurityManager securityManager;
    protected final CommandLineArgumentsHandler cmdHandler;
    private final FileIDValidator fileIDValidator;

    /**
     * Runs a specific command-line-client operation.
     * Handles also the closing of connections and deals with exceptions.
     *
     * @throws Exception if an error occurs
     */
    public void runCommand() throws Exception {
        try {
            performOperation();
        } catch (InvalidChecksumException | IllegalArgumentException ie) {
            output.warn(ie.getMessage());
            shutdown();
            System.exit(Constants.EXIT_OPERATION_FAILURE);
        } catch (Exception e) {
            output.error("Unexpected Exception.", e);
            shutdown();
            System.exit(Constants.EXIT_OPERATION_FAILURE);
        } finally {
            shutdown();
        }
    }

    /**
     * @param args The generic command line arguments for defining the operation.
     */
    protected CommandLineClient(String... args) {
        cmdHandler = new CommandLineArgumentsHandler();
        createOptionsForCmdArgumentHandler();
        try {
            cmdHandler.parseArguments(args);
        } catch (ParseException pe) {
            output.error(cmdHandler.listArguments() + "Missing argument: " + pe.getMessage());
            throw new IllegalArgumentException("Missing arguments", pe);
        }
        if (cmdHandler.hasOption(Constants.VERBOSITY_ARG)) {
            output.setVerbosity(true);
        }
        settings = cmdHandler.loadSettings();
        componentID = settings.getComponentID();
        securityManager = cmdHandler.loadSecurityManager(settings);
        fileIDValidator = new FileIDValidator(settings);

        try {
            validateArguments();
{'fraction_non_alphanumeric': 0.08155095504975408, 'fraction_numerical': 0.0032025620496397116, 'mean_word_length': 3.6609808102345416, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3114202', 'n_tokens_mistral': 2162, 'n_tokens_neox': 2143, 'n_words': 624}