Example usage for org.apache.commons.cli Options hasOption

List of usage examples for org.apache.commons.cli Options hasOption

Introduction

In this page you can find the example usage for org.apache.commons.cli Options hasOption.

Prototype

public boolean hasOption(String opt) 

Source Link

Document

Returns whether the named Option is a member of this Options .

Usage

From source file:org.apache.stratos.cli.commands.AddNetworkPartitionCommand.java

/**
 * Executing the commands. Returns a code
 *
 * @param context The context assoicated with the Command Line Application
 * @param args    The arguments for the command
 * @return The status code/*from   w w  w. j a va2s . co  m*/
 * @throws org.apache.stratos.cli.exception.CommandException if any errors occur when executing the command
 */
@Override
public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts)
        throws CommandException {
    if (log.isDebugEnabled()) {
        log.debug("Executing {} command...", getName());
        log.debug("Get name" + getName());
    }

    if (args != null && args.length > 0) {
        String resourcePath = null;
        String resourceFileContent = null;

        final CommandLineParser parser = new GnuParser();
        CommandLine commandLine;

        try {
            commandLine = parser.parse(options, args);
            //merge newly discovered options with previously discovered ones.
            Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions());

            if (log.isDebugEnabled()) {
                log.debug("Network Partition deployment");
            }

            if (opts.hasOption(CliConstants.RESOURCE_PATH)) {
                if (log.isTraceEnabled()) {
                    log.trace("Resource path option is passed");
                }

                resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue();
                resourceFileContent = CliUtils.readResource(resourcePath);
            }

            if (resourcePath == null) {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }

            RestCommandLineService.getInstance().addNetworkPartition(resourceFileContent);
            return CliConstants.COMMAND_SUCCESSFULL;

        } catch (ParseException e) {
            log.error("Error parsing arguments", e);
            System.out.println(e.getMessage());
            return CliConstants.COMMAND_FAILED;
        } catch (IOException e) {
            System.out.println("Invalid resource path");
            return CliConstants.COMMAND_FAILED;
        }

    } else {
        context.getStratosApplication().printUsage(getName());
        return CliConstants.COMMAND_FAILED;
    }
}

From source file:org.apache.stratos.cli.commands.AddUserCommand.java

public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts)
        throws CommandException {
    if (log.isDebugEnabled()) {
        log.debug("Executing {} command...", getName());
    }/*from w ww  .  j a v a2  s  .co  m*/

    if (args != null && args.length > 0) {
        String userName = null;
        String credential = null;
        String role = null;
        String firstName = null;
        String lastName = null;
        String email = null;
        String profileName = null;

        final CommandLineParser parser = new GnuParser();
        CommandLine commandLine;

        try {
            commandLine = parser.parse(options, args);
            //merge newly discovered options with previously discovered ones.
            Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions());

            if (log.isDebugEnabled()) {
                log.debug("Add user");
            }

            if (opts.hasOption(CliConstants.USERNAME_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("Username option is passed");
                }
                userName = opts.getOption(CliConstants.USERNAME_OPTION).getValue();
            }
            if (opts.hasOption(CliConstants.PASSWORD_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("Credential option is passed");
                }
                credential = opts.getOption(CliConstants.PASSWORD_OPTION).getValue();
            }
            if (opts.hasOption(CliConstants.ROLE_NAME_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("Role option is passed");
                }
                role = opts.getOption(CliConstants.ROLE_NAME_OPTION).getValue();
            }
            if (opts.hasOption(CliConstants.FIRST_NAME_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("First name option is passed");
                }
                firstName = opts.getOption(CliConstants.FIRST_NAME_OPTION).getValue();
                ;
            }
            if (opts.hasOption(CliConstants.LAST_NAME_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("Last name option is passed");
                }
                lastName = opts.getOption(CliConstants.LAST_NAME_OPTION).getValue();
            }
            if (opts.hasOption(CliConstants.EMAIL_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("Email option is passed");
                }
                email = opts.getOption(CliConstants.EMAIL_OPTION).getValue();
            }
            if (opts.hasOption(CliConstants.PROFILE_NAME_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("Profile name option is passed");
                }
                profileName = opts.getOption(CliConstants.PROFILE_NAME_OPTION).getValue();
            }

            if (userName == null || credential == null || role == null || firstName == null || lastName == null
                    || email == null) {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }

            RestCommandLineService.getInstance().addUser(userName, credential, role, firstName, lastName, email,
                    profileName);
            return CliConstants.COMMAND_SUCCESSFULL;

        } catch (ParseException e) {
            log.error("Error parsing arguments", e);
            System.out.println(e.getMessage());
            return CliConstants.COMMAND_FAILED;
        }

    } else {
        context.getStratosApplication().printUsage(getName());
        return CliConstants.COMMAND_FAILED;
    }
}

