Coverage Report - org.apache.camel.bam.BamProcessorSupport
 
Classes in this File Line Coverage Branch Coverage Complexity
BamProcessorSupport
0% 
0% 
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;
 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.support.TransactionTemplate;
 26  
 import org.springframework.transaction.support.TransactionCallbackWithoutResult;
 27  
 import org.springframework.transaction.support.TransactionCallback;
 28  
 import org.springframework.transaction.TransactionStatus;
 29  
 import org.springframework.transaction.TransactionException;
 30  
 
 31  
 import java.lang.reflect.Type;
 32  
 import java.lang.reflect.ParameterizedType;
 33  
 
 34  
 /**
 35  0
  * A {@link Processor} for working on
 36  
  * <a href="http://activemq.apache.org/camel/bam.html">BAM</a>
 37  
  *
 38  
  * @version $Revision: $
 39  
  */
 40  0
 public abstract class BamProcessorSupport<T> implements Processor {
 41  0
     private static final transient Log log = LogFactory.getLog(BamProcessorSupport.class);
 42  0
 
 43  
     private Class<T> entityType;
 44  0
     private Expression<Exchange> correlationKeyExpression;
 45  0
     private TransactionTemplate transactionTemplate;
 46  0
 
 47  0
 
 48  0
     protected BamProcessorSupport(TransactionTemplate transactionTemplate, Expression<Exchange> correlationKeyExpression) {
 49  0
         this.transactionTemplate = transactionTemplate;
 50  0
         this.correlationKeyExpression = correlationKeyExpression;
 51  0
 
 52  0
         Type type = getClass().getGenericSuperclass();
 53  0
         if (type instanceof ParameterizedType) {
 54  0
             ParameterizedType parameterizedType = (ParameterizedType) type;
 55  0
             Type[] arguments = parameterizedType.getActualTypeArguments();
 56  0
             if (arguments.length > 0) {
 57  0
                 Type argumentType = arguments[0];
 58  0
                 if (argumentType instanceof Class) {
 59  0
                     this.entityType = (Class<T>) argumentType;
 60  0
                 }
 61  0
             }
 62  0
         }
 63  0
         if (entityType == null) {
 64  0
             throw new IllegalArgumentException("Could not infer the entity type!");
 65  
         }
 66  0
     }
 67  
 
 68  0
     protected BamProcessorSupport(TransactionTemplate transactionTemplate, Expression<Exchange> correlationKeyExpression, Class<T> entitytype) {
 69  0
         this.transactionTemplate = transactionTemplate;
 70  0
         this.entityType = entitytype;
 71  0
         this.correlationKeyExpression = correlationKeyExpression;
 72  0
     }
 73  
 
 74  0
     public void process(final Exchange exchange) {
 75  0
         try {
 76  0
             Object entity = transactionTemplate.execute(new TransactionCallback() {
 77  0
                 public Object doInTransaction(TransactionStatus status) {
 78  
                     try {
 79  0
                         Object key = getCorrelationKey(exchange);
 80  0
 
 81  0
                         T entity = loadEntity(exchange, key);
 82  
 
 83  0
                         log.info("Correlation key: " + key + " with entity: " + entity);
 84  
 
 85  0
                         //storeProcessInExchange(exchange, entity);
 86  0
                         processEntity(exchange, entity);
 87  
 
 88  0
                         return entity;
 89  
                     }
 90  0
                     catch (Exception e) {
 91  0
                         throw new RuntimeCamelException(e);
 92  
                     }
 93  
             }});
 94  
 
 95  0
             log.info("After transaction process instance is: " + entity);
 96  0
         }
 97  0
         catch (Throwable e) {
 98  0
             log.error("Caught: " + e, e);            
 99  0
         }
 100  0
     }
 101  
 
 102  
     // Properties
 103  
     //-----------------------------------------------------------------------
 104  
     public Expression<Exchange> getCorrelationKeyExpression() {
 105  0
         return correlationKeyExpression;
 106  
     }
 107  
 
 108  
 
 109  
     public Class<T> getEntityType() {
 110  0
         return entityType;
 111  
     }
 112  
 
 113  
     // Implemenation methods
 114  
     //-----------------------------------------------------------------------
 115  
     protected abstract void processEntity(Exchange exchange, T entity) throws Exception;
 116  
 
 117  
     protected abstract T loadEntity(Exchange exchange, Object key);
 118  
 
 119  
 
 120  
     protected Object getCorrelationKey(Exchange exchange) throws NoCorrelationKeyException {
 121  0
         Object value = correlationKeyExpression.evaluate(exchange);
 122  0
         if (value == null) {
 123  0
             throw new NoCorrelationKeyException(this, exchange);
 124  
         }
 125  0
         return value;
 126  
     }
 127  
 }