| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.model; |
| 18 |
|
|
| 19 |
|
import java.util.ArrayList; |
| 20 |
|
import java.util.Collection; |
| 21 |
|
import java.util.List; |
| 22 |
|
|
| 23 |
|
import javax.xml.bind.annotation.XmlAccessType; |
| 24 |
|
import javax.xml.bind.annotation.XmlAccessorType; |
| 25 |
|
import javax.xml.bind.annotation.XmlRootElement; |
| 26 |
|
import javax.xml.bind.annotation.XmlTransient; |
| 27 |
|
|
| 28 |
|
import org.apache.camel.Endpoint; |
| 29 |
|
import org.apache.camel.Processor; |
| 30 |
|
import org.apache.camel.impl.RouteContext; |
| 31 |
|
import org.apache.camel.processor.CatchProcessor; |
| 32 |
|
import org.apache.camel.processor.TryProcessor; |
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
@XmlRootElement(name = "try") |
| 38 |
|
@XmlAccessorType(XmlAccessType.FIELD) |
| 39 |
6 |
public class TryType extends OutputType { |
| 40 |
|
@XmlTransient |
| 41 |
|
private List<CatchType> catchClauses; |
| 42 |
|
@XmlTransient |
| 43 |
|
private FinallyType finallyClause; |
| 44 |
|
@XmlTransient |
| 45 |
|
private boolean initialized; |
| 46 |
|
@XmlTransient |
| 47 |
|
private List<ProcessorType> outputsWithoutCatches; |
| 48 |
|
|
| 49 |
|
@Override |
| 50 |
|
public String toString() { |
| 51 |
0 |
return "Try[ " + getOutputs() + "]"; |
| 52 |
|
} |
| 53 |
|
|
| 54 |
|
@Override |
| 55 |
|
public Processor createProcessor(RouteContext routeContext) throws Exception { |
| 56 |
6 |
Processor tryProcessor = createOutputsProcessor(routeContext, getOutputsWithoutCatches()); |
| 57 |
|
|
| 58 |
6 |
Processor finallyProcessor = null; |
| 59 |
6 |
if (finallyClause != null) { |
| 60 |
0 |
finallyProcessor = finallyClause.createProcessor(routeContext); |
| 61 |
|
} |
| 62 |
6 |
List<CatchProcessor> catchProcessors = new ArrayList<CatchProcessor>(); |
| 63 |
6 |
if (catchClauses != null) { |
| 64 |
6 |
for (CatchType catchClause : catchClauses) { |
| 65 |
6 |
catchProcessors.add(catchClause.createProcessor(routeContext)); |
| 66 |
6 |
} |
| 67 |
|
} |
| 68 |
6 |
return new TryProcessor(tryProcessor, catchProcessors, finallyProcessor); |
| 69 |
|
} |
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
public CatchType handle(Class<?> exceptionType) { |
| 74 |
6 |
CatchType answer = new CatchType(exceptionType); |
| 75 |
6 |
addOutput(answer); |
| 76 |
6 |
return answer; |
| 77 |
|
} |
| 78 |
|
|
| 79 |
|
public FinallyType handleAll() { |
| 80 |
0 |
FinallyType answer = new FinallyType(); |
| 81 |
0 |
addOutput(answer); |
| 82 |
0 |
return answer; |
| 83 |
|
} |
| 84 |
|
|
| 85 |
|
public TryType process(Processor processor) { |
| 86 |
6 |
super.process(processor); |
| 87 |
6 |
return this; |
| 88 |
|
} |
| 89 |
|
|
| 90 |
|
public TryType to(Endpoint endpoint) { |
| 91 |
0 |
super.to(endpoint); |
| 92 |
0 |
return this; |
| 93 |
|
} |
| 94 |
|
|
| 95 |
|
public TryType to(Collection<Endpoint> endpoints) { |
| 96 |
0 |
super.to(endpoints); |
| 97 |
0 |
return this; |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
public TryType to(Endpoint... endpoints) { |
| 101 |
0 |
super.to(endpoints); |
| 102 |
0 |
return this; |
| 103 |
|
} |
| 104 |
|
|
| 105 |
|
public TryType to(String uri) { |
| 106 |
6 |
super.to(uri); |
| 107 |
6 |
return this; |
| 108 |
|
} |
| 109 |
|
|
| 110 |
|
public TryType to(String... uris) { |
| 111 |
0 |
super.to(uris); |
| 112 |
0 |
return this; |
| 113 |
|
} |
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
public List<CatchType> getCatchClauses() { |
| 119 |
0 |
if (catchClauses == null) { |
| 120 |
0 |
checkInitialized(); |
| 121 |
|
} |
| 122 |
0 |
return catchClauses; |
| 123 |
|
} |
| 124 |
|
|
| 125 |
|
public FinallyType getFinallyClause() { |
| 126 |
0 |
if (finallyClause == null) { |
| 127 |
0 |
checkInitialized(); |
| 128 |
|
} |
| 129 |
0 |
return finallyClause; |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
public List<ProcessorType> getOutputsWithoutCatches() { |
| 133 |
6 |
if (outputsWithoutCatches == null) { |
| 134 |
6 |
checkInitialized(); |
| 135 |
|
} |
| 136 |
6 |
return outputsWithoutCatches; |
| 137 |
|
} |
| 138 |
|
|
| 139 |
|
public void setOutputs(List<ProcessorType> outputs) { |
| 140 |
0 |
initialized = false; |
| 141 |
0 |
super.setOutputs(outputs); |
| 142 |
0 |
} |
| 143 |
|
|
| 144 |
|
public void addOutput(ProcessorType output) { |
| 145 |
18 |
initialized = false; |
| 146 |
18 |
getOutputs().add(output); |
| 147 |
18 |
} |
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| 152 |
|
protected void checkInitialized() { |
| 153 |
6 |
if (!initialized) { |
| 154 |
6 |
initialized = true; |
| 155 |
6 |
outputsWithoutCatches = new ArrayList<ProcessorType>(); |
| 156 |
6 |
catchClauses = new ArrayList<CatchType>(); |
| 157 |
6 |
finallyClause = null; |
| 158 |
|
|
| 159 |
6 |
for (ProcessorType output : outputs) { |
| 160 |
18 |
if (output instanceof CatchType) { |
| 161 |
6 |
catchClauses.add((CatchType)output); |
| 162 |
6 |
} else if (output instanceof FinallyType) { |
| 163 |
0 |
if (finallyClause != null) { |
| 164 |
0 |
throw new IllegalArgumentException("Multiple finally clauses added: " + finallyClause |
| 165 |
|
+ " and " + output); |
| 166 |
|
} else { |
| 167 |
0 |
finallyClause = (FinallyType)output; |
| 168 |
|
} |
| 169 |
0 |
} else { |
| 170 |
12 |
outputsWithoutCatches.add(output); |
| 171 |
|
} |
| 172 |
18 |
} |
| 173 |
|
} |
| 174 |
6 |
} |
| 175 |
|
} |