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 redeliveryPolicyRef;
059 @XmlElement
060 private RedeliveryPolicyDefinition redeliveryPolicy;
061 @XmlTransient
062 private BlueprintContainer blueprintContainer;
063
064 @Override
065 public ErrorHandlerBuilder getObject() throws Exception {
066 ErrorHandlerBuilder errorHandler = getObjectType().newInstance();
067 if (errorHandler instanceof DefaultErrorHandlerBuilder) {
068 DefaultErrorHandlerBuilder handler = (DefaultErrorHandlerBuilder) errorHandler;
069 if (deadLetterUri != null) {
070 handler.setDeadLetterUri(deadLetterUri);
071 }
072 if (useOriginalMessage != null) {
073 handler.setUseOriginalMessage(useOriginalMessage);
074 }
075 if (redeliveryPolicy != null) {
076 handler.setRedeliveryPolicy(redeliveryPolicy.createRedeliveryPolicy(getCamelContext(), null));
077 }
078 if (redeliveryPolicyRef != null) {
079 // lookup redelivery
080 RedeliveryPolicy policy = CamelContextHelper.mandatoryLookup(getCamelContext(), redeliveryPolicyRef, RedeliveryPolicy.class);
081 handler.setRedeliveryPolicy(policy);
082 }
083 if (onRedeliveryRef != null) {
084 handler.setOnRedelivery(lookup(onRedeliveryRef, Processor.class));
085 }
086 if (retryWhileRef != null) {
087 handler.setRetryWhileRef(retryWhileRef);
088 }
089 } else if (errorHandler instanceof LoggingErrorHandlerBuilder) {
090 LoggingErrorHandlerBuilder handler = (LoggingErrorHandlerBuilder) errorHandler;
091 if (level != null) {
092 handler.setLevel(level);
093 }
094 if (logName != null) {
095 handler.setLogName(logName);
096 }
097 }
098 return errorHandler;
099 }
100
101 @Override
102 @SuppressWarnings("unchecked")
103 public Class<? extends ErrorHandlerBuilder> getObjectType() {
104 return (Class<ErrorHandlerBuilder>) type.getTypeAsClass();
105 }
106
107 public void setBlueprintContainer(BlueprintContainer blueprintContainer) {
108 this.blueprintContainer = blueprintContainer;
109 }
110
111 protected CamelContext getCamelContextWithId(String camelContextId) {
112 if (blueprintContainer != null) {
113 return (CamelContext) blueprintContainer.getComponentInstance(camelContextId);
114 }
115 return null;
116 }
117
118 protected <T> T lookup(String name, Class<T> type) {
119 try {
120 return type.cast(blueprintContainer.getComponentInstance(name));
121 } catch (NoSuchComponentException e) {
122 return null;
123 }
124 }
125
126 }