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.bitrepositoryelements.ResponseCode;
25  import org.bitrepository.bitrepositoryelements.ResponseInfo;
26  import org.bitrepository.bitrepositorymessages.MessageRequest;
27  import org.bitrepository.bitrepositorymessages.MessageResponse;
28  import org.bitrepository.common.settings.Settings;
29  import org.bitrepository.protocol.messagebus.MessageBus;
30  import org.bitrepository.service.AlarmDispatcher;
31  import org.bitrepository.service.audit.AuditTrailManager;
32  import org.bitrepository.service.contributor.handler.GetAuditTrailsRequestHandler;
33  import org.bitrepository.service.contributor.handler.GetStatusRequestHandler;
34  import org.bitrepository.service.contributor.handler.IdentifyContributorsForGetAuditTrailsRequestHandler;
35  import org.bitrepository.service.contributor.handler.IdentifyContributorsForGetStatusRequestHandler;
36  import org.bitrepository.service.contributor.handler.RequestHandler;
37  import org.bitrepository.service.exception.RequestHandlerException;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  import java.util.ArrayList;
42  import java.util.List;
43  
44  /**
45   * Simple implementation of a contributor mediator.
46   * It supports the handling of the GetStatus and GetAuditTrail operations.
47   * 
48   * If the optional AuditTrailManager is given as argument, then this mediator will also be able to handle the 
49   * GetAuditTrails identification and operation. 
50   */
51  public class SimpleContributorMediator extends AbstractContributorMediator {
52      /** The log.*/
53      private Logger log = LoggerFactory.getLogger(getClass());
54      /** The context for this simple contributor mediator.*/
55      private final ContributorContext context;
56      /** The audit trail manager. */
57      private AuditTrailManager auditManager;
58      
59      /**
60       * Constructor.
61       * @param messageBus The messagebus for the mediator.
62       * @param settings the settings for the mediator.
63       * @param auditManager [OPTIONAL] The manager of audit trails. Only if the contributor has audit trails.
64       */
65      public SimpleContributorMediator(MessageBus messageBus, Settings settings, AuditTrailManager auditManager) {
66          super(messageBus);
67          context = new ContributorContext(
68              new ResponseDispatcher(settings, messageBus),
69              new AlarmDispatcher(settings, messageBus),
70              settings);
71          this.auditManager = auditManager;
72      }
73  
74      @SuppressWarnings("rawtypes")
75      @Override
76      protected RequestHandler[] createListOfHandlers() {
77          List<RequestHandler> handlers = new ArrayList<RequestHandler>();
78          
79          handlers.add(new IdentifyContributorsForGetStatusRequestHandler(getContext()));
80          handlers.add(new GetStatusRequestHandler(getContext()));
81          
82          if(auditManager != null) {
83              handlers.add(new IdentifyContributorsForGetAuditTrailsRequestHandler(getContext()));
84              handlers.add(new GetAuditTrailsRequestHandler(getContext(), auditManager));
85          }
86          
87          return handlers.toArray(new RequestHandler[handlers.size()]);
88      }
89      
90      @Override
91      protected ContributorContext getContext() {
92          return context;
93      }
94  
95      @SuppressWarnings({ "rawtypes", "unchecked" })
96      @Override
97      protected void handleRequest(MessageRequest request, RequestHandler handler) {
98          try {
99              handler.processRequest(request);
100         } catch (RequestHandlerException e) {
101             log.info("Invalid Message exception caught. Sending failed response.", e);
102             MessageResponse response = handler.generateFailedResponse(request);
103             response.setResponseInfo(e.getResponseInfo());
104             context.getResponseDispatcher().dispatchResponse(response, request);
105         } catch (Exception e) {
106             log.warn("Unexpected exception caught.", e);
107             ResponseInfo responseInfo = new ResponseInfo();
108             responseInfo.setResponseCode(ResponseCode.FAILURE);
109             responseInfo.setResponseText(e.toString());
110             
111             MessageResponse response = handler.generateFailedResponse(request);
112             response.setResponseInfo(responseInfo);
113             context.getResponseDispatcher().dispatchResponse(response, request);
114         }
115     }
116 }