View Javadoc

1   /*
2    Spring-Annotation, an easy way to configre Spring-Framework without all that XML.
3    Copyright (C) 2007 Spring-Annotation Development Team (http://www.urubatan.com.br)
4   
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9   
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14  
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  
19   <a href="http://www.gnu.org/licenses/lgpl.html">http://www.gnu.org/licenses/lgpl.html</a>
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  		 * if (beanFactory != null && fc != null) { BeanDefinition def = beanFactory.getBeanDefinition(beanName); if (def != null) { MutablePropertyValues pvs =
75  		 * def.getPropertyValues(); BeanWrapper bw = new BeanWrapperImpl(bean); for (PropertyValue pv : pvs.getPropertyValues()) { if (pv.getValue() instanceof String) { String v =
76  		 * (String) pv.getValue(); if (v.startsWith("#{") && v.endsWith("}")) { ValueBinding vb = fc.getApplication().createValueBinding(v); bw.setPropertyValue(pv.getName(),
77  		 * vb.getValue(fc)); } } } } }
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  }