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.Any;
28 import javax.enterprise.inject.Default;
29 import javax.enterprise.inject.spi.Bean;
30 import javax.enterprise.inject.spi.BeanManager;
31 import javax.enterprise.inject.spi.InjectionPoint;
32 import javax.enterprise.util.AnnotationLiteral;
33
34 import de.smartics.properties.api.config.app.ConfigurationPropertiesFactory;
35 import de.smartics.properties.api.config.domain.ConfigurationPropertiesManagement;
36 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
37
38
39
40
41 public class PropertySetTypeCdiBean implements Bean<Object>, Serializable
42 {
43
44
45
46
47
48
49
50
51 private static final long serialVersionUID = 1L;
52
53
54
55
56 private final Class<?> type;
57
58
59
60
61 private final BeanManager beanManager;
62
63
64
65
66 private final ConfigurationPropertiesFactory factory;
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public PropertySetTypeCdiBean(final Class<?> type,
82 final BeanManager beanManager,
83 final ConfigurationPropertiesFactory factory)
84 {
85 this.type = type;
86 this.beanManager = beanManager;
87 this.factory = factory;
88 }
89
90
91
92
93
94
95
96
97
98
99
100 @Override
101 public Class<?> getBeanClass()
102 {
103 return type;
104 }
105
106 @Override
107 public Set<InjectionPoint> getInjectionPoints()
108 {
109 return Collections.emptySet();
110 }
111
112 @Override
113 public String getName()
114 {
115 return type.getName();
116 }
117
118 @SuppressWarnings("serial")
119 @Override
120 public Set<Annotation> getQualifiers()
121 {
122 final Set<Annotation> qualifiers = new HashSet<Annotation>();
123 qualifiers.add(new AnnotationLiteral<Default>()
124 {
125 });
126 qualifiers.add(new AnnotationLiteral<Any>()
127 {
128 });
129 return qualifiers;
130 }
131
132 @Override
133 public Class<? extends Annotation> getScope()
134 {
135 return RequestScoped.class;
136 }
137
138 @Override
139 public Set<Class<? extends Annotation>> getStereotypes()
140 {
141 return Collections.emptySet();
142 }
143
144 @Override
145 public Set<Type> getTypes()
146 {
147 final Set<Type> types = new HashSet<Type>();
148 types.add(type);
149 return types;
150 }
151
152 @Override
153 public boolean isAlternative()
154 {
155 return false;
156 }
157
158 @Override
159 public boolean isNullable()
160 {
161 return false;
162 }
163
164 @Override
165 public Object create(final CreationalContext<Object> ctx)
166 {
167 final ConfigurationKey<?> configKey =
168 CdiBeanHelper.find(beanManager, ConfigurationKey.class).get(0);
169 final ConfigurationPropertiesManagement configuration =
170 factory.createManagement(configKey);
171
172
173 return configuration.getProperties(type);
174 }
175
176 @Override
177 public void destroy(final Object instance, final CreationalContext<Object> ctx)
178 {
179
180 }
181
182
183
184 }