Skip to content

Commit

Permalink
Merge pull request #99 from basil/setcontextclassloader
Browse files Browse the repository at this point in the history
Use `jenkins.util.SetContextClassLoader` where possible
  • Loading branch information
jtnord authored Nov 5, 2024
2 parents 2670ef7 + dee0c81 commit 0332fda
Showing 1 changed file with 2 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import jenkins.model.Jenkins;
import jenkins.util.SetContextClassLoader;
import jenkins.util.Timer;
import org.acegisecurity.Authentication;
import org.kohsuke.accmod.Restricted;
Expand Down Expand Up @@ -97,8 +98,7 @@ KubernetesClient getKubernetesClient() {
if (client == null) {
ConfigBuilder cb = new ConfigBuilder();
Config config = cb.build();
// TODO post 2.362 use jenkins.util.SetContextClassLoader
try (WithContextClassLoader ignored = new WithContextClassLoader(getClass().getClassLoader())) {
try (SetContextClassLoader ignored = new SetContextClassLoader(KubernetesCredentialProvider.class)) {
client = new KubernetesClientBuilder().withConfig(config).build();
}
}
Expand Down Expand Up @@ -347,19 +347,4 @@ public CredentialsStore getStore(ModelObject object) {
public String getIconClassName() {
return "icon-credentials-kubernetes-store";
}

private static class WithContextClassLoader implements AutoCloseable {

private final ClassLoader previousClassLoader;

public WithContextClassLoader(ClassLoader classLoader) {
this.previousClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(classLoader);
}

@Override
public void close() {
Thread.currentThread().setContextClassLoader(previousClassLoader);
}
}
}

0 comments on commit 0332fda

Please sign in to comment.