Skip to content

Commit

Permalink
[FIXED JENKINS-26156] Fixed BodyInvoker.withDisplayName implementatio…
Browse files Browse the repository at this point in the history
…n to allow a non-null argument.
  • Loading branch information
jglick committed Apr 8, 2016
1 parent 2ef59e1 commit ff4ab3e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 70 deletions.
Expand Up @@ -6,7 +6,6 @@
import com.cloudbees.groovy.cps.Outcome;
import com.cloudbees.groovy.cps.impl.CpsCallableInvocation;
import com.cloudbees.groovy.cps.impl.FunctionCallEnv;
import com.cloudbees.groovy.cps.impl.SourceLocation;
import com.cloudbees.groovy.cps.impl.TryBlockEnv;
import com.cloudbees.groovy.cps.sandbox.SandboxInvoker;
import com.google.common.util.concurrent.FutureCallback;
Expand All @@ -18,14 +17,12 @@
import org.jenkinsci.plugins.workflow.cps.nodes.StepEndNode;
import org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode;
import org.jenkinsci.plugins.workflow.cps.persistence.PersistIn;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.steps.BodyExecution;
import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback;
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepExecution;

import javax.annotation.CheckForNull;
import javax.annotation.concurrent.GuardedBy;
import java.io.IOException;
import java.util.Collection;
Expand All @@ -37,6 +34,7 @@
import java.util.logging.Logger;

import static java.util.logging.Level.*;
import javax.annotation.Nonnull;
import static org.jenkinsci.plugins.workflow.cps.persistence.PersistenceContext.*;

/**
Expand Down Expand Up @@ -86,15 +84,9 @@ class CpsBodyExecution extends BodyExecution {
@GuardedBy("this")
private Outcome outcome;

/**
* @see CpsBodyInvoker#createBodyBlockNode
*/
private final boolean createBodyBlockNode;

