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 public void setApplicationContext(ApplicationContext applicationContext) { 084 this.applicationContext = applicationContext; 085 } 086 087 /** 088 * Creates a transaction error handler that will lookup in application context for 089 * an exiting transaction manager. 090 * 091 * @return the created error handler 092 */ 093 public TransactionErrorHandlerBuilder transactionErrorHandler() { 094 return new TransactionErrorHandlerBuilder(); 095 } 096 097 /** 098 * Creates a transaction error handler. 099 * 100 * @param policy using this transaction policy (eg: required, supports, ...) 101 * @return the created error handler 102 */ 103 public TransactionErrorHandlerBuilder transactionErrorHandler(SpringTransactionPolicy policy) { 104 return transactionErrorHandler(policy.getTransactionTemplate()); 105 } 106 107 /** 108 * Creates a transaction error handler. 109 * 110 * @param template the spring transaction template 111 * @return the created error handler 112 */ 113 public TransactionErrorHandlerBuilder transactionErrorHandler(TransactionTemplate template) { 114 TransactionErrorHandlerBuilder answer = new TransactionErrorHandlerBuilder(); 115 answer.setTransactionTemplate(template); 116 return answer; 117 } 118 119 /** 120 * Creates a transaction error handler. 121 * 122 * @param transactionManager the spring transaction manager 123 * @return the created error handler 124 */ 125 public TransactionErrorHandlerBuilder transactionErrorHandler(PlatformTransactionManager transactionManager) { 126 TransactionTemplate template = new TransactionTemplate(transactionManager); 127 return transactionErrorHandler(template); 128 } 129 130}