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