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