this.locale = locale;
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()))
break; // the chain is complete.
// 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(),this,parser));
// authentication
CliAuthenticator authenticator = Jenkins.getInstance().getSecurityRealm().createCliAuthenticator(this);
new ClassParser().parse(authenticator,parser);
// fill up all the binders
parser.parseArgument(args);
Authentication auth = authenticator.authenticate();
if (auth== Jenkins.ANONYMOUS)
auth = loadStoredAuthentication();
sc.setAuthentication(auth); // run the CLI with the right credential
hudson.checkPermission(Jenkins.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 {
sc.setAuthentication(old); // restore
}
} catch (CmdLineException e) {
stderr.println(e.getMessage());
printUsage(stderr,parser);
return 1;