Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gratuitous @Inject #162

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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