From 5b4ff26a787e3d2a68963dfa7f5f254733488061 Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Wed, 26 Oct 2016 11:01:39 +0100 Subject: [PATCH] [JENKINS-39232] Make vSphereCloudLauncher inherit from DelegatingComputerLauncher --- .../plugins/vSphereCloudLauncher.java | 62 ++++++++++++++----- .../jenkinsci/plugins/vSphereCloudSlave.java | 2 +- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/vSphereCloudLauncher.java b/src/main/java/org/jenkinsci/plugins/vSphereCloudLauncher.java index 254e123b..a80c3d35 100644 --- a/src/main/java/org/jenkinsci/plugins/vSphereCloudLauncher.java +++ b/src/main/java/org/jenkinsci/plugins/vSphereCloudLauncher.java @@ -8,9 +8,11 @@ import hudson.model.TaskListener; import hudson.model.Descriptor; import hudson.slaves.ComputerLauncher; +import hudson.slaves.DelegatingComputerLauncher; import hudson.slaves.SlaveComputer; import java.io.IOException; +import java.io.ObjectStreamException; import java.util.Calendar; import org.jenkinsci.plugins.vsphere.tools.VSphere; @@ -28,9 +30,10 @@ * * @author Admin */ -public class vSphereCloudLauncher extends ComputerLauncher { +public class vSphereCloudLauncher extends DelegatingComputerLauncher { - private final ComputerLauncher delegate; + @Deprecated + private transient ComputerLauncher delegate; private final Boolean overrideLaunchSupported; private final String vsDescription; private final String vmName; @@ -51,13 +54,12 @@ public enum MACHINE_ACTION { } @DataBoundConstructor - public vSphereCloudLauncher(ComputerLauncher delegate, + public vSphereCloudLauncher(ComputerLauncher launcher, String vsDescription, String vmName, Boolean overrideLaunchSupported, Boolean waitForVMTools, String snapName, String launchDelay, String idleOption, String LimitedTestRunCount) { - super(); - this.delegate = delegate; + super(launcher); this.overrideLaunchSupported = overrideLaunchSupported; this.vsDescription = vsDescription; this.vmName = vmName; @@ -82,6 +84,33 @@ public vSphereCloudLauncher(ComputerLauncher delegate, this.LimitedTestRunCount = Util.tryParseNumber(LimitedTestRunCount, 0).intValue(); } + private vSphereCloudLauncher(ComputerLauncher launcher, Boolean overrideLaunchSupported, String vsDescription, + String vmName, Boolean waitForVMTools, String snapName, int launchDelay, + MACHINE_ACTION idleAction, int limitedTestRunCount) { + super(launcher); + this.overrideLaunchSupported = overrideLaunchSupported; + this.vsDescription = vsDescription; + this.vmName = vmName; + this.waitForVMTools = waitForVMTools; + this.snapName = snapName; + this.launchDelay = launchDelay; + this.idleAction = idleAction; + LimitedTestRunCount = limitedTestRunCount; + } + + /** + * Migrates instances from the old parent class to the new parent class. + * @return the deserialized instance. + * @throws ObjectStreamException if something went wrong. + */ + private Object readResolve() throws ObjectStreamException { + if (delegate != null) { + return new vSphereCloudLauncher(delegate, overrideLaunchSupported, vsDescription, vmName, waitForVMTools, + snapName, launchDelay, idleAction, LimitedTestRunCount); + } + return this; + } + public vSphereCloud findOurVsInstance() throws RuntimeException { if (vsDescription != null && vmName != null) { for (vSphereCloud cloud : vSphereCloud.findAllVsphereClouds()) { @@ -193,15 +222,15 @@ public void launch(SlaveComputer slaveComputer, TaskListener taskListener) /* At this point we have told vSphere to get the VM going. * Now we wait our launch delay amount before trying to connect. */ - if (delegate.isLaunchSupported()) { + if (launcher.isLaunchSupported()) { if (launchDelay > 0) { vSphereCloud.Log(slaveComputer, taskListener, "Waiting for " + launchDelay - + " seconds before asking " + delegate + " to launch slave."); + + " seconds before asking " + launcher + " to launch slave."); // Delegate is going to do launch. Thread.sleep(launchDelay * 1000); } - vSphereCloud.Log(slaveComputer, taskListener, "Asking " + delegate.getClass().getSimpleName() + " to launch slave."); - delegate.launch(slaveComputer, taskListener); + vSphereCloud.Log(slaveComputer, taskListener, "Asking " + launcher.getClass().getSimpleName() + " to launch slave."); + super.launch(slaveComputer, taskListener); } else { vSphereCloud.Log(slaveComputer, taskListener, "Waiting for up to " + launchDelay + " seconds for slave to come online."); @@ -263,7 +292,7 @@ public synchronized void afterDisconnect(SlaveComputer slaveComputer, TaskListen VSphere v = null; try { vSphereCloud.Log(slaveComputer, taskListener, "Running disconnect procedure..."); - delegate.afterDisconnect(slaveComputer, taskListener); + super.afterDisconnect(slaveComputer, taskListener); vSphereCloud.Log(slaveComputer, taskListener, "Shutting down Virtual Machine..."); MACHINE_ACTION localIdle = idleAction; if (localIdle == null) { @@ -323,8 +352,12 @@ public synchronized void afterDisconnect(SlaveComputer slaveComputer, TaskListen } } + /** + * @deprecated use {@link #getLauncher()} + */ + @Deprecated public ComputerLauncher getDelegate() { - return delegate; + return launcher; } public String getVmName() { @@ -354,17 +387,12 @@ public Integer getLimitedTestRunCount() { @Override public boolean isLaunchSupported() { if (this.overrideLaunchSupported == null) { - return delegate.isLaunchSupported(); + return launcher.isLaunchSupported(); } else { return overrideLaunchSupported; } } - @Override - public void beforeDisconnect(SlaveComputer slaveComputer, TaskListener taskListener) { - delegate.beforeDisconnect(slaveComputer, taskListener); //this call does nothing. - } - @Override public Descriptor getDescriptor() { // Don't allow creation of launcher from UI diff --git a/src/main/java/org/jenkinsci/plugins/vSphereCloudSlave.java b/src/main/java/org/jenkinsci/plugins/vSphereCloudSlave.java index 02667060..44a2aee2 100644 --- a/src/main/java/org/jenkinsci/plugins/vSphereCloudSlave.java +++ b/src/main/java/org/jenkinsci/plugins/vSphereCloudSlave.java @@ -323,7 +323,7 @@ public boolean EndLimitedTestRun(Run r) { * @return original launcher */ public ComputerLauncher getDelegateLauncher() { - return ((vSphereCloudLauncher) getLauncher()).getDelegate(); + return ((vSphereCloudLauncher) getLauncher()).getLauncher(); } @Extension