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 | 0 | 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 | 22 | 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 | 22 | super(applicationContext); |
90 | 22 | locale2definitionMap = new HashMap<Locale, Map<String, Definition>>(); |
91 | 22 | } |
92 | |
|
93 | |
|
94 | |
public void setPatternDefinitionResolver( |
95 | |
PatternDefinitionResolver<Locale> definitionResolver) { |
96 | 22 | this.definitionResolver = definitionResolver; |
97 | 22 | } |
98 | |
|
99 | |
|
100 | |
public Definition getDefinition(String name, Locale customizationKey) { |
101 | 55 | Definition retValue = null; |
102 | 55 | if (customizationKey == null) { |
103 | 26 | customizationKey = Locale.ROOT; |
104 | |
} |
105 | 55 | Map<String, Definition> definitions = getDefinitions(customizationKey); |
106 | 55 | if (definitions != null) { |
107 | 55 | retValue = definitions.get(name); |
108 | |
|
109 | 55 | if (retValue == null) { |
110 | 10 | retValue = getDefinitionFromResolver(name, customizationKey); |
111 | |
|
112 | 10 | if (retValue != null) { |
113 | 10 | synchronized (definitions) { |
114 | 10 | definitions.put(name, retValue); |
115 | 10 | } |
116 | |
} |
117 | |
} |
118 | |
} |
119 | |
|
120 | 55 | return retValue; |
121 | |
} |
122 | |
|
123 | |
|
124 | |
public Map<String, Definition> getDefinitions(Locale customizationKey) { |
125 | 65 | if (customizationKey == null) { |
126 | 2 | customizationKey = Locale.ROOT; |
127 | |
} |
128 | 65 | Map<String, Definition> retValue = locale2definitionMap |
129 | |
.get(customizationKey); |
130 | 65 | if (retValue == null || (checkRefresh && refreshRequired())) { |
131 | 28 | retValue = checkAndloadDefinitions(customizationKey); |
132 | |
} |
133 | 65 | return retValue; |
134 | |
} |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public void setCheckRefresh(boolean checkRefresh) { |
145 | 0 | this.checkRefresh = checkRefresh; |
146 | 0 | } |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
protected Definition getDefinitionFromResolver(String name, |
156 | |
Locale customizationKey) { |
157 | 10 | 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 | 28 | Map<String, Definition> existingDefinitions = locale2definitionMap.get(customizationKey); |
171 | 28 | boolean definitionsAlreadyLoaded = existingDefinitions != null; |
172 | 28 | if (definitionsAlreadyLoaded) { |
173 | 0 | return existingDefinitions; |
174 | |
} |
175 | 28 | if (checkRefresh && refreshRequired()) { |
176 | 0 | locale2definitionMap.clear(); |
177 | 0 | definitionResolver.clearPatternPaths(customizationKey); |
178 | |
} |
179 | 28 | loadDefinitions(customizationKey); |
180 | 28 | return locale2definitionMap.get(customizationKey); |
181 | |
} |
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
protected Map<String, Definition> loadDefinitions(Locale customizationKey) { |
191 | 28 | Map<String, Definition> localeDefsMap = locale2definitionMap |
192 | |
.get(customizationKey); |
193 | 28 | if (localeDefsMap != null) { |
194 | 0 | return localeDefsMap; |
195 | |
} |
196 | |
|
197 | 28 | return loadDefinitionsFromResources(customizationKey); |
198 | |
} |
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
protected Map<String, Definition> loadDefinitionsFromResources(Locale customizationKey) { |
208 | 28 | Map<String, Definition> localeDefsMap = loadRawDefinitionsFromResources(customizationKey); |
209 | 28 | Map<String, Definition> defsMap = definitionResolver |
210 | |
.storeDefinitionPatterns(copyDefinitionMap(localeDefsMap), |
211 | |
customizationKey); |
212 | 28 | locale2definitionMap.put(customizationKey, defsMap); |
213 | 28 | 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 | 64 | Locale parentLocale = LocaleUtil.getParentLocale(customizationKey); |
228 | 64 | localeDefsMap = new LinkedHashMap<String, Definition>(); |
229 | 64 | if (parentLocale != null) { |
230 | 36 | Map<String, Definition> parentDefs = loadRawDefinitionsFromResources(parentLocale); |
231 | 36 | if (parentDefs != null) { |
232 | 36 | localeDefsMap.putAll(parentDefs); |
233 | |
} |
234 | |
} |
235 | |
|
236 | 64 | for (ApplicationResource resource : sources) { |
237 | 160 | ApplicationResource newResource = applicationContext.getResource(resource, customizationKey); |
238 | 160 | if (newResource != null) { |
239 | 86 | Map<String, Definition> defsMap = loadDefinitionsFromResource(newResource); |
240 | 86 | if (defsMap != null) { |
241 | 86 | localeDefsMap.putAll(defsMap); |
242 | |
} |
243 | |
} |
244 | 160 | } |
245 | 64 | return localeDefsMap; |
246 | |
} |
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
protected Map<String, Definition> loadParentDefinitions(Locale parentLocale) { |
256 | 0 | 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 | 14 | return localeDefsMap; |
271 | |
} |
272 | |
} |