View Javadoc

1   /*
2    * #%L
3    * Bitrepository Access
4    * 
5    * $Id: IdentifyPillarsForDeleteFile.java 639 2011-12-15 10:24:45Z jolf $
6    * $HeadURL: https://sbforge.org/svn/bitrepository/bitrepository-reference/trunk/bitrepository-modifying-client/src/main/java/org/bitrepository/modify/deletefile/conversation/IdentifyPillarsForDeleteFile.java $
7    * %%
8    * Copyright (C) 2010 - 2011 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.modify.replacefile.conversation;
26  
27  import org.bitrepository.bitrepositorymessages.IdentifyPillarsForReplaceFileRequest;
28  import org.bitrepository.bitrepositorymessages.IdentifyPillarsForReplaceFileResponse;
29  import org.bitrepository.bitrepositorymessages.MessageResponse;
30  import org.bitrepository.client.conversation.ConversationContext;
31  import org.bitrepository.client.conversation.GeneralConversationState;
32  import org.bitrepository.client.conversation.IdentifyingState;
33  import org.bitrepository.common.exceptions.UnableToFinishException;
34  
35  /**
36   * The first state of the ReplaceFile communication. The identification of the pillars involved.
37   */
38  public class IdentifyPillarsForReplaceFile extends IdentifyingState {
39      private final ReplaceFileConversationContext context;
40  
41      /**
42       * @param context The conversation's context.
43       */
44      public IdentifyPillarsForReplaceFile(ReplaceFileConversationContext context) {
45          super(context.getContributors());
46          this.context = context;
47      }
48  
49  
50      /**
51       * Extends the default behaviour with a idempotent aspects. This assumes that the replace on a pillar is successful if
52       * if new file is the one already present on the pillar.
53       *
54       * Any other none-positive response is handled as a fatal problem.
55       */
56      @Override
57      protected void handleFailureResponse(MessageResponse msg) throws UnableToFinishException {
58          //ToDo implement idempotent behaviour, BITMAG-659.
59          IdentifyPillarsForReplaceFileResponse response = (IdentifyPillarsForReplaceFileResponse) msg;
60          getContext().getMonitor().contributorFailed(
61                  msg.getResponseInfo().getResponseText(), msg.getFrom(), msg.getResponseInfo().getResponseCode());
62          throw new UnableToFinishException("Can not continue with replace operation, as " + msg.getFrom() +
63                  " is unable to perform the deletion.");
64      }
65  
66      @Override
67      public GeneralConversationState getOperationState() {
68          return new ReplacingFile(context, getSelector().getSelectedComponents());
69      }
70  
71      @Override
72      protected void sendRequest() {
73          IdentifyPillarsForReplaceFileRequest msg = new IdentifyPillarsForReplaceFileRequest();
74          initializeMessage(msg);
75          msg.setFileID(context.getFileID());
76          msg.setDestination(context.getSettings().getCollectionDestination());
77          context.getMessageSender().sendMessage(msg);
78          context.getMonitor().identifyRequestSent("Identifying pillars for replace file");
79      }
80  
81      @Override
82      protected ConversationContext getContext() {
83          return context;
84      }
85  
86      @Override
87      protected String getPrimitiveName() {
88          return "IdentifyPillarsForReplaceFile";
89      }
90  
91      @Override
92      protected boolean canFinish() {
93          return (getOutstandingComponents().isEmpty());
94      }
95  
96      @Override
97      protected void checkForChecksumPillar(MessageResponse msg) {
98          IdentifyPillarsForReplaceFileResponse response = (IdentifyPillarsForReplaceFileResponse) msg;
99          if (response.getPillarChecksumSpec() != null) {
100             context.addChecksumPillar(response.getPillarID());
101         }
102     }
103 }