1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.integration.cdi.extension;
17
18 import java.util.Iterator;
19 import java.util.Set;
20 import java.util.logging.Level;
21 import java.util.logging.Logger;
22
23 import javax.enterprise.event.Observes;
24 import javax.enterprise.inject.spi.AfterBeanDiscovery;
25 import javax.enterprise.inject.spi.BeanManager;
26 import javax.enterprise.inject.spi.Extension;
27
28 import de.smartics.exceptions.i18n.AbstractMessageRuntimeException;
29 import de.smartics.properties.api.config.app.ConfigurationPropertiesFactory;
30 import de.smartics.properties.api.config.app.ConfigurationPropertiesFactoryFactory;
31 import de.smartics.properties.api.config.domain.ConfigurationProperties;
32 import de.smartics.properties.api.config.domain.ConfigurationPropertiesManagement;
33 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
34
35
36
37
38
39 public class SmarticsPropertiesCdiExtension implements Extension
40 {
41
42
43
44
45 private final Logger log = Logger
46 .getLogger(SmarticsPropertiesCdiExtension.class.getName());
47
48
49
50
51
52
53
54
55
56 public void addSmarticsPropertiesToBeanManager(
57 @Observes final AfterBeanDiscovery abd, final BeanManager manager,
58 final ConfigurationKey<?> configKey)
59 {
60 try
61 {
62
63 final ConfigurationPropertiesFactory factory = createFactory();
64
65 abd.addBean(new ConfigurationPropertiesFactoryCdiBean(
66 ConfigurationPropertiesFactory.class, manager, factory));
67
68 addManagemenAsCdiBeanToBeanMnager(abd, manager, factory);
69 addConfigurationPropertiesAsCdiBeanToBeanManager(abd, manager, factory);
70 addPropertyTypesAsCdiBeansToBeanManager(abd, manager, factory, configKey);
71 }
72 catch (final AbstractMessageRuntimeException e)
73 {
74 log.log(Level.SEVERE, e.getMessage());
75 throw e;
76 }
77
78 }
79
80 private void addPropertyTypesAsCdiBeansToBeanManager(
81 final AfterBeanDiscovery abd, final BeanManager manager,
82 final ConfigurationPropertiesFactory factory,
83 final ConfigurationKey<?> configKey)
84 {
85 final ConfigurationPropertiesManagement configurationManagement =
86 factory.createManagement(configKey);
87 final Set<Class<?>> propertySetTypes =
88 fetchPropertyTypes(configurationManagement);
89
90 for (final Iterator<Class<?>> iterator = propertySetTypes.iterator(); iterator
91 .hasNext();)
92 {
93 final Class<?> propertySetType = (Class<?>) iterator.next();
94 abd.addBean(new PropertySetTypeCdiBean(propertySetType, manager, factory));
95 }
96 }
97
98 private Set<Class<?>> fetchPropertyTypes(
99 final ConfigurationPropertiesManagement configurationManagement)
100 {
101 final Set<Class<?>> propertySetTypes =
102 configurationManagement.getRegistry().getPropertySetTypes();
103 return propertySetTypes;
104 }
105
106 private void addConfigurationPropertiesAsCdiBeanToBeanManager(
107 final AfterBeanDiscovery abd, final BeanManager manager,
108 final ConfigurationPropertiesFactory factory)
109 {
110 abd.addBean(new ConfigurationPropertiesCdiBean(
111 ConfigurationProperties.class, manager, factory, true));
112 abd.addBean(new ConfigurationPropertiesCdiBean(
113 ConfigurationProperties.class, manager, factory, false));
114 }
115
116 private void addManagemenAsCdiBeanToBeanMnager(final AfterBeanDiscovery abd,
117 final BeanManager manager, final ConfigurationPropertiesFactory factory)
118 {
119 abd.addBean(new ConfigurationPropertiesCdiBean(
120 ConfigurationPropertiesManagement.class, manager, factory, true));
121 abd.addBean(new ConfigurationPropertiesCdiBean(
122 ConfigurationPropertiesManagement.class, manager, factory, false));
123 }
124
125 private ConfigurationPropertiesFactory createFactory()
126 {
127 final ConfigurationPropertiesFactory factory =
128 ConfigurationPropertiesFactoryFactory.createDefaultFactory();
129 return factory;
130 }
131 }