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: 750767 $
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 public <T> T bean(Class<T> type, String beanName) {
052 ApplicationContext context = getApplicationContext();
053 return (T)context.getBean(beanName, type);
054 }
055
056 /**
057 * Looks up the bean with the given type in the application context and
058 * returns it, or throws an exception if the bean is not present or there
059 * are multiple possible beans to choose from for the given type
060 *
061 * @param type the type of the bean
062 * @return the bean
063 */
064 public <T> T bean(Class<T> type) {
065 ApplicationContext context = getApplicationContext();
066 String[] names = context.getBeanNamesForType(type, true, true);
067 if (names != null) {
068 int count = names.length;
069 if (count == 1) {
070 // lets instantiate the single bean
071 return (T)context.getBean(names[0]);
072 } else if (count > 1) {
073 throw new IllegalArgumentException("Too many beans in the application context of type: " + type + ". Found: " + count);
074 }
075 }
076 throw new IllegalArgumentException("No bean available in the application context of type: " + type);
077 }
078
079 /**
080 * Returns the application context which has been configured via the
081 * {@link #setApplicationContext(ApplicationContext)} method or from the
082 * underlying {@link SpringCamelContext}
083 */
084 public ApplicationContext getApplicationContext() {
085 if (applicationContext == null) {
086 CamelContext camelContext = getContext();
087 if (camelContext instanceof SpringCamelContext) {
088 SpringCamelContext springCamelContext = (SpringCamelContext)camelContext;
089 return springCamelContext.getApplicationContext();
090 } else {
091 throw new IllegalArgumentException("This SpringBuilder is not being used with a SpringCamelContext and there is no applicationContext property configured");
092 }
093 }
094 return applicationContext;
095 }
096
097 /**
098 * Sets the application context to use to lookup beans
099 */
100 public void setApplicationContext(ApplicationContext applicationContext) {
101 this.applicationContext = applicationContext;
102 }
103
104 /**
105 * Creates a transaction error handler.
106 *
107 * @param policy using this transaction policy (eg: required, supports, ...)
108 * @return the created error handler
109 */
110 public TransactionErrorHandlerBuilder transactionErrorHandler(SpringTransactionPolicy policy) {
111 TransactionErrorHandlerBuilder answer = new TransactionErrorHandlerBuilder();
112 answer.setTransactionTemplate(policy.getTemplate());
113 return answer;
114 }
115
116 }