package org.apache.jasper.compiler; import javax.servlet.ServletContext; import org.apache.jasper.JspCompilationContext; /** * ELInterpreter Factory * * Supports how to get a right ELInterpreter from ServletContext. * * 1. It supports get ELInterpreter instance from ServletContext, so that Web Application can provide its own ELInterpreter. * 2. Customized ELInterpreter can be passed-in by setting "org.apache.jasper.compiler.ELInterpreter=xxx" in ServletContext's initialization parameters. * 3. If there is no any ELInterpreter, the DefaultELInterpreter will be used * * @author Sheldon Shao (XShao@ebay.com) */ public class ELInterpreterFactory { private ELInterpreterFactory() { } public static final String EL_INTERPRETER_CLASS_NAME = ELInterpreter.class.getName(); /** * Default Instance */ private static final ELInterpreter DEFAULT_INSTANCE = new DefaultELInterpreter(); public static class DefaultELInterpreter implements ELInterpreter { /** * Produces a String representing a call to the EL interpreter. * * @param context JspCompilationContext * @param expression * a String containing zero or more "${}" expressions * @param expectedType * the expected type of the interpreted result * @param fnmapvar * Variable pointing to a function map. * @param XmlEscape * True if the result should do XML escaping * @return a String representing a call to the EL interpreter. */ @Override public String interpreterCall(JspCompilationContext context, boolean isTagFile, String expression, Class expectedType, String fnmapvar, boolean xmlEscape) { return JspUtil.interpreterCall(isTagFile, expression, expectedType, fnmapvar, xmlEscape); } } /** * Retrieve ELInterpreter implementation from ServletContext * * @param context ServletContext */ public static ELInterpreter getELInterpreter(ServletContext context) throws Exception { ELInterpreter interpreter = null; //Check ELInterpreter in Object conf = context.getAttribute(EL_INTERPRETER_CLASS_NAME); if (conf == null) { String className = context.getInitParameter(EL_INTERPRETER_CLASS_NAME); if (className == null) { //If there is no any parameter from Context, try to resolve the implementation from System property className = System.getProperty(EL_INTERPRETER_CLASS_NAME); } if (className != null) { interpreter = toInterpreter(context, className); } else { interpreter = DEFAULT_INSTANCE; } } else if (conf instanceof ELInterpreter) { interpreter = (ELInterpreter)conf; return interpreter; } else if (conf instanceof String) { interpreter = toInterpreter(context, (String)conf); } context.setAttribute(EL_INTERPRETER_CLASS_NAME, interpreter); return interpreter; } /** * Create ELInterpreter by given class name * * @param context ServletContext * @param className Class Name * @return */ private static ELInterpreter toInterpreter(ServletContext context, String className) throws Exception { return (ELInterpreter)context.getClassLoader().loadClass(className).newInstance(); } }