| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
package org.apache.camel.spring.spi; |
| 19 |
|
|
| 20 |
|
import org.apache.camel.Processor; |
| 21 |
|
import org.apache.camel.RuntimeCamelException; |
| 22 |
|
import org.apache.camel.Exchange; |
| 23 |
|
import org.apache.camel.processor.DelegateProcessor; |
| 24 |
|
import org.apache.camel.spi.Policy; |
| 25 |
|
import org.apache.commons.logging.Log; |
| 26 |
|
import org.apache.commons.logging.LogFactory; |
| 27 |
|
import org.springframework.transaction.TransactionDefinition; |
| 28 |
|
import org.springframework.transaction.TransactionStatus; |
| 29 |
|
import org.springframework.transaction.support.TransactionCallbackWithoutResult; |
| 30 |
|
import org.springframework.transaction.support.TransactionTemplate; |
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
public class SpringTransactionPolicy<E> implements Policy<E> { |
| 38 |
0 |
private static final transient Log log = LogFactory.getLog(SpringTransactionPolicy.class); |
| 39 |
|
|
| 40 |
|
private TransactionTemplate template; |
| 41 |
|
|
| 42 |
0 |
public SpringTransactionPolicy() { |
| 43 |
0 |
} |
| 44 |
|
|
| 45 |
0 |
public SpringTransactionPolicy(TransactionTemplate template) { |
| 46 |
0 |
this.template = template; |
| 47 |
0 |
} |
| 48 |
|
|
| 49 |
|
public Processor wrap(Processor processor) { |
| 50 |
0 |
final TransactionTemplate transactionTemplate = getTemplate(); |
| 51 |
0 |
if (transactionTemplate == null) { |
| 52 |
0 |
log.warn("No TransactionTemplate available so transactions will not be enabled!"); |
| 53 |
0 |
return processor; |
| 54 |
|
} |
| 55 |
|
|
| 56 |
0 |
return new DelegateProcessor(processor) { |
| 57 |
|
|
| 58 |
|
public void process(final Exchange exchange) { |
| 59 |
0 |
transactionTemplate.execute(new TransactionCallbackWithoutResult() { |
| 60 |
0 |
protected void doInTransactionWithoutResult(TransactionStatus status) { |
| 61 |
|
try { |
| 62 |
0 |
processNext(exchange); |
| 63 |
0 |
} catch (Exception e) { |
| 64 |
0 |
throw new RuntimeCamelException(e); |
| 65 |
0 |
} |
| 66 |
0 |
} |
| 67 |
|
}); |
| 68 |
0 |
} |
| 69 |
|
|
| 70 |
|
@Override |
| 71 |
|
public String toString() { |
| 72 |
0 |
return "SpringTransactionPolicy:"+propagationBehaviorToString(transactionTemplate.getPropagationBehavior())+"[" + getNext() + "]"; |
| 73 |
|
} |
| 74 |
|
|
| 75 |
0 |
private String propagationBehaviorToString(int propagationBehavior) { |
| 76 |
0 |
switch( propagationBehavior ) { |
| 77 |
|
case TransactionDefinition.PROPAGATION_MANDATORY: |
| 78 |
0 |
return "PROPAGATION_MANDATORY"; |
| 79 |
|
case TransactionDefinition.PROPAGATION_NESTED: |
| 80 |
0 |
return "PROPAGATION_NESTED"; |
| 81 |
|
case TransactionDefinition.PROPAGATION_NEVER: |
| 82 |
0 |
return "PROPAGATION_NEVER"; |
| 83 |
|
case TransactionDefinition.PROPAGATION_NOT_SUPPORTED: |
| 84 |
0 |
return "PROPAGATION_NOT_SUPPORTED"; |
| 85 |
|
case TransactionDefinition.PROPAGATION_REQUIRED: |
| 86 |
0 |
return "PROPAGATION_REQUIRED"; |
| 87 |
|
case TransactionDefinition.PROPAGATION_REQUIRES_NEW: |
| 88 |
0 |
return "PROPAGATION_REQUIRES_NEW"; |
| 89 |
|
case TransactionDefinition.PROPAGATION_SUPPORTS: |
| 90 |
0 |
return "PROPAGATION_SUPPORTS"; |
| 91 |
|
} |
| 92 |
0 |
return "UNKOWN"; |
| 93 |
|
} |
| 94 |
|
}; |
| 95 |
|
} |
| 96 |
|
|
| 97 |
|
public TransactionTemplate getTemplate() { |
| 98 |
0 |
return template; |
| 99 |
|
} |
| 100 |
|
|
| 101 |
|
public void setTemplate(TransactionTemplate template) { |
| 102 |
0 |
this.template = template; |
| 103 |
0 |
} |
| 104 |
|
} |