1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.processor; |
18 |
|
|
19 |
|
import java.util.List; |
20 |
|
|
21 |
|
import org.apache.camel.Exchange; |
22 |
|
import org.apache.camel.Processor; |
23 |
|
import org.apache.camel.impl.ServiceSupport; |
24 |
|
import org.apache.camel.util.ServiceHelper; |
25 |
|
import org.apache.commons.logging.Log; |
26 |
|
import org.apache.commons.logging.LogFactory; |
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
public class TryProcessor extends ServiceSupport implements Processor { |
34 |
3 |
private static final Log LOG = LogFactory.getLog(TryProcessor.class); |
35 |
|
|
36 |
|
private final Processor tryProcessor; |
37 |
|
private final List<CatchProcessor> catchClauses; |
38 |
|
private final Processor finallyProcessor; |
39 |
|
|
40 |
6 |
public TryProcessor(Processor tryProcessor, List<CatchProcessor> catchClauses, Processor finallyProcessor) { |
41 |
6 |
this.tryProcessor = tryProcessor; |
42 |
6 |
this.catchClauses = catchClauses; |
43 |
6 |
this.finallyProcessor = finallyProcessor; |
44 |
6 |
} |
45 |
|
|
46 |
|
public String toString() { |
47 |
6 |
String finallyText = (finallyProcessor == null) ? "" : " Finally {" + finallyProcessor + "}"; |
48 |
6 |
return "Try {" + tryProcessor + "} " + catchClauses + finallyText; |
49 |
|
} |
50 |
|
|
51 |
|
public void process(Exchange exchange) throws Exception { |
52 |
6 |
boolean doneTry = false; |
53 |
|
try { |
54 |
6 |
tryProcessor.process(exchange); |
55 |
3 |
doneTry = true; |
56 |
|
|
57 |
3 |
if (finallyProcessor != null) { |
58 |
0 |
finallyProcessor.process(exchange); |
59 |
|
} |
60 |
3 |
} catch (Exception e) { |
61 |
3 |
handleException(exchange, e); |
62 |
|
|
63 |
3 |
if (!doneTry && finallyProcessor != null) { |
64 |
|
try { |
65 |
0 |
finallyProcessor.process(exchange); |
66 |
0 |
} catch (Exception e2) { |
67 |
0 |
LOG.warn("Caught exception in finally block while handling other exception: " + e2, e2); |
68 |
0 |
} |
69 |
|
} |
70 |
3 |
} |
71 |
6 |
} |
72 |
|
|
73 |
|
protected void doStart() throws Exception { |
74 |
6 |
ServiceHelper.startServices(tryProcessor, catchClauses, finallyProcessor); |
75 |
6 |
} |
76 |
|
|
77 |
|
protected void doStop() throws Exception { |
78 |
6 |
ServiceHelper.stopServices(tryProcessor, catchClauses, finallyProcessor); |
79 |
6 |
} |
80 |
|
|
81 |
|
protected void handleException(Exchange exchange, Exception e) throws Exception { |
82 |
3 |
for (CatchProcessor catchClause : catchClauses) { |
83 |
3 |
if (catchClause.catches(e)) { |
84 |
|
|
85 |
3 |
exchange.setException(e); |
86 |
|
try { |
87 |
3 |
catchClause.process(exchange); |
88 |
0 |
} catch (Exception e1) { |
89 |
0 |
LOG.warn("Caught exception inside catch clause: " + e1, e1); |
90 |
0 |
throw e1; |
91 |
3 |
} |
92 |
3 |
return; |
93 |
|
} |
94 |
0 |
} |
95 |
|
|
96 |
|
|
97 |
0 |
throw e; |
98 |
|
} |
99 |
|
} |