From 848cf7832dd891afd437d57ac98a8f938b0e4fc0 Mon Sep 17 00:00:00 2001 From: Stefan Spieker Date: Mon, 2 Oct 2023 22:47:54 +0200 Subject: [PATCH] use Jenkins.get() instead of deprecated method and minor improvements --- .../com/dubture/jenkins/digitalocean/DigitalOceanCloud.java | 2 -- .../jenkins/digitalocean/DigitalOceanComputerLauncher.java | 5 +++-- src/main/java/com/dubture/jenkins/digitalocean/Slave.java | 4 ++-- .../com/dubture/jenkins/digitalocean/SlaveTemplate.java | 6 +++--- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/dubture/jenkins/digitalocean/DigitalOceanCloud.java b/src/main/java/com/dubture/jenkins/digitalocean/DigitalOceanCloud.java index 36b63923..7b68ea14 100644 --- a/src/main/java/com/dubture/jenkins/digitalocean/DigitalOceanCloud.java +++ b/src/main/java/com/dubture/jenkins/digitalocean/DigitalOceanCloud.java @@ -65,7 +65,6 @@ import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundSetter; import org.kohsuke.stapler.QueryParameter; -import org.kohsuke.stapler.StaplerRequest; import edu.umd.cs.findbugs.annotations.NonNull; import hudson.Extension; @@ -81,7 +80,6 @@ import hudson.util.Secret; import hudson.util.XStream2; import jenkins.model.Jenkins; -import net.sf.json.JSONObject; /** * The {@link DigitalOceanCloud} contains the main configuration values for running diff --git a/src/main/java/com/dubture/jenkins/digitalocean/DigitalOceanComputerLauncher.java b/src/main/java/com/dubture/jenkins/digitalocean/DigitalOceanComputerLauncher.java index 62eb4445..46ea56d3 100644 --- a/src/main/java/com/dubture/jenkins/digitalocean/DigitalOceanComputerLauncher.java +++ b/src/main/java/com/dubture/jenkins/digitalocean/DigitalOceanComputerLauncher.java @@ -45,6 +45,7 @@ import java.io.IOException; import java.io.PrintStream; import java.net.SocketTimeoutException; +import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Collection; @@ -208,7 +209,7 @@ public void onClosed(Channel channel, IOException cause) { } catch (Exception e) { LOGGER.log(Level.WARNING, e.getMessage(), e); try { - Jenkins.getInstance().removeNode(node); + Jenkins.get().removeNode(node); } catch (Exception ee) { ee.printStackTrace(logger); } @@ -242,7 +243,7 @@ private boolean runInitScript(final DigitalOceanComputer digitalOceanComputer, f } logger.println("Executing init script"); - scp.put(initScript.getBytes("UTF-8"), "init.sh", "/tmp", "0700"); + scp.put(initScript.getBytes(StandardCharsets.UTF_8), "init.sh", "/tmp", "0700"); Session session = conn.openSession(); session.requestDumbPTY(); // so that the remote side bundles stdout and stderr session.execCommand(buildUpCommand(digitalOceanComputer, "/tmp/init.sh")); diff --git a/src/main/java/com/dubture/jenkins/digitalocean/Slave.java b/src/main/java/com/dubture/jenkins/digitalocean/Slave.java index 534842fb..a0895120 100644 --- a/src/main/java/com/dubture/jenkins/digitalocean/Slave.java +++ b/src/main/java/com/dubture/jenkins/digitalocean/Slave.java @@ -125,7 +125,7 @@ public DigitalOceanComputer createComputer() { * @return the DigitalOceanCloud associated with the specified cloudName */ public DigitalOceanCloud getCloud() { - return (DigitalOceanCloud) Jenkins.getInstance().getCloud(cloudName); + return (DigitalOceanCloud) Jenkins.get().getCloud(cloudName); } /** @@ -133,7 +133,7 @@ public DigitalOceanCloud getCloud() { * @return the remote admin user, defaulting to "root" */ public String getRemoteAdmin() { - if (remoteAdmin == null || remoteAdmin.length() == 0) + if (remoteAdmin == null || remoteAdmin.isEmpty()) return "root"; return remoteAdmin; } diff --git a/src/main/java/com/dubture/jenkins/digitalocean/SlaveTemplate.java b/src/main/java/com/dubture/jenkins/digitalocean/SlaveTemplate.java index 35c2beb2..35d030ca 100644 --- a/src/main/java/com/dubture/jenkins/digitalocean/SlaveTemplate.java +++ b/src/main/java/com/dubture/jenkins/digitalocean/SlaveTemplate.java @@ -201,7 +201,7 @@ public boolean isInstanceCapReachedLocal(String cloudName) { LOGGER.log(Level.INFO, "agent limit check"); int count = 0; - List nodes = Jenkins.getInstance().getNodes(); + List nodes = Jenkins.get().getNodes(); for (Node n : nodes) { if (DropletName.isDropletInstanceOfSlave(n.getDisplayName(), cloudName, name)) { count++; @@ -258,7 +258,7 @@ public Slave provision(ProvisioningActivity.Id provisioningId, droplet.setSize(sizeId); droplet.setRegion(new Region(regionId)); droplet.setImage(DigitalOcean.newImage(imageId)); - droplet.setKeys(Arrays.asList(new Key(sshKeyId))); + droplet.setKeys(Collections.singletonList(new Key(sshKeyId))); droplet.setInstallMonitoring(installMonitoringAgent); droplet.setEnablePrivateNetworking( (usePrivateNetworking == null ? false : usePrivateNetworking) || (setupPrivateNetworking == null ? false : setupPrivateNetworking) @@ -484,7 +484,7 @@ public ListBoxModel doFillRegionIdItems(@RelativePath("..") @QueryParameter Stri @SuppressWarnings("unchecked") public Descriptor getDescriptor() { - return Jenkins.getInstance().getDescriptor(getClass()); + return Jenkins.get().getDescriptor(getClass()); } public String getName() {