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.spring; 018 019import org.apache.camel.CamelContext; 020import org.apache.camel.builder.RouteBuilder; 021import org.apache.camel.spring.spi.SpringTransactionPolicy; 022import org.apache.camel.spring.spi.TransactionErrorHandlerBuilder; 023import org.springframework.context.ApplicationContext; 024import org.springframework.context.ApplicationContextAware; 025import org.springframework.transaction.PlatformTransactionManager; 026import org.springframework.transaction.support.TransactionTemplate; 027 028/** 029 * An extension of the {@link RouteBuilder} to provide some additional helper 030 * methods 031 */ 032public abstract class SpringRouteBuilder extends RouteBuilder implements ApplicationContextAware { 033 private ApplicationContext applicationContext; 034 035 /** 036 * Looks up the bean with the given name in the application context and 037 * returns it, or throws an exception if the bean is not present or is not 038 * of the given type 039 * 040 * @param beanName the name of the bean in the application context 041 * @param type the type of the bean 042 * @return the bean 043 */ 044 public <T> T lookup(String beanName, Class<T> type) { 045 ApplicationContext context = getApplicationContext(); 046 return context.getBean(beanName, type); 047 } 048 049 /** 050 * Looks up the bean with the given type in the application context and 051 * returns it, or throws an exception if the bean is not present or there 052 * are multiple possible beans to choose from for the given type 053 * 054 * @param type the type of the bean 055 * @return the bean 056 */ 057 public <T> T lookup(Class<T> type) { 058 ApplicationContext context = getApplicationContext(); 059 return context.getBean(type); 060 } 061 062 /** 063 * Returns the application context which has been configured via the 064 * {@link #setApplicationContext(ApplicationContext)} method or from the 065 * underlying {@link SpringCamelContext} 066 */ 067 public ApplicationContext getApplicationContext() { 068 if (applicationContext == null) { 069 CamelContext camelContext = getContext(); 070 if (camelContext instanceof SpringCamelContext) { 071 SpringCamelContext springCamelContext = (SpringCamelContext)camelContext; 072 return springCamelContext.getApplicationContext(); 073 } else { 074 throw new IllegalArgumentException("This SpringBuilder is not being used with a SpringCamelContext and there is no applicationContext property configured"); 075 } 076 } 077 return applicationContext; 078 } 079 080 /** 081 * Sets the application context to use to lookup beans 082 */ 083 @Override 084 public void setApplicationContext(ApplicationContext applicationContext) { 085 this.applicationContext = applicationContext; 086 } 087 088 /** 089 * Creates a transaction error handler that will lookup in application context for 090 * an exiting transaction manager. 091 * 092 * @return the created error handler 093 */ 094 public TransactionErrorHandlerBuilder transactionErrorHandler() { 095 return new TransactionErrorHandlerBuilder(); 096 } 097 098 /** 099 * Creates a transaction error handler. 100 * 101 * @param policy using this transaction policy (eg: required, supports, ...) 102 * @return the created error handler 103 */ 104 public TransactionErrorHandlerBuilder transactionErrorHandler(SpringTransactionPolicy policy) { 105 return transactionErrorHandler(policy.getTransactionTemplate()); 106 } 107 108 /** 109 * Creates a transaction error handler. 110 * 111 * @param template the spring transaction template 112 * @return the created error handler 113 */ 114 public TransactionErrorHandlerBuilder transactionErrorHandler(TransactionTemplate template) { 115 TransactionErrorHandlerBuilder answer = new TransactionErrorHandlerBuilder(); 116 answer.setTransactionTemplate(template); 117 return answer; 118 } 119 120 /** 121 * Creates a transaction error handler. 122 * 123 * @param transactionManager the spring transaction manager 124 * @return the created error handler 125 */ 126 public TransactionErrorHandlerBuilder transactionErrorHandler(PlatformTransactionManager transactionManager) { 127 TransactionTemplate template = new TransactionTemplate(transactionManager); 128 return transactionErrorHandler(template); 129 } 130 131}