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 methods
030 *
031 * @deprecated use plain {@link RouteBuilder}
032 */
033@Deprecated
034public abstract class SpringRouteBuilder extends RouteBuilder implements ApplicationContextAware {
035    private ApplicationContext applicationContext;
036
037    /**
038     * Looks up the bean with the given name in the application context and returns it, or throws an exception if the
039     * bean is not present or is not of the given type
040     *
041     * @param  beanName the name of the bean in the application context
042     * @param  type     the type of the bean
043     * @return          the bean
044     */
045    public <T> T lookup(String beanName, Class<T> type) {
046        ApplicationContext context = getApplicationContext();
047        return context.getBean(beanName, type);
048    }
049
050    /**
051     * Looks up the bean with the given type in the application context and returns it, or throws an exception if the
052     * bean is not present or there 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 underlying {@link SpringCamelContext}
065     */
066    public ApplicationContext getApplicationContext() {
067        if (applicationContext == null) {
068            CamelContext camelContext = getContext();
069            if (camelContext instanceof SpringCamelContext) {
070                SpringCamelContext springCamelContext = (SpringCamelContext) camelContext;
071                return springCamelContext.getApplicationContext();
072            } else {
073                throw new IllegalArgumentException(
074                        "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    @Override
084    public void setApplicationContext(ApplicationContext applicationContext) {
085        this.applicationContext = applicationContext;
086    }
087
088    /**
089     * Creates a transaction error handler that will lookup in application context for 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}