/* * The MIT License * * Copyright (c) 2004-2009, Sun Microsystems, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package hudson.cli; import hudson.AbortException; import hudson.Extension; import hudson.ExtensionList; import hudson.ExtensionPoint; import hudson.cli.declarative.CLIMethod; import hudson.ExtensionPoint.LegacyInstancesAreScopedToHudson; import hudson.model.Hudson; import hudson.remoting.Callable; import hudson.remoting.Channel; import org.kohsuke.args4j.CmdLineException; import org.kohsuke.args4j.CmdLineParser; import java.io.BufferedInputStream; import java.io.InputStream; import java.io.PrintStream; import java.util.List; import java.util.Locale; /** * Base class for Hudson CLI. * *

How does a CLI command work

*

* The users starts {@linkplain CLI the "CLI agent"} on a remote system, by specifying arguments, like * "java -jar hudson-cli.jar command arg1 arg2 arg3". The CLI agent creates * a remoting channel with the server, and it sends the entire arguments to the server, along with * the remoted stdin/out/err. * *

* The Hudson master then picks the right {@link CLICommand} to execute, clone it, and * calls {@link #main(List, Locale, InputStream, PrintStream, PrintStream)} method. * *

Note for CLI command implementor

* Start with this document * to get the general idea of CLI. * * * * @author Kohsuke Kawaguchi * @since 1.302 * @see CLIMethod */ @LegacyInstancesAreScopedToHudson public abstract class CLICommand implements ExtensionPoint, Cloneable { /** * Connected to stdout and stderr of the CLI agent that initiated the session. * IOW, if you write to these streams, the person who launched the CLI command * will see the messages in his terminal. * *

* (In contrast, calling {@code System.out.println(...)} would print out * the message to the server log file, which is probably not what you want. */ protected transient PrintStream stdout,stderr; /** * Connected to stdin of the CLI agent. * *

* This input stream is buffered to hide the latency in the remoting. */ protected transient InputStream stdin; /** * {@link Channel} that represents the CLI JVM. You can use this to * execute {@link Callable} on the CLI JVM, among other things. */ protected transient Channel channel; /** * The locale of the client. Messages should be formatted with this resource. */ protected transient Locale locale; /** * Gets the command name. * *

* For example, if the CLI is invoked as java -jar cli.jar foo arg1 arg2 arg4, * on the server side {@link CLICommand} that returns "foo" from {@link #getName()} * will be invoked. * *

* By default, this method creates "foo-bar-zot" from "FooBarZotCommand". */ public String getName() { String name = getClass().getName(); name = name.substring(name.lastIndexOf('.')+1); // short name if(name.endsWith("Command")) name = name.substring(0,name.length()-7); // trim off the command // convert "FooBarZot" into "foo-bar-zot" // Locale is fixed so that "CreateInstance" always become "create-instance" no matter where this is run. return name.replaceAll("([a-z0-9])([A-Z])","$1-$2").toLowerCase(Locale.ENGLISH); } /** * Gets the quick summary of what this command does. * Used by the help command to generate the list of commands. */ public abstract String getShortDescription(); public int main(List args, Locale locale, InputStream stdin, PrintStream stdout, PrintStream stderr) { this.stdin = new BufferedInputStream(stdin); this.stdout = stdout; this.stderr = stderr; this.locale = locale; this.channel = Channel.current(); CmdLineParser p = new CmdLineParser(this); try { p.parseArgument(args.toArray(new String[args.size()])); return run(); } catch (CmdLineException e) { stderr.println(e.getMessage()); printUsage(stderr, p); return -1; } catch (AbortException e) { // signals an error without stack trace stderr.println(e.getMessage()); return -1; } catch (Exception e) { e.printStackTrace(stderr); return -1; } } /** * Executes the command, and return the exit code. * * @return * 0 to indicate a success, otherwise an error code. * @throws AbortException * If the processing should be aborted. Hudson will report the error message * without stack trace, and then exits this command. * @throws Exception * All the other exceptions cause the stack trace to be dumped, and then * the command exits with an error code. */ protected abstract int run() throws Exception; protected void printUsage(PrintStream stderr, CmdLineParser p) { stderr.println("java -jar hudson-cli.jar "+getName()+" args..."); p.printUsage(stderr); } /** * Creates a clone to be used to execute a command. */ protected CLICommand createClone() { try { return getClass().newInstance(); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InstantiationException e) { throw new AssertionError(e); } } /** * Returns all the registered {@link CLICommand}s. */ public static ExtensionList all() { return Hudson.getInstance().getExtensionList(CLICommand.class); } /** * Obtains a copy of the command for invocation. */ public static CLICommand clone(String name) { for (CLICommand cmd : all()) if(name.equals(cmd.getName())) return cmd.createClone(); return null; } }