public CpsBodyExecution(CpsStepContext context, List<BodyExecutionCallback> callbacks, boolean createBodyBlockNode) {
CpsBodyExecution(CpsStepContext context, List<BodyExecutionCallback> callbacks) {
this.context = context;
this.callbacks = callbacks;
this.createBodyBlockNode = createBodyBlockNode;
}

/**
Expand All @@ -117,7 +109,7 @@ public CpsBodyExecution(CpsStepContext context, List<BodyExecutionCallback> call
sn.addAction(a);
}

StepContext sc = subContext(sn);
StepContext sc = new CpsBodySubContext(context, sn);
for (BodyExecutionCallback c : callbacks) {
c.onStart(sc);
}
Expand Down Expand Up @@ -289,7 +281,7 @@ public Next receive(Object o) {
en.addAction(new ErrorAction(t));

setOutcome(new Outcome(null,t));
StepContext sc = subContext(en);
StepContext sc = new CpsBodySubContext(context, en);
for (BodyExecutionCallback c : callbacks) {
c.onFailure(sc, t);
}
Expand All @@ -306,7 +298,7 @@ public Next receive(Object o) {
StepEndNode en = addBodyEndFlowNode();

setOutcome(new Outcome(o,null));
StepContext sc = subContext(en);
StepContext sc = new CpsBodySubContext(context, en);
for (BodyExecutionCallback c : callbacks) {
c.onSuccess(sc, o);
}
Expand All @@ -317,58 +309,38 @@ public Next receive(Object o) {
private static final long serialVersionUID = 1L;
}

/**
* Creates a sub-context to call {@link BodyExecutionCallback}.
* If {@link #createBodyBlockNode} is false, then we don't have distinctive
* {@link FlowNode}, so we just hand out the master context.
*/
private StepContext subContext(FlowNode n) {
if (n==null)
return context;
else
return new CpsBodySubContext(context,n);
}

/**
* Inserts the flow node that indicates the beginning of the body invocation.
*
* @see #addBodyEndFlowNode()
*/
private @CheckForNull StepStartNode addBodyStartFlowNode(FlowHead head) {
if (createBodyBlockNode) {
StepStartNode start = new StepStartNode(head.getExecution(),
context.getStepDescriptor(), head.get());
this.startNodeId = start.getId();
start.addAction(new BodyInvocationAction());
head.setNewHead(start);
return start;
} else {
return null;
}
private @Nonnull StepStartNode addBodyStartFlowNode(FlowHead head) {
StepStartNode start = new StepStartNode(head.getExecution(),
context.getStepDescriptor(), head.get());
this.startNodeId = start.getId();
start.addAction(new BodyInvocationAction());
head.setNewHead(start);
return start;
}

/**
* Inserts the flow node that indicates the beginning of the body invocation.
*
* @see #addBodyStartFlowNode(FlowHead)
*/
private @CheckForNull StepEndNode addBodyEndFlowNode() {
if (createBodyBlockNode) {
try {
FlowHead head = CpsThread.current().head;

StepEndNode end = new StepEndNode(head.getExecution(),
getBodyStartNode(), head.get());
end.addAction(new BodyInvocationAction());
head.setNewHead(end);

return end;
} catch (IOException e) {
LOGGER.log(WARNING, "Failed to grow the flow graph", e);
throw new Error(e);
}
} else {
return null;
private @Nonnull StepEndNode addBodyEndFlowNode() {
try {
FlowHead head = CpsThread.current().head;

StepEndNode end = new StepEndNode(head.getExecution(),
getBodyStartNode(), head.get());
end.addAction(new BodyInvocationAction());
head.setNewHead(end);

return end;
} catch (IOException e) {
LOGGER.log(WARNING, "Failed to grow the flow graph", e);
throw new Error(e);
}
}

Expand Down
Expand Up @@ -27,18 +27,16 @@
import com.google.common.util.concurrent.FutureCallback;
import hudson.model.Action;
import org.jenkinsci.plugins.workflow.actions.LabelAction;
import org.jenkinsci.plugins.workflow.cps.nodes.StepEndNode;
import org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode;
import org.jenkinsci.plugins.workflow.cps.persistence.PersistIn;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback;
import org.jenkinsci.plugins.workflow.steps.BodyInvoker;

import javax.annotation.Nullable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.annotation.Nonnull;

import static org.jenkinsci.plugins.workflow.cps.persistence.PersistenceContext.*;

Expand Down Expand Up @@ -67,11 +65,6 @@ public final class CpsBodyInvoker extends BodyInvoker {

private String displayName;

/**
* If false, do not create inner {@link StepStartNode}/{@link StepEndNode}.
*/
private boolean createBodyBlockNode = true;

/**
* Set to non-null once {@linkplain #start() started}.
*/
Expand Down Expand Up @@ -106,9 +99,8 @@ public CpsBodyInvoker withCallback(BodyExecutionCallback callback) {
}

@Override
public CpsBodyInvoker withDisplayName(@Nullable String name) {
public CpsBodyInvoker withDisplayName(@Nonnull String name) {
this.displayName = name;
createBodyBlockNode = (name==null);
return this;
}

Expand All @@ -120,16 +112,11 @@ public CpsBodyInvoker withDisplayName(@Nullable String name) {
@Override
public CpsBodyExecution start() {
if (execution!=null) throw new IllegalStateException("Already started");
execution = new CpsBodyExecution(owner, callbacks, createBodyBlockNode);
execution = new CpsBodyExecution(owner, callbacks);

if (displayName!=null)
startNodeActions.add(new LabelAction(displayName));

if (!createBodyBlockNode) {
if (!startNodeActions.isEmpty())
throw new IllegalStateException("Can't specify Actions if there will be no StepStartNode");
}

if (owner.isCompleted()) {
// if this step is already done, no further body invocations can happen doing so will end up
// causing two CpsThreads competing on the same FlowHead.
Expand Down
Expand Up @@ -82,7 +82,7 @@ public static class Execution extends AbstractStepExecutionImpl {
context.newBodyInvoker().
withContexts(EnvironmentExpander.merge(context.get(EnvironmentExpander.class), new ExpanderImpl(this))).
withCallback(BodyExecutionCallback.wrap(context)).
withDisplayName(null).start();
start();
return false;
}
@Override public void onResume() {
Expand Down

0 comments on commit ff4ab3e

Please sign in to comment.