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    import javax.xml.bind.annotation.XmlTransient;
024    
025    import org.apache.camel.Expression;
026    import org.apache.camel.Processor;
027    import org.apache.camel.builder.ExpressionClause;
028    import org.apache.camel.processor.idempotent.IdempotentConsumer;
029    import org.apache.camel.spi.IdempotentRepository;
030    import org.apache.camel.spi.RouteContext;
031    
032    /**
033     * Represents an XML <idempotentConsumer/> element
034     *
035     * @version $Revision: 782534 $
036     */
037    @XmlRootElement(name = "idempotentConsumer")
038    @XmlAccessorType(XmlAccessType.FIELD)
039    public class IdempotentConsumerDefinition extends ExpressionNode {
040        @XmlAttribute
041        private String messageIdRepositoryRef;
042        @XmlAttribute
043        private Boolean eager = Boolean.TRUE;
044        @XmlTransient
045        private IdempotentRepository idempotentRepository;
046    
047        public IdempotentConsumerDefinition() {
048        }
049    
050        public IdempotentConsumerDefinition(Expression messageIdExpression, IdempotentRepository idempotentRepository) {
051            super(messageIdExpression);
052            this.idempotentRepository = idempotentRepository;
053        }
054    
055        @Override
056        public String toString() {
057            return "IdempotentConsumer[" + getExpression() + " -> " + getOutputs() + "]";
058        }
059    
060        @Override
061        public String getShortName() {
062            return "idempotentConsumer";
063        }
064        
065        // Fluent API
066        //-------------------------------------------------------------------------
067        /**
068         * Set the expression that IdempotentConsumerType will use
069         * @return the builder
070         */
071        public ExpressionClause<IdempotentConsumerDefinition> expression() {
072            return ExpressionClause.createAndSetExpression(this);
073        }
074        
075        /**
076         * Sets the reference name of the message id repository
077         *
078         * @param messageIdRepositoryRef  the reference name of message id repository
079         * @return builder
080         */
081        public IdempotentConsumerDefinition messageIdRepositoryRef(String messageIdRepositoryRef) {
082            setMessageIdRepositoryRef(messageIdRepositoryRef);
083            return this;
084        }
085        
086        /**
087         * Sets the the message id repository for the IdempotentConsumerType
088         *
089         * @param idempotentRepository  the repository instance of idempotent
090         * @return builder
091         */
092        public IdempotentConsumerDefinition messageIdRepository(IdempotentRepository idempotentRepository) {
093            setMessageIdRepository(idempotentRepository);
094            return this;
095        }
096    
097        /**
098         * Sets whether to eagerly add the key to the idempotent repository or wait until the exchange
099         * is complete. Eager is default enabled.
100         *
101         * @param eager  <tt>true</tt> to add the key before processing, <tt>false</tt> to wait until
102         * the exchange is complete.
103         * @return builder
104         */
105        public IdempotentConsumerDefinition eager(boolean eager) {
106            setEager(eager);
107            return this;
108        }
109    
110        public String getMessageIdRepositoryRef() {
111            return messageIdRepositoryRef;
112        }
113    
114        public void setMessageIdRepositoryRef(String messageIdRepositoryRef) {
115            this.messageIdRepositoryRef = messageIdRepositoryRef;
116        }
117    
118        public IdempotentRepository getMessageIdRepository() {
119            return idempotentRepository;
120        }
121    
122        public void setMessageIdRepository(IdempotentRepository idempotentRepository) {
123            this.idempotentRepository = idempotentRepository;
124        }
125    
126        public Boolean isEager() {
127            return eager;
128        }
129    
130        public void setEager(Boolean eager) {
131            this.eager = eager;
132        }
133    
134        @Override
135        public Processor createProcessor(RouteContext routeContext) throws Exception {
136            Processor childProcessor = routeContext.createProcessor(this);
137            IdempotentRepository idempotentRepository = resolveMessageIdRepository(routeContext);
138            Expression expression = getExpression().createExpression(routeContext);
139            return new IdempotentConsumer(expression, idempotentRepository, eager, childProcessor);
140        }
141    
142        /**
143         * Strategy method to resolve the {@link org.apache.camel.spi.IdempotentRepository} to use
144         *
145         * @param routeContext  route context
146         * @return the repository
147         */
148        protected IdempotentRepository resolveMessageIdRepository(RouteContext routeContext) {
149            if (idempotentRepository == null) {
150                idempotentRepository = routeContext.lookup(messageIdRepositoryRef, IdempotentRepository.class);
151            }
152            return idempotentRepository;
153        }
154    }