1
|
package eu.dnetlib.enabling.is.sn;
|
2
|
|
3
|
import java.util.Collection;
|
4
|
import java.util.Date;
|
5
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
6
|
|
7
|
import javax.xml.ws.wsaddressing.W3CEndpointReference;
|
8
|
|
9
|
/**
|
10
|
* In memory implementation of a notification invocation logger.
|
11
|
*
|
12
|
* @author marko
|
13
|
*
|
14
|
*/
|
15
|
public class MemoryNotificationInvocationLogger implements NotificationInvocationLogger {
|
16
|
|
17
|
/**
|
18
|
* default log size.
|
19
|
*/
|
20
|
private static final int DEFAULT_SIZE = 100;
|
21
|
|
22
|
/**
|
23
|
* Entry.
|
24
|
*
|
25
|
* @author marko
|
26
|
*
|
27
|
*/
|
28
|
public class MemoryEntry implements Entry {
|
29
|
|
30
|
/**
|
31
|
* logged message.
|
32
|
*/
|
33
|
private NotificationMessage message;
|
34
|
|
35
|
/**
|
36
|
* message status.
|
37
|
*/
|
38
|
private Status status = Status.Queued;
|
39
|
|
40
|
/**
|
41
|
* saved exception in case of Failed state.
|
42
|
*/
|
43
|
private Throwable exception;
|
44
|
|
45
|
/**
|
46
|
* destination.
|
47
|
*/
|
48
|
private W3CEndpointReference destinationEpr;
|
49
|
|
50
|
/**
|
51
|
* start date time.
|
52
|
*/
|
53
|
private Date start;
|
54
|
|
55
|
/**
|
56
|
* finish date time.
|
57
|
*/
|
58
|
private Date finish;
|
59
|
|
60
|
/**
|
61
|
* construct a new message.
|
62
|
*
|
63
|
* @param endpointReference
|
64
|
* destination
|
65
|
*
|
66
|
* @param message
|
67
|
* message
|
68
|
*/
|
69
|
public MemoryEntry(final W3CEndpointReference endpointReference, final NotificationMessage message) {
|
70
|
this.destinationEpr = endpointReference;
|
71
|
this.message = message;
|
72
|
this.start = new Date();
|
73
|
}
|
74
|
|
75
|
/**
|
76
|
* {@inheritDoc}
|
77
|
*
|
78
|
* @see eu.dnetlib.enabling.is.sn.NotificationInvocationLogger.Entry#ongoing()
|
79
|
*/
|
80
|
@Override
|
81
|
public void ongoing() {
|
82
|
status = Status.Ongoing;
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* {@inheritDoc}
|
87
|
*
|
88
|
* @see eu.dnetlib.enabling.is.sn.NotificationInvocationLogger.Entry#failure(java.lang.Throwable)
|
89
|
*/
|
90
|
@Override
|
91
|
public void failure(final Throwable exc) {
|
92
|
status = Status.Failed;
|
93
|
this.exception = exc;
|
94
|
commit();
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* {@inheritDoc}
|
99
|
*
|
100
|
* @see eu.dnetlib.enabling.is.sn.NotificationInvocationLogger.Entry#success()
|
101
|
*/
|
102
|
@Override
|
103
|
public void success() {
|
104
|
status = Status.Succeeded;
|
105
|
commit();
|
106
|
}
|
107
|
|
108
|
/**
|
109
|
* finalize log entry.
|
110
|
*/
|
111
|
protected void commit() {
|
112
|
this.finish = new Date();
|
113
|
}
|
114
|
|
115
|
@Override
|
116
|
public NotificationMessage getMessage() {
|
117
|
return message;
|
118
|
}
|
119
|
|
120
|
public void setMessage(final NotificationMessage message) {
|
121
|
this.message = message;
|
122
|
}
|
123
|
|
124
|
@Override
|
125
|
public Throwable getException() {
|
126
|
return exception;
|
127
|
}
|
128
|
|
129
|
public void setException(final Throwable exception) {
|
130
|
this.exception = exception;
|
131
|
}
|
132
|
|
133
|
@Override
|
134
|
public Status getStatus() {
|
135
|
return status;
|
136
|
}
|
137
|
|
138
|
public void setStatus(final Status status) {
|
139
|
this.status = status;
|
140
|
}
|
141
|
|
142
|
public W3CEndpointReference getDestinationEpr() {
|
143
|
return destinationEpr;
|
144
|
}
|
145
|
|
146
|
public void setDestinationEpr(final W3CEndpointReference destinationEpr) {
|
147
|
this.destinationEpr = destinationEpr;
|
148
|
}
|
149
|
|
150
|
@Override
|
151
|
public String getDestination() {
|
152
|
return EPRUtil.getAddress(destinationEpr);
|
153
|
}
|
154
|
|
155
|
/**
|
156
|
* {@inheritDoc}
|
157
|
*
|
158
|
* @see eu.dnetlib.enabling.is.sn.NotificationInvocationLogger.Entry#getErrorMessage()
|
159
|
*/
|
160
|
@Override
|
161
|
public String getErrorMessage() {
|
162
|
if (getException() == null)
|
163
|
return "";
|
164
|
return getException().getMessage();
|
165
|
}
|
166
|
|
167
|
@Override
|
168
|
public Date getStart() {
|
169
|
return start;
|
170
|
}
|
171
|
|
172
|
public void setStart(final Date start) {
|
173
|
this.start = start;
|
174
|
}
|
175
|
|
176
|
@Override
|
177
|
public Date getFinish() {
|
178
|
return finish;
|
179
|
}
|
180
|
|
181
|
public void setFinish(final Date finish) {
|
182
|
this.finish = finish;
|
183
|
}
|
184
|
|
185
|
/**
|
186
|
* {@inheritDoc}
|
187
|
* @see eu.dnetlib.enabling.is.sn.NotificationInvocationLogger.Entry#getDuration()
|
188
|
*/
|
189
|
@Override
|
190
|
public String getDuration() {
|
191
|
if (finish == null || start == null)
|
192
|
return "-";
|
193
|
return (finish.getTime() - start.getTime()) + " ms";
|
194
|
}
|
195
|
}
|
196
|
|
197
|
/**
|
198
|
* the actual log storage.
|
199
|
*/
|
200
|
private ConcurrentLinkedQueue<Entry> queue = new ConcurrentLinkedQueue<Entry>();
|
201
|
|
202
|
/**
|
203
|
* max log size.
|
204
|
*/
|
205
|
private int size = DEFAULT_SIZE;
|
206
|
|
207
|
/**
|
208
|
* {@inheritDoc}
|
209
|
*
|
210
|
* @see eu.dnetlib.enabling.is.sn.NotificationInvocationLogger#startLogging(eu.dnetlib.enabling.is.sn.NotificationMessage)
|
211
|
*/
|
212
|
@Override
|
213
|
public Entry startLogging(final W3CEndpointReference dest, final NotificationMessage message) {
|
214
|
final Entry entry = createLoggingEntry(dest, message);
|
215
|
queue.add(entry);
|
216
|
ensureQueueLength();
|
217
|
return entry;
|
218
|
}
|
219
|
|
220
|
/**
|
221
|
* ensure that the queue doesn't grow beyond size.
|
222
|
*/
|
223
|
private void ensureQueueLength() {
|
224
|
if (queue.size() > size)
|
225
|
queue.poll();
|
226
|
}
|
227
|
|
228
|
/**
|
229
|
* create a new memory log object.
|
230
|
*
|
231
|
* @param dest
|
232
|
* destination
|
233
|
*
|
234
|
* @param message
|
235
|
* message to be logged
|
236
|
* @return new entry instance
|
237
|
*/
|
238
|
protected Entry createLoggingEntry(final W3CEndpointReference dest, final NotificationMessage message) {
|
239
|
return new MemoryEntry(dest, message);
|
240
|
}
|
241
|
|
242
|
public ConcurrentLinkedQueue<Entry> getQueue() {
|
243
|
return queue;
|
244
|
}
|
245
|
|
246
|
public void setQueue(final ConcurrentLinkedQueue<Entry> queue) {
|
247
|
this.queue = queue;
|
248
|
}
|
249
|
|
250
|
public int getSize() {
|
251
|
return size;
|
252
|
}
|
253
|
|
254
|
public void setSize(final int size) {
|
255
|
this.size = size;
|
256
|
}
|
257
|
|
258
|
@Override
|
259
|
public Collection<Entry> getEntries() {
|
260
|
return getQueue();
|
261
|
}
|
262
|
|
263
|
}
|