1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.jasper.compiler;
18
19 import javax.servlet.ServletContext;
20
21 import org.apache.jasper.JspCompilationContext;
22
23 /**
24  * Provides {@link ELInterpreter} instances for JSP compilation.
25  *
26  * The search order is as follows:
27  * <ol>
28  * <li>ELInterpreter instance or implementation class name provided as a
29  *     ServletContext attribute</li>
30  * <li>Implementation class named in a ServletContext initialisation parameter
31  *     </li>
32  * <li>Default implementation</li>
33  * </ol>
34  */

35 public class ELInterpreterFactory {
36
37     public static final String EL_INTERPRETER_CLASS_NAME =
38             ELInterpreter.class.getName();
39
40     private static final ELInterpreter DEFAULT_INSTANCE =
41             new DefaultELInterpreter();
42
43
44     /**
45      * Obtain the correct EL Interpreter for the given web application.
46      * @param context The Servlet context
47      * @return the EL interpreter
48      * @throws Exception If an error occurs creating the interpreter
49      */

50     public static ELInterpreter getELInterpreter(ServletContext context)
51             throws Exception {
52
53         ELInterpreter result = null;
54
55         // Search for an implementation
56         // 1. ServletContext attribute (set by application or cached by a
57         //    previous call to this method).
58         Object attribute = context.getAttribute(EL_INTERPRETER_CLASS_NAME);
59         if (attribute instanceof ELInterpreter) {
60             return (ELInterpreter) attribute;
61         } else if (attribute instanceof String) {
62             result = createInstance(context, (String) attribute);
63         }
64
65         // 2. ServletContext init parameter
66         if (result == null) {
67             String className =
68                     context.getInitParameter(EL_INTERPRETER_CLASS_NAME);
69             if (className != null) {
70                 result = createInstance(context, className);
71             }
72         }
73
74         // 3. Default
75         if (result == null) {
76             result = DEFAULT_INSTANCE;
77         }
78
79         // Cache the result for next time
80         context.setAttribute(EL_INTERPRETER_CLASS_NAME, result);
81         return result;
82     }
83
84
85     private static ELInterpreter createInstance(ServletContext context,
86             String className) throws Exception {
87         return (ELInterpreter) context.getClassLoader().loadClass(
88                     className).getConstructor().newInstance();
89     }
90
91
92     private ELInterpreterFactory() {
93         // Utility class. Hide default constructor.
94     }
95
96
97     public static class DefaultELInterpreter implements ELInterpreter {
98
99         @Override
100         public String interpreterCall(JspCompilationContext context,
101                 boolean isTagFile, String expression,
102                 Class<?> expectedType, String fnmapvar) {
103             return JspUtil.interpreterCall(isTagFile, expression, expectedType,
104                     fnmapvar);
105         }
106     }
107 }
108