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