Search in sources :

Example 6 with CmdLineException

use of org.kohsuke.args4j.CmdLineException in project hudson-2.x by hudson.

the class CLIRegisterer method discover.

private List<ExtensionComponent<CLICommand>> discover(final Hudson hudson) {
    LOGGER.fine("Listing up @CLIMethod");
    List<ExtensionComponent<CLICommand>> r = new ArrayList<ExtensionComponent<CLICommand>>();
    try {
        for (final Method m : Util.filter(Index.list(CLIMethod.class, hudson.getPluginManager().uberClassLoader), Method.class)) {
            try {
                // command name
                final String name = m.getAnnotation(CLIMethod.class).name();
                final ResourceBundleHolder res = loadMessageBundle(m);
                // make sure we have the resource, to fail early
                res.format("CLI." + name + ".shortDescription");
                r.add(new ExtensionComponent<CLICommand>(new CloneableCLICommand() {

                    @Override
                    public String getName() {
                        return name;
                    }

                    public String getShortDescription() {
                        // format by using the right locale
                        return res.format("CLI." + name + ".shortDescription");
                    }

                    @Override
                    public int main(List<String> args, Locale locale, InputStream stdin, PrintStream stdout, PrintStream stderr) {
                        this.stdout = stdout;
                        this.stderr = stderr;
                        this.locale = locale;
                        this.channel = Channel.current();
                        registerOptionHandlers();
                        CmdLineParser parser = new CmdLineParser(null);
                        try {
                            SecurityContext sc = SecurityContextHolder.getContext();
                            Authentication old = sc.getAuthentication();
                            try {
                                //  build up the call sequence
                                Stack<Method> chains = new Stack<Method>();
                                Method method = m;
                                while (true) {
                                    chains.push(method);
                                    if (Modifier.isStatic(method.getModifiers()))
                                        // the chain is complete.
                                        break;
                                    // the method in question is an instance method, so we need to resolve the instance by using another resolver
                                    Class<?> type = method.getDeclaringClass();
                                    method = findResolver(type);
                                    if (method == null) {
                                        stderr.println("Unable to find the resolver method annotated with @CLIResolver for " + type);
                                        return 1;
                                    }
                                }
                                List<MethodBinder> binders = new ArrayList<MethodBinder>();
                                while (!chains.isEmpty()) binders.add(new MethodBinder(chains.pop(), parser));
                                // authentication
                                CliAuthenticator authenticator = Hudson.getInstance().getSecurityRealm().createCliAuthenticator(this);
                                new ClassParser().parse(authenticator, parser);
                                // fill up all the binders
                                parser.parseArgument(args);
                                Authentication auth = authenticator.authenticate();
                                if (auth == Hudson.ANONYMOUS)
                                    auth = loadStoredAuthentication();
                                // run the CLI with the right credential
                                sc.setAuthentication(auth);
                                hudson.checkPermission(Hudson.READ);
                                // resolve them
                                Object instance = null;
                                for (MethodBinder binder : binders) instance = binder.call(instance);
                                if (instance instanceof Integer)
                                    return (Integer) instance;
                                else
                                    return 0;
                            } catch (InvocationTargetException e) {
                                Throwable t = e.getTargetException();
                                if (t instanceof Exception)
                                    throw (Exception) t;
                                throw e;
                            } finally {
                                // restore
                                sc.setAuthentication(old);
                            }
                        } catch (CmdLineException e) {
                            stderr.println(e.getMessage());
                            printUsage(stderr, parser);
                            return 1;
                        } catch (Exception e) {
                            e.printStackTrace(stderr);
                            return 1;
                        }
                    }

                    protected int run() throws Exception {
                        throw new UnsupportedOperationException();
                    }
                }));
            } catch (ClassNotFoundException e) {
                LOGGER.log(SEVERE, "Failed to process @CLIMethod: " + m, e);
            }
        }
    } catch (IOException e) {
        LOGGER.log(SEVERE, "Failed to discvoer @CLIMethod", e);
    }
    return r;
}
Also used : Locale(java.util.Locale) ExtensionComponent(hudson.ExtensionComponent) CloneableCLICommand(hudson.cli.CloneableCLICommand) ArrayList(java.util.ArrayList) CliAuthenticator(hudson.security.CliAuthenticator) ArrayList(java.util.ArrayList) List(java.util.List) PrintStream(java.io.PrintStream) CmdLineParser(org.kohsuke.args4j.CmdLineParser) InputStream(java.io.InputStream) Method(java.lang.reflect.Method) IOException(java.io.IOException) ResourceBundleHolder(org.jvnet.localizer.ResourceBundleHolder) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CmdLineException(org.kohsuke.args4j.CmdLineException) Stack(java.util.Stack) CLICommand(hudson.cli.CLICommand) CloneableCLICommand(hudson.cli.CloneableCLICommand) Authentication(org.acegisecurity.Authentication) SecurityContext(org.acegisecurity.context.SecurityContext) CmdLineException(org.kohsuke.args4j.CmdLineException) ClassParser(org.kohsuke.args4j.ClassParser)

