View Javadoc

1   /*
2    * #%L
3    * Bitrepository Core
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.bitrepositorymessages.MessageRequest;
26  import org.bitrepository.protocol.messagebus.MessageBus;
27  import org.bitrepository.protocol.messagebus.MessageListener;
28  import org.bitrepository.protocol.utils.MessageUtils;
29  import org.bitrepository.service.contributor.handler.RequestHandler;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  /**
37   * Defines the general functionality for handling a set of requests. Does this by delegating the
38   * handling of the specific request to appropriate <code>RequestHandler</code>s.
39   */
40  @SuppressWarnings("rawtypes")
41  public abstract class AbstractContributorMediator implements ContributorMediator {
42      /** The log.*/
43      private Logger log = LoggerFactory.getLogger(getClass());
44      /** The map of request handlers. Mapping between request name and message handler for the given request.*/
45      private final Map<String, RequestHandler> handlerMap = new HashMap<String, RequestHandler>();
46      /** The message bus.*/
47      private final MessageBus messageBus;
48      /** The intermediate message handler for retrieving the messages from the messagebus. */
49      private final GeneralRequestHandler messageHandler;
50  
51      /**
52       * Constructor.
53       * @param messageBus The messagebus for this mediator.
54       */
55      public AbstractContributorMediator(MessageBus messageBus) {
56          this.messageBus = messageBus;
57          messageHandler = new GeneralRequestHandler();
58      }
59  
60      /**
61       * Starts listening for requests.
62       * Listens both to the general collection destination and to the local destination for the contributor.
63       */
64      public final void start() {
65          for (RequestHandler handler : createListOfHandlers()) {
66              handlerMap.put(handler.getRequestClass().getSimpleName(), handler);
67          }
68          messageBus.addListener(getContext().getSettings().getContributorDestinationID(), messageHandler);
69          System.out.println("Referencepillar: adding lister to pillar topic: " + getContext().getSettings().getContributorDestinationID());
70          messageBus.addListener(getContext().getSettings().getCollectionDestination(), messageHandler);
71      }
72  
73      /**
74       * @return The set of <code>RequestHandler</code>s used for this contributor.
75       */
76      protected abstract RequestHandler[] createListOfHandlers();
77  
78      /**
79       * @return The concrete context used for this contributor.
80       */
81      protected abstract ContributorContext getContext();
82      
83      /**
84       * Make the inheriting class create the environment for safely handling the request. 
85       * E.g. creating the specific fault barrier.
86       * @param request The request to handle.
87       * @param handler The handler for the request.
88       */
89      protected abstract void handleRequest(MessageRequest request, RequestHandler handler);
90  
91      /**
92       * The message listener, which delegates the request-messages to the request handlers.
93       */
94      private class GeneralRequestHandler implements MessageListener {
95          @Override
96          public void onMessage(Message message) {
97              if (message instanceof MessageRequest) {
98                  RequestHandler handler = handlerMap.get(message.getClass().getSimpleName());
99                  if (handler != null) {
100                     handleRequest((MessageRequest) message, handler);
101                 } else {
102                     if (MessageUtils.isIdentifyRequest(message)) {
103                         log.trace("Received unhandled identity request: \n{}", message);
104                     } else
105                     log.warn("Unable to handle messages of this type: \n{}", message);
106                 }
107             } else {
108                 log.trace("Can only handle message requests, but received: \n{}", message);
109             }
110         }
111     }
112     
113     /**
114     * Closes the mediator by removing all the message handler.
115     */
116     @Override
117     public void close() {
118         messageBus.removeListener(getContext().getSettings().getCollectionDestination(), messageHandler);
119         messageBus.removeListener(getContext().getSettings().getReceiverDestinationID(), messageHandler);
120     }
121 }