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.blueprint;
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.XmlElement;
023    import javax.xml.bind.annotation.XmlRootElement;
024    import javax.xml.bind.annotation.XmlTransient;
025    
026    import org.apache.camel.CamelContext;
027    import org.apache.camel.LoggingLevel;
028    import org.apache.camel.Processor;
029    import org.apache.camel.builder.DefaultErrorHandlerBuilder;
030    import org.apache.camel.builder.ErrorHandlerBuilder;
031    import org.apache.camel.builder.LoggingErrorHandlerBuilder;
032    import org.apache.camel.core.xml.AbstractCamelFactoryBean;
033    import org.apache.camel.model.RedeliveryPolicyDefinition;
034    import org.apache.camel.processor.RedeliveryPolicy;
035    import org.apache.camel.util.CamelContextHelper;
036    import org.osgi.service.blueprint.container.BlueprintContainer;
037    import org.osgi.service.blueprint.container.NoSuchComponentException;
038    
039    @XmlRootElement(name = "errorHandler")
040    @XmlAccessorType(XmlAccessType.FIELD)
041    public class CamelErrorHandlerFactoryBean extends AbstractCamelFactoryBean<ErrorHandlerBuilder> {
042    
043        @XmlAttribute
044        private ErrorHandlerType type = ErrorHandlerType.DefaultErrorHandler;
045        @XmlAttribute
046        private String deadLetterUri;
047        @XmlAttribute
048        private LoggingLevel level = LoggingLevel.ERROR;
049        @XmlAttribute
050        private String logName;
051        @XmlAttribute
052        private Boolean useOriginalMessage;
053        @XmlAttribute
054        private String onRedeliveryRef;
055        @XmlAttribute
056        private String retryWhileRef;
057        @XmlAttribute
058        private String executorServiceRef;
059        @XmlAttribute
060        private String redeliveryPolicyRef;
061        @XmlElement
062        private RedeliveryPolicyDefinition redeliveryPolicy;
063        @XmlTransient
064        private BlueprintContainer blueprintContainer;
065    
066        @Override
067        public ErrorHandlerBuilder getObject() throws Exception {
068            ErrorHandlerBuilder errorHandler = getObjectType().newInstance();
069            if (errorHandler instanceof DefaultErrorHandlerBuilder) {
070                DefaultErrorHandlerBuilder handler = (DefaultErrorHandlerBuilder) errorHandler;
071                if (deadLetterUri != null) {
072                    handler.setDeadLetterUri(deadLetterUri);
073                }
074                if (useOriginalMessage != null) {
075                    handler.setUseOriginalMessage(useOriginalMessage);
076                }
077                if (redeliveryPolicy != null) {
078                    handler.setRedeliveryPolicy(redeliveryPolicy.createRedeliveryPolicy(getCamelContext(), null));
079                }
080                if (redeliveryPolicyRef != null) {
081                    // lookup redelivery
082                    RedeliveryPolicy policy = CamelContextHelper.mandatoryLookup(getCamelContext(), redeliveryPolicyRef, RedeliveryPolicy.class);
083                    handler.setRedeliveryPolicy(policy);
084                }
085                if (onRedeliveryRef != null) {
086                    handler.setOnRedelivery(lookup(onRedeliveryRef, Processor.class));
087                }
088                if (retryWhileRef != null) {
089                    handler.setRetryWhileRef(retryWhileRef);
090                }
091                if (executorServiceRef != null) {
092                    handler.setExecutorServiceRef(executorServiceRef);
093                }
094            } else if (errorHandler instanceof LoggingErrorHandlerBuilder) {
095                LoggingErrorHandlerBuilder handler = (LoggingErrorHandlerBuilder) errorHandler;
096                if (level != null) {
097                    handler.setLevel(level);
098                }
099                if (logName != null) {
100                    handler.setLogName(logName);
101                }
102            }
103            return errorHandler;
104        }
105    
106        @Override
107        @SuppressWarnings("unchecked")
108        public Class<? extends ErrorHandlerBuilder> getObjectType() {
109            return (Class<ErrorHandlerBuilder>) type.getTypeAsClass();
110        }
111    
112        public void setBlueprintContainer(BlueprintContainer blueprintContainer) {
113            this.blueprintContainer = blueprintContainer;
114        }
115    
116        protected CamelContext getCamelContextWithId(String camelContextId) {
117            if (blueprintContainer != null) {
118                return (CamelContext) blueprintContainer.getComponentInstance(camelContextId);
119            }
120            return null;
121        }
122    
123        protected <T> T lookup(String name, Class<T> type) {
124            try {
125                return type.cast(blueprintContainer.getComponentInstance(name));
126            } catch (NoSuchComponentException e) {
127                return null;
128            }
129        }
130    
131    }