Coverage Report - org.apache.camel.bam.processor.BamProcessorSupport
 
Classes in this File Line Coverage Branch Coverage Complexity
BamProcessorSupport
50% 
33% 
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 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  
  * A base {@link Processor} for working on
 34  
  * <a href="http://activemq.apache.org/camel/bam.html">BAM</a> which a derived class would do the actual
 35  
  * persistence such as the {@link JpaBamProcessor}
 36  
  *
 37  
  * @version $Revision: $
 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  
     // Properties
 94  
     //-----------------------------------------------------------------------
 95  
     public Expression<Exchange> getCorrelationKeyExpression() {
 96  0
         return correlationKeyExpression;
 97  
     }
 98  
 
 99  
     public Class<T> getEntityType() {
 100  2
         return entityType;
 101  
     }
 102  
 
 103  
     // Implemenation methods
 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  
 }