PluginManager#whichPlugin does not work for JenkinsRule (#6982)

Co-authored-by: Jesse Glick <jglick@cloudbees.com>
This commit is contained in:
Basil CrowandJesse Glick authored and GitHub committed 2022-08-15 22:49:39 +01:00
1 parent 26c924b39d
commit a837997e68
2 files changed
+81

No files matched your search

@@ -79,7 +79,10 @@ import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.security.CodeSource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -1324,6 +1327,39 @@ public abstract class PluginManager extends AbstractModelObject implements OnMas
oneAndOnly = p;
}
}
if (oneAndOnly == null && Main.isUnitTest) {
// compare jenkins.security.ClassFilterImpl
CodeSource cs = c.getProtectionDomain().getCodeSource();
if (cs != null) {
URL loc = cs.getLocation();
if (loc != null) {
if ("file".equals(loc.getProtocol())) {
File file;
try {
file = Paths.get(loc.toURI()).toFile();
} catch (InvalidPathException | URISyntaxException e) {
LOGGER.log(Level.WARNING, "could not inspect " + loc, e);
return null;
}
if (file.isFile()) { // ignore directories
try (JarFile jf = new JarFile(file)) {
Manifest mf = jf.getManifest();
if (mf != null) {
java.util.jar.Attributes attr = mf.getMainAttributes();
if (attr.getValue("Plugin-Version") != null) {
String shortName = attr.getValue("Short-Name");
LOGGER.fine(() -> "found " + shortName + " for " + c);
return getPlugin(shortName);
}
}
} catch (IOException e) {
LOGGER.log(Level.WARNING, "could not inspect " + loc, e);
}
}
}
}
}
}
return oneAndOnly;
}