/
ServiceHandler.java
440 lines (379 loc) · 15.8 KB
/
ServiceHandler.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
* ome.services.util.ServiceHandler
*
* Copyright 2006 University of Dundee. All rights reserved.
* Use is subject to license terms supplied in LICENSE.txt
*/
package ome.services.util;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;
import ome.annotations.AnnotationUtils;
import ome.annotations.ApiConstraintChecker;
import ome.annotations.Hidden;
import ome.conditions.ApiUsageException;
import ome.conditions.ConcurrencyException;
import ome.conditions.DatabaseBusyException;
import ome.conditions.InternalException;
import ome.conditions.OptimisticLockException;
import ome.conditions.RootException;
import ome.conditions.TryAgain;
import ome.conditions.ValidationException;
import ome.security.basic.CurrentDetails;
import ome.services.messages.RegisterServiceCleanupMessage;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.hibernate.PropertyValueException;
import org.perf4j.StopWatch;
import org.perf4j.slf4j.Slf4JStopWatch;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DeadlockLoserDataAccessException;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.dao.TransientDataAccessResourceException;
import org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateSystemException;
import org.springframework.transaction.CannotCreateTransactionException;
/**
*
*/
public class ServiceHandler implements MethodInterceptor, ApplicationListener {
/**
* Maxiumum length of a string that will be returned.
*/
private final static int MAX_STRING_LEN = 100;
private static Logger log = LoggerFactory.getLogger(ServiceHandler.class);
private final CurrentDetails cd;
private final long methodTimeError;
private final long methodTimeWarn;
public void onApplicationEvent(ApplicationEvent arg0) {
if (arg0 instanceof RegisterServiceCleanupMessage) {
RegisterServiceCleanupMessage cleanup = (RegisterServiceCleanupMessage) arg0;
cd.addCleanup(cleanup);
}
}
public ServiceHandler(CurrentDetails cd) {
this(cd, 5000, 15000);
}
public ServiceHandler(CurrentDetails cd, long methodTimeWarn, long methodTimeError) {
this.cd = cd;
this.methodTimeWarn = methodTimeWarn;
this.methodTimeError = methodTimeError;
}
/**
* @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
*/
public Object invoke(MethodInvocation arg0) throws Throwable {
if (arg0 == null) {
throw new InternalException(
"Cannot act on null MethodInvocation. Stopping.");
}
Class implClass = arg0.getThis().getClass();
Method mthd = arg0.getMethod();
Object[] args = arg0.getArguments();
ApiConstraintChecker.errorOnViolation(implClass, mthd, args);
if (log.isInfoEnabled()) {
// Method and arguments
if (Executor.LoggedWork.class.isAssignableFrom(arg0.getThis().getClass())) {
Executor.LoggedWork work = (Executor.LoggedWork) arg0.getThis();
log.info(" Executor.doWork -- " + work.description());
} else {
log.info(" Meth:\t" + arg0.getMethod().getDeclaringClass()
+ "." + arg0.getMethod().getName());
}
log.info(" Args:\t" + getArgumentsString(arg0));
}
// Results and/or Exceptions
Object o;
StringBuilder finalOutput = new StringBuilder();
StopWatch stopWatch = new Slf4JStopWatch();
try {
o = arg0.proceed();
finalOutput.append(" Rslt:\t");
finalOutput.append(getResultsString(o, null));
stopWatch.stop("omero.call.success." + implClass.getName() + "." + mthd.getName());
return o;
} catch (Throwable t) {
finalOutput.append(" Excp:\t");
finalOutput.append(t.toString());
stopWatch.stop("omero.call.exception");
throw getAndLogException(t);
} finally {
if (log.isInfoEnabled()) {
log.info(finalOutput.toString()); // slf4j migration: toString()
}
// Logging long invocations. Very long invocations are indicative
// of a server undergoing stress.
long time = stopWatch.getElapsedTime();
String msg = String.format("Method %s.%s invocation took %s",
arg0.getMethod().getDeclaringClass(),
arg0.getMethod().getName(), time);
if (time > methodTimeError) {
log.error(msg);
} else if (time > methodTimeWarn) {
log.warn(msg);
}
cleanup();
}
}
protected void cleanup() {
Set<RegisterServiceCleanupMessage> cleanups = cd.emptyCleanups();
for (RegisterServiceCleanupMessage registerServiceCleanupMessage : cleanups) {
try {
log.info("Cleanup: " + registerServiceCleanupMessage.resource);
registerServiceCleanupMessage.close();
} catch (Exception e) {
log.warn("Error while cleaning up", e);
}
}
}
protected Throwable getAndLogException(Throwable t) {
if (null == t) {
log.error("Exception thrown. (null)");
return new InternalException("Exception thrown with null message");
} else {
String msg = " Wrapped Exception: (" + t.getClass().getName()
+ "):\n" + t.getMessage();
// Base type of the hierarchy that we are converting to.
// Just rethrow.
if (RootException.class.isAssignableFrom(t.getClass())) {
return t;
}
//
// Spring's transient exception hierarchy
//
if (DeadlockLoserDataAccessException.class.isAssignableFrom(
t.getClass())) {
DeadlockLoserDataAccessException dldae = (DeadlockLoserDataAccessException) t;
TryAgain ta = new TryAgain(dldae.getMessage(), 500L); // ticket:5639
ta.setStackTrace(t.getStackTrace());
printException("Deadlock exception thrown.", t);
return ta;
} else if (OptimisticLockingFailureException.class
.isAssignableFrom(t.getClass())) {
OptimisticLockException ole = new OptimisticLockException(t
.getMessage());
ole.setStackTrace(t.getStackTrace());
printException("OptimisticLockingFailureException thrown.", t);
return ole;
} else if (ConcurrencyFailureException.class.isAssignableFrom(t.getClass())) {
ConcurrencyFailureException cfe = (ConcurrencyFailureException) t;
ConcurrencyException ce = new ConcurrencyException(cfe.getMessage(), 500);
ce.setStackTrace(t.getStackTrace());
printException("Unknown concurrency failure", t);
return ce;
} else if (TransientDataAccessResourceException.class.isAssignableFrom(t.getClass())) {
ConcurrencyFailureException cfe = (ConcurrencyFailureException) t;
ConcurrencyException ce = new ConcurrencyException(cfe.getMessage(), 500);
ce.setStackTrace(t.getStackTrace());
printException("Unknown transient failure", t);
return ce;
} else if (IllegalArgumentException.class.isAssignableFrom(t
.getClass())) {
ApiUsageException aue = new ApiUsageException(t.getMessage());
aue.setStackTrace(t.getStackTrace());
printException("IllegalArgumentException thrown.", t);
return aue;
}
else if (InvalidDataAccessResourceUsageException.class
.isAssignableFrom(t.getClass())) {
ApiUsageException aue = new ApiUsageException(t.getMessage());
aue.setStackTrace(t.getStackTrace());
printException(
"InvalidDataAccessResourceUsageException thrown.", t);
return aue;
}
else if (DataIntegrityViolationException.class.isAssignableFrom(t
.getClass())) {
ValidationException ve = new ValidationException(t.getMessage());
ve.setStackTrace(t.getStackTrace());
printException("DataIntegrityViolationException thrown.", t);
return ve;
}
else if (CannotCreateTransactionException.class.isAssignableFrom(t
.getClass())) {
DatabaseBusyException dbe = new DatabaseBusyException(
"cannot create transaction", 5000L);
dbe.setStackTrace(t.getStackTrace());
printException("CannotCreateTransactionException thrown.", t);
return dbe;
}
else if (HibernateObjectRetrievalFailureException.class
.isAssignableFrom(t.getClass())) {
ValidationException ve = new ValidationException(t.getMessage());
ve.setStackTrace(t.getStackTrace());
printException(
"HibernateObjectRetrievalFailureException thrown.", t);
return ve;
}
else if (HibernateSystemException.class.isAssignableFrom(t
.getClass())) {
Throwable cause = t.getCause();
if (cause == null || cause == t) {
return wrapUnknown(t, msg);
} else if (PropertyValueException.class.isAssignableFrom(cause
.getClass())) {
ValidationException ve = new ValidationException(cause
.getMessage());
ve.setStackTrace(cause.getStackTrace());
printException("PropertyValueException thrown.", cause);
return ve;
} else {
return wrapUnknown(t, msg);
}
}
return wrapUnknown(t, msg);
}
}
private Throwable wrapUnknown(Throwable t, String msg) {
// If this is an Error, then we want to log a message
// since these are most likely: AssertionError (bad assumptions),
// LinkageError (bad jar versions), ThreadDeath, or one of the
// VirtualMachineErrors: OutOfMemory, InternalError, StackOverflowError,
// UnknownError
if (t instanceof Error) {
log.error("java.lang.Error: " + msg, t);
}
// Wrap all other exceptions in InternalException
InternalException re = new InternalException(msg);
re.setStackTrace(t.getStackTrace());
printException("Unknown exception thrown.", t);
return re;
}
/**
* produces a String from the arguments array. Argument parameters marked as
* {@link Hidden} will be replaced by "*******".
*/
private String getArgumentsString(MethodInvocation mi) {
String arguments;
Object[] args = mi.getArguments();
if (args == null || args.length < 1) {
return "()";
}
String[] prnt = new String[args.length];
for (int i = 0; i < prnt.length; i++) {
prnt[i] = args[i] == null ? "null" : getResultsString(args[i], null);
}
Object[] allAnnotations = AnnotationUtils.findParameterAnnotations(mi
.getThis().getClass(), mi.getMethod());
for (int j = 0; j < allAnnotations.length; j++) {
Annotation[][] anns = (Annotation[][]) allAnnotations[j];
if (anns == null) {
continue;
}
for (int i = 0; i < args.length; i++) {
Annotation[] annotations = anns[i];
for (Annotation annotation : annotations) {
if (Hidden.class.equals(annotation.annotationType())) {
prnt[i] = "********";
}
}
}
}
arguments = Arrays.asList(prnt).toString();
return arguments;
}
/**
* public for testing purposes.
*/
public static String getResultsString(Object o, IdentityHashMap<Object, String> cache) {
if (o == null) {
return "null";
}
if (cache == null) {
cache = new IdentityHashMap<Object, String>();
} else {
if (cache.containsKey(o)) {
return (String) cache.get(o);
}
}
if (o instanceof Collection) {
int count = 0;
StringBuilder sb = new StringBuilder(128);
sb.append("(");
Collection c = (Collection) o;
for (Object obj : (c)) {
if (count > 0) {
sb.append(", ");
}
if (count > 2) {
sb.append("... ");
sb.append(c.size() - 3);
sb.append(" more");
break;
}
sb.append(getResultsString(obj, cache));
count++;
}
sb.append(")");
return sb.toString();
} else if (o instanceof Map) {
Map map = (Map) o;
int count = 0;
StringBuilder sb = new StringBuilder();
sb.append("{");
for (Object k : map.keySet()) {
if (count > 0) {
sb.append(", ");
}
if (count > 2) {
sb.append("... ");
sb.append(map.size() - 3);
sb.append(" more");
break;
}
sb.append(k);
sb.append("=");
cache.put(o, o.getClass().getName() + ":" + System.identityHashCode(o));
sb.append(getResultsString(map.get(k), cache));
count++;
}
sb.append("}");
return sb.toString();
} else if (o.getClass().isArray()) {
int length = Array.getLength(o);
if (length == 0) {
return "[]";
}
StringBuilder sb = new StringBuilder(128);
sb.append("[");
for (int i = 0; i < length; i++) {
if (i != 0) {
sb.append(", ");
}
if (i > 2) {
sb.append("... ");
sb.append(i - 2);
sb.append(" more");
break;
}
sb.append(getResultsString(Array.get(o, i), cache));
}
sb.append("]");
return sb.toString();
} else {
String s = o.toString();
if (s == null) {
return null;
} else if (s.length() > MAX_STRING_LEN) {
s = s.substring(0, MAX_STRING_LEN);
}
return s;
}
}
private void printException(String msg, Throwable ex) {
if (log.isWarnEnabled()) {
log.warn(msg + "\n", ex);
}
}
}