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.model;
018    
019    import javax.xml.bind.annotation.XmlAccessType;
020    import javax.xml.bind.annotation.XmlAccessorType;
021    import javax.xml.bind.annotation.XmlAttribute;
022    import javax.xml.bind.annotation.XmlRootElement;
023    
024    import org.apache.camel.Expression;
025    import org.apache.camel.Processor;
026    import org.apache.camel.builder.ExpressionClause;
027    import org.apache.camel.model.language.ExpressionDefinition;
028    import org.apache.camel.processor.Delayer;
029    import org.apache.camel.spi.RouteContext;
030    import org.apache.camel.util.ObjectHelper;
031    
032    /**
033     * Represents an XML <delay/> element
034     *
035     * @version $Revision: 751373 $
036     */
037    @XmlRootElement(name = "delay")
038    @XmlAccessorType(XmlAccessType.FIELD)
039    public class DelayDefinition extends ExpressionNode {
040        @XmlAttribute
041        private Long delayTime = 0L;
042    
043        public DelayDefinition() {
044        }
045    
046        public DelayDefinition(Expression processAtExpression) {
047            super(processAtExpression);
048        }
049    
050        public DelayDefinition(ExpressionDefinition processAtExpression) {
051            super(processAtExpression);
052        }
053    
054        public DelayDefinition(Expression processAtExpression, long delayTime) {
055            super(processAtExpression);
056            this.delayTime = delayTime;
057        }
058    
059        @Override
060        public String toString() {
061            return "Delay[on: " + getExpression() + " delay: " + delayTime + " -> " + getOutputs() + "]";
062        }
063        
064        // Fluent API
065        // -------------------------------------------------------------------------
066    
067        /**
068         * Sets the delay time in millis to delay
069         * @param delay delay time in millis
070         * @return the builder
071         */
072        public DelayDefinition delayTime(Long delay) {
073            setDelayTime(delay);
074            return this;
075        }
076        
077        /**
078         * Set the expression that the delayer will use
079         * @return the builder
080         */
081        public ExpressionClause<DelayDefinition> expression() {
082            return ExpressionClause.createAndSetExpression(this);
083        }
084    
085        @Override
086        public String getShortName() {
087            return "delay";
088        }
089    
090        public Long getDelayTime() {
091            return delayTime;
092        }
093    
094        public void setDelayTime(Long delayTime) {
095            this.delayTime = delayTime;
096        }   
097        
098    
099        @Override
100        public Processor createProcessor(RouteContext routeContext) throws Exception {
101            Processor childProcessor = routeContext.createProcessor(this);
102            Expression processAtExpression = createAbsoluteTimeDelayExpression(routeContext);
103            return new Delayer(childProcessor, processAtExpression, delayTime);
104        }
105    
106        private Expression createAbsoluteTimeDelayExpression(RouteContext routeContext) {
107            ExpressionDefinition expr = getExpression();
108            if (expr != null) {
109                if (ObjectHelper.isNotEmpty(expr.getExpression())
110                    || expr.getExpressionValue() != null) {
111                    return expr.createExpression(routeContext);
112                } 
113            } 
114            return null;
115        }
116    }