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 */
017package org.apache.camel.blueprint;
018
019import java.util.Set;
020
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlAttribute;
024import javax.xml.bind.annotation.XmlElement;
025import javax.xml.bind.annotation.XmlRootElement;
026import javax.xml.bind.annotation.XmlTransient;
027
028import org.apache.camel.CamelContext;
029import org.apache.camel.Processor;
030import org.apache.camel.builder.DefaultErrorHandlerBuilder;
031import org.apache.camel.builder.ErrorHandlerBuilder;
032import org.apache.camel.core.xml.AbstractCamelFactoryBean;
033import org.apache.camel.model.RedeliveryPolicyDefinition;
034import org.apache.camel.processor.errorhandler.RedeliveryPolicy;
035import org.apache.camel.reifier.errorhandler.ErrorHandlerReifier;
036import org.osgi.service.blueprint.container.BlueprintContainer;
037
038@XmlRootElement(name = "errorHandler")
039@XmlAccessorType(XmlAccessType.FIELD)
040public class CamelErrorHandlerFactoryBean extends AbstractCamelFactoryBean<ErrorHandlerBuilder> {
041
042    @XmlAttribute
043    private ErrorHandlerType type = ErrorHandlerType.DefaultErrorHandler;
044    @XmlAttribute
045    private String deadLetterUri;
046    @XmlAttribute
047    private Boolean deadLetterHandleNewException;
048    @XmlAttribute
049    private Boolean useOriginalMessage;
050    @XmlAttribute
051    private Boolean useOriginalBody;
052    @XmlAttribute
053    private String onRedeliveryRef;
054    @XmlAttribute
055    private String onPrepareFailureRef;
056    @XmlAttribute
057    private String onExceptionOccurredRef;
058    @XmlAttribute
059    private String retryWhileRef;
060    @XmlAttribute
061    private String executorServiceRef;
062    @XmlAttribute
063    private String redeliveryPolicyRef;
064    @XmlElement
065    private RedeliveryPolicyDefinition redeliveryPolicy;
066    @XmlTransient
067    private BlueprintContainer blueprintContainer;
068
069    @Override
070    public ErrorHandlerBuilder getObject() throws Exception {
071        ErrorHandlerBuilder errorHandler = getObjectType().newInstance();
072        if (errorHandler instanceof DefaultErrorHandlerBuilder) {
073            DefaultErrorHandlerBuilder handler = (DefaultErrorHandlerBuilder) errorHandler;
074            if (deadLetterUri != null) {
075                handler.setDeadLetterUri(deadLetterUri);
076            }
077            if (deadLetterHandleNewException != null) {
078                handler.setDeadLetterHandleNewException(deadLetterHandleNewException);
079            }
080            if (useOriginalMessage != null) {
081                handler.setUseOriginalMessage(useOriginalMessage);
082            }
083            if (useOriginalBody != null) {
084                handler.setUseOriginalBody(useOriginalBody);
085            }
086            if (redeliveryPolicy != null) {
087                handler.setRedeliveryPolicy(ErrorHandlerReifier.createRedeliveryPolicy(redeliveryPolicy, getCamelContext(), null));
088            }
089            if (redeliveryPolicyRef != null) {
090                handler.setRedeliveryPolicy(lookup(redeliveryPolicyRef, RedeliveryPolicy.class));
091            }
092            if (onRedeliveryRef != null) {
093                handler.setOnRedelivery(lookup(onRedeliveryRef, Processor.class));
094            }
095            if (onPrepareFailureRef != null) {
096                handler.setOnPrepareFailure(lookup(onPrepareFailureRef, Processor.class));
097            }
098            if (onExceptionOccurredRef != null) {
099                handler.setOnExceptionOccurred(lookup(onExceptionOccurredRef, Processor.class));
100            }
101            if (retryWhileRef != null) {
102                handler.setRetryWhileRef(retryWhileRef);
103            }
104            if (executorServiceRef != null) {
105                handler.setExecutorServiceRef(executorServiceRef);
106            }
107        }
108        return errorHandler;
109    }
110
111    @Override
112    public Class<? extends ErrorHandlerBuilder> getObjectType() {
113        return type.getTypeAsClass();
114    }
115
116    public void setBlueprintContainer(BlueprintContainer blueprintContainer) {
117        this.blueprintContainer = blueprintContainer;
118    }
119
120    @Override
121    protected CamelContext getCamelContextWithId(String camelContextId) {
122        if (blueprintContainer != null) {
123            return (CamelContext) blueprintContainer.getComponentInstance(camelContextId);
124        }
125        return null;
126    }
127
128    @Override
129    protected CamelContext discoverDefaultCamelContext() {
130        if (blueprintContainer != null) {
131            Set<String> ids = BlueprintCamelContextLookupHelper.lookupBlueprintCamelContext(blueprintContainer);
132            if (ids.size() == 1) {
133                // there is only 1 id for a BlueprintCamelContext so fallback and use this
134                return getCamelContextWithId(ids.iterator().next());
135            }
136        }
137        return null;
138    }
139
140    protected <T> T lookup(String name, Class<T> type) {
141        return type.cast(blueprintContainer.getComponentInstance(name));
142    }
143
144}