View Javadoc

1   /*
2    * #%L
3    * Bitrepository Integrity Service
4    * %%
5    * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 2.1 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20   * #L%
21   */
22  package org.bitrepository.service.workflow;
23  
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  /**
28   * Abstract interface for running a SchedulableJob based on WorkflowSteps.
29   * @see SchedulableJob {@link SchedulableJob}
30   * @see WorkflowStep {@link WorkflowStep} 
31   */
32  public abstract class Workflow implements SchedulableJob {
33      /** The jobID */
34      protected JobID jobID;
35      /** The log.*/
36      private Logger log = LoggerFactory.getLogger(getClass());
37  
38      /** The current step running.*/
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       * Initiates the given step and sets it to the current running step.
50       * @param step The step to start.
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       * For telling that the workflow has finished its task.
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       * @return The statistics for this workflow.
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 }