From source file:org.apache.stratos.cli.commands.CreateApplicationCommand.java

public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts)
        throws CommandException {
    if (log.isDebugEnabled()) {
        log.debug("Executing {} command...", getName());
    }/*  w  w  w.  j  a v  a2 s .  c  o  m*/

    if (args != null && args.length > 0) {
        String resourcePath = null;
        String resourceFileContent = null;

        final CommandLineParser parser = new GnuParser();
        CommandLine commandLine;

        try {
            commandLine = parser.parse(options, args);
            //merge newly discovered options with previously discovered ones.
            Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions());

            if (log.isDebugEnabled()) {
                log.debug("Creating application...");
            }

            if (opts.hasOption(CliConstants.RESOURCE_PATH)) {
                if (log.isTraceEnabled()) {
                    log.trace("Resource path option is passed");
                }
                resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue();
                resourceFileContent = CliUtils.readResource(resourcePath);
            }

            if (resourcePath == null) {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }

            RestCommandLineService.getInstance().addApplication(resourceFileContent);
            return CliConstants.COMMAND_SUCCESSFULL;

        } catch (ParseException e) {
            log.error("Error parsing arguments", e);
            System.out.println(e.getMessage());
            return CliConstants.COMMAND_FAILED;
        } catch (IOException e) {
            System.out.println("Invalid resource path");
            return CliConstants.COMMAND_FAILED;
        }
    } else {
        context.getStratosApplication().printUsage(getName());
        return CliConstants.COMMAND_FAILED;
    }
}

From source file:org.apache.stratos.cli.commands.DeployApplicationCommand.java

@Override
public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts)
        throws CommandException {
    if (log.isDebugEnabled()) {
        log.debug("Executing {} command...", getName());
    }//from  w  ww .  j ava 2 s .  c om

    if (args != null && args.length > 0) {
        String applicationId = null;
        String applicationPolicyId = null;

        final CommandLineParser parser = new GnuParser();
        CommandLine commandLine;

        try {
            commandLine = parser.parse(options, args);

            //merge newly discovered options with previously discovered ones.
            Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions());

            if (log.isDebugEnabled()) {
                log.debug("Deploy application");
            }

            if (opts.hasOption(CliConstants.APPLICATION_ID_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("Application id option is passed");
                }
                applicationId = opts.getOption(CliConstants.APPLICATION_ID_OPTION).getValue();
            }
            if (opts.hasOption(CliConstants.APPLICATION_POLICY_ID_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("Application policy id option is passed");
                }
                applicationPolicyId = opts.getOption(CliConstants.APPLICATION_POLICY_ID_OPTION).getValue();
            }

            if (applicationId == null || applicationPolicyId == null) {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }

            RestCommandLineService.getInstance().deployApplication(applicationId, applicationPolicyId);
            return CliConstants.COMMAND_SUCCESSFULL;

        } catch (ParseException e) {
            log.error("Error parsing arguments", e);
            System.out.println(e.getMessage());
            return CliConstants.COMMAND_FAILED;
        }

    } else {
        context.getStratosApplication().printUsage(getName());
        return CliConstants.COMMAND_FAILED;
    }
}

From source file:org.apache.stratos.cli.commands.ListKubernetesHostsCommand.java

