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.LoggingLevel; 029import org.apache.camel.Processor; 030import org.apache.camel.builder.DefaultErrorHandlerBuilder; 031import org.apache.camel.builder.ErrorHandlerBuilder; 032import org.apache.camel.builder.LoggingErrorHandlerBuilder; 033import org.apache.camel.core.xml.AbstractCamelFactoryBean; 034import org.apache.camel.model.RedeliveryPolicyDefinition; 035import org.apache.camel.processor.RedeliveryPolicy; 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 LoggingLevel level; 050 @XmlAttribute 051 private String logName; 052 @XmlAttribute 053 private Boolean useOriginalMessage; 054 @XmlAttribute 055 private String onRedeliveryRef; 056 @XmlAttribute 057 private String onPrepareFailureRef; 058 @XmlAttribute 059 private String onExceptionOccurredRef; 060 @XmlAttribute 061 private String retryWhileRef; 062 @XmlAttribute 063 private String executorServiceRef; 064 @XmlAttribute 065 private String redeliveryPolicyRef; 066 @XmlElement 067 private RedeliveryPolicyDefinition redeliveryPolicy; 068 @XmlTransient 069 private BlueprintContainer blueprintContainer; 070 071 @Override 072 public ErrorHandlerBuilder getObject() throws Exception { 073 ErrorHandlerBuilder errorHandler = getObjectType().newInstance(); 074 if (errorHandler instanceof DefaultErrorHandlerBuilder) { 075 DefaultErrorHandlerBuilder handler = (DefaultErrorHandlerBuilder) errorHandler; 076 if (deadLetterUri != null) { 077 handler.setDeadLetterUri(deadLetterUri); 078 } 079 if (deadLetterHandleNewException != null) { 080 handler.setDeadLetterHandleNewException(deadLetterHandleNewException); 081 } 082 if (useOriginalMessage != null) { 083 handler.setUseOriginalMessage(useOriginalMessage); 084 } 085 if (redeliveryPolicy != null) { 086 handler.setRedeliveryPolicy(redeliveryPolicy.createRedeliveryPolicy(getCamelContext(), null)); 087 } 088 if (redeliveryPolicyRef != null) { 089 handler.setRedeliveryPolicy(lookup(redeliveryPolicyRef, RedeliveryPolicy.class)); 090 } 091 if (onRedeliveryRef != null) { 092 handler.setOnRedelivery(lookup(onRedeliveryRef, Processor.class)); 093 } 094 if (onPrepareFailureRef != null) { 095 handler.setOnPrepareFailure(lookup(onPrepareFailureRef, Processor.class)); 096 } 097 if (onExceptionOccurredRef != null) { 098 handler.setOnExceptionOccurred(lookup(onExceptionOccurredRef, Processor.class)); 099 } 100 if (retryWhileRef != null) { 101 handler.setRetryWhileRef(retryWhileRef); 102 } 103 if (executorServiceRef != null) { 104 handler.setExecutorServiceRef(executorServiceRef); 105 } 106 } else if (errorHandler instanceof LoggingErrorHandlerBuilder) { 107 LoggingErrorHandlerBuilder handler = (LoggingErrorHandlerBuilder) errorHandler; 108 if (level != null) { 109 handler.setLevel(level); 110 } 111 if (logName != null) { 112 handler.setLogName(logName); 113 } 114 } 115 return errorHandler; 116 } 117 118 @Override 119 public Class<? extends ErrorHandlerBuilder> getObjectType() { 120 return type.getTypeAsClass(); 121 } 122 123 public void setBlueprintContainer(BlueprintContainer blueprintContainer) { 124 this.blueprintContainer = blueprintContainer; 125 } 126 127 protected CamelContext getCamelContextWithId(String camelContextId) { 128 if (blueprintContainer != null) { 129 return (CamelContext) blueprintContainer.getComponentInstance(camelContextId); 130 } 131 return null; 132 } 133 134 @Override 135 protected CamelContext discoverDefaultCamelContext() { 136 if (blueprintContainer != null) { 137 Set<String> ids = BlueprintCamelContextLookupHelper.lookupBlueprintCamelContext(blueprintContainer); 138 if (ids.size() == 1) { 139 // there is only 1 id for a BlueprintCamelContext so fallback and use this 140 return getCamelContextWithId(ids.iterator().next()); 141 } 142 } 143 return null; 144 } 145 146 protected <T> T lookup(String name, Class<T> type) { 147 return type.cast(blueprintContainer.getComponentInstance(name)); 148 } 149 150}