提交 dca3317d 编写于 作者: K kohsuke

Added a mechanism to register CLI option handler as an extension point.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@32827 71c3de6d-444a-0410-be80-ed276b4c234a
上级 10db7d10
......@@ -29,6 +29,7 @@ import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.cli.declarative.CLIMethod;
import hudson.ExtensionPoint.LegacyInstancesAreScopedToHudson;
import hudson.cli.declarative.OptionHandlerExtension;
import hudson.model.Hudson;
import hudson.remoting.Callable;
import hudson.remoting.Channel;
......@@ -38,14 +39,20 @@ import hudson.security.SecurityRealm;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import org.jvnet.hudson.annotation_indexer.Index;
import org.jvnet.hudson.annotation_indexer.Indexed;
import org.jvnet.tiger_types.Types;
import org.kohsuke.args4j.ClassParser;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.spi.OptionHandler;
import java.io.BufferedInputStream;
import java.io.IOError;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Locale;
import java.util.logging.Logger;
......@@ -158,6 +165,7 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable {
this.stderr = stderr;
this.locale = locale;
this.channel = Channel.current();
registerOptionHandlers();
CmdLineParser p = new CmdLineParser(this);
// add options from the authenticator
......@@ -308,7 +316,19 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable {
}
}
/**
* Auto-discovers {@link OptionHandler}s and add them to the given command line parser.
*/
protected void registerOptionHandlers() {
try {
for (Class c : Index.list(OptionHandlerExtension.class,Hudson.getInstance().pluginManager.uberClassLoader,Class.class)) {
Type t = Types.getBaseClass(c, OptionHandler.class);
CmdLineParser.registerHandler(Types.erasure(Types.getTypeArgument(t,0)), c);
}
} catch (IOException e) {
throw new Error(e);
}
}
/**
* Returns all the registered {@link CLICommand}s.
......
......@@ -112,7 +112,8 @@ public class CLIRegisterer extends ExtensionFinder {
this.stderr = stderr;
this.locale = locale;
this.channel = Channel.current();
registerOptionHandlers();
CmdLineParser parser = new CmdLineParser(null);
try {
SecurityContext sc = SecurityContextHolder.getContext();
......
/*
* The MIT License
*
* Copyright (c) 2010, InfraDNA, 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.declarative;
import org.jvnet.hudson.annotation_indexer.Indexed;
import org.kohsuke.args4j.spi.OptionHandler;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* {@link OptionHandler}s that should be auto-discovered.
*
* @author Kohsuke Kawaguchi
*/
@Indexed
@Retention(RUNTIME)
@Target({TYPE})
@Documented
public @interface OptionHandlerExtension {
}
......@@ -25,9 +25,20 @@ package hudson.model;
import com.thoughtworks.xstream.converters.SingleValueConverter;
import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;
import hudson.Extension;
import hudson.cli.declarative.OptionHandlerExtension;
import hudson.util.EditDistance;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.*;
import org.kohsuke.stapler.export.CustomExportedBean;
import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
/**
* The build outcome.
......@@ -116,11 +127,18 @@ public final class Result implements Serializable, CustomExportedBean {
public static Result fromString(String s) {
for (Result r : all)
if (s.equals(r.name))
if (s.equalsIgnoreCase(r.name))
return r;
return FAILURE;
}
private static List<String> getNames() {
List<String> l = new ArrayList<String>();
for (Result r : all)
l.add(r.name);
return l;
}
// Maintain each Result as a singleton deserialized (like build result from a slave node)
private Object readResolve() {
for (Result r : all)
......@@ -142,4 +160,27 @@ public final class Result implements Serializable, CustomExportedBean {
return Result.fromString(s);
}
};
@OptionHandlerExtension
public static final class OptionHandlerImpl extends OptionHandler<Result> {
public OptionHandlerImpl(CmdLineParser parser, OptionDef option, Setter<? super Result> setter) {
super(parser, option, setter);
}
@Override
public int parseArguments(Parameters params) throws CmdLineException {
String param = params.getParameter(0);
Result v = fromString(param.replace('-', '_'));
if (v==null)
throw new CmdLineException(owner,"No such status '"+param+"'. Did you mean "+
EditDistance.findNearest(param.replace('-', '_').toUpperCase(), getNames()));
setter.addValue(v);
return 1;
}
@Override
public String getDefaultMetaVariable() {
return "STATUS";
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册