001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.bam.processor;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.Expression;
021    import org.apache.camel.Processor;
022    import org.apache.camel.RuntimeCamelException;
023    import org.apache.commons.logging.Log;
024    import org.apache.commons.logging.LogFactory;
025    import org.springframework.transaction.TransactionStatus;
026    import org.springframework.transaction.support.TransactionCallback;
027    import org.springframework.transaction.support.TransactionTemplate;
028    
029    import java.lang.reflect.ParameterizedType;
030    import java.lang.reflect.Type;
031    
032    /**
033     * A base {@link Processor} for working on
034     * <a href="http://activemq.apache.org/camel/bam.html">BAM</a> which a derived class would do the actual
035     * persistence such as the {@link JpaBamProcessor}
036     *
037     * @version $Revision: $
038     */
039    public abstract class BamProcessorSupport<T> implements Processor {
040        private static final transient Log log = LogFactory.getLog(BamProcessorSupport.class);
041        private Class<T> entityType;
042        private Expression<Exchange> correlationKeyExpression;
043        private TransactionTemplate transactionTemplate;
044    
045        protected BamProcessorSupport(TransactionTemplate transactionTemplate, Expression<Exchange> correlationKeyExpression) {
046            this.transactionTemplate = transactionTemplate;
047            this.correlationKeyExpression = correlationKeyExpression;
048    
049            Type type = getClass().getGenericSuperclass();
050            if (type instanceof ParameterizedType) {
051                ParameterizedType parameterizedType = (ParameterizedType) type;
052                Type[] arguments = parameterizedType.getActualTypeArguments();
053                if (arguments.length > 0) {
054                    Type argumentType = arguments[0];
055                    if (argumentType instanceof Class) {
056                        this.entityType = (Class<T>) argumentType;
057                    }
058                }
059            }
060            if (entityType == null) {
061                throw new IllegalArgumentException("Could not infer the entity type!");
062            }
063        }
064    
065        protected BamProcessorSupport(TransactionTemplate transactionTemplate, Expression<Exchange> correlationKeyExpression, Class<T> entitytype) {
066            this.transactionTemplate = transactionTemplate;
067            this.entityType = entitytype;
068            this.correlationKeyExpression = correlationKeyExpression;
069        }
070    
071        public void process(final Exchange exchange) {
072            transactionTemplate.execute(new TransactionCallback() {
073                public Object doInTransaction(TransactionStatus status) {
074                    try {
075                        Object key = getCorrelationKey(exchange);
076    
077                        T entity = loadEntity(exchange, key);
078    
079                        if (log.isDebugEnabled()) {
080                            log.debug("Correlation key: " + key + " with entity: " + entity);
081                        }
082                        processEntity(exchange, entity);
083    
084                        return entity;
085                    }
086                    catch (Exception e) {
087                        throw new RuntimeCamelException(e);
088                    }
089                }
090            });
091        }
092    
093        // Properties
094        //-----------------------------------------------------------------------
095        public Expression<Exchange> getCorrelationKeyExpression() {
096            return correlationKeyExpression;
097        }
098    
099        public Class<T> getEntityType() {
100            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            Object value = correlationKeyExpression.evaluate(exchange);
111            if (value == null) {
112                throw new NoCorrelationKeyException(this, exchange);
113            }
114            return value;
115        }
116    }