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: 750806 $
036 */
037 @XmlRootElement(name = "idempotentConsumer")
038 @XmlAccessorType(XmlAccessType.FIELD)
039 public class IdempotentConsumerDefinition extends ExpressionNode {
040 @XmlAttribute
041 private String messageIdRepositoryRef;
042 @XmlTransient
043 private IdempotentRepository idempotentRepository;
044
045 public IdempotentConsumerDefinition() {
046 }
047
048 public IdempotentConsumerDefinition(Expression messageIdExpression, IdempotentRepository idempotentRepository) {
049 super(messageIdExpression);
050 this.idempotentRepository = idempotentRepository;
051 }
052
053 @Override
054 public String toString() {
055 return "IdempotentConsumer[" + getExpression() + " -> " + getOutputs() + "]";
056 }
057
058 @Override
059 public String getShortName() {
060 return "idempotentConsumer";
061 }
062
063 // Fluent API
064 //-------------------------------------------------------------------------
065 /**
066 * Set the expression that IdempotentConsumerType will use
067 * @return the builder
068 */
069 public ExpressionClause<IdempotentConsumerDefinition> expression() {
070 return ExpressionClause.createAndSetExpression(this);
071 }
072
073 /**
074 * Sets the reference name of the message id repository
075 *
076 * @param messageIdRepositoryRef the reference name of message id repository
077 * @return builder
078 */
079 public IdempotentConsumerDefinition messageIdRepositoryRef(String messageIdRepositoryRef) {
080 setMessageIdRepositoryRef(messageIdRepositoryRef);
081 return this;
082 }
083
084 /**
085 * Sets the the message id repository for the IdempotentConsumerType
086 *
087 * @param idempotentRepository the repository instance of idempotent
088 * @return builder
089 */
090 public IdempotentConsumerDefinition messageIdRepository(IdempotentRepository idempotentRepository) {
091 setMessageIdRepository(idempotentRepository);
092 return this;
093 }
094
095 public String getMessageIdRepositoryRef() {
096 return messageIdRepositoryRef;
097 }
098
099 public void setMessageIdRepositoryRef(String messageIdRepositoryRef) {
100 this.messageIdRepositoryRef = messageIdRepositoryRef;
101 }
102
103 public IdempotentRepository getMessageIdRepository() {
104 return idempotentRepository;
105 }
106
107 public void setMessageIdRepository(IdempotentRepository idempotentRepository) {
108 this.idempotentRepository = idempotentRepository;
109 }
110
111 @Override
112 public Processor createProcessor(RouteContext routeContext) throws Exception {
113 Processor childProcessor = routeContext.createProcessor(this);
114 IdempotentRepository idempotentRepository = resolveMessageIdRepository(routeContext);
115 return new IdempotentConsumer(getExpression().createExpression(routeContext), idempotentRepository,
116 childProcessor);
117 }
118
119 /**
120 * Strategy method to resolve the {@link org.apache.camel.spi.IdempotentRepository} to use
121 *
122 * @param routeContext route context
123 * @return the repository
124 */
125 protected IdempotentRepository resolveMessageIdRepository(RouteContext routeContext) {
126 if (idempotentRepository == null) {
127 idempotentRepository = routeContext.lookup(messageIdRepositoryRef, IdempotentRepository.class);
128 }
129 return idempotentRepository;
130 }
131 }