Skip to content

Commit

Permalink
Merge pull request #162 from jglick/inject
Browse files Browse the repository at this point in the history
Gratuitous `@Inject`
  • Loading branch information
jglick authored Jan 18, 2024
2 parents 31688d0 + 842e2be commit 159e91f
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/org/jenkinsci/main/modules/sshd/SSHD.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -161,6 +157,7 @@ public synchronized void start() throws IOException, InterruptedException {
sshd.setKeyPairProvider(new AbstractKeyPairProvider() {
@Override
public Iterable<KeyPair> loadKeys(SessionContext session) throws IOException, GeneralSecurityException {
InstanceIdentity identity = InstanceIdentity.get();
return Collections.singletonList(new KeyPair(identity.getPublic(), identity.getPrivate()));
}
});
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions src/test/java/org/jenkinsci/main/modules/sshd/SSHDTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -47,8 +46,6 @@
* @author Kohsuke Kawaguchi
*/
public class SSHDTest {
@Inject
SSHD sshd;

@Rule
public JenkinsRule j = new JenkinsRule();
Expand All @@ -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);
Expand Down

0 comments on commit 159e91f

Please sign in to comment.