diff --git a/core/src/main/java/hudson/PluginManager.java b/core/src/main/java/hudson/PluginManager.java index 231a68a3de..57247738d1 100644 --- a/core/src/main/java/hudson/PluginManager.java +++ b/core/src/main/java/hudson/PluginManager.java @@ -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; } diff --git a/test/src/test/java/hudson/PluginManagerWhichTest.java b/test/src/test/java/hudson/PluginManagerWhichTest.java new file mode 100644 index 0000000000..dea563300c --- /dev/null +++ b/test/src/test/java/hudson/PluginManagerWhichTest.java @@ -0,0 +1,45 @@ +/* + * The MIT License + * + * Copyright 2022 CloudBees, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package hudson; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import com.cloudbees.hudson.plugins.folder.Folder; +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; + +public class PluginManagerWhichTest { + + @Rule public JenkinsRule r = new JenkinsRule(); + + @Test + public void whichPlugin() throws Exception { + PluginWrapper expected = r.getPluginManager().getPlugin("cloudbees-folder"); + assertNotNull(expected); + assertEquals(expected, r.getPluginManager().whichPlugin(Folder.class)); + } +}