| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.bam.processor; |
| 18 |
|
|
| 19 |
|
import org.apache.camel.Exchange; |
| 20 |
|
import org.apache.camel.Expression; |
| 21 |
|
import org.apache.camel.Processor; |
| 22 |
|
import org.apache.camel.RuntimeCamelException; |
| 23 |
|
import org.apache.commons.logging.Log; |
| 24 |
|
import org.apache.commons.logging.LogFactory; |
| 25 |
|
import org.springframework.transaction.TransactionStatus; |
| 26 |
|
import org.springframework.transaction.support.TransactionCallback; |
| 27 |
|
import org.springframework.transaction.support.TransactionTemplate; |
| 28 |
|
|
| 29 |
|
import java.lang.reflect.ParameterizedType; |
| 30 |
|
import java.lang.reflect.Type; |
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
1 |
public abstract class BamProcessorSupport<T> implements Processor { |
| 40 |
1 |
private static final transient Log log = LogFactory.getLog(BamProcessorSupport.class); |
| 41 |
|
private Class<T> entityType; |
| 42 |
|
private Expression<Exchange> correlationKeyExpression; |
| 43 |
|
private TransactionTemplate transactionTemplate; |
| 44 |
|
|
| 45 |
0 |
protected BamProcessorSupport(TransactionTemplate transactionTemplate, Expression<Exchange> correlationKeyExpression) { |
| 46 |
0 |
this.transactionTemplate = transactionTemplate; |
| 47 |
0 |
this.correlationKeyExpression = correlationKeyExpression; |
| 48 |
|
|
| 49 |
0 |
Type type = getClass().getGenericSuperclass(); |
| 50 |
0 |
if (type instanceof ParameterizedType) { |
| 51 |
0 |
ParameterizedType parameterizedType = (ParameterizedType) type; |
| 52 |
0 |
Type[] arguments = parameterizedType.getActualTypeArguments(); |
| 53 |
0 |
if (arguments.length > 0) { |
| 54 |
0 |
Type argumentType = arguments[0]; |
| 55 |
0 |
if (argumentType instanceof Class) { |
| 56 |
0 |
this.entityType = (Class<T>) argumentType; |
| 57 |
|
} |
| 58 |
|
} |
| 59 |
|
} |
| 60 |
0 |
if (entityType == null) { |
| 61 |
0 |
throw new IllegalArgumentException("Could not infer the entity type!"); |
| 62 |
|
} |
| 63 |
0 |
} |
| 64 |
|
|
| 65 |
3 |
protected BamProcessorSupport(TransactionTemplate transactionTemplate, Expression<Exchange> correlationKeyExpression, Class<T> entitytype) { |
| 66 |
3 |
this.transactionTemplate = transactionTemplate; |
| 67 |
3 |
this.entityType = entitytype; |
| 68 |
3 |
this.correlationKeyExpression = correlationKeyExpression; |
| 69 |
3 |
} |
| 70 |
|
|
| 71 |
|
public void process(final Exchange exchange) { |
| 72 |
1 |
transactionTemplate.execute(new TransactionCallback() { |
| 73 |
1 |
public Object doInTransaction(TransactionStatus status) { |
| 74 |
|
try { |
| 75 |
1 |
Object key = getCorrelationKey(exchange); |
| 76 |
|
|
| 77 |
1 |
T entity = loadEntity(exchange, key); |
| 78 |
|
|
| 79 |
1 |
if (log.isDebugEnabled()) { |
| 80 |
0 |
log.debug("Correlation key: " + key + " with entity: " + entity); |
| 81 |
|
} |
| 82 |
1 |
processEntity(exchange, entity); |
| 83 |
|
|
| 84 |
1 |
return entity; |
| 85 |
|
} |
| 86 |
0 |
catch (Exception e) { |
| 87 |
0 |
throw new RuntimeCamelException(e); |
| 88 |
|
} |
| 89 |
|
} |
| 90 |
|
}); |
| 91 |
1 |
} |
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
public Expression<Exchange> getCorrelationKeyExpression() { |
| 96 |
0 |
return correlationKeyExpression; |
| 97 |
|
} |
| 98 |
|
|
| 99 |
|
public Class<T> getEntityType() { |
| 100 |
2 |
return entityType; |
| 101 |
|
} |
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
protected abstract void processEntity(Exchange exchange, T entity) throws Exception; |
| 106 |
|
|
| 107 |
|
protected abstract T loadEntity(Exchange exchange, Object key); |
| 108 |
|
|
| 109 |
|
protected Object getCorrelationKey(Exchange exchange) throws NoCorrelationKeyException { |
| 110 |
1 |
Object value = correlationKeyExpression.evaluate(exchange); |
| 111 |
1 |
if (value == null) { |
| 112 |
0 |
throw new NoCorrelationKeyException(this, exchange); |
| 113 |
|
} |
| 114 |
1 |
return value; |
| 115 |
|
} |
| 116 |
|
} |