1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.processor; |
19 |
|
|
20 |
|
import org.apache.camel.Exchange; |
21 |
|
import org.apache.camel.Processor; |
22 |
|
import org.apache.camel.impl.ServiceSupport; |
23 |
|
import org.apache.camel.model.ExceptionType; |
24 |
|
|
25 |
|
import java.util.IdentityHashMap; |
26 |
|
import java.util.List; |
27 |
|
import java.util.Map; |
28 |
|
import java.util.Set; |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
294 |
public abstract class ErrorHandlerSupport extends ServiceSupport implements ErrorHandler { |
34 |
294 |
private Map<Class, ExceptionType> exceptionPolicices = new IdentityHashMap<Class, ExceptionType>(); |
35 |
|
|
36 |
|
public void addExceptionPolicy(ExceptionType exception) { |
37 |
24 |
Processor processor = exception.getErrorHandler(); |
38 |
24 |
addChildService(processor); |
39 |
|
|
40 |
24 |
List<Class> list = exception.getExceptionClasses(); |
41 |
|
|
42 |
24 |
for (Class exceptionType : list) { |
43 |
24 |
exceptionPolicices.put(exceptionType, exception); |
44 |
24 |
} |
45 |
24 |
} |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
protected boolean customProcessorForException(Exchange exchange, Throwable exception) throws Exception { |
55 |
0 |
ExceptionType policy = getExceptionPolicy(exchange, exception); |
56 |
0 |
Processor processor = policy.getErrorHandler(); |
57 |
0 |
if (processor != null) { |
58 |
0 |
processor.process(exchange); |
59 |
0 |
return true; |
60 |
|
} |
61 |
0 |
return false; |
62 |
|
} |
63 |
|
|
64 |
|
protected ExceptionType getExceptionPolicy(Exchange exchange, Throwable exception) { |
65 |
21 |
Set<Map.Entry<Class, ExceptionType>> entries = exceptionPolicices.entrySet(); |
66 |
21 |
for (Map.Entry<Class, ExceptionType> entry : entries) { |
67 |
16 |
Class type = entry.getKey(); |
68 |
16 |
if (type.isInstance(exception)) { |
69 |
12 |
return entry.getValue(); |
70 |
|
} |
71 |
4 |
} |
72 |
9 |
return null; |
73 |
|
} |
74 |
|
} |