1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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 }