未验证 提交 86da98c3 编写于 作者: B Baptiste Mathus 提交者: GitHub

Merge pull request #3208 from oleg-nenashev/cleanup/cli

Bulk cleanup of code in hudson.cli.
......@@ -89,7 +89,7 @@ public class BuildCommand extends CLICommand {
public boolean checkSCM = false;
@Option(name="-p",usage="Specify the build parameters in the key=value format.")
public Map<String,String> parameters = new HashMap<String, String>();
public Map<String,String> parameters = new HashMap<>();
@Option(name="-v",usage="Prints out the console output of the build. Use with -s")
public boolean consoleOutput = false;
......@@ -109,7 +109,7 @@ public class BuildCommand extends CLICommand {
throw new IllegalStateException(job.getFullDisplayName()+" is not parameterized but the -p option was specified.");
//TODO: switch to type annotations after the migration to Java 1.8
List<ParameterValue> values = new ArrayList<ParameterValue>();
List<ParameterValue> values = new ArrayList<>();
for (Entry<String, String> e : parameters.entrySet()) {
String name = e.getKey();
......
......@@ -250,7 +250,7 @@ public class CLIAction implements UnprotectedRootAction, StaplerProxy {
// do not require any permission to establish a CLI connection
// the actual authentication for the connecting Channel is done by CLICommand
return new FullDuplexHttpChannel(uuid, !Jenkins.getInstance().hasPermission(Jenkins.ADMINISTER)) {
return new FullDuplexHttpChannel(uuid, !Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
@SuppressWarnings("deprecation")
@Override
protected void main(Channel channel) throws IOException, InterruptedException {
......
......@@ -636,9 +636,9 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable {
* Key for {@link Channel#getProperty(Object)} that links to the {@link Authentication} object
* which captures the identity of the client given by the transport layer.
*/
public static final ChannelProperty<Authentication> TRANSPORT_AUTHENTICATION = new ChannelProperty<Authentication>(Authentication.class,"transportAuthentication");
public static final ChannelProperty<Authentication> TRANSPORT_AUTHENTICATION = new ChannelProperty<>(Authentication.class, "transportAuthentication");
private static final ThreadLocal<CLICommand> CURRENT_COMMAND = new ThreadLocal<CLICommand>();
private static final ThreadLocal<CLICommand> CURRENT_COMMAND = new ThreadLocal<>();
/*package*/ static CLICommand setCurrent(CLICommand cmd) {
CLICommand old = getCurrent();
......@@ -656,7 +656,7 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable {
static {
// register option handlers that are defined
ClassLoaders cls = new ClassLoaders();
Jenkins j = Jenkins.getActiveInstance();
Jenkins j = Jenkins.getInstanceOrNull();
if (j!=null) {// only when running on the master
cls.put(j.getPluginManager().uberClassLoader);
......
......@@ -26,7 +26,6 @@ package hudson.cli;
import hudson.Extension;
import jenkins.model.Jenkins;
import org.acegisecurity.AccessDeniedException;
import java.util.logging.Logger;
......
......@@ -90,7 +90,7 @@ public class CliManagerImpl implements CliEntryPoint, Serializable {
cmd.channel = Channel.current();
final CLICommand old = CLICommand.setCurrent(cmd);
try {
transportAuth = Channel.current().getProperty(CLICommand.TRANSPORT_AUTHENTICATION);
transportAuth = Channel.currentOrFail().getProperty(CLICommand.TRANSPORT_AUTHENTICATION);
cmd.setTransportAuth(transportAuth);
return cmd.main(args.subList(1,args.size()),locale, stdin, out, err);
} finally {
......@@ -99,7 +99,7 @@ public class CliManagerImpl implements CliEntryPoint, Serializable {
}
err.println("No such command: "+subCmd);
new HelpCommand().main(Collections.<String>emptyList(), locale, stdin, out, err);
new HelpCommand().main(Collections.emptyList(), locale, stdin, out, err);
return -1;
}
......
......@@ -62,7 +62,7 @@ public class ConnectNodeCommand extends CLICommand {
boolean errorOccurred = false;
final Jenkins jenkins = Jenkins.getActiveInstance();
final HashSet<String> hs = new HashSet<String>();
final HashSet<String> hs = new HashSet<>();
hs.addAll(nodes);
List<String> names = null;
......
......@@ -32,7 +32,6 @@ import hudson.model.User;
import jenkins.model.Jenkins;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
/**
* @author ogondza
......
......@@ -57,7 +57,7 @@ public class DeleteBuildsCommand extends RunRangeCommand {
protected int act(List<Run<?, ?>> builds) throws IOException {
job.checkPermission(Run.DELETE);
final HashSet<Integer> hsBuilds = new HashSet<Integer>();
final HashSet<Integer> hsBuilds = new HashSet<>();
for (Run<?, ?> build : builds) {
if (!hsBuilds.contains(build.number)) {
......
......@@ -31,7 +31,6 @@ import org.kohsuke.args4j.Argument;
import java.util.List;
import java.util.HashSet;
import java.util.logging.Logger;
/**
* CLI command, which deletes a job or multiple jobs.
......@@ -56,7 +55,7 @@ public class DeleteJobCommand extends CLICommand {
boolean errorOccurred = false;
final Jenkins jenkins = Jenkins.getActiveInstance();
final HashSet<String> hs = new HashSet<String>();
final HashSet<String> hs = new HashSet<>();
hs.addAll(jobs);
for (String job_s: hs) {
......
......@@ -31,7 +31,6 @@ import org.kohsuke.args4j.Argument;
import java.util.HashSet;
import java.util.List;
import java.util.logging.Logger;
/**
* CLI command, which deletes Jenkins nodes.
......@@ -56,7 +55,7 @@ public class DeleteNodeCommand extends CLICommand {
boolean errorOccurred = false;
final Jenkins jenkins = Jenkins.getActiveInstance();
final HashSet<String> hs = new HashSet<String>();
final HashSet<String> hs = new HashSet<>();
hs.addAll(nodes);
for (String node_s : hs) {
......
......@@ -33,7 +33,6 @@ import org.kohsuke.args4j.Argument;
import java.util.HashSet;
import java.util.List;
import java.util.logging.Logger;
/**
* @author ogondza, pjanouse
......@@ -57,7 +56,7 @@ public class DeleteViewCommand extends CLICommand {
boolean errorOccurred = false;
// Remove duplicates
final HashSet<String> hs = new HashSet<String>();
final HashSet<String> hs = new HashSet<>();
hs.addAll(views);
ViewOptionHandler voh = new ViewOptionHandler(null, null, null);
......
......@@ -61,7 +61,7 @@ public class DisconnectNodeCommand extends CLICommand {
boolean errorOccurred = false;
final Jenkins jenkins = Jenkins.getActiveInstance();
final HashSet<String> hs = new HashSet<String>();
final HashSet<String> hs = new HashSet<>();
hs.addAll(nodes);
List<String> names = null;
......
......@@ -59,7 +59,7 @@ public class GroovyCommand extends CLICommand {
* Remaining arguments.
*/
@Argument(metaVar="ARGUMENTS", index=1, usage="Command line arguments to pass into script.")
public List<String> remaining = new ArrayList<String>();
public List<String> remaining = new ArrayList<>();
protected int run() throws Exception {
// this allows the caller to manipulate the JVM state, so require the execute script privilege.
......
......@@ -56,7 +56,7 @@ public class GroovyshCommand extends CLICommand {
return Messages.GroovyshCommand_ShortDescription();
}
@Argument(metaVar="ARGS") public List<String> args = new ArrayList<String>();
@Argument(metaVar="ARGS") public List<String> args = new ArrayList<>();
@Override
protected int run() {
......
......@@ -65,7 +65,7 @@ public class HelpCommand extends CLICommand {
}
private int showAllCommands() {
Map<String,CLICommand> commands = new TreeMap<String,CLICommand>();
Map<String,CLICommand> commands = new TreeMap<>();
for (CLICommand c : CLICommand.all())
commands.put(c.getName(),c);
......
......@@ -36,7 +36,6 @@ import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.HashSet;
......@@ -166,7 +165,7 @@ public class InstallPluginCommand extends CLICommand {
if (h.getUpdateCenter().getSites().isEmpty()) {
stdout.println(Messages.InstallPluginCommand_NoUpdateCenterDefined());
} else {
Set<String> candidates = new HashSet<String>();
Set<String> candidates = new HashSet<>();
for (UpdateSite s : h.getUpdateCenter().getSites()) {
Data dt = s.getData();
if (dt==null)
......
......@@ -78,11 +78,11 @@ public class InstallToolCommand extends CLICommand {
throw new IllegalStateException("No such job found: "+id.job);
p.checkPermission(Item.CONFIGURE);
List<String> toolTypes = new ArrayList<String>();
List<String> toolTypes = new ArrayList<>();
for (ToolDescriptor<?> d : ToolInstallation.all()) {
toolTypes.add(d.getDisplayName());
if (d.getDisplayName().equals(toolType)) {
List<String> toolNames = new ArrayList<String>();
List<String> toolNames = new ArrayList<>();
for (ToolInstallation t : d.getInstallations()) {
toolNames.add(t.getName());
if (t.getName().equals(toolName))
......
......@@ -26,7 +26,6 @@ package hudson.cli;
import java.util.Collection;
import hudson.model.Item;
import hudson.model.Items;
import hudson.model.TopLevelItem;
import hudson.model.View;
import hudson.Extension;
......
......@@ -34,7 +34,6 @@ import jenkins.model.Jenkins;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
......@@ -60,8 +59,8 @@ public class OfflineNodeCommand extends CLICommand {
@Override
protected int run() throws Exception {
boolean errorOccurred = false;
final Jenkins jenkins = Jenkins.getInstance();
final HashSet<String> hs = new HashSet<String>(nodes);
final Jenkins jenkins = Jenkins.get();
final HashSet<String> hs = new HashSet<>(nodes);
List<String> names = null;
for (String node_s : hs) {
......
......@@ -56,7 +56,7 @@ public class OnlineNodeCommand extends CLICommand {
protected int run() throws Exception {
boolean errorOccurred = false;
final Jenkins jenkins = Jenkins.getActiveInstance();
final HashSet<String> hs = new HashSet<String>(nodes);
final HashSet<String> hs = new HashSet<>(nodes);
List<String> names = null;
for (String node_s : hs) {
......
......@@ -46,7 +46,7 @@ public class ReloadConfigurationCommand extends CLICommand {
@Override
protected int run() throws Exception {
Jenkins j = Jenkins.getInstance();
Jenkins j = Jenkins.get();
// Or perhaps simpler to inline the thread body of doReload?
j.doReload();
Object app;
......
......@@ -26,11 +26,9 @@ package hudson.cli;
import hudson.AbortException;
import hudson.Extension;
import hudson.model.AbstractItem;
import hudson.model.AbstractProject;
import hudson.model.Item;
import hudson.model.Items;
import hudson.model.TopLevelItem;
import jenkins.model.Jenkins;
import org.kohsuke.args4j.Argument;
......@@ -64,7 +62,7 @@ public class ReloadJobCommand extends CLICommand {
boolean errorOccurred = false;
final Jenkins jenkins = Jenkins.getActiveInstance();
final HashSet<String> hs = new HashSet<String>();
final HashSet<String> hs = new HashSet<>();
hs.addAll(jobs);
for (String job_s: hs) {
......
......@@ -31,7 +31,6 @@ import hudson.model.DirectlyModifiableView;
import hudson.model.View;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
/**
* @author ogondza
......
......@@ -47,6 +47,7 @@ import org.kohsuke.args4j.ClassParser;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.CmdLineException;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
......@@ -90,7 +91,7 @@ public class CLIRegisterer extends ExtensionFinder {
* Finds a resolved method annotated with {@link CLIResolver}.
*/
private Method findResolver(Class type) throws IOException {
List<Method> resolvers = Util.filter(Index.list(CLIResolver.class, Jenkins.getInstance().getPluginManager().uberClassLoader), Method.class);
List<Method> resolvers = Util.filter(Index.list(CLIResolver.class, Jenkins.get().getPluginManager().uberClassLoader), Method.class);
for ( ; type!=null; type=type.getSuperclass())
for (Method m : resolvers)
if (m.getReturnType()==type)
......@@ -98,12 +99,12 @@ public class CLIRegisterer extends ExtensionFinder {
return null;
}
private List<ExtensionComponent<CLICommand>> discover(final Jenkins hudson) {
private List<ExtensionComponent<CLICommand>> discover(@Nonnull final Jenkins jenkins) {
LOGGER.fine("Listing up @CLIMethod");
List<ExtensionComponent<CLICommand>> r = new ArrayList<ExtensionComponent<CLICommand>>();
List<ExtensionComponent<CLICommand>> r = new ArrayList<>();
try {
for ( final Method m : Util.filter(Index.list(CLIMethod.class, hudson.getPluginManager().uberClassLoader),Method.class)) {
for ( final Method m : Util.filter(Index.list(CLIMethod.class, jenkins.getPluginManager().uberClassLoader),Method.class)) {
try {
// command name
final String name = m.getAnnotation(CLIMethod.class).name();
......@@ -111,7 +112,7 @@ public class CLIRegisterer extends ExtensionFinder {
final ResourceBundleHolder res = loadMessageBundle(m);
res.format("CLI."+name+".shortDescription"); // make sure we have the resource, to fail early
r.add(new ExtensionComponent<CLICommand>(new CloneableCLICommand() {
r.add(new ExtensionComponent<>(new CloneableCLICommand() {
@Override
public String getName() {
return name;
......@@ -120,12 +121,12 @@ public class CLIRegisterer extends ExtensionFinder {
@Override
public String getShortDescription() {
// format by using the right locale
return res.format("CLI."+name+".shortDescription");
return res.format("CLI." + name + ".shortDescription");
}
@Override
protected CmdLineParser getCmdLineParser() {
return bindMethod(new ArrayList<MethodBinder>());
return bindMethod(new ArrayList<>());
}
private CmdLineParser bindMethod(List<MethodBinder> binders) {
......@@ -134,7 +135,7 @@ public class CLIRegisterer extends ExtensionFinder {
CmdLineParser parser = new CmdLineParser(null);
// build up the call sequence
Stack<Method> chains = new Stack<Method>();
Stack<Method> chains = new Stack<>();
Method method = m;
while (true) {
chains.push(method);
......@@ -146,15 +147,15 @@ public class CLIRegisterer extends ExtensionFinder {
try {
method = findResolver(type);
} catch (IOException ex) {
throw new RuntimeException("Unable to find the resolver method annotated with @CLIResolver for "+type, ex);
throw new RuntimeException("Unable to find the resolver method annotated with @CLIResolver for " + type, ex);
}
if (method==null) {
throw new RuntimeException("Unable to find the resolver method annotated with @CLIResolver for "+type);
if (method == null) {
throw new RuntimeException("Unable to find the resolver method annotated with @CLIResolver for " + type);
}
}
while (!chains.isEmpty())
binders.add(new MethodBinder(chains.pop(),this,parser));
binders.add(new MethodBinder(chains.pop(), this, parser));
return parser;
}
......@@ -199,7 +200,7 @@ public class CLIRegisterer extends ExtensionFinder {
this.stderr = stderr;
this.locale = locale;
List<MethodBinder> binders = new ArrayList<MethodBinder>();
List<MethodBinder> binders = new ArrayList<>();
CmdLineParser parser = bindMethod(binders);
try {
......@@ -207,7 +208,7 @@ public class CLIRegisterer extends ExtensionFinder {
Authentication old = sc.getAuthentication();
try {
// authentication
CliAuthenticator authenticator = Jenkins.getInstance().getSecurityRealm().createCliAuthenticator(this);
CliAuthenticator authenticator = Jenkins.get().getSecurityRealm().createCliAuthenticator(this);
new ClassParser().parse(authenticator, parser);
// fill up all the binders
......@@ -217,7 +218,7 @@ public class CLIRegisterer extends ExtensionFinder {
if (auth == Jenkins.ANONYMOUS)
auth = loadStoredAuthentication();
sc.setAuthentication(auth); // run the CLI with the right credential
hudson.checkPermission(Jenkins.READ);
jenkins.checkPermission(Jenkins.READ);
// resolve them
Object instance = null;
......
......@@ -57,7 +57,7 @@ public abstract class GenericItemOptionHandler<T extends Item> extends OptionHan
protected abstract Class<T> type();
@Override public int parseArguments(Parameters params) throws CmdLineException {
final Jenkins j = Jenkins.getInstance();
final Jenkins j = Jenkins.get();
final String src = params.getParameter(0);
T s = j.getItemByFullName(src, type());
if (s == null) {
......
......@@ -53,7 +53,7 @@ public class NodeOptionHandler extends OptionHandler<Node> {
String nodeName = params.getParameter(0);
final Node node = Jenkins.getInstance().getNode(nodeName);
final Node node = Jenkins.get().getNode(nodeName);
if (node == null) throw new IllegalArgumentException("No such node '" + nodeName + "'");
setter.addValue(node);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册