From 842e2be11f7f2e3b7fe923c6a72acf5428142e33 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Wed, 17 Jan 2024 19:54:17 -0500 Subject: [PATCH] Gratuitous `@Inject` --- .../org/jenkinsci/main/modules/sshd/ItemListenerImpl.java | 6 ++---- .../org/jenkinsci/main/modules/sshd/PortAdvertiser.java | 5 +---- src/main/java/org/jenkinsci/main/modules/sshd/SSHD.java | 7 ++----- .../java/org/jenkinsci/main/modules/sshd/SSHDTest.java | 5 +---- 4 files changed, 6 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/jenkinsci/main/modules/sshd/ItemListenerImpl.java b/src/main/java/org/jenkinsci/main/modules/sshd/ItemListenerImpl.java index a347787..c5b59de 100644 --- a/src/main/java/org/jenkinsci/main/modules/sshd/ItemListenerImpl.java +++ b/src/main/java/org/jenkinsci/main/modules/sshd/ItemListenerImpl.java @@ -3,7 +3,6 @@ import hudson.Extension; import hudson.model.listeners.ItemListener; -import javax.inject.Inject; import java.util.logging.Level; import java.util.logging.Logger; @@ -12,12 +11,11 @@ */ @Extension public class ItemListenerImpl extends ItemListener { - @Inject - public SSHD sshd; + @Override public void onBeforeShutdown() { try { - sshd.stop(); + SSHD.get().stop(); } catch (Exception e) { LOGGER.log(Level.WARNING, "Failed to shutdown SSHD",e); } diff --git a/src/main/java/org/jenkinsci/main/modules/sshd/PortAdvertiser.java b/src/main/java/org/jenkinsci/main/modules/sshd/PortAdvertiser.java index 851d905..99bfb15 100644 --- a/src/main/java/org/jenkinsci/main/modules/sshd/PortAdvertiser.java +++ b/src/main/java/org/jenkinsci/main/modules/sshd/PortAdvertiser.java @@ -5,7 +5,6 @@ import hudson.model.PageDecorator; import jenkins.model.Jenkins; -import javax.inject.Inject; import java.net.URL; import java.util.logging.Level; import java.util.logging.Logger; @@ -17,13 +16,11 @@ */ @Extension public class PortAdvertiser extends PageDecorator { - @Inject - public SSHD sshd; @CheckForNull public String getEndpoint() { try { - int p = sshd.getActualPort(); + int p = SSHD.get().getActualPort(); if (p>0) { final Jenkins jenkins = Jenkins.get(); return (host != null ? host : new URL(jenkins.getRootUrl()).getHost()) + ":" + p; diff --git a/src/main/java/org/jenkinsci/main/modules/sshd/SSHD.java b/src/main/java/org/jenkinsci/main/modules/sshd/SSHD.java index 704a3d2..2a3d2b1 100644 --- a/src/main/java/org/jenkinsci/main/modules/sshd/SSHD.java +++ b/src/main/java/org/jenkinsci/main/modules/sshd/SSHD.java @@ -15,7 +15,6 @@ import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; -import javax.inject.Inject; import jenkins.model.GlobalConfiguration; import jenkins.model.GlobalConfigurationCategory; @@ -72,9 +71,6 @@ public GlobalConfigurationCategory getCategory() { @GuardedBy("this") private transient SshServer sshd; - @Inject - private transient InstanceIdentity identity; - private volatile int port = -1; public SSHD() { @@ -161,6 +157,7 @@ public synchronized void start() throws IOException, InterruptedException { sshd.setKeyPairProvider(new AbstractKeyPairProvider() { @Override public Iterable loadKeys(SessionContext session) throws IOException, GeneralSecurityException { + InstanceIdentity identity = InstanceIdentity.get(); return Collections.singletonList(new KeyPair(identity.getPublic(), identity.getPrivate())); } }); @@ -257,7 +254,7 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti private static final Logger LOGGER = Logger.getLogger(SSHD.class.getName()); public static SSHD get() { - return ExtensionList.lookup(GlobalConfiguration.class).get(SSHD.class); + return ExtensionList.lookupSingleton(SSHD.class); } @Initializer(after= InitMilestone.JOB_LOADED,fatal=false) diff --git a/src/test/java/org/jenkinsci/main/modules/sshd/SSHDTest.java b/src/test/java/org/jenkinsci/main/modules/sshd/SSHDTest.java index 0ce2c7d..d0e448c 100644 --- a/src/test/java/org/jenkinsci/main/modules/sshd/SSHDTest.java +++ b/src/test/java/org/jenkinsci/main/modules/sshd/SSHDTest.java @@ -28,7 +28,6 @@ import org.jvnet.hudson.test.JenkinsRule; import org.springframework.dao.DataAccessException; -import javax.inject.Inject; import java.io.IOException; import java.net.InetSocketAddress; import java.security.KeyPair; @@ -47,8 +46,6 @@ * @author Kohsuke Kawaguchi */ public class SSHDTest { - @Inject - SSHD sshd; @Rule public JenkinsRule j = new JenkinsRule(); @@ -63,7 +60,7 @@ public void setUp() { */ @Test public void configRoundtrip() throws Exception { - j.jenkins.getInjector().injectMembers(this); + SSHD sshd = SSHD.get(); for (int i : new int[]{-1,0,100}) { sshd.setPort(i);