-
Notifications
You must be signed in to change notification settings - Fork 23
/
GetChecksumsEventHandler.java
120 lines (106 loc) · 4.37 KB
/
GetChecksumsEventHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package dk.netarkivet.common.distribute.bitrepository.action.getchecksums;
import java.util.List;
import org.bitrepository.access.getchecksums.conversation.ChecksumsCompletePillarEvent;
import org.bitrepository.bitrepositoryelements.ChecksumDataForChecksumSpecTYPE;
import org.bitrepository.client.eventhandler.EventHandler;
import org.bitrepository.client.eventhandler.OperationEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Event handler to handle results from a GetChecksums request made to a single pillar.
*/
public class GetChecksumsEventHandler implements EventHandler {
private final Logger log = LoggerFactory.getLogger(getClass());
private boolean partialResults = false;
private final Object finishLock = new Object();
private boolean finished = false;
private boolean failed = false;
private List<ChecksumDataForChecksumSpecTYPE> checksumData = null;
private String pillarID;
/**
* Constructor
* @param pillarID The pillar which is expected to deliver the results.
*/
public GetChecksumsEventHandler(String pillarID) {
this.pillarID = pillarID;
}
@Override
public void handleEvent(OperationEvent event) {
log.info("Got event from client: {}", event.getEventType());
switch(event.getEventType()) {
case COMPONENT_COMPLETE:
log.debug("Got COMPONENT_COMPLETE event {}", event);
if(event instanceof ChecksumsCompletePillarEvent) {
ChecksumsCompletePillarEvent checksumsEvent = (ChecksumsCompletePillarEvent) event;
if(checksumsEvent.getContributorID().equals(pillarID)) {
checksumData = checksumsEvent.getChecksums().getChecksumDataItems();
partialResults = checksumsEvent.isPartialResult();
} else {
log.warn("Got an event from an unexpected contributor '{}' expected '{}'",
checksumsEvent.getContributorID(), pillarID);
}
}
case COMPLETE:
log.info("Finished get checksum for file '{}'", event.getFileID());
finish();
break;
case FAILED:
log.warn("Failed get checksum for file '{}'", event.getFileID());
failed = true;
finish();
break;
default:
break;
}
}
/**
* Method to obtain the received checksum data.
* The method should not be called prior to a call to {@link #waitForFinish()} have returned.
* @return The checksum data if it have been returned by the pillar, otherwise null.
*/
public List<ChecksumDataForChecksumSpecTYPE> getChecksumData() {
return checksumData;
}
/**
* Method to indicate the operation have finished (regardless if is successful or not).
*/
private void finish() {
log.trace("Finish method invoked");
synchronized (finishLock) {
log.trace("Finish method entered synchronized block");
finished = true;
finishLock.notifyAll();
log.trace("Finish method notified All");
}
}
/**
* Method to wait for the operation to complete. The method is blocking.
* @throws InterruptedException if the thread is interrupted
*/
public void waitForFinish() throws InterruptedException {
synchronized (finishLock) {
if(finished == false) {
log.trace("Thread waiting for client to finish");
finishLock.wait();
}
log.trace("Client have indicated it's finished.");
}
}
/**
* Method to determine if the received results is a partial result set. I.e. should
* the client send a new request to get more results.
* The method should not be called prior to a call to {@link #waitForFinish()} have returned.
* @return true if the results are partial
*/
public boolean partialResults() {
return partialResults;
}
/**
* Method to determine if the operation was successful.
* The method should not be called prior to a call to {@link #waitForFinish()} have returned.
* @return true if the operation succeeded, otherwise false.
*/
public boolean hasFailed() {
return failed;
}
}