View Javadoc

1   /*
2    * #%L
3    * Bitrepository 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.contributor;
23  
24  import org.bitrepository.bitrepositorymessages.Message;
25  import org.bitrepository.common.ArgumentValidator;
26  import org.bitrepository.common.settings.Settings;
27  import org.bitrepository.protocol.ProtocolVersionLoader;
28  import org.bitrepository.protocol.messagebus.MessageSender;
29  
30  /**
31   * Provides the general functionality for sending reponses from a pillar.
32   */
33  public class MessageDispatcher {
34      protected final Settings settings;
35      private final MessageSender sender;
36  
37      public MessageDispatcher(Settings settings, MessageSender sender) {
38          ArgumentValidator.checkNotNull(settings, "settings");
39          ArgumentValidator.checkNotNull(sender, "sender");
40          this.settings = settings;
41          this.sender = sender;
42      }
43  
44      /**
45       * Completes and sends a given message.
46       * All the values of the specific response elements has to be set, including the ResponseInfo.
47       * <br/> Sets the fields:
48       * <br/> CollectionID
49       * <br/> From
50       * <br/> MinVersion
51       * <br/> Version
52       *
53       * @param message The message which only needs the basic information to be send.
54       */
55      protected void dispatchMessage(Message message) {
56          message.setFrom(settings.getComponentID());
57          message.setMinVersion(ProtocolVersionLoader.loadProtocolVersion().getMinVersion());
58          message.setVersion(ProtocolVersionLoader.loadProtocolVersion().getVersion());
59          sender.sendMessage(message);
60      }
61  }