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