DelegatingComputerLauncher.java 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * The MIT License
 *
 * Copyright (c) 2010, InfraDNA, 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.slaves;

26
import hudson.RestrictedSince;
27
import hudson.model.Descriptor;
28
import hudson.model.Slave;
K
kohsuke 已提交
29
import hudson.model.TaskListener;
30

K
kohsuke 已提交
31
import java.io.IOException;
32 33
import java.util.ArrayList;
import java.util.List;
34 35 36 37 38
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import jenkins.model.Jenkins;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;
39 40

/**
41 42 43
 * Base implementation of {@link ComputerLauncher} that to be used by launchers that
 * perform some initialization (typically something cloud/v12n related
 * to power up the machine), and then delegate to another {@link ComputerLauncher}
44 45
 * to connect.
 *
46 47
 * <strong>If you are delegating to another {@link ComputerLauncher} you must extend this base class</strong>
 *
48 49 50 51 52 53 54 55 56 57 58 59 60 61
 * @author Kohsuke Kawaguchi
 * @since 1.382
 */
public abstract class DelegatingComputerLauncher extends ComputerLauncher {
    protected ComputerLauncher launcher;

    protected DelegatingComputerLauncher(ComputerLauncher launcher) {
        this.launcher = launcher;
    }

    public ComputerLauncher getLauncher() {
        return launcher;
    }

K
kohsuke 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    @Override
    public void launch(SlaveComputer computer, TaskListener listener) throws IOException, InterruptedException {
        getLauncher().launch(computer, listener);
    }

    @Override
    public void afterDisconnect(SlaveComputer computer, TaskListener listener) {
        getLauncher().afterDisconnect(computer, listener);
    }

    @Override
    public void beforeDisconnect(SlaveComputer computer, TaskListener listener) {
        getLauncher().beforeDisconnect(computer, listener);
    }

77 78 79 80
    public static abstract class DescriptorImpl extends Descriptor<ComputerLauncher> {
        /**
         * Returns the applicable nested computer launcher types.
         * The default implementation avoids all delegating descriptors, as that creates infinite recursion.
81
         * @since 2.12
82
         */
83 84
        public List<Descriptor<ComputerLauncher>> applicableDescriptors(@CheckForNull Slave it,
                                                                        @Nonnull Slave.SlaveDescriptor itDescriptor) {
85
            List<Descriptor<ComputerLauncher>> r = new ArrayList<>();
86 87 88 89 90 91 92 93 94 95 96 97 98 99
            for (Descriptor<ComputerLauncher> d : itDescriptor.computerLauncherDescriptors(it)) {
                if (DelegatingComputerLauncher.class.isAssignableFrom(d.getKlass().toJavaClass()))  continue;
                r.add(d);
            }
            return r;
        }
        /**
         * Returns the applicable nested computer launcher types.
         * The default implementation avoids all delegating descriptors, as that creates infinite recursion.
         * @deprecated use {@link #applicableDescriptors(Slave, Slave.SlaveDescriptor)}
         */
        @Deprecated
        @Restricted(DoNotUse.class)
        @RestrictedSince("2.12")
100
        public List<Descriptor<ComputerLauncher>> getApplicableDescriptors() {
101
            List<Descriptor<ComputerLauncher>> r = new ArrayList<>();
102
            for (Descriptor<ComputerLauncher> d :
103
                    Jenkins.get().getDescriptorList(ComputerLauncher.class)) {
104
                if (DelegatingComputerLauncher.class.isAssignableFrom(d.getKlass().toJavaClass()))  continue;
105 106 107 108 109 110 111
                r.add(d);
            }
            return r;
        }
    }

}