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