View Javadoc

1   /*
2    * #%L
3    * Bitrepository Reference Pillar
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.pillar.checksumpillar;
23  
24  import org.bitrepository.common.ArgumentValidator;
25  import org.bitrepository.common.settings.Settings;
26  import org.bitrepository.common.utils.SettingsUtils;
27  import org.bitrepository.pillar.Pillar;
28  import org.bitrepository.pillar.cache.ChecksumStore;
29  import org.bitrepository.pillar.checksumpillar.messagehandler.ChecksumPillarMediator;
30  import org.bitrepository.pillar.common.MessageHandlerContext;
31  import org.bitrepository.pillar.common.PillarAlarmDispatcher;
32  import org.bitrepository.pillar.common.SettingsHelper;
33  import org.bitrepository.protocol.messagebus.MessageBus;
34  import org.bitrepository.service.audit.AuditDatabaseManager;
35  import org.bitrepository.service.audit.AuditTrailContributerDAO;
36  import org.bitrepository.service.audit.AuditTrailManager;
37  import org.bitrepository.service.contributor.ResponseDispatcher;
38  import org.bitrepository.service.database.DBConnector;
39  import org.bitrepository.service.database.DatabaseManager;
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  
43  import javax.jms.JMSException;
44  
45  import java.util.Arrays;
46  
47  /**
48   * The checksum pillar.
49   */
50  public class ChecksumPillar implements Pillar {
51      /** The log.*/
52      private Logger log = LoggerFactory.getLogger(getClass());
53      /** The messagebus for the pillar.*/
54      private final MessageBus messageBus;
55      /** The cache for persisting the checksum data.*/
56      private final ChecksumStore cache;
57      /** The mediator for delegating the communication to the message handler.*/
58      private ChecksumPillarMediator mediator;
59   
60      /**
61       * Constructor.
62       * @param messageBus The message bus for the 
63       * @param settings The settings for the checksum pillar.
64       * @param refCache The cache for the checksum data to be stored.
65       */
66      public ChecksumPillar(MessageBus messageBus, Settings settings, ChecksumStore refCache) {
67          ArgumentValidator.checkNotNull(messageBus, "messageBus");
68          ArgumentValidator.checkNotNull(settings, "settings");
69          ArgumentValidator.checkNotNull(refCache, "ChecksumCache refCache");
70  
71          SettingsUtils.initialize(settings);
72          this.messageBus = messageBus;
73          this.cache = refCache;
74          
75          log.info("Starting the ChecksumPillar");
76          DatabaseManager auditDatabaseManager = new AuditDatabaseManager(
77                  settings.getReferenceSettings().getPillarSettings().getAuditTrailContributerDatabase());
78          AuditTrailManager audits = new AuditTrailContributerDAO(settings, auditDatabaseManager);
79          MessageHandlerContext context = new MessageHandlerContext(settings,
80                  SettingsHelper.getPillarCollections(settings.getComponentID(), settings.getCollections()),
81              new ResponseDispatcher(settings, messageBus),
82              new PillarAlarmDispatcher(settings, messageBus),
83              audits);
84          messageBus.setCollectionFilter(Arrays.asList(context.getPillarCollections()));
85          mediator = new ChecksumPillarMediator(messageBus, context, cache);
86          mediator.start();
87          log.info("ChecksumPillar started!");
88      }
89      
90      /**
91       * Close the pillar, and thus also the mediator and the connection to the messagebus.
92       */
93      public void close() {
94          try {
95              mediator.close();
96              messageBus.close();
97              log.info("ChecksumPillar stopped");
98          } catch (JMSException e) {
99              log.warn("Could not close the message bus properly.", e);
100         }
101     }
102 }