GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / src / commons / org / codehaus / groovy / grails / orm / support / TransactionManagerPostProcessor.java
blobc6c32c9a98b9f64d4f79805a8a1253b00129dfb5
1 /*
2 * Copyright 2004-2006 Graeme Rocher
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.codehaus.groovy.grails.orm.support;
18 import org.springframework.beans.BeansException;
19 import org.springframework.beans.factory.BeanFactory;
20 import org.springframework.beans.factory.BeanFactoryAware;
21 import org.springframework.beans.factory.BeanFactoryUtils;
22 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
23 import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
24 import org.springframework.transaction.PlatformTransactionManager;
26 /**
27 * This is a bean post processor that injects the platform transaction
28 * manager into beans that implement the {@link TransactionManagerAware}
29 * interface.
30 * @author Graeme Rocher
31 * @since 0.4
33 public class TransactionManagerPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements BeanFactoryAware {
34 private ConfigurableListableBeanFactory beanFactory;
35 private PlatformTransactionManager transactionManager;
36 private boolean initialized = false;
38 /**
39 * Gets the platform transaction manager from the bean factory if
40 * there is one.
41 * @param beanFactory The bean factory handling this post processor.
42 * @throws BeansException
44 public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
45 if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
46 throw new IllegalArgumentException(
47 "TransactionManagerPostProcessor requires a ConfigurableListableBeanFactory");
50 this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
53 /**
54 * Injects the platform transaction manager into the given bean if
55 * that bean implements the {@link TransactionManagerAware} interface.
56 * @param bean The bean to process.
57 * @param name The name of the bean.
58 * @return The bean after the transaction manager has been injected.
59 * @throws BeansException
61 public synchronized boolean postProcessAfterInstantiation(Object bean, String name) throws BeansException {
62 // Lazily retrieve the transaction manager from the bean factory.
63 // Attempting to retrieve it within 'setBeanFactory()' blocks
64 // other bean post processors from processing the beans in the
65 // factory!
66 if (!this.initialized) {
67 // Fetch the names of all the beans that are of type
68 // PlatformTransactionManager. Note that we have to pass
69 // "false" for the last argument to avoid eager initialisation,
70 // otherwise we end up in an endless loop (it triggers the
71 // current method).
72 String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
73 this.beanFactory,
74 PlatformTransactionManager.class,
75 false,
76 false);
78 // If at least one is found, use the first of them as the
79 // transaction manager for the application.
80 if (beanNames.length > 0) {
81 this.transactionManager = (PlatformTransactionManager)beanFactory.getBean(beanNames[0]);
84 // Don't attempt to retrieve the transaction manager again.
85 this.initialized = true;
88 if (bean instanceof TransactionManagerAware) {
89 TransactionManagerAware tma = (TransactionManagerAware) bean;
90 tma.setTransactionManager(this.transactionManager);
92 return true;