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.integrityservice.workflow;
23  
24  import java.io.IOException;
25  
26  import org.bitrepository.common.utils.ChecksumUtils;
27  import org.bitrepository.integrityservice.IntegrityServiceManager;
28  import org.bitrepository.integrityservice.reports.BasicIntegrityReporter;
29  import org.bitrepository.integrityservice.reports.IntegrityReporter;
30  import org.bitrepository.integrityservice.workflow.step.CreateStatisticsEntryStep;
31  import org.bitrepository.integrityservice.workflow.step.HandleChecksumValidationStep;
32  import org.bitrepository.integrityservice.workflow.step.HandleDeletedFilesStep;
33  import org.bitrepository.integrityservice.workflow.step.HandleMissingChecksumsStep;
34  import org.bitrepository.integrityservice.workflow.step.HandleMissingFilesStep;
35  import org.bitrepository.integrityservice.workflow.step.HandleObsoleteChecksumsStep;
36  import org.bitrepository.integrityservice.workflow.step.UpdateChecksumsStep;
37  import org.bitrepository.integrityservice.workflow.step.UpdateFileIDsStep;
38  import org.bitrepository.service.workflow.JobID;
39  import org.bitrepository.service.workflow.Workflow;
40  import org.bitrepository.service.workflow.WorkflowContext;
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  
44  /**
45   * Simple workflow for performing integrity checks of the system. 
46   * Starts by updating the file ids in the integrity model, followed by updating the checksums in the integrity model.
47   * Then the data is validated for integrity issues.
48   * And finally it is verified whether any missing or obsolete checksums can be found.
49   */
50  public abstract class IntegrityCheckWorkflow extends Workflow {
51      private final Logger log = LoggerFactory.getLogger(getClass());
52      /** The context for the workflow.*/
53      protected IntegrityWorkflowContext context;
54      protected String collectionID;
55      protected IntegrityReporter latestReport = null;
56      /**
57       * Remember to call the initialise method needs to be called before the start method.
58       */
59      public IntegrityCheckWorkflow() {}
60  
61      @Override
62      public void initialise(WorkflowContext context, String collectionID) {
63          this.context = (IntegrityWorkflowContext)context;
64          this.collectionID = collectionID;
65          jobID = new JobID(getClass().getSimpleName(), collectionID);
66      }
67      
68      protected abstract UpdateFileIDsStep getUpdateFileIDsStep();
69      
70      public IntegrityReporter getLatestIntegrityReport() {
71          return latestReport;
72      }
73      
74      @Override
75      public void start() {
76  
77          if (context == null) {
78              throw new IllegalStateException("The workflow can not be started before the initialise method has been " +
79                      "called.");
80          }
81          IntegrityReporter reporter = new BasicIntegrityReporter(jobID.getCollectionID(), jobID.getWorkflowName(),
82                  IntegrityServiceManager.getIntegrityReportStorageDir());
83          
84          super.start();
85          try {
86              UpdateFileIDsStep updateFileIDsStep = getUpdateFileIDsStep();
87              performStep(updateFileIDsStep);
88              
89              UpdateChecksumsStep updateChecksumStep = new UpdateChecksumsStep(
90                      context.getCollector(), context.getStore(), context.getAlerter(),
91                      ChecksumUtils.getDefault(context.getSettings()), context.getSettings(), collectionID);
92              performStep(updateChecksumStep);
93  
94              HandleDeletedFilesStep handleDeletedFilesStep = new HandleDeletedFilesStep(context.getStore(), reporter);
95              performStep(handleDeletedFilesStep);
96              
97              HandleMissingFilesStep handleMissingFilesStep = new HandleMissingFilesStep(context.getStore(),reporter);
98              performStep(handleMissingFilesStep);
99              
100             HandleChecksumValidationStep handleChecksumValidationStep 
101                     = new HandleChecksumValidationStep(context.getStore(), context.getAuditManager(), reporter);
102             performStep(handleChecksumValidationStep);
103             
104             HandleMissingChecksumsStep handleMissingChecksumsStep 
105                     = new HandleMissingChecksumsStep(context.getStore(), reporter);
106             performStep(handleMissingChecksumsStep);
107             
108             HandleObsoleteChecksumsStep handleObsoleteChecksumsStep 
109                     = new HandleObsoleteChecksumsStep(context.getSettings(), context.getStore(), reporter);
110             performStep(handleObsoleteChecksumsStep);
111             
112             CreateStatisticsEntryStep createStatistics = new CreateStatisticsEntryStep(
113                     context.getStore(), collectionID);
114             performStep(createStatistics);
115 
116             if(reporter.hasIntegrityIssues()) {
117                 context.getAlerter().integrityFailed(reporter.generateSummaryOfReport(), collectionID);
118             }
119             try {
120                 reporter.generateReport();
121             } catch (IOException e) {
122                 log.error("Failed to generate integrity report", e);
123             }
124             
125             latestReport = reporter;
126             
127         } finally {
128             finish();
129         }
130     }
131 }