All Downloads are FREE. Search and download functionalities are using the official Maven repository.

clime.messadmin.providers.locale.ReflexionTapestryProvider Maven / Gradle / Ivy

Go to download

Notification system and Session administration for J2EE Web Applications

There is a newer version: 4.1.1
Show newest version
/**
 * 
 */
package clime.messadmin.providers.locale;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpSession;

import clime.messadmin.providers.spi.LocaleProvider;

/**
 * Note: this provider implementation uses reflexion, to avoid linking against Tapestry libs.
 * @author Cédrik LIME
 */
public class ReflexionTapestryProvider implements LocaleProvider {

	/**
	 * 
	 */
	public ReflexionTapestryProvider() {
		super();
	}

	/**
	 * {@inheritDoc}
	 */
	public int getPriority() {
		return 30;
	}

	/**
	 * {@inheritDoc}
	 */
	public Locale guessLocaleFromSession(HttpSession httpSession) {
		Locale locale = null;

		// Tapestry 3.0: Engine stored in session under "org.apache.tapestry.engine:" + config.getServletName()
		// TODO: Tapestry 4+
		final List tapestryArray = new ArrayList();
		for (Enumeration enumeration = httpSession.getAttributeNames(); enumeration.hasMoreElements();) {
			String name = (String) enumeration.nextElement();
			if (name.indexOf("tapestry") > -1 && name.indexOf("engine") > -1 && null != httpSession.getAttribute(name)) {//$NON-NLS-1$ //$NON-NLS-2$
				tapestryArray.add(httpSession.getAttribute(name));
			}
		}
		if (tapestryArray.size() == 1) {
			// found a potential Engine! Let's call getLocale() on it.
			Object probableEngine = tapestryArray.get(0);
			if (null != probableEngine) {
				try {
					Method readMethod = probableEngine.getClass().getMethod("getLocale", null);//$NON-NLS-1$
					if (null != readMethod) {
						// Call the property getter and return the value
						Object possibleLocale = readMethod.invoke(probableEngine, null);
						if (null != possibleLocale && possibleLocale instanceof Locale) {
							locale = (Locale) possibleLocale;
						}
					}
				} catch (Exception e) {
					// stay silent
				}
			}
		}

		return locale;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy