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