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