1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.bitrepository.service.workflow;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27
28
29
30
31
32 public abstract class Workflow implements SchedulableJob {
33
34 protected JobID jobID;
35
36 private Logger log = LoggerFactory.getLogger(getClass());
37
38
39 private WorkflowStep currentStep = null;
40 private WorkflowStatistic statistics;
41
42 @Override
43 public synchronized void start() {
44 statistics = new WorkflowStatistic(getClass().getSimpleName());
45 statistics.start();
46 }
47
48
49
50
51
52 protected void performStep(WorkflowStep step) {
53 this.currentStep = step;
54 log.info("Starting step: '" + step.getName() + "'");
55 try {
56 statistics.startSubStatistic(step.getName());
57 step.performStep();
58 statistics.finishSubStatistic();
59 log.info(statistics.getCurrentSubStatistic().toString());
60
61 } catch (Exception e) {
62 log.error("Failure in step: '" + step.getName() + "'.", e);
63 throw new RuntimeException("Failed to run step " + step.getName(), e);
64 }
65 }
66
67
68
69
70 protected void finish() {
71 statistics.finish();
72 this.currentStep = null;
73 log.info(statistics.getFullStatistics());
74 }
75
76 @Override
77 public String currentState() {
78 if(currentStep == null) {
79 return NOT_RUNNING;
80 } else {
81 return currentStep.getName();
82 }
83 }
84
85
86
87
88 public synchronized WorkflowStatistic getWorkflowStatistics() {
89 if(statistics == null) {
90 statistics = new WorkflowStatistic(getClass().getSimpleName());
91 }
92 return statistics;
93 }
94
95 @Override
96 public JobID getJobID() {
97 return jobID;
98 }
99
100 @Override
101 public boolean equals(Object o) {
102 if (this == o) return true;
103 Workflow that = (Workflow) o;
104
105 if (!jobID.equals(that.jobID)) return false;
106
107 return true;
108 }
109
110 @Override
111 public int hashCode() {
112 return jobID.hashCode();
113 }
114 }