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;
26  
27  import java.util.Date;
28  import javax.jms.JMSException;
29  
30  import org.bitrepository.audittrails.collector.AuditTrailCollector;
31  import org.bitrepository.audittrails.store.AuditTrailStore;
32  import org.bitrepository.bitrepositoryelements.FileAction;
33  import org.bitrepository.common.ArgumentValidator;
34  import org.bitrepository.common.settings.Settings;
35  import org.bitrepository.protocol.messagebus.MessageBus;
36  import org.bitrepository.protocol.messagebus.MessageBusManager;
37  import org.bitrepository.service.LifeCycledService;
38  import org.bitrepository.service.contributor.ContributorMediator;
39  import org.bitrepository.audittrails.store.AuditEventIterator;
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  
43  /**
44   * Class to expose the functionality of the AuditTrailService. 
45   * Aggregates the needed classes.   
46   */
47  public class AuditTrailService implements LifeCycledService {
48      private final Logger log = LoggerFactory.getLogger(getClass());
49      /** The storage of audit trail information.*/
50      private final AuditTrailStore store;
51      /** The collector of new audit trails.*/
52      private final AuditTrailCollector collector;
53      /** The mediator for handling the messages.*/
54      private final ContributorMediator mediator;
55      /** The settings.*/
56      private final Settings settings;
57  
58      /**
59       * Constructor.
60       * @param store The store for the audit trail data.
61       * @param collector The collector of new audit trail data.
62       * @param mediator The mediator for the communication of this contributor.
63       * @param settings The AuditTrailService settings.
64       */
65      public AuditTrailService(
66              AuditTrailStore store,
67              AuditTrailCollector collector,
68              ContributorMediator mediator,
69              Settings settings) {
70          ArgumentValidator.checkNotNull(collector, "AuditTrailCollector collector");
71          ArgumentValidator.checkNotNull(store, "AuditTrailStore store");
72          ArgumentValidator.checkNotNull(mediator, "ContributorMediator mediator");
73          ArgumentValidator.checkNotNull(settings, "Settings settings");
74  
75          this.store = store;
76          this.collector = collector;
77          this.mediator = mediator;
78          this.settings = settings;
79  
80          mediator.start();
81      }
82      
83      /**
84       * Retrieve an iterator to all AuditTrailEvents matching the criteria from the parameters.
85       * All parameters are allowed to be null, meaning that the parameter imposes no restriction on the result
86       * @param fromDate Restrict the results to only provide events after this point in time
87       * @param toDate Restrict the results to only provide events up till this point in time
88       * @param fileID Restrict the results to only be about this fileID
89       * @param reportingComponent Restrict the results to only be reported by this component
90       * @param actor Restrict the results to only be events caused by this actor
91       * @param action Restrict the results to only be about this type of action
92       */
93      public AuditEventIterator queryAuditTrailEventsByIterator(Date fromDate, Date toDate, String fileID, 
94              String collectionID, String reportingComponent, String actor, FileAction action, 
95              String fingerprint, String operationID) {
96          return store.getAuditTrailsByIterator(fileID, collectionID, reportingComponent, null, null, actor, action, 
97                  fromDate, toDate, fingerprint, operationID);
98      }
99  
100     /**
101      * Collects all the newest audit trails from the given collection.
102      * TODO this currently calls all collections. It should only call a specified collection, which should be given
103      * as argument.
104      */
105     public void collectAuditTrails() {
106         for(org.bitrepository.settings.repositorysettings.Collection c 
107                 : settings.getRepositorySettings().getCollections().getCollection()) {
108             collector.collectNewestAudits(c.getID());
109         }
110     }
111 
112     @Override
113     public void start() {
114         mediator.start();
115     }
116 
117     @Override
118     public void shutdown() {
119         collector.close();
120         store.close();
121         mediator.close();
122         MessageBus messageBus = MessageBusManager.getMessageBus();
123         if ( messageBus != null) {
124             try {
125                 messageBus.close();
126             } catch (JMSException e) {
127                 log.warn("Failed to close message bus cleanly, " + e.getMessage());
128             }
129         }
130     }
131 }