Skip to content
This repository was archived by the owner on Feb 13, 2020. It is now read-only.

Commit

Permalink
Extract callback as a class
Browse files Browse the repository at this point in the history
  • Loading branch information
ikikko committed Dec 31, 2016
1 parent 336e5fa commit e52acdf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public TypetalkBuildWrapper(String name, String topicNumber, boolean notifyStart

@Override
public Environment setUp(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
BuildWrapperDelegate delegate = new BuildWrapperDelegate(name, Long.valueOf(topicNumber), listener, build);
final BuildWrapperDelegate delegate = new BuildWrapperDelegate(name, Long.valueOf(topicNumber), listener, build);
delegate.notifyStart(notifyStart, notifyStartMessage);

return new Environment() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,7 @@ public static class TypetalkBuildWrapperStepExecution extends AbstractStepExecut

@Override
public boolean start() throws Exception {
BuildWrapperDelegate delegate = new BuildWrapperDelegate(step.name, step.topicId, listener, run);

delegate.notifyStart(step.notifyStart, step.notifyStartMessage);
getContext().newBodyInvoker().withCallback(new BodyExecutionCallback.TailCall() {
@Override
protected void finished(StepContext context) throws Exception {
delegate.notifyEnd(step.notifyEnd, step.notifyEndMessage);
}
}).start();

getContext().newBodyInvoker().withCallback(new Callback(step, listener, run)).start();
return false;
}

Expand All @@ -131,4 +122,46 @@ public void stop(@Nonnull Throwable throwable) throws Exception {
}
}

public static class Callback extends BodyExecutionCallback {

private static final long serialVersionUID = 1L;

private transient final TypetalkBuildWrapperStep step;

private transient final BuildWrapperDelegate delegate;

Callback(TypetalkBuildWrapperStep step, TaskListener listener, Run run) {
this.step = step;
delegate = new BuildWrapperDelegate(step.name, step.topicId, listener, run);
}

@Override
public void onStart(StepContext context) {
try {
delegate.notifyStart(step.notifyStart, step.notifyStartMessage);
} catch (Exception x) {
context.onFailure(x);
}
}

@Override
public void onSuccess(StepContext context, Object result) {
try {
delegate.notifyEnd(step.notifyEnd, step.notifyEndMessage);
context.onSuccess(result);
} catch (Exception x) {
context.onFailure(x);
}
}

@Override
public void onFailure(StepContext context, Throwable t) {
try {
context.onFailure(t);
} catch (Exception x) {
context.onFailure(x);
}
}
}

}

0 comments on commit e52acdf

Please sign in to comment.