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.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 }