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.Predicate; 026import org.apache.camel.Processor; 027import org.apache.camel.model.language.ExpressionDefinition; 028import org.apache.camel.processor.LoopProcessor; 029import org.apache.camel.spi.AsPredicate; 030import org.apache.camel.spi.Metadata; 031import org.apache.camel.spi.RouteContext; 032 033/** 034 * Processes a message multiple times 035 * 036 * @version 037 */ 038@Metadata(label = "eip,routing") 039@XmlRootElement(name = "loop") 040@XmlAccessorType(XmlAccessType.FIELD) 041public class LoopDefinition extends ExpressionNode { 042 043 @XmlAttribute 044 private Boolean copy; 045 @XmlAttribute 046 private Boolean doWhile; 047 048 public LoopDefinition() { 049 } 050 051 public LoopDefinition(Expression expression) { 052 super(expression); 053 } 054 055 public LoopDefinition(Predicate predicate) { 056 super(predicate); 057 setDoWhile(true); 058 } 059 060 public LoopDefinition(ExpressionDefinition expression) { 061 super(expression); 062 } 063 064 /** 065 * Enables copy mode so a copy of the input Exchange is used for each iteration. 066 * @return the builder 067 */ 068 public LoopDefinition copy() { 069 setCopy(true); 070 return this; 071 } 072 073 public Boolean getCopy() { 074 return copy; 075 } 076 077 public Boolean getDoWhile() { 078 return doWhile; 079 } 080 081 /** 082 * Enables the while loop that loops until the predicate evaluates to false or null. 083 */ 084 public void setDoWhile(Boolean doWhile) { 085 this.doWhile = doWhile; 086 } 087 088 /** 089 * If the copy attribute is true, a copy of the input Exchange is used for each iteration. 090 * That means each iteration will start from a copy of the same message. 091 * <p/> 092 * By default loop will loop the same exchange all over, so each iteration may 093 * have different message content. 094 */ 095 public void setCopy(Boolean copy) { 096 this.copy = copy; 097 } 098 099 @Override 100 public String toString() { 101 return "Loop[" + getExpression() + " -> " + getOutputs() + "]"; 102 } 103 104 @Override 105 public String getLabel() { 106 return "loop[" + getExpression() + "]"; 107 } 108 109 @Override 110 public Processor createProcessor(RouteContext routeContext) throws Exception { 111 Processor output = this.createChildProcessor(routeContext, true); 112 boolean isCopy = getCopy() != null && getCopy(); 113 boolean isWhile = getDoWhile() != null && getDoWhile(); 114 115 Predicate predicate = null; 116 Expression expression = null; 117 if (isWhile) { 118 predicate = getExpression().createPredicate(routeContext); 119 } else { 120 expression = getExpression().createExpression(routeContext); 121 } 122 return new LoopProcessor(output, expression, predicate, isCopy); 123 } 124 125 /** 126 * Expression to define how many times we should loop. Notice the expression is only evaluated once, and should return 127 * 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, 128 * if you want a while loop, then the dynamic router may be a better choice. 129 */ 130 @Override 131 public void setExpression(ExpressionDefinition expression) { 132 // override to include javadoc what the expression is used for 133 super.setExpression(expression); 134 } 135}