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.jsf.utils;
22  
23  import java.lang.reflect.Field;
24  import java.util.HashMap;
25  
26  import net.sourceforge.sannotations.annotation.DataModel;
27  
28  import org.springframework.beans.BeansException;
29  import org.springframework.beans.factory.config.BeanDefinition;
30  import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
31  import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
32  
33  /***
34   * A registry for the JSF
35   * 
36   * @DataModel parameters with the factory attribte configured, the next version will introduce a
37   * @Factory annotation
38   * @author Urubatan
39   */
40  public class DataModelFactoryRegistry implements BeanFactoryPostProcessor {
41  	private final HashMap<String, String>	registry	= new HashMap<String, String>();
42  
43  	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
44  		for (final String bdn : beanFactory.getBeanDefinitionNames()) {
45  			final BeanDefinition bd = beanFactory.getBeanDefinition(bdn);
46  			final Class<?> c = this.getDefinitionClass(bd);
47  			if (c != null) {
48  				for (final Field f : c.getDeclaredFields()) {
49  					final DataModel dm = f.getAnnotation(DataModel.class);
50  					if (dm != null) {
51  						if (!"".equals(dm.factory())) {
52  							this.registry.put("".equals(dm.name()) ? f.getName() : dm.name(), dm.factory());
53  						}
54  					}
55  				}
56  			}
57  		}
58  	}
59  
60  	public boolean hasFactory(String name) {
61  		return this.registry.containsKey(name);
62  	}
63  
64  	public String getFactory(String name) {
65  		return this.registry.get(name);
66  	}
67  
68  	private Class<?> getDefinitionClass(BeanDefinition bd) {
69  		try {
70  			return Class.forName(bd.getBeanClassName());
71  		} catch (final Throwable e) {
72  			return null;
73  		}
74  	}
75  }