View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  package org.apache.james.container.spring.lifecycle;
20  
21  import org.springframework.beans.BeansException;
22  import org.springframework.beans.factory.BeanFactory;
23  import org.springframework.beans.factory.BeanFactoryAware;
24  import org.springframework.beans.factory.config.BeanDefinition;
25  import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
26  
27  import java.util.Collection;
28  
29  /**
30   * visitor. iterating over all spring beans having some specific implementation 
31   */
32  public abstract class AbstractPropagator implements BeanFactoryAware {
33  
34      private Collection excludeBeans;
35      private BeanFactory beanFactory;
36  
37      
38      /**
39       * Gets the bean factory
40       * @return the beanFactory not null
41       */
42      public final BeanFactory getBeanFactory() {
43          return beanFactory;
44      }
45  
46      public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
47          this.beanFactory = beanFactory;
48      }
49      
50      public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
51          return bean;
52      }
53  
54      protected BeanDefinition getBeanDefinition(String beanName) {
55          if (beanFactory instanceof ConfigurableListableBeanFactory) {
56              ConfigurableListableBeanFactory configurableListableBeanFactory = (ConfigurableListableBeanFactory) beanFactory;
57              return configurableListableBeanFactory.getBeanDefinition(beanName); 
58          }
59          return null; // cannot lookup bean definition
60      }
61  
62      public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
63          if (excludeBeans == null || !excludeBeans.contains(beanName)) {
64              BeanDefinition beanDefinition = getBeanDefinition(beanName);
65              invokeLifecycleWorker(beanName, bean, beanDefinition);
66          }
67          return bean;
68      }
69  
70      public void setExcludeBeans(Collection excludeBeans) {
71          this.excludeBeans=excludeBeans;
72      }
73  
74      protected abstract Class getLifecycleInterface();
75  
76      protected abstract void invokeLifecycleWorker(String beanName, Object bean, BeanDefinition beanDefinition);
77  
78  }