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.List; |
22 | |
|
23 | |
import javax.el.ExpressionFactory; |
24 | |
import javax.faces.context.ExternalContext; |
25 | |
import javax.servlet.ServletContext; |
26 | |
|
27 | |
import org.apache.commons.logging.Log; |
28 | |
import org.apache.commons.logging.LogFactory; |
29 | |
import org.apache.myfaces.application.ApplicationImpl; |
30 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam; |
31 | |
import org.apache.myfaces.config.FacesConfigValidator; |
32 | |
import org.apache.myfaces.config.FacesConfigurator; |
33 | |
import org.apache.myfaces.config.RuntimeConfig; |
34 | |
import org.apache.myfaces.context.servlet.ServletExternalContextImpl; |
35 | |
import org.apache.myfaces.shared_impl.util.StateUtils; |
36 | |
import org.apache.myfaces.shared_impl.webapp.webxml.WebXml; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public abstract class AbstractFacesInitializer implements FacesInitializer |
43 | |
{ |
44 | |
|
45 | |
|
46 | |
|
47 | 0 | private static final Log log = LogFactory.getLog(AbstractFacesInitializer.class); |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
@JSFWebConfigParam(since="1.2.7") |
53 | |
protected static final String EXPRESSION_FACTORY = "org.apache.myfaces.EXPRESSION_FACTORY"; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public void initFaces(ServletContext servletContext) |
60 | |
{ |
61 | |
try { |
62 | 0 | if (log.isTraceEnabled()) { |
63 | 0 | log.trace("Initializing MyFaces"); |
64 | |
} |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | 0 | ExternalContext externalContext = new ServletExternalContextImpl( |
71 | |
servletContext, null, null); |
72 | |
|
73 | |
|
74 | 0 | WebXml webXml = WebXml.getWebXml(externalContext); |
75 | 0 | if (webXml == null) { |
76 | 0 | if (log.isWarnEnabled()) { |
77 | 0 | log.warn("Couldn't find the web.xml configuration file. " |
78 | |
+ "Abort initializing MyFaces."); |
79 | |
} |
80 | |
|
81 | 0 | return; |
82 | 0 | } else if (webXml.getFacesServletMappings().isEmpty()) { |
83 | 0 | if (log.isWarnEnabled()) { |
84 | 0 | log.warn("No mappings of FacesServlet found. Abort initializing MyFaces."); |
85 | |
} |
86 | |
|
87 | 0 | return; |
88 | |
} |
89 | |
|
90 | 0 | initContainerIntegration(servletContext, externalContext); |
91 | |
|
92 | 0 | String useEncryption = servletContext.getInitParameter(StateUtils.USE_ENCRYPTION); |
93 | 0 | if (!"false".equals(useEncryption)) { |
94 | 0 | StateUtils.initSecret(servletContext); |
95 | |
} |
96 | |
|
97 | 0 | if (log.isInfoEnabled()) { |
98 | 0 | log.info("ServletContext '" + servletContext.getRealPath("/") + "' initialized."); |
99 | |
} |
100 | 0 | } catch (Exception ex) { |
101 | 0 | log.error("An error occured while initializing MyFaces: " |
102 | |
+ ex.getMessage(), ex); |
103 | 0 | } |
104 | 0 | } |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
public void destroyFaces(ServletContext servletContext) |
111 | |
{ |
112 | |
|
113 | 0 | } |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
protected RuntimeConfig buildConfiguration(ServletContext servletContext, |
130 | |
ExternalContext externalContext, ExpressionFactory expressionFactory) |
131 | |
{ |
132 | 0 | RuntimeConfig runtimeConfig = RuntimeConfig.getCurrentInstance(externalContext); |
133 | 0 | runtimeConfig.setExpressionFactory(expressionFactory); |
134 | |
|
135 | 0 | ApplicationImpl.setInitializingRuntimeConfig(runtimeConfig); |
136 | |
|
137 | |
|
138 | 0 | new FacesConfigurator(externalContext).configure(); |
139 | |
|
140 | 0 | validateFacesConfig(servletContext, externalContext); |
141 | |
|
142 | 0 | return runtimeConfig; |
143 | |
} |
144 | |
|
145 | |
protected void validateFacesConfig(ServletContext servletContext, ExternalContext externalContext) |
146 | |
{ |
147 | 0 | String validate = servletContext.getInitParameter(FacesConfigValidator.VALIDATE_CONTEXT_PARAM); |
148 | 0 | if ("true".equals(validate) && log.isWarnEnabled()) { |
149 | 0 | List<String> warnings = FacesConfigValidator.validate( |
150 | |
externalContext, servletContext.getRealPath("/")); |
151 | |
|
152 | 0 | for (String warning : warnings) { |
153 | 0 | log.warn(warning); |
154 | |
} |
155 | |
} |
156 | 0 | } |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
protected static ExpressionFactory getUserDefinedExpressionFactory(ExternalContext externalContext) |
169 | |
{ |
170 | 0 | String expressionFactoryClassName = externalContext.getInitParameter(EXPRESSION_FACTORY); |
171 | 0 | if (expressionFactoryClassName != null |
172 | |
&& expressionFactoryClassName.trim().length() > 0) { |
173 | 0 | if (log.isDebugEnabled()) { |
174 | 0 | log.debug("Attempting to load the ExpressionFactory implementation " |
175 | |
+ "you've specified: '" + expressionFactoryClassName + "'."); |
176 | |
} |
177 | |
|
178 | 0 | return loadExpressionFactory(expressionFactoryClassName); |
179 | |
} |
180 | |
|
181 | 0 | return null; |
182 | |
} |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
protected static ExpressionFactory loadExpressionFactory(String expressionFactoryClassName) |
194 | |
{ |
195 | |
try { |
196 | 0 | Class<?> expressionFactoryClass = Class.forName(expressionFactoryClassName); |
197 | 0 | return (ExpressionFactory) expressionFactoryClass.newInstance(); |
198 | 0 | } catch (Exception ex) { |
199 | 0 | if (log.isDebugEnabled()) { |
200 | 0 | log.debug("An error occured while instantiating a new ExpressionFactory. " |
201 | |
+ "Attempted to load class '" + expressionFactoryClassName + "'.", ex); |
202 | |
} |
203 | |
} |
204 | |
|
205 | 0 | return null; |
206 | |
} |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
protected abstract void initContainerIntegration( |
217 | |
ServletContext servletContext, ExternalContext externalContext); |
218 | |
|
219 | |
} |