1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.services.impl; |
16 |
|
|
17 |
|
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; |
18 |
|
import org.apache.commons.io.IOUtils; |
19 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
20 |
|
import org.apache.hivemind.Messages; |
21 |
|
import org.apache.hivemind.Resource; |
22 |
|
import org.apache.hivemind.util.Defense; |
23 |
|
import org.apache.hivemind.util.LocalizedNameGenerator; |
24 |
|
import org.apache.tapestry.IComponent; |
25 |
|
import org.apache.tapestry.INamespace; |
26 |
|
import org.apache.tapestry.event.ResetEventListener; |
27 |
|
import org.apache.tapestry.resolver.IComponentResourceResolver; |
28 |
|
import org.apache.tapestry.services.ClasspathResourceFactory; |
29 |
|
import org.apache.tapestry.services.ComponentMessagesSource; |
30 |
|
import org.apache.tapestry.services.ComponentPropertySource; |
31 |
|
import org.apache.tapestry.util.text.LocalizedProperties; |
32 |
|
|
33 |
|
import java.io.BufferedInputStream; |
34 |
|
import java.io.IOException; |
35 |
|
import java.io.InputStream; |
36 |
|
import java.net.URL; |
37 |
|
import java.util.*; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
21 |
public class ComponentMessagesSourceImpl implements ComponentMessagesSource, ResetEventListener |
47 |
|
{ |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
public static final String MESSAGES_ENCODING_PROPERTY_NAME = "org.apache.tapestry.messages-encoding"; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
public static final String NAMESPACE_PROPERTIES_NAME = "org.apache.tapestry.namespace-properties-name"; |
61 |
|
|
62 |
|
private static final String SUFFIX = ".properties"; |
63 |
|
|
64 |
21 |
private Properties _emptyProperties = new Properties(); |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
21 |
private Map _componentCache = new ConcurrentHashMap(); |
73 |
|
|
74 |
|
private ComponentPropertySource _componentPropertySource; |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
private ClasspathResourceFactory _classpathResourceFactory; |
80 |
|
|
81 |
|
private IComponentResourceResolver _resourceResolver; |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
protected Properties getLocalizedProperties(IComponent component) |
96 |
|
{ |
97 |
22 |
Defense.notNull(component, "component"); |
98 |
|
|
99 |
22 |
Resource specificationLocation = component.getSpecification().getSpecificationLocation(); |
100 |
|
|
101 |
22 |
Locale locale = component.getPage().getLocale(); |
102 |
|
|
103 |
22 |
Map propertiesMap = findPropertiesMapForResource(specificationLocation); |
104 |
|
|
105 |
22 |
Properties result = (Properties) propertiesMap.get(locale); |
106 |
|
|
107 |
22 |
if (result == null) |
108 |
|
{ |
109 |
|
|
110 |
|
|
111 |
22 |
result = assembleComponentProperties(component, specificationLocation, |
112 |
|
propertiesMap, locale); |
113 |
|
|
114 |
22 |
propertiesMap.put(locale, result); |
115 |
|
} |
116 |
|
|
117 |
22 |
return result; |
118 |
|
} |
119 |
|
|
120 |
|
private Map findPropertiesMapForResource(Resource resource) |
121 |
|
{ |
122 |
153 |
Map result = (Map) _componentCache.get(resource); |
123 |
|
|
124 |
153 |
if (result == null) |
125 |
|
{ |
126 |
44 |
result = new HashMap(); |
127 |
|
|
128 |
44 |
_componentCache.put(resource, result); |
129 |
|
} |
130 |
|
|
131 |
153 |
return result; |
132 |
|
} |
133 |
|
|
134 |
|
private Properties getNamespaceProperties(IComponent component, Locale locale) |
135 |
|
{ |
136 |
63 |
INamespace namespace = component.getNamespace(); |
137 |
|
|
138 |
63 |
Resource namespaceLocation = namespace.getSpecificationLocation(); |
139 |
|
|
140 |
63 |
Map propertiesMap = findPropertiesMapForResource(namespaceLocation); |
141 |
|
|
142 |
63 |
Properties result = (Properties) propertiesMap.get(locale); |
143 |
|
|
144 |
63 |
if (result == null) |
145 |
|
{ |
146 |
62 |
result = new Properties(); |
147 |
|
|
148 |
|
|
149 |
|
|
150 |
62 |
List spaceList = new ArrayList(); |
151 |
62 |
spaceList.add(namespace); |
152 |
|
|
153 |
62 |
INamespace parent = namespace; |
154 |
68 |
while (parent.getParentNamespace() != null) |
155 |
|
{ |
156 |
6 |
parent = parent.getParentNamespace(); |
157 |
|
|
158 |
6 |
spaceList.add(parent); |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
130 |
for (int i=spaceList.size() - 1; i > -1; i--) |
164 |
|
{ |
165 |
68 |
INamespace space = (INamespace)spaceList.get(i); |
166 |
|
|
167 |
68 |
result.putAll(assembleNamespaceProperties(space, findPropertiesMapForResource(space.getSpecificationLocation()), locale)); |
168 |
|
} |
169 |
|
|
170 |
62 |
propertiesMap.put(locale, result); |
171 |
|
} |
172 |
|
|
173 |
63 |
return result; |
174 |
|
} |
175 |
|
|
176 |
|
private Properties assembleComponentProperties(IComponent component, Resource baseResourceLocation, |
177 |
|
Map propertiesMap, Locale locale) |
178 |
|
{ |
179 |
22 |
List localizations = findLocalizationsForResource(component, baseResourceLocation, locale, |
180 |
|
component.getSpecification().getProperty(NAMESPACE_PROPERTIES_NAME)); |
181 |
|
|
182 |
22 |
Properties parent = null; |
183 |
22 |
Properties assembledProperties = null; |
184 |
|
|
185 |
22 |
Iterator i = localizations.iterator(); |
186 |
|
|
187 |
85 |
while(i.hasNext()) |
188 |
|
{ |
189 |
63 |
ResourceLocalization rl = (ResourceLocalization) i.next(); |
190 |
63 |
Locale l = rl.getLocale(); |
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
63 |
Properties namespaceProperties = getNamespaceProperties(component, l); |
196 |
|
|
197 |
|
|
198 |
|
|
199 |
63 |
assembledProperties = new Properties(namespaceProperties); |
200 |
|
|
201 |
|
|
202 |
|
|
203 |
63 |
Properties properties = readComponentProperties(component, l, rl.getResource(), null); |
204 |
|
|
205 |
|
|
206 |
|
|
207 |
63 |
if (parent != null) |
208 |
|
{ |
209 |
39 |
if (properties != null) |
210 |
35 |
parent.putAll(properties); |
211 |
|
} |
212 |
|
else |
213 |
24 |
parent = properties; |
214 |
|
|
215 |
|
|
216 |
63 |
if (parent != null) |
217 |
61 |
assembledProperties.putAll(parent); |
218 |
|
|
219 |
|
|
220 |
63 |
propertiesMap.put(l, assembledProperties); |
221 |
63 |
} |
222 |
|
|
223 |
22 |
if (assembledProperties == null) |
224 |
0 |
assembledProperties = new Properties(); |
225 |
|
|
226 |
22 |
return assembledProperties; |
227 |
|
} |
228 |
|
|
229 |
|
private Properties assembleNamespaceProperties(INamespace namespace, Map propertiesMap, Locale locale) |
230 |
|
{ |
231 |
68 |
List localizations = findLocalizationsForResource(namespace.getSpecificationLocation(), locale, |
232 |
|
namespace.getPropertyValue(NAMESPACE_PROPERTIES_NAME)); |
233 |
|
|
234 |
|
|
235 |
|
|
236 |
68 |
Properties parent = _emptyProperties; |
237 |
|
|
238 |
68 |
Iterator i = localizations.iterator(); |
239 |
|
|
240 |
206 |
while(i.hasNext()) |
241 |
|
{ |
242 |
138 |
ResourceLocalization rl = (ResourceLocalization) i.next(); |
243 |
|
|
244 |
138 |
Locale l = rl.getLocale(); |
245 |
|
|
246 |
138 |
Properties properties = (Properties) propertiesMap.get(l); |
247 |
|
|
248 |
138 |
if (properties == null) |
249 |
|
{ |
250 |
68 |
properties = readNamespaceProperties(namespace, l, rl.getResource(), parent); |
251 |
|
|
252 |
68 |
propertiesMap.put(l, properties); |
253 |
|
} |
254 |
|
|
255 |
138 |
parent = properties; |
256 |
138 |
} |
257 |
|
|
258 |
68 |
return parent; |
259 |
|
|
260 |
|
} |
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
private List findLocalizationsForResource(Resource resource, Locale locale, String alternateName) |
270 |
|
{ |
271 |
68 |
List result = new ArrayList(); |
272 |
|
|
273 |
68 |
String baseName = null; |
274 |
68 |
if (alternateName != null) { |
275 |
|
|
276 |
6 |
baseName = alternateName.replace('.', '/'); |
277 |
|
} else { |
278 |
|
|
279 |
62 |
baseName = extractBaseName(resource); |
280 |
|
} |
281 |
|
|
282 |
68 |
LocalizedNameGenerator g = new LocalizedNameGenerator(baseName, locale, SUFFIX); |
283 |
|
|
284 |
206 |
while(g.more()) |
285 |
|
{ |
286 |
138 |
String localizedName = g.next(); |
287 |
138 |
Locale l = g.getCurrentLocale(); |
288 |
|
|
289 |
138 |
Resource localizedResource = resource.getRelativeResource(localizedName); |
290 |
|
|
291 |
138 |
if (localizedResource.getResourceURL() == null) |
292 |
|
{ |
293 |
62 |
localizedResource = _classpathResourceFactory.newResource(baseName + SUFFIX); |
294 |
|
} |
295 |
|
|
296 |
138 |
result.add(new ResourceLocalization(l, localizedResource)); |
297 |
138 |
} |
298 |
|
|
299 |
68 |
Collections.reverse(result); |
300 |
|
|
301 |
68 |
return result; |
302 |
|
} |
303 |
|
|
304 |
|
private List findLocalizationsForResource(IComponent component, Resource resource, Locale locale, String alternateName) |
305 |
|
{ |
306 |
22 |
List result = new ArrayList(); |
307 |
|
|
308 |
22 |
String baseName = null; |
309 |
22 |
if (alternateName != null) { |
310 |
|
|
311 |
0 |
baseName = alternateName.replace('.', '/'); |
312 |
|
} else { |
313 |
|
|
314 |
22 |
baseName = extractBaseName(resource); |
315 |
|
} |
316 |
|
|
317 |
22 |
LocalizedNameGenerator g = new LocalizedNameGenerator(baseName, locale, ""); |
318 |
|
|
319 |
85 |
while(g.more()) |
320 |
|
{ |
321 |
63 |
String localizedName = g.next(); |
322 |
63 |
Locale l = g.getCurrentLocale(); |
323 |
|
|
324 |
63 |
Resource localizedResource = _resourceResolver.findComponentResource(component, null, localizedName, SUFFIX, null); |
325 |
|
|
326 |
63 |
if (localizedResource == null) |
327 |
0 |
continue; |
328 |
|
|
329 |
63 |
result.add(new ResourceLocalization(l, localizedResource)); |
330 |
63 |
} |
331 |
|
|
332 |
22 |
Collections.reverse(result); |
333 |
|
|
334 |
22 |
return result; |
335 |
|
} |
336 |
|
|
337 |
|
private String extractBaseName(Resource baseResourceLocation) |
338 |
|
{ |
339 |
84 |
String fileName = baseResourceLocation.getName(); |
340 |
84 |
int dotx = fileName.lastIndexOf('.'); |
341 |
|
|
342 |
84 |
return dotx > -1 ? fileName.substring(0, dotx) : fileName; |
343 |
|
} |
344 |
|
|
345 |
|
private Properties readComponentProperties(IComponent component, |
346 |
|
Locale locale, Resource propertiesResource, Properties parent) |
347 |
|
{ |
348 |
63 |
String encoding = getComponentMessagesEncoding(component, locale); |
349 |
|
|
350 |
63 |
return readPropertiesResource(propertiesResource.getResourceURL(), encoding, parent); |
351 |
|
} |
352 |
|
|
353 |
|
private Properties readNamespaceProperties(INamespace namespace, |
354 |
|
Locale locale, Resource propertiesResource, Properties parent) |
355 |
|
{ |
356 |
68 |
String encoding = getNamespaceMessagesEncoding(namespace, locale); |
357 |
|
|
358 |
68 |
return readPropertiesResource(propertiesResource.getResourceURL(), encoding, parent); |
359 |
|
} |
360 |
|
|
361 |
|
private Properties readPropertiesResource(URL resourceURL, String encoding, Properties parent) |
362 |
|
{ |
363 |
131 |
if (resourceURL == null) |
364 |
42 |
return parent; |
365 |
|
|
366 |
89 |
Properties result = new Properties(parent); |
367 |
|
|
368 |
89 |
LocalizedProperties wrapper = new LocalizedProperties(result); |
369 |
|
|
370 |
89 |
InputStream input = null; |
371 |
|
|
372 |
|
try |
373 |
|
{ |
374 |
89 |
input = new BufferedInputStream(resourceURL.openStream()); |
375 |
|
|
376 |
89 |
if (encoding == null) |
377 |
89 |
wrapper.load(input); |
378 |
|
else |
379 |
0 |
wrapper.load(input, encoding); |
380 |
|
|
381 |
89 |
input.close(); |
382 |
|
} |
383 |
0 |
catch (IOException ex) |
384 |
|
{ |
385 |
0 |
throw new ApplicationRuntimeException(ImplMessages.unableToLoadProperties(resourceURL, ex), ex); |
386 |
|
} |
387 |
|
finally |
388 |
|
{ |
389 |
89 |
IOUtils.closeQuietly(input); |
390 |
89 |
} |
391 |
|
|
392 |
89 |
return result; |
393 |
|
} |
394 |
|
|
395 |
|
|
396 |
|
|
397 |
|
|
398 |
|
|
399 |
|
public void resetEventDidOccur() |
400 |
|
{ |
401 |
0 |
_componentCache.clear(); |
402 |
0 |
} |
403 |
|
|
404 |
|
public Messages getMessages(IComponent component) |
405 |
|
{ |
406 |
22 |
return new ComponentMessages(component.getPage().getLocale(), |
407 |
|
getLocalizedProperties(component)); |
408 |
|
} |
409 |
|
|
410 |
|
private String getComponentMessagesEncoding(IComponent component, Locale locale) |
411 |
|
{ |
412 |
63 |
String encoding = _componentPropertySource.getLocalizedComponentProperty(component, locale, |
413 |
|
MESSAGES_ENCODING_PROPERTY_NAME); |
414 |
|
|
415 |
63 |
if (encoding == null) |
416 |
63 |
encoding = _componentPropertySource. |
417 |
|
getLocalizedComponentProperty(component, locale, TemplateSourceImpl.TEMPLATE_ENCODING_PROPERTY_NAME); |
418 |
|
|
419 |
63 |
return encoding; |
420 |
|
} |
421 |
|
|
422 |
|
private String getNamespaceMessagesEncoding(INamespace namespace, Locale locale) |
423 |
|
{ |
424 |
68 |
return _componentPropertySource. |
425 |
|
getLocalizedNamespaceProperty(namespace, locale, MESSAGES_ENCODING_PROPERTY_NAME); |
426 |
|
} |
427 |
|
|
428 |
|
public void setComponentPropertySource(ComponentPropertySource componentPropertySource) |
429 |
|
{ |
430 |
21 |
_componentPropertySource = componentPropertySource; |
431 |
21 |
} |
432 |
|
|
433 |
|
public void setClasspathResourceFactory(ClasspathResourceFactory factory) |
434 |
|
{ |
435 |
21 |
_classpathResourceFactory = factory; |
436 |
21 |
} |
437 |
|
|
438 |
|
public void setComponentResourceResolver(IComponentResourceResolver resourceResolver) |
439 |
|
{ |
440 |
21 |
_resourceResolver = resourceResolver; |
441 |
21 |
} |
442 |
|
} |