01: /**
02: *
03: */package clime.messadmin.providers.locale;
04:
05: import java.lang.reflect.Method;
06: import java.util.ArrayList;
07: import java.util.Enumeration;
08: import java.util.List;
09: import java.util.Locale;
10:
11: import javax.servlet.http.HttpSession;
12:
13: import clime.messadmin.providers.spi.LocaleProvider;
14:
15: /**
16: * Note: this provider implementation uses reflexion, to avoid linking against Tapestry libs.
17: * @author Cédrik LIME
18: */
19: public class ReflectionTapestryProvider implements LocaleProvider {
20:
21: /**
22: *
23: */
24: public ReflectionTapestryProvider() {
25: super ();
26: }
27:
28: /**
29: * {@inheritDoc}
30: */
31: public int getPriority() {
32: return 30;
33: }
34:
35: /**
36: * {@inheritDoc}
37: */
38: public Locale guessLocaleFromSession(HttpSession httpSession) {
39: Locale locale = null;
40:
41: // Tapestry 3.0: Engine stored in session under "org.apache.tapestry.engine:" + config.getServletName()
42: // TODO: Tapestry 4+
43: final List tapestryArray = new ArrayList();
44: for (Enumeration enumeration = httpSession.getAttributeNames(); enumeration
45: .hasMoreElements();) {
46: String name = (String) enumeration.nextElement();
47: if (name.indexOf("tapestry") > -1 && name.indexOf("engine") > -1 && null != httpSession.getAttribute(name)) {//$NON-NLS-1$ //$NON-NLS-2$
48: tapestryArray.add(httpSession.getAttribute(name));
49: }
50: }
51: if (tapestryArray.size() == 1) {
52: // found a potential Engine! Let's call getLocale() on it.
53: Object probableEngine = tapestryArray.get(0);
54: if (null != probableEngine) {
55: try {
56: Method readMethod = probableEngine.getClass()
57: .getMethod("getLocale", null);//$NON-NLS-1$
58: if (null != readMethod) {
59: // Call the property getter and return the value
60: Object possibleLocale = readMethod.invoke(
61: probableEngine, null);
62: if (null != possibleLocale
63: && possibleLocale instanceof Locale) {
64: locale = (Locale) possibleLocale;
65: }
66: }
67: } catch (Exception e) {
68: // stay silent
69: }
70: }
71: }
72:
73: return locale;
74: }
75:
76: }
|