1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.spi.config.support;
17
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Map.Entry;
21
22 import javax.annotation.CheckForNull;
23 import javax.annotation.concurrent.ThreadSafe;
24
25 import de.smartics.properties.api.config.domain.ConfigurationPropertiesManagement;
26 import de.smartics.properties.api.config.domain.ConfigurationRepositoryManagement;
27 import de.smartics.properties.api.config.domain.MissingConfigurationException;
28 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
29 import de.smartics.properties.api.core.domain.ConfigException;
30 import de.smartics.properties.api.core.domain.PropertiesContext;
31 import de.smartics.properties.api.core.domain.PropertyDescriptor;
32 import de.smartics.properties.api.core.domain.PropertyDescriptorRegistry;
33 import de.smartics.util.lang.Arg;
34
35
36
37
38
39
40
41
42
43
44
45 @ThreadSafe
46 public final class FactoryCache<T extends ConfigurationPropertiesManagement>
47 {
48
49
50
51
52
53
54
55
56
57
58
59 private final ConfigurationRepositoryManagement cache;
60
61
62
63
64
65 private final AbstractConfigurationPropertiesFactory<?> factory;
66
67
68
69
70 private final PropertyDescriptorRegistry registry;
71
72
73
74
75
76
77
78
79
80
81
82
83 public FactoryCache(final ConfigurationRepositoryManagement cache,
84 final AbstractConfigurationPropertiesFactory<?> factory)
85 {
86 this.cache = cache;
87 this.factory = factory;
88 this.registry = factory.getRegistry();
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 public ConfigurationRepositoryManagement getCache()
105 {
106 return cache;
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120 @CheckForNull
121 public PropertiesContext getContext(final Class<?> declaringType)
122 throws NullPointerException, ConfigException
123 {
124 return registry.getContext(declaringType);
125 }
126
127
128
129
130
131
132
133
134
135 public void registerDescriptors(
136 final Map<Class<?>, List<PropertyDescriptor>> descriptorMap)
137 throws NullPointerException
138 {
139 Arg.checkNotNull("descriptorMap", descriptorMap);
140
141 for (final Entry<Class<?>, List<PropertyDescriptor>> entry : descriptorMap
142 .entrySet())
143 {
144 final Class<?> declaringType = entry.getKey();
145 final List<PropertyDescriptor> descriptors = entry.getValue();
146 registry.addDescriptors(declaringType, descriptors);
147 }
148 }
149
150
151
152
153
154
155
156 public ConfigurationPropertiesManagement ensureManagement(
157 final ConfigurationKey<?> key)
158 {
159 try
160 {
161 return cache.getPropertiesManagement(key);
162 }
163 catch (final MissingConfigurationException e)
164 {
165 return createPropertiesManagement(key);
166 }
167 }
168
169
170
171
172
173
174
175 public ConfigurationPropertiesManagement createPropertiesManagement(
176 final ConfigurationKey<?> key)
177 {
178 final ConfigurationPropertiesManagement configuration =
179 factory.createNewConfiguration(key);
180 cache.registerProperties(key, configuration);
181 return configuration;
182 }
183
184
185
186
187
188
189
190
191 @Override
192 public String toString()
193 {
194 final StringBuilder buffer = new StringBuilder();
195
196 buffer.append("== Descriptor Registry:\n").append(registry);
197 buffer.append("\n== Configuration Cache:\n").append(cache);
198 buffer.append("\n== Factory Type : ").append(
199 factory.getClass().getName());
200
201 return buffer.toString();
202 }
203 }