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 */
017package org.apache.camel.model;
018
019import javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlRootElement;
023
024import org.apache.camel.Expression;
025import org.apache.camel.Processor;
026import org.apache.camel.model.language.ExpressionDefinition;
027import org.apache.camel.processor.LoopProcessor;
028import org.apache.camel.spi.Metadata;
029import org.apache.camel.spi.RouteContext;
030
031/**
032 * Processes a message multiple times
033 *
034 * @version 
035 */
036@Metadata(label = "eip,routing")
037@XmlRootElement(name = "loop")
038@XmlAccessorType(XmlAccessType.FIELD)
039public class LoopDefinition extends ExpressionNode {
040
041    @XmlAttribute
042    private Boolean copy;
043
044    public LoopDefinition() {
045    }
046
047    public LoopDefinition(Expression expression) {
048        super(expression);
049    }
050
051    public LoopDefinition(ExpressionDefinition expression) {
052        super(expression);
053    }
054
055    /**
056     * Enables copy mode so a copy of the input Exchange is used for each iteration.
057     * @return the builder
058     */
059    public LoopDefinition copy() {
060        setCopy(true);
061        return this;
062    }
063
064    public Boolean getCopy() {
065        return copy;
066    }
067
068    /**
069     * If the copy attribute is true, a copy of the input Exchange is used for each iteration.
070     * That means each iteration will start from a copy of the same message.
071     * <p/>
072     * By default loop will loop the same exchange all over, so each iteration may
073     * have different message content.
074     */
075    public void setCopy(Boolean copy) {
076        this.copy = copy;
077    }
078
079    @Override
080    public String toString() {
081        return "Loop[" + getExpression() + " -> " + getOutputs() + "]";
082    }
083    
084    @Override
085    public String getLabel() {
086        return "loop[" + getExpression() + "]";
087    }
088    
089    @Override
090    public Processor createProcessor(RouteContext routeContext) throws Exception {
091        Processor output = this.createChildProcessor(routeContext, true);
092        boolean isCopy = getCopy() != null && getCopy();
093        return new LoopProcessor(output, getExpression().createExpression(routeContext), isCopy);
094    }
095
096    /**
097     * Expression to define how many times we should loop. Notice the expression is only evaluated once, and should return
098     * a number as how many times to loop. A value of zero or negative means no looping. The loop is like a for-loop fashion,
099     * if you want a while loop, then the dynamic router may be a better choice.
100     */
101    @Override
102    public void setExpression(ExpressionDefinition expression) {
103        // override to include javadoc what the expression is used for
104        super.setExpression(expression);
105    }
106}