From 65de7809baa90065d138a18ac3e525c69af5de73 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Thu, 3 Sep 2009 23:45:47 +0000 Subject: [PATCH] recording these code before I delete them. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@21445 71c3de6d-444a-0410-be80-ed276b4c234a --- .../cli/declarative/CLIInstanceResolver.java | 43 +++++++++++++++++++ .../CLIInstanceResolverFactory.java | 25 +++++++++++ 2 files changed, 68 insertions(+) create mode 100644 core/src/main/java/hudson/cli/declarative/CLIInstanceResolver.java create mode 100644 core/src/main/java/hudson/cli/declarative/CLIInstanceResolverFactory.java diff --git a/core/src/main/java/hudson/cli/declarative/CLIInstanceResolver.java b/core/src/main/java/hudson/cli/declarative/CLIInstanceResolver.java new file mode 100644 index 0000000000..19cd97f6c7 --- /dev/null +++ b/core/src/main/java/hudson/cli/declarative/CLIInstanceResolver.java @@ -0,0 +1,43 @@ +package hudson.cli.declarative; + +import org.kohsuke.args4j.CmdLineParser; +import org.kohsuke.args4j.ClassParser; +import org.kohsuke.args4j.CmdLineException; +import hudson.model.Hudson; + +/** + * Used to parse a portion of arguments to resolve to a model object, for which {@link CLIMethod} will be invoked. + * + *

+ * This object is stateful. Typical subtype contains a number of fields and setter methods with args4j annotations. + * The CLI framework creates an instance of {@link CLIInstanceResolver} from {@link CLIInstanceResolverFactory}, + * call {@link #defineOptionsTo(CmdLineParser)}, then let {@link CmdLineParser} parses the arguments, which + * fills this object with parameters and arguments. + * + *

+ * Finally, the CLI framework will call {@link #resolve()} to obtain the object, and that is the object + * that the command will act on. + * + * @author Kohsuke Kawaguchi + * @see CLIInstanceResolverFactory + * @since 1.324 + */ +public abstract class CLIInstanceResolver { + /** + * Fills the given {@link CmdLineParser} by defining options and arguments that this resolver uses. + * + *

+ * The default implementation expects the resolver instance itself to be annotated with args4j annotations. + */ + public void defineOptionsTo(CmdLineParser parser) { + new ClassParser().parse(this,parser); + } + + /** + * Called after {@link CmdLineParser} parsed arguments, to resolve the instance. + * + * @return + * must not be null. + */ + public abstract T resolve() throws CmdLineException; +} diff --git a/core/src/main/java/hudson/cli/declarative/CLIInstanceResolverFactory.java b/core/src/main/java/hudson/cli/declarative/CLIInstanceResolverFactory.java new file mode 100644 index 0000000000..f4b3264dec --- /dev/null +++ b/core/src/main/java/hudson/cli/declarative/CLIInstanceResolverFactory.java @@ -0,0 +1,25 @@ +package hudson.cli.declarative; + +import hudson.ExtensionPoint; +import hudson.Extension; + + +/** + * Extension point for resolving an instance of a model object, from the arguments and options given to CLI. + * + *

+ * To have your implementation registered, put {@link Extension} on your implementation. + * + * @author Kohsuke Kawaguchi + * @since 1.324 + */ +public abstract class CLIInstanceResolverFactory implements ExtensionPoint { + /** + * Creates a new instance of {@link CLIInstanceResolver} that can resolve the given type. + * + * @return + * null if this factory doens't understand the given type, in which case the caller + * will continue to search the rest of the factories for a match. + */ + public abstract CLIInstanceResolver create(Class type); +} -- GitLab