View Javadoc

1   /*
2    * #%L
3    * Bitrepository Audit Trail Service
4    * 
5    * $Id$
6    * $HeadURL$
7    * %%
8    * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser General Public License as 
12   * published by the Free Software Foundation, either version 2.1 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Lesser Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Lesser Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
23   * #L%
24   */
25  package org.bitrepository.audittrails.collector;
26  
27  import java.util.Date;
28  import java.util.HashMap;
29  import java.util.Map;
30  import java.util.Timer;
31  import java.util.TimerTask;
32  
33  import access.getaudittrails.AuditTrailClient;
34  import org.bitrepository.audittrails.store.AuditTrailStore;
35  import org.bitrepository.common.ArgumentValidator;
36  import org.bitrepository.common.settings.Settings;
37  import org.bitrepository.common.utils.SettingsUtils;
38  import org.bitrepository.common.utils.TimeUtils;
39  import org.bitrepository.settings.repositorysettings.Collection;
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  
43  /**
44   * Manages the retrieval of of AuditTrails from contributors.
45   */
46  public class AuditTrailCollector {
47      private Logger log = LoggerFactory.getLogger(getClass());
48      /** The task for collecting the audits.*/
49      private final Map<String, AuditTrailCollectionTimerTask> collectorTasks = new HashMap<String, 
50              AuditTrailCollectionTimerTask>();
51      /** The timer for keeping track of the collecting task.*/
52      private Timer timer;
53      private final Settings settings;
54  
55      /** Initial grace period in milliseconds after startup to allow the system to finish startup. */
56      private static final int DEFAULT_GRACE_PERIOD = 0;
57  
58      /**
59       * @param settings The settings for this collector.
60       * @param client The client for handling the conversation for collecting the audit trails.
61       * @param store The storage of the audit trails data.
62       */
63      public AuditTrailCollector(Settings settings, AuditTrailClient client, AuditTrailStore store) {
64          ArgumentValidator.checkNotNull(settings, "settings");
65          ArgumentValidator.checkNotNull(client, "AuditTrailClient client");
66          ArgumentValidator.checkNotNull(store, "AuditTrailStore store");
67  
68          this.settings = settings;
69          this.timer = new Timer(true);
70          long collectionInterval = settings.getReferenceSettings().getAuditTrailServiceSettings().getCollectAuditInterval();
71          
72          for(Collection c : settings.getRepositorySettings().getCollections().getCollection()) {
73              IncrementalCollector collector = new IncrementalCollector(c.getID(),
74                      settings.getReferenceSettings().getAuditTrailServiceSettings().getID(),
75                      client, store,
76                      settings.getReferenceSettings().getAuditTrailServiceSettings().getMaxNumberOfEventsInRequest());
77              AuditTrailCollectionTimerTask collectorTask = new AuditTrailCollectionTimerTask( 
78                      collector, collectionInterval);
79              log.info("Will start collection of audit trail every " +
80                      TimeUtils.millisecondsToHuman(collectionInterval) + ", " +
81                      "after a grace period of " + TimeUtils.millisecondsToHuman(getGracePeriod()));
82              timer.scheduleAtFixedRate(collectorTask, getGracePeriod(), collectionInterval/10);
83              collectorTasks.put(c.getID(), collectorTask);
84          }
85      }
86      
87      /**
88       * Instantiates a collection of all the newest audit trails.
89       */
90      public void collectNewestAudits(String collectionID) {
91          collectorTasks.get(collectionID).runCollection();
92      }
93  
94      /**
95       * @return The time to wait before starting collection of audit trails. This enables the system to have time to
96       * finish startup before they have to start delivering/process audit trails.
97       */
98      private int getGracePeriod() {
99          if (settings.getReferenceSettings().getAuditTrailServiceSettings().isSetGracePeriod()) {
100             return settings.getReferenceSettings().getAuditTrailServiceSettings().getGracePeriod().intValue();
101         } else {
102             return DEFAULT_GRACE_PERIOD;
103         }
104     }
105     
106     /**
107      * Closes the AuditTrailCollector.
108      */
109     public void close() {
110         for(AuditTrailCollectionTimerTask atctt : collectorTasks.values()) {
111             atctt.cancel();
112         }
113         timer.cancel();
114     }
115 
116     /**
117      * Timer task for keeping track of the automated collecting of audit trails.
118      */
119     private class AuditTrailCollectionTimerTask extends TimerTask {
120         /** The interval between running this timer task.*/
121         private final long interval;
122         /** The date for the next run.*/
123         private Date nextRun;
124         private final IncrementalCollector collector;
125         
126         /**
127          * @param interval The interval between running this timer task.
128          */
129         private AuditTrailCollectionTimerTask(IncrementalCollector collector, long interval) {
130             this.collector = collector;
131             this.interval = interval;
132             nextRun = new Date(System.currentTimeMillis() + getGracePeriod());
133             log.info("Scheduled next collection of audit trails for " + nextRun);
134         }
135         
136         /**
137          * Run the operation and when finished set the date for the next collection.
138          */
139         public synchronized void runCollection() {
140             collector.performCollection(SettingsUtils.getAuditContributorsForCollection(collector.getCollectionID()));
141             nextRun = new Date(System.currentTimeMillis() + interval);
142             log.info("Scheduled next collection of audit trails for " + nextRun);
143         }
144 
145         @Override
146         public void run() {
147             if(nextRun.getTime() < System.currentTimeMillis()) {
148                 runCollection();
149             }
150         }
151     }
152 }