public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts)
        throws CommandException {
    if (log.isDebugEnabled()) {
        log.debug("Executing {} command...", getName());
    }//from   w  w  w .j  a  va2s .  c  o  m

    if (args != null && args.length > 0) {
        String clusterId = null;

        final CommandLineParser parser = new GnuParser();
        CommandLine commandLine;

        try {
            commandLine = parser.parse(options, args);
            //merge newly discovered options with previously discovered ones.
            Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions());

            if (log.isDebugEnabled()) {
                log.debug("List Kubernetes hosts of a cluster");
            }

            if (opts.hasOption(CliConstants.CLUSTER_ID_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("Cluster id option is passed");
                }
                clusterId = opts.getOption(CliConstants.CLUSTER_ID_OPTION).getValue();
            }

            if (clusterId == null) {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }

            RestCommandLineService.getInstance().listKubernetesHosts(clusterId);
            return CliConstants.COMMAND_SUCCESSFULL;

        } catch (ParseException e) {
            log.error("Error parsing arguments", e);
            System.out.println(e.getMessage());
            return CliConstants.COMMAND_FAILED;
        }

    } else {
        context.getStratosApplication().printUsage(getName());
        return CliConstants.COMMAND_FAILED;
    }
}

From source file:org.apache.stratos.cli.commands.ListPartialSearchTenantsCommand.java

public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts)
        throws CommandException {
    if (log.isDebugEnabled()) {
        log.debug("Executing {} command...", getName());
    }//from w  w  w  . j ava2s .c  om

    if (args != null && args.length > 0) {
        String partialDomain = null;

        final CommandLineParser parser = new GnuParser();
        CommandLine commandLine;

        try {
            commandLine = parser.parse(options, args);
            //merge newly discovered options with previously discovered ones.
            Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions());

            if (log.isDebugEnabled()) {
                log.debug("List cartridges by partial domain search");
            }

            if (opts.hasOption(CliConstants.TENANT_PARTIAL_SEARCH_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("Partial domain option is passed");
                }
                partialDomain = opts.getOption(CliConstants.TENANT_PARTIAL_SEARCH_OPTION).getValue();
            }

            if (partialDomain == null) {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }

            RestCommandLineService.getInstance().listTenantsByPartialDomain(partialDomain);
            return CliConstants.COMMAND_SUCCESSFULL;

        } catch (ParseException e) {
            log.error("Error parsing arguments", e);
            System.out.println(e.getMessage());
            return CliConstants.COMMAND_FAILED;
        }

    } else {
        context.getStratosApplication().printUsage(getName());
        return CliConstants.COMMAND_FAILED;
    }
}

From source file:org.apache.stratos.cli.commands.UpdateApplicationCommand.java

public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts)
        throws CommandException {
    if (log.isDebugEnabled()) {
        log.debug("Executing {} command...", getName());
    }/*from   ww w .ja  va  2  s  .com*/

    if (args != null && args.length > 0) {
        String resourcePath = null;
        String resourceFileContent = null;

        final CommandLineParser parser = new GnuParser();
        CommandLine commandLine;

        try {
            commandLine = parser.parse(options, args);
            //merge newly discovered options with previously discovered ones.
            Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions());

            if (log.isDebugEnabled()) {
                log.debug("Updating application...");
            }

            if (opts.hasOption(CliConstants.RESOURCE_PATH)) {
                if (log.isTraceEnabled()) {
                    log.trace("Resource path option is passed");
                }
                resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue();
                resourceFileContent = CliUtils.readResource(resourcePath);
            }

            if (resourcePath == null) {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }

            RestCommandLineService.getInstance().updateApplication(resourceFileContent);
            return CliConstants.COMMAND_SUCCESSFULL;

        } catch (ParseException e) {
            log.error("Error parsing arguments", e);
            System.out.println(e.getMessage());
            return CliConstants.COMMAND_FAILED;
        } catch (IOException e) {
            System.out.println("Invalid resource path");
            return CliConstants.COMMAND_FAILED;
        }
    } else {
        context.getStratosApplication().printUsage(getName());
        return CliConstants.COMMAND_FAILED;
    }
}

From source file:org.apache.stratos.cli.commands.UpdateAutoscalingPolicyCommand.java

