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.CamelContext;
025    import org.apache.camel.processor.RedeliveryPolicy;
026    import org.apache.camel.util.CamelContextHelper;
027    
028    /**
029     * Represents an XML <redeliveryPolicy/> element
030     *
031     * @version $Revision: 62894 $
032     */
033    @XmlRootElement(name = "redeliveryPolicy")
034    @XmlAccessorType(XmlAccessType.FIELD)
035    public class RedeliveryPolicyType {
036        @XmlAttribute()
037        private String ref;
038        @XmlAttribute
039        private Integer maximumRedeliveries;
040        @XmlAttribute
041        private Long initialRedeliveryDelay;
042        @XmlAttribute
043        private Double backOffMultiplier;
044        @XmlAttribute
045        private Boolean useExponentialBackOff;
046        @XmlAttribute
047        private Double collisionAvoidanceFactor;
048        @XmlAttribute
049        private Boolean useCollisionAvoidance;
050        @XmlAttribute
051        private Long maximumRedeliveryDelay;
052        @XmlAttribute
053        private LoggingLevel retriesExhaustedLogLevel;
054        @XmlAttribute
055        private LoggingLevel retryAttemptedLogLevel;
056    
057        public RedeliveryPolicy createRedeliveryPolicy(CamelContext context, RedeliveryPolicy parentPolicy) {
058            if (ref != null) {
059                // lookup in registry if ref provided
060                return CamelContextHelper.mandatoryLookup(context, ref, RedeliveryPolicy.class);
061            }
062    
063            RedeliveryPolicy answer = parentPolicy.copy();
064    
065            // copy across the properties - if they are set
066            if (maximumRedeliveries != null) {
067                answer.setMaximumRedeliveries(maximumRedeliveries);
068            }
069            if (initialRedeliveryDelay != null) {
070                answer.setDelay(initialRedeliveryDelay);
071            }
072            if (retriesExhaustedLogLevel != null) {
073                answer.setRetriesExhaustedLogLevel(retriesExhaustedLogLevel);
074            }
075            if (retryAttemptedLogLevel != null) {
076                answer.setRetryAttemptedLogLevel(retryAttemptedLogLevel);
077            }
078            if (backOffMultiplier != null) {
079                answer.setBackOffMultiplier(backOffMultiplier);
080            }
081            if (useExponentialBackOff != null) {
082                answer.setUseExponentialBackOff(useExponentialBackOff);
083            }
084            if (collisionAvoidanceFactor != null) {
085                answer.setCollisionAvoidanceFactor(collisionAvoidanceFactor);
086            }
087            if (useCollisionAvoidance != null) {
088                answer.setUseCollisionAvoidance(useCollisionAvoidance);
089            }
090            if (maximumRedeliveryDelay != null) {
091                answer.setMaximumRedeliveryDelay(maximumRedeliveryDelay);
092            }
093            return answer;
094        }
095    
096        public String toString() {
097            return "RedeliveryPolicy[maximumRedeliveries: " + maximumRedeliveries + "]";
098        }
099    
100        // Fluent API
101        //-------------------------------------------------------------------------
102        public RedeliveryPolicyType backOffMultiplier(double backOffMultiplier) {
103            setBackOffMultiplier(backOffMultiplier);
104            return this;
105        }
106    
107        public RedeliveryPolicyType collisionAvoidancePercent(double collisionAvoidancePercent) {
108            setCollisionAvoidanceFactor(collisionAvoidancePercent * 0.01d);
109            return this;
110        }
111    
112        public RedeliveryPolicyType collisionAvoidanceFactor(double collisionAvoidanceFactor) {
113            setCollisionAvoidanceFactor(collisionAvoidanceFactor);
114            return this;
115        }
116    
117        public RedeliveryPolicyType initialRedeliveryDelay(long initialRedeliveryDelay) {
118            setInitialRedeliveryDelay(initialRedeliveryDelay);
119            return this;
120        }
121    
122        public RedeliveryPolicyType retriesExhaustedLogLevel(LoggingLevel retriesExhaustedLogLevel) {
123            setRetriesExhaustedLogLevel(retriesExhaustedLogLevel);
124            return this;
125        }    
126        
127        public RedeliveryPolicyType retryAttemptedLogLevel(LoggingLevel retryAttemptedLogLevel) {
128            setRetryAttemptedLogLevel(retryAttemptedLogLevel);
129            return this;
130        }
131            
132        public RedeliveryPolicyType maximumRedeliveries(int maximumRedeliveries) {
133            setMaximumRedeliveries(maximumRedeliveries);
134            return this;
135        }
136    
137        public RedeliveryPolicyType useCollisionAvoidance() {
138            setUseCollisionAvoidance(Boolean.TRUE);
139            return this;
140        }
141    
142        public RedeliveryPolicyType useExponentialBackOff() {
143            setUseExponentialBackOff(Boolean.TRUE);
144            return this;
145        }
146    
147        public RedeliveryPolicyType maximumRedeliveryDelay(long maximumRedeliveryDelay) {
148            setMaximumRedeliveryDelay(maximumRedeliveryDelay);
149            return this;
150        }
151    
152        public RedeliveryPolicyType ref(String ref) {
153            setRef(ref);
154            return this;
155        }
156    
157        // Properties
158        //-------------------------------------------------------------------------
159    
160        public Double getBackOffMultiplier() {
161            return backOffMultiplier;
162        }
163    
164        public void setBackOffMultiplier(Double backOffMultiplier) {
165            this.backOffMultiplier = backOffMultiplier;
166        }
167    
168        public Double getCollisionAvoidanceFactor() {
169            return collisionAvoidanceFactor;
170        }
171    
172        public void setCollisionAvoidanceFactor(Double collisionAvoidanceFactor) {
173            this.collisionAvoidanceFactor = collisionAvoidanceFactor;
174        }
175    
176        public Long getInitialRedeliveryDelay() {
177            return initialRedeliveryDelay;
178        }
179    
180        public void setInitialRedeliveryDelay(Long initialRedeliveryDelay) {
181            this.initialRedeliveryDelay = initialRedeliveryDelay;
182        }
183    
184        public Integer getMaximumRedeliveries() {
185            return maximumRedeliveries;
186        }
187    
188        public void setMaximumRedeliveries(Integer maximumRedeliveries) {
189            this.maximumRedeliveries = maximumRedeliveries;
190        }
191    
192        public Boolean getUseCollisionAvoidance() {
193            return useCollisionAvoidance;
194        }
195    
196        public void setUseCollisionAvoidance(Boolean useCollisionAvoidance) {
197            this.useCollisionAvoidance = useCollisionAvoidance;
198        }
199    
200        public Boolean getUseExponentialBackOff() {
201            return useExponentialBackOff;
202        }
203    
204        public void setUseExponentialBackOff(Boolean useExponentialBackOff) {
205            this.useExponentialBackOff = useExponentialBackOff;
206        }
207    
208        public Long getMaximumRedeliveryDelay() {
209            return maximumRedeliveryDelay;
210        }
211    
212        public void setMaximumRedeliveryDelay(Long maximumRedeliveryDelay) {
213            this.maximumRedeliveryDelay = maximumRedeliveryDelay;
214        }
215    
216        public void setRetriesExhaustedLogLevel(LoggingLevel retriesExhaustedLogLevel) {
217            this.retriesExhaustedLogLevel = retriesExhaustedLogLevel;
218        }
219    
220        public LoggingLevel getRetriesExhaustedLogLevel() {
221            return retriesExhaustedLogLevel;
222        } 
223    
224        public void setRetryAttemptedLogLevel(LoggingLevel retryAttemptedLogLevel) {
225            this.retryAttemptedLogLevel = retryAttemptedLogLevel;
226        }
227    
228        public LoggingLevel getRetryAttemptedLogLevel() {
229            return retryAttemptedLogLevel;
230        }
231    
232        public String getRef() {
233            return ref;
234        }
235    
236        public void setRef(String ref) {
237            this.ref = ref;
238        }
239    }