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.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.camel.util.ExchangeHelper;
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    
027    /**
028     * The processor which sends messages in a loop.
029     *
030     * @version $Revision: 1315 $
031     */
032    public class LoopProcessor extends DelegateProcessor {
033        public static final String PROP_ITER_COUNT = "CamelIterationCount";
034        public static final String PROP_ITER_INDEX = "CamelIterationIndex";
035    
036        private static final Log LOG = LogFactory.getLog(LoopProcessor.class);
037    
038        private Expression<Exchange> expression;
039    
040        public LoopProcessor(Expression<Exchange> expression, Processor processor) {
041            super(processor);
042            this.expression = expression;
043        }
044    
045        @Override
046        public void process(Exchange exchange) throws Exception {
047            // Intermediate conversion to String is needed when direct conversion to Integer is not available
048            // but evaluation result is a textual representation of a numeric value.
049            String text = ExchangeHelper.convertToType(exchange, String.class, expression.evaluate(exchange));
050            Integer value = ExchangeHelper.convertToType(exchange, Integer.class, text);
051            if (value == null) {
052                // TODO: we should probably catch evaluate/convert exception an set is as fault (after fix for CAMEL-316)
053                throw new RuntimeCamelException("Expression \"" + expression + "\" does not evaluate to an int.");
054            }
055            int count = value.intValue();
056            exchange.setProperty(PROP_ITER_COUNT, count);
057            for (int i = 0; i < count; i++) {
058                LOG.debug("LoopProcessor: iteration #" + i);
059                exchange.setProperty(PROP_ITER_INDEX, i);
060                super.process(exchange);
061            }
062        }
063    
064        @Override
065        public String toString() {
066            return "Loop[for: " + expression + " times do: " + getProcessor() + "]";
067        }
068    
069        public Expression<Exchange> getExpression() {
070            return expression;
071        }
072    }