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 java.lang.reflect.InvocationTargetException;
22  import java.lang.reflect.Method;
23  
24  import javax.annotation.PostConstruct;
25  import javax.annotation.Resource;
26  
27  import org.apache.avalon.framework.activity.Initializable;
28  import org.apache.avalon.framework.container.ContainerUtil;
29  import org.apache.james.api.kernel.ServiceLocator;
30  import org.springframework.beans.factory.config.BeanDefinition;
31  import org.springframework.beans.factory.config.BeanPostProcessor;
32  import org.springframework.core.Ordered;
33  
34  /**
35   * calls initialize() for all avalon components
36   */
37  public class InitializationPropagator extends AbstractPropagator implements BeanPostProcessor, Ordered, ServiceLocator {
38  
39      protected Class getLifecycleInterface() {
40          return Initializable.class;
41      }
42  
43      protected void invokeLifecycleWorker(String beanName, Object bean, BeanDefinition beanDefinition) {
44          // TODO: share reflection code
45          Method[] methods = bean.getClass().getMethods();
46          for (Method method : methods) {
47              Resource resourceAnnotation = method.getAnnotation(Resource.class);
48              if (resourceAnnotation != null) {
49                  final String name = resourceAnnotation.name();
50                  final Object service;
51                  if ("org.apache.james.ServiceLocator".equals(name)) {
52                      service = this;
53                  } else {
54                      service = get(name);
55                  }
56                  
57                  if (bean == null) {
58                 } else {
59                      try {
60                          Object[] args = {service};
61                          method.invoke(bean, args);
62                      } catch (IllegalAccessException e) {
63                          throw new RuntimeException("Injection failed", e);
64                      } catch (IllegalArgumentException e) {
65                          throw new RuntimeException("Injection failed", e);
66                      } catch (InvocationTargetException e) {
67                          throw new RuntimeException("Injection failed", e);
68                      }
69                  }
70              }
71          }
72          try {
73              ContainerUtil.initialize(bean);;
74              for (Method method : methods) {
75                  PostConstruct postConstructAnnotation = method.getAnnotation(PostConstruct.class);
76                  if (postConstructAnnotation != null) {
77                      Object[] args = {};
78                      method.invoke(bean, args);
79                  }
80              }
81          } catch (Exception e) {
82              throw new RuntimeException("could not initialize component of type " + bean.getClass(), e);
83          }
84      }
85  
86      public int getOrder() {
87          return 4;
88      }
89  
90      public Object get(String name) {
91          return getBeanFactory().getBean(name);
92      }
93  
94  }