public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts)
        throws CommandException {
    if (log.isDebugEnabled()) {
        log.debug("Executing {} command...", getName());
    }//from   w  ww.ja v  a 2  s .co m

    if (args != null && args.length > 0) {
        String resourcePath = null;
        String resourceFileContent = null;

        final CommandLineParser parser = new GnuParser();
        CommandLine commandLine;

        try {
            commandLine = parser.parse(options, args);
            //merge newly discovered options with previously discovered ones.
            Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions());

            if (log.isDebugEnabled()) {
                log.debug("Updating auto scaling policy");
            }

            if (opts.hasOption(CliConstants.RESOURCE_PATH)) {
                if (log.isTraceEnabled()) {
                    log.trace("Resource path option is passed");
                }
                resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue();
                resourceFileContent = CliUtils.readResource(resourcePath);
            }

            if (resourcePath == null) {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }

            RestCommandLineService.getInstance().updateAutoscalingPolicy(resourceFileContent);
            return CliConstants.COMMAND_SUCCESSFULL;

        } catch (ParseException e) {
            log.error("Error parsing arguments", e);
            System.out.println(e.getMessage());
            return CliConstants.COMMAND_FAILED;
        } catch (IOException ignore) {
            System.out.println("Invalid resource path");
            return CliConstants.COMMAND_FAILED;
        }
    } else {
        context.getStratosApplication().printUsage(getName());
        return CliConstants.COMMAND_FAILED;
    }
}

From source file:org.apache.stratos.cli.commands.UpdateCartridgeCommand.java

public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts)
        throws CommandException {
    if (log.isDebugEnabled()) {
        log.debug("Executing {} command...", getName());
    }//from  w ww  .j  a  v  a 2  s .  co m

    if (args != null && args.length > 0) {
        String resourcePath = null;
        String resourceFileContent = null;

        final CommandLineParser parser = new GnuParser();
        CommandLine commandLine;

        try {
            commandLine = parser.parse(options, args);
            //merge newly discovered options with previously discovered ones.
            Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions());

            if (log.isDebugEnabled()) {
                log.debug("Cartridge deployment");
            }

            if (opts.hasOption(CliConstants.RESOURCE_PATH)) {
                if (log.isTraceEnabled()) {
                    log.trace("Resource path option is passed");
                }
                resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue();
                resourceFileContent = CliUtils.readResource(resourcePath);
            }

            if (resourcePath == null) {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }

            RestCommandLineService.getInstance().updateCartridge(resourceFileContent);
            return CliConstants.COMMAND_SUCCESSFULL;

        } catch (ParseException e) {
            log.error("Error parsing arguments", e);
            System.out.println(e.getMessage());
            return CliConstants.COMMAND_FAILED;
        } catch (IOException e) {
            System.out.println("Invalid resource path");
            return CliConstants.COMMAND_FAILED;
        }

    } else {
        context.getStratosApplication().printUsage(getName());
        return CliConstants.COMMAND_FAILED;
    }
}

From source file:org.apache.stratos.cli.commands.UpdateDeploymentPolicyCommand.java

public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts)
        throws CommandException {
    if (log.isDebugEnabled()) {
        log.debug("Executing {} command...", getName());
    }/*  www .  j a v a  2  s . c o m*/

    if (args != null && args.length > 0) {
        String resourcePath = null;
        String resourceFileContent = null;

        final CommandLineParser parser = new GnuParser();
        CommandLine commandLine;

        try {
            commandLine = parser.parse(options, args);
            //merge newly discovered options with previously discovered ones.
            Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions());

            if (log.isDebugEnabled()) {
                log.debug("Updating deployment policy");
            }

            if (opts.hasOption(CliConstants.RESOURCE_PATH)) {
                if (log.isTraceEnabled()) {
                    log.trace("Resource path option is passed");
                }
                resourcePath = commandLine.getOptionValue(CliConstants.RESOURCE_PATH);
                resourceFileContent = CliUtils.readResource(resourcePath);
            }

            if (resourcePath == null) {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }

            RestCommandLineService.getInstance().updateDeploymentPolicy(resourceFileContent);
            return CliConstants.COMMAND_SUCCESSFULL;

        } catch (ParseException e) {
            log.error("Error parsing arguments", e);
            System.out.println(e.getMessage());
            return CliConstants.COMMAND_FAILED;
        } catch (IOException ignore) {
            System.out.println("Invalid resource path");
            return CliConstants.COMMAND_FAILED;
        }
    } else {
        context.getStratosApplication().printUsage(getName());
        return CliConstants.COMMAND_FAILED;
    }
}