This project has retired. For details please refer to its
Attic page.
CachingLocaleUrlDefinitionDAO xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.tiles.definition.dao;
23
24 import java.util.HashMap;
25 import java.util.LinkedHashMap;
26 import java.util.Locale;
27 import java.util.Map;
28
29 import org.apache.tiles.Definition;
30 import org.apache.tiles.definition.pattern.PatternDefinitionResolver;
31 import org.apache.tiles.definition.pattern.PatternDefinitionResolverAware;
32 import org.apache.tiles.request.ApplicationContext;
33 import org.apache.tiles.request.ApplicationResource;
34 import org.apache.tiles.request.locale.LocaleUtil;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class CachingLocaleUrlDefinitionDAO extends BaseLocaleUrlDefinitionDAO
50 implements PatternDefinitionResolverAware<Locale> {
51
52
53
54
55
56
57
58 public static final String CHECK_REFRESH_INIT_PARAMETER =
59 "org.apache.tiles.definition.dao.LocaleUrlDefinitionDAO.CHECK_REFRESH";
60
61
62
63
64
65
66 protected Map<Locale, Map<String, Definition>> locale2definitionMap;
67
68
69
70
71
72
73
74 protected boolean checkRefresh = false;
75
76
77
78
79
80
81 protected PatternDefinitionResolver<Locale> definitionResolver;
82
83
84
85
86
87
88 public CachingLocaleUrlDefinitionDAO(ApplicationContext applicationContext) {
89 super(applicationContext);
90 locale2definitionMap = new HashMap<Locale, Map<String, Definition>>();
91 }
92
93
94 public void setPatternDefinitionResolver(
95 PatternDefinitionResolver<Locale> definitionResolver) {
96 this.definitionResolver = definitionResolver;
97 }
98
99
100 public Definition getDefinition(String name, Locale customizationKey) {
101 Definition retValue = null;
102 if (customizationKey == null) {
103 customizationKey = Locale.ROOT;
104 }
105 Map<String, Definition> definitions = getDefinitions(customizationKey);
106 if (definitions != null) {
107 retValue = definitions.get(name);
108
109 if (retValue == null) {
110 retValue = getDefinitionFromResolver(name, customizationKey);
111
112 if (retValue != null) {
113 synchronized (definitions) {
114 definitions.put(name, retValue);
115 }
116 }
117 }
118 }
119
120 return retValue;
121 }
122
123
124 public Map<String, Definition> getDefinitions(Locale customizationKey) {
125 if (customizationKey == null) {
126 customizationKey = Locale.ROOT;
127 }
128 Map<String, Definition> retValue = locale2definitionMap
129 .get(customizationKey);
130 if (retValue == null || (checkRefresh && refreshRequired())) {
131 retValue = checkAndloadDefinitions(customizationKey);
132 }
133 return retValue;
134 }
135
136
137
138
139
140
141
142
143
144 public void setCheckRefresh(boolean checkRefresh) {
145 this.checkRefresh = checkRefresh;
146 }
147
148
149
150
151
152
153
154
155 protected Definition getDefinitionFromResolver(String name,
156 Locale customizationKey) {
157 return definitionResolver.resolveDefinition(name,
158 customizationKey);
159 }
160
161
162
163
164
165
166
167
168
169 protected synchronized Map<String, Definition> checkAndloadDefinitions(Locale customizationKey) {
170 Map<String, Definition> existingDefinitions = locale2definitionMap.get(customizationKey);
171 boolean definitionsAlreadyLoaded = existingDefinitions != null;
172 if (definitionsAlreadyLoaded) {
173 return existingDefinitions;
174 }
175 if (checkRefresh && refreshRequired()) {
176 locale2definitionMap.clear();
177 definitionResolver.clearPatternPaths(customizationKey);
178 }
179 loadDefinitions(customizationKey);
180 return locale2definitionMap.get(customizationKey);
181 }
182
183
184
185
186
187
188
189
190 protected Map<String, Definition> loadDefinitions(Locale customizationKey) {
191 Map<String, Definition> localeDefsMap = locale2definitionMap
192 .get(customizationKey);
193 if (localeDefsMap != null) {
194 return localeDefsMap;
195 }
196
197 return loadDefinitionsFromResources(customizationKey);
198 }
199
200
201
202
203
204
205
206
207 protected Map<String, Definition> loadDefinitionsFromResources(Locale customizationKey) {
208 Map<String, Definition> localeDefsMap = loadRawDefinitionsFromResources(customizationKey);
209 Map<String, Definition> defsMap = definitionResolver
210 .storeDefinitionPatterns(copyDefinitionMap(localeDefsMap),
211 customizationKey);
212 locale2definitionMap.put(customizationKey, defsMap);
213 return localeDefsMap;
214 }
215
216
217
218
219
220
221
222
223 protected Map<String, Definition> loadRawDefinitionsFromResources(
224 Locale customizationKey) {
225 Map<String, Definition> localeDefsMap;
226
227 Locale parentLocale = LocaleUtil.getParentLocale(customizationKey);
228 localeDefsMap = new LinkedHashMap<String, Definition>();
229 if (parentLocale != null) {
230 Map<String, Definition> parentDefs = loadRawDefinitionsFromResources(parentLocale);
231 if (parentDefs != null) {
232 localeDefsMap.putAll(parentDefs);
233 }
234 }
235
236 for (ApplicationResource resource : sources) {
237 ApplicationResource newResource = applicationContext.getResource(resource, customizationKey);
238 if (newResource != null) {
239 Map<String, Definition> defsMap = loadDefinitionsFromResource(newResource);
240 if (defsMap != null) {
241 localeDefsMap.putAll(defsMap);
242 }
243 }
244 }
245 return localeDefsMap;
246 }
247
248
249
250
251
252
253
254
255 protected Map<String, Definition> loadParentDefinitions(Locale parentLocale) {
256 return loadDefinitions(parentLocale);
257 }
258
259
260
261
262
263
264
265
266
267
268 protected Map<String, Definition> copyDefinitionMap(
269 Map<String, Definition> localeDefsMap) {
270 return localeDefsMap;
271 }
272 }