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.io.Serializable;
19 import java.lang.annotation.Annotation;
20 import java.lang.reflect.Type;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.Set;
24
25 import javax.enterprise.context.RequestScoped;
26 import javax.enterprise.context.spi.CreationalContext;
27 import javax.enterprise.inject.spi.Bean;
28 import javax.enterprise.inject.spi.BeanManager;
29 import javax.enterprise.inject.spi.InjectionPoint;
30 import javax.enterprise.util.AnnotationLiteral;
31
32 import de.smartics.properties.api.config.app.ConfigurationPropertiesFactory;
33 import de.smartics.properties.api.config.domain.ConfigurationPropertiesManagement;
34 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
35 import de.smartics.properties.integration.cdi.DefaultManagement;
36 import de.smartics.properties.integration.cdi.Management;
37 import de.smartics.properties.spi.config.domain.key.ConfigurationKeyContext;
38 import de.smartics.properties.spi.config.domain.key.ConfigurationKeyContextManager;
39
40
41
42
43 public class ConfigurationPropertiesCdiBean implements Bean<Object>,
44 Serializable
45 {
46
47
48
49
50
51
52
53
54 private static final long serialVersionUID = 1L;
55
56
57
58
59 private final Class<?> type;
60
61
62
63
64 private final BeanManager beanManager;
65
66
67
68
69 private final ConfigurationPropertiesFactory factory;
70
71
72
73
74
75 private final boolean useOnlyStaticKeyParts;
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 public ConfigurationPropertiesCdiBean(final Class<?> type,
94 final BeanManager beanManager,
95 final ConfigurationPropertiesFactory factory,
96 final boolean useOnlyStaticKeyParts)
97 {
98 this.type = type;
99 this.beanManager = beanManager;
100 this.factory = factory;
101 this.useOnlyStaticKeyParts = useOnlyStaticKeyParts;
102 }
103
104
105
106
107
108
109
110
111
112
113
114 @Override
115 public Class<?> getBeanClass()
116 {
117 return type;
118 }
119
120 @Override
121 public Set<InjectionPoint> getInjectionPoints()
122 {
123 return Collections.emptySet();
124 }
125
126 @Override
127 public String getName()
128 {
129 final StringBuilder beanName = new StringBuilder();
130 if (useOnlyStaticKeyParts)
131 {
132 beanName.append("Default");
133 }
134 beanName.append(type.getName());
135 return beanName.toString();
136 }
137
138 @SuppressWarnings("serial")
139 @Override
140 public Set<Annotation> getQualifiers()
141 {
142 final Set<Annotation> qualifiers = new HashSet<Annotation>();
143 if (useOnlyStaticKeyParts)
144 {
145 qualifiers.add(new AnnotationLiteral<DefaultManagement>()
146 {
147 });
148 }
149 else
150 {
151 qualifiers.add(new AnnotationLiteral<Management>()
152 {
153 });
154 }
155 return qualifiers;
156 }
157
158 @Override
159 public Class<? extends Annotation> getScope()
160 {
161 return RequestScoped.class;
162 }
163
164 @Override
165 public Set<Class<? extends Annotation>> getStereotypes()
166 {
167 return Collections.emptySet();
168 }
169
170 @Override
171 public Set<Type> getTypes()
172 {
173 final Set<Type> types = new HashSet<Type>();
174 types.add(type);
175 return types;
176 }
177
178 @Override
179 public boolean isAlternative()
180 {
181 return false;
182 }
183
184 @Override
185 public boolean isNullable()
186 {
187 return false;
188 }
189
190 @Override
191 public Object create(final CreationalContext<Object> ctx)
192 {
193 final ConfigurationKey<?> actualKey =
194 CdiBeanHelper.find(beanManager, ConfigurationKey.class).get(0);
195 final ConfigurationKey<?> keyToUse;
196 if (useOnlyStaticKeyParts)
197 {
198 final ConfigurationKeyContext context =
199 ConfigurationKeyContextManager.INSTANCE.context();
200 final de.smartics.properties.api.config.domain.key.ConfigurationKeyFactory<?> ckfactory =
201 context.configurationKeyFactory();
202 final ConfigurationKey<?> defaultKey =
203 ckfactory.createStaticKey(actualKey);
204 keyToUse = defaultKey;
205 }
206 else
207 {
208 keyToUse = actualKey;
209 }
210 final ConfigurationPropertiesManagement configuration =
211 factory.createManagement(keyToUse);
212
213 return configuration;
214 }
215
216 @Override
217 public void destroy(final Object instance, final CreationalContext<Object> ctx)
218 {
219
220 }
221
222
223 }