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