|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | +package hudson.remoting; |
| 25 | + |
| 26 | +import edu.umd.cs.findbugs.annotations.CheckForNull; |
| 27 | +import java.io.IOException; |
| 28 | +import java.net.URL; |
| 29 | +import org.jenkinsci.remoting.Role; |
| 30 | +import org.jenkinsci.remoting.RoleChecker; |
| 31 | + |
| 32 | +/** |
| 33 | + * {@link Callable} used to deliver a jar file to {@link RemoteClassLoader}. |
| 34 | + * <p> |
| 35 | + * This replaces {@link hudson.remoting.PreloadJarTask} and delivers the jar contents as part of the Callable rather |
| 36 | + * than needing to call {@link hudson.remoting.RemoteClassLoader#prefetch(java.net.URL)}. |
| 37 | + * </p> |
| 38 | + * @since TODO 2024-08 |
| 39 | + */ |
| 40 | +final class PreloadJarTask2 implements DelegatingCallable<Boolean, IOException> { |
| 41 | + /** |
| 42 | + * Jar file to be preloaded. |
| 43 | + */ |
| 44 | + private final URL[] jars; |
| 45 | + |
| 46 | + private final byte[][] contents; |
| 47 | + |
| 48 | + // TODO: This implementation exists starting from |
| 49 | + // https://github.com/jenkinsci/remoting/commit/f3d0a81fdf46a10c3c6193faf252efaeaee98823 |
| 50 | + // Since this time nothing has blown up, but it still seems to be suspicious. |
| 51 | + // The solution for null classloaders is available in RemoteDiagnostics.Script#call() in the Jenkins core codebase |
| 52 | + @CheckForNull |
| 53 | + private transient ClassLoader target = null; |
| 54 | + |
| 55 | + PreloadJarTask2(URL[] jars, byte[][] contents, @CheckForNull ClassLoader target) { |
| 56 | + if (jars.length != contents.length) { |
| 57 | + throw new IllegalArgumentException("Got " + jars.length + " jars and " + contents.length + " contents"); |
| 58 | + } |
| 59 | + this.jars = jars; |
| 60 | + this.contents = contents; |
| 61 | + this.target = target; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public ClassLoader getClassLoader() { |
| 66 | + return target; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public Boolean call() throws IOException { |
| 71 | + ClassLoader cl = Thread.currentThread().getContextClassLoader(); |
| 72 | + |
| 73 | + try { |
| 74 | + if (!(cl instanceof RemoteClassLoader)) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + final RemoteClassLoader rcl = (RemoteClassLoader) cl; |
| 78 | + |
| 79 | + boolean r = false; |
| 80 | + for (int i = 0; i < jars.length; i++) { |
| 81 | + r |= rcl.prefetch(jars[i], contents[i]); |
| 82 | + } |
| 83 | + return r; |
| 84 | + } catch (IllegalAccessError iae) { |
| 85 | + // Catch the IAE instead of letting it be wrapped by remoting to suppress warnings logged on the agent-side |
| 86 | + throw new IOException(iae); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * This task is only useful in the context that allows remote classloading, and by that point |
| 92 | + * any access control check is pointless. So just declare the worst possible role. |
| 93 | + */ |
| 94 | + @Override |
| 95 | + public void checkRoles(RoleChecker checker) throws SecurityException { |
| 96 | + checker.check(this, Role.UNKNOWN); |
| 97 | + } |
| 98 | + |
| 99 | + private static final long serialVersionUID = -773448303394727271L; |
| 100 | +} |
0 commit comments