Example 7 with CmdLineException

use of org.kohsuke.args4j.CmdLineException in project hudson-2.x by hudson.

the class Launcher method run.

public void run() throws Exception {
    if (auth != null) {
        final int idx = auth.indexOf(':');
        if (idx < 0)
            throw new CmdLineException(null, "No ':' in the -auth option");
        Authenticator.setDefault(new Authenticator() {

            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(auth.substring(0, idx), auth.substring(idx + 1).toCharArray());
            }
        });
    }
    if (connectionTarget != null) {
        runAsTcpClient();
        System.exit(0);
    } else if (slaveJnlpURL != null) {
        List<String> jnlpArgs = parseJnlpArguments();
        try {
            hudson.remoting.jnlp.Main._main(jnlpArgs.toArray(new String[jnlpArgs.size()]));
        } catch (CmdLineException e) {
            System.err.println("JNLP file " + slaveJnlpURL + " has invalid arguments: " + jnlpArgs);
            System.err.println("Most likely a configuration error in the master");
            System.err.println(e.getMessage());
            System.exit(1);
        }
    } else if (tcpPortFile != null) {
        runAsTcpServer();
        System.exit(0);
    } else {
        runWithStdinStdout();
        System.exit(0);
    }
}
Also used : ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) CmdLineException(org.kohsuke.args4j.CmdLineException) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 8 with CmdLineException

use of org.kohsuke.args4j.CmdLineException in project pinot by linkedin.

the class PinotAdministrator method execute.

public void execute(String[] args) throws Exception {
    try {
        CmdLineParser parser = new CmdLineParser(this);
        parser.parseArgument(args);
        if ((_subCommand == null) || _help) {
            printUsage();
        } else if (_subCommand.getHelp()) {
            _subCommand.printUsage();
            _status = true;
        } else {
            _status = _subCommand.execute();
        }
    } catch (CmdLineException e) {
        LOGGER.error("Error: {}", e.getMessage());
    } catch (Exception e) {
        LOGGER.error("Exception caught: ", e);
    }
}
Also used : CmdLineParser(org.kohsuke.args4j.CmdLineParser) CmdLineException(org.kohsuke.args4j.CmdLineException) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 9 with CmdLineException

use of org.kohsuke.args4j.CmdLineException in project pinot by linkedin.

the class PinotToolLauncher method execute.

public void execute(String[] args) throws Exception {
    try {
        CmdLineParser parser = new CmdLineParser(this);
        parser.parseArgument(args);
        if ((_subCommand == null) || _help) {
            printUsage();
        } else if (_subCommand.getHelp()) {
            _subCommand.printUsage();
        } else {
            _subCommand.execute();
        }
    } catch (CmdLineException e) {
        LOGGER.error("Error: {}", e.getMessage());
    } catch (Exception e) {
        LOGGER.error("Exception caught: ", e);
    }
}
Also used : CmdLineParser(org.kohsuke.args4j.CmdLineParser) CmdLineException(org.kohsuke.args4j.CmdLineException) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 10 with CmdLineException

use of org.kohsuke.args4j.CmdLineException in project pinot by linkedin.

the class MoveReplicaGroup method main.

public static void main(String[] args) throws Exception {
    MoveReplicaGroup mrg = new MoveReplicaGroup();
    CmdLineParser parser = new CmdLineParser(mrg);
    try {
        parser.parseArgument(args);
    } catch (CmdLineException e) {
        LOGGER.error("Failed to parse arguments: {}", e);
        parser.printUsage(System.err);
        System.exit(1);
    }
    mrg.execute();
}
Also used : CmdLineParser(org.kohsuke.args4j.CmdLineParser) CmdLineException(org.kohsuke.args4j.CmdLineException)

Aggregations

CmdLineException (org.kohsuke.args4j.CmdLineException)100 CmdLineParser (org.kohsuke.args4j.CmdLineParser)75 IOException (java.io.IOException)16 File (java.io.File)14 ArrayList (java.util.ArrayList)11 PrintStream (java.io.PrintStream)7 StringWriter (java.io.StringWriter)6 List (java.util.List)5 FileOutputStream (java.io.FileOutputStream)4 Path (java.nio.file.Path)4 CmdLineParser (com.google.gerrit.util.cli.CmdLineParser)3 FeatureExtractors (io.anserini.ltr.feature.FeatureExtractors)3 Qrels (io.anserini.util.Qrels)3 Directory (org.apache.lucene.store.Directory)3 FSDirectory (org.apache.lucene.store.FSDirectory)3 ConsoleReporter (com.codahale.metrics.ConsoleReporter)2 MetricRegistry (com.codahale.metrics.MetricRegistry)2 Project (com.google.gerrit.reviewdb.client.Project)2 OrmException (com.google.gwtorm.server.OrmException)2 Hudson (hudson.model.Hudson)2