1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sourceforge.sannotations.web;
22
23 import java.lang.reflect.Field;
24
25 import javax.faces.context.FacesContext;
26 import javax.faces.el.ValueBinding;
27
28 import net.sourceforge.sannotations.annotation.Value;
29
30 import org.springframework.beans.BeanWrapper;
31 import org.springframework.beans.BeanWrapperImpl;
32 import org.springframework.beans.BeansException;
33 import org.springframework.beans.factory.config.BeanPostProcessor;
34 import org.springframework.core.Ordered;
35
36 /***
37 * Post Processor that enables the use of
38 *
39 * @Value annotations with expressions from the JSF EL
40 * @author Urubatan
41 */
42 public class JSFBeanPostProcessor implements BeanPostProcessor, Ordered {
43 private int order = Integer.MAX_VALUE;
44
45 public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException {
46 return bean;
47 }
48
49 public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException {
50 FacesContext fc;
51 final BeanWrapper bw = new BeanWrapperImpl(bean);
52 try {
53 fc = FacesContext.getCurrentInstance();
54 if (fc != null) {
55 final Field[] fields = bean.getClass().getDeclaredFields();
56 for (final Field f : fields) {
57 Value a;
58 if (((a = f.getAnnotation(Value.class)) != null) && a.value().startsWith("#{")) {
59 final ValueBinding vb = fc.getApplication().createValueBinding(a.value());
60 final Object val = vb.getValue(fc);
61 try {
62 if (val != null) {
63 bw.setPropertyValue(f.getName(), val);
64 }
65 } catch (final Throwable t) {
66 t.printStackTrace();
67 }
68 }
69 }
70 }
71 } catch (final Throwable t) {
72 }
73
74
75
76
77
78
79 return bean;
80 }
81
82 public int getOrder() {
83 return this.order;
84 }
85
86 public void setOrder(final int order) {
87 this.order = order;
88 }
89 }