1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.webapp;
20
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23
24 import javax.el.ELContext;
25 import javax.el.ExpressionFactory;
26 import javax.el.MethodExpression;
27 import javax.el.ValueExpression;
28 import javax.faces.FacesException;
29 import javax.faces.context.ExternalContext;
30 import javax.servlet.ServletContext;
31
32
33
34
35
36 public class Jsp20FacesInitializer extends AbstractFacesInitializer
37 {
38
39
40
41
42 private static final Logger log = Logger.getLogger(Jsp20FacesInitializer.class.getName());
43
44
45
46
47 private static final String EL_RI_EXPRESSION_FACTORY_IMPL = "com.sun.el.ExpressionFactoryImpl";
48
49
50
51
52 private static final String JASPER_EL_EXPRESSION_FACTORY_IMPL = "org.apache.el.ExpressionFactoryImpl";
53
54
55
56
57 private static final String[] KNOWN_EXPRESSION_FACTORIES =
58 new String[] { EL_RI_EXPRESSION_FACTORY_IMPL, JASPER_EL_EXPRESSION_FACTORY_IMPL };
59
60 @Override
61 protected void initContainerIntegration(ServletContext servletContext, ExternalContext externalContext)
62 {
63 if (log.isLoggable(Level.INFO))
64 {
65 log.info("This application isn't running in a JSP 2.1 container.");
66 }
67
68
69
70
71
72
73
74 ExpressionFactory expressionFactory = getUserDefinedExpressionFactory(externalContext);
75
76 if (expressionFactory == null)
77 {
78 if (log.isLoggable(Level.INFO))
79 {
80 log.info("Either you haven't specified the ExpressionFactory implementation, or an "
81 + "error occured while instantiating the implementation you've specified. "
82 + "However, attempting to load a known implementation.");
83 }
84
85 expressionFactory = findExpressionFactory(KNOWN_EXPRESSION_FACTORIES);
86 if (expressionFactory == null)
87 {
88 if (log.isLoggable(Level.SEVERE))
89 {
90 log.severe("No valid ExpressionFactory implementation is available "
91 + "but that's required as this application isn't running in a JSP 2.1 container.");
92 }
93
94
95 expressionFactory = new ErrorExpressionFactory();
96 }
97 }
98
99 if (log.isLoggable(Level.FINE))
100 {
101 log.fine("The following ExpressionFactory implementation will " + "be used: '" + expressionFactory + "'.");
102 }
103
104 buildConfiguration(servletContext, externalContext, expressionFactory);
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119 private static ExpressionFactory findExpressionFactory(String[] expressionFactoryClassNames)
120 {
121 for (String expressionFactoryClassName : expressionFactoryClassNames)
122 {
123 ExpressionFactory expressionFactory = loadExpressionFactory(expressionFactoryClassName);
124 if (expressionFactory != null)
125 {
126 return expressionFactory;
127 }
128 }
129
130 return null;
131 }
132
133
134
135
136
137
138
139 private class ErrorExpressionFactory extends ExpressionFactory
140 {
141
142 @Override
143 public Object coerceToType(Object obj, Class<?> targetType)
144 {
145 throw new FacesException("No valid ExpressionFactory implementation is available "
146 + "but that's required as this application isn't running in a JSP 2.1 container.");
147 }
148
149 @Override
150 public MethodExpression createMethodExpression(ELContext context, String expression,
151 Class<?> expectedReturnType, Class<?>[] expectedParamTypes)
152 {
153 throw new FacesException("No valid ExpressionFactory implementation is available "
154 + "but that's required as this application isn't running in a JSP 2.1 container.");
155 }
156
157 @Override
158 public ValueExpression createValueExpression(Object instance, Class<?> expectedType)
159 {
160 throw new FacesException("No valid ExpressionFactory implementation is available "
161 + "but that's required as this application isn't running in a JSP 2.1 container.");
162 }
163
164 @Override
165 public ValueExpression createValueExpression(ELContext context, String expression, Class<?> expectedType)
166 {
167 throw new FacesException("No valid ExpressionFactory implementation is available "
168 + "but that's required as this application isn't running in a JSP 2.1 container.");
169 }
170
171 }
172
173 }