(→AHPSCRIPTS-133)
|
(→AHPSCRIPTS-122)
|
Line 2615: | |||
BuildService.getInstance().runWorkflow(newBuildRequest);</pre> | BuildService.getInstance().runWorkflow(newBuildRequest);</pre> | ||
+ | = Run Originating Workflow and Wait Until Workflow is Finished = | ||
+ | ==== AHPSCRIPTS-132 ==== | ||
+ | <pre>import com.urbancode.anthill3.domain.buildrequest.BuildRequest; | ||
+ | import com.urbancode.anthill3.domain.project.Project; | ||
+ | import com.urbancode.anthill3.domain.project.ProjectFactory; | ||
+ | import com.urbancode.anthill3.domain.security.UserFactory; | ||
+ | import com.urbancode.anthill3.domain.workflow.WorkflowCase; | ||
+ | import com.urbancode.anthill3.domain.workflow.WorkflowEndEvent; | ||
+ | import com.urbancode.anthill3.domain.workflow.WorkflowEvent; | ||
+ | import com.urbancode.anthill3.persistence.UnitOfWork; | ||
+ | import com.urbancode.anthill3.services.build.BuildService; | ||
+ | import com.urbancode.anthill3.services.event.EventListener; | ||
+ | import com.urbancode.anthill3.services.event.EventService; | ||
+ | import com.urbancode.anthill3.services.event.criteria.Criteria; | ||
+ | import com.urbancode.anthill3.services.event.criteria.FieldEqualsCriteria; | ||
+ | import com.urbancode.anthill3.persistence.UnitOfWork; | ||
+ | import com.urbancode.anthill3.domain.buildrequest.*; | ||
+ | import com.urbancode.anthill3.domain.project.*; | ||
+ | import com.urbancode.anthill3.domain.security.*; | ||
+ | import com.urbancode.anthill3.services.build.*; | ||
+ | import java.util.EventObject; | ||
+ | class WorkflowEndEventListener implements EventListener { | ||
+ | |||
+ | private BuildRequest request; | ||
+ | private boolean success = false; | ||
+ | |||
+ | WorkflowEndEventListener(BuildRequest request) { | ||
+ | this.request = request; | ||
+ | } | ||
+ | |||
+ | public boolean isSuccess(){ | ||
+ | return success; | ||
+ | } | ||
+ | |||
+ | synchronized public void handleEvent(EventObject event) { | ||
+ | if (!(event instanceof WorkflowEvent)) { | ||
+ | throw new IllegalArgumentException( "The parameter event must be an instance of a WorkflowEvent!" ); | ||
+ | } | ||
+ | UnitOfWork uow = null; | ||
+ | try { | ||
+ | uow = UnitOfWork.create(UserFactory.getSystemUser()); | ||
+ | WorkflowCase wcase = ((WorkflowEvent) event).getCase(); | ||
+ | success = (wcase.getStatus() != null && wcase.getStatus().isSuccess()); | ||
+ | } | ||
+ | catch (Exception e) { | ||
+ | throw new RuntimeException(e.getMessage(), e); | ||
+ | } | ||
+ | finally { | ||
+ | if (uow != null) { | ||
+ | uow.close(); | ||
+ | } | ||
+ | } | ||
+ | this.notify(); | ||
+ | } | ||
+ | public Class getEventClass() { | ||
+ | return WorkflowEndEvent.class; | ||
+ | } | ||
+ | public Criteria[] getCriteria() { | ||
+ | return new Criteria[]{ | ||
+ | new FieldEqualsCriteria("request", request) | ||
+ | }; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | buildAndWait(String projectName, String workflowName) | ||
+ | throws Exception { | ||
+ | project = ProjectFactory.getInstance().restoreForName(projectName); | ||
+ | wflow = project.getWorkflow(workflowName); | ||
+ | buildService = BuildService.getInstance(); | ||
+ | user = UnitOfWork.getCurrent().getUser(); | ||
+ | request = new BuildRequest(wflow.getBuildProfile(), user, RequestSourceEnum.REPOSITORY, user ); | ||
+ | request.setForcedFlag(true); | ||
+ | request.store(); | ||
+ | UnitOfWork.getCurrent().commit(); | ||
+ | listener = new WorkflowEndEventListener(request); | ||
+ | EventService.getInstance().registerEventListener(listener); | ||
+ | try { | ||
+ | BuildService.getInstance().runBuild(request); | ||
+ | synchronized (listener) { | ||
+ | listener.wait(); | ||
+ | } | ||
+ | if (!listener.isSuccess()) { | ||
+ | throw new Exception(projectName + " - " + workflowName + " failed."); | ||
+ | } | ||
+ | } | ||
+ | finally { | ||
+ | EventService.getInstance().removeEventListener(listener); | ||
+ | } | ||
+ | } | ||
+ | buildAndWait("project", "workflow");</pre> |