View Javadoc

1   /*
2    * #%L
3    * Bitrepository Protocol
4    * 
5    * $Id$
6    * $HeadURL$
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.client.conversation;
26  
27  import org.bitrepository.bitrepositorymessages.Message;
28  import org.bitrepository.client.eventhandler.OperationFailedEvent;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * Abstract super class for conversations.
34   * This super class will handle sending all messages with the correct
35   * conversation id, and simply log messages received. Overriding implementations should override the behaviour for
36   * receiving specific messages.
37   */
38  public class StateBasedConversation implements Conversation {
39      private Logger log = LoggerFactory.getLogger(getClass());
40      private long startTime;
41      private ConversationContext context;
42  
43      /**
44       * Initialize a conversation on the given message bus.
45       *
46       * @param context The cointext to use for this conversation.
47       */
48      public StateBasedConversation(ConversationContext context) {
49          this.startTime = System.currentTimeMillis();
50          this.context = context;
51      }
52  
53      @Override
54      public synchronized void onMessage(Message message) {
55          context.getState().handleMessage(message);
56      }
57  
58      @Override
59      public String getConversationID() {
60          return context.getConversationID();
61      }
62      
63      @Override
64      public long getStartTime() {
65          return startTime;
66      }
67  
68      @Override
69      public synchronized void startConversation() {
70          context.getState().start();
71      }
72  
73      @Override
74      public void endConversation() {
75          context.setState(new FinishedState(context));
76      }
77  
78      @Override
79      public synchronized void failConversation(OperationFailedEvent failedEvent) {
80          getMonitor().operationFailed(failedEvent);
81          endConversation();
82      }
83  
84      /**
85       * @return The monitor for distributing update information
86       */
87      public ConversationEventMonitor getMonitor() {
88          return context.getMonitor();
89      }
90  
91      @Override
92      public boolean hasEnded() {
93          return context.getState() instanceof FinishedState;
94      }
95  }