001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.myfaces.webapp;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import org.apache.myfaces.application.ApplicationImpl;
024: import org.apache.myfaces.config.FacesConfigValidator;
025: import org.apache.myfaces.config.FacesConfigurator;
026: import org.apache.myfaces.config.RuntimeConfig;
027: import org.apache.myfaces.context.servlet.ServletExternalContextImpl;
028: import org.apache.myfaces.el.ResolverForJSPInitializer;
029: import org.apache.myfaces.el.unified.ELResolverBuilder;
030: import org.apache.myfaces.el.unified.ResolverBuilderForJSP;
031: import org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver;
032: import org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.Scope;
033: import org.apache.myfaces.shared_impl.util.StateUtils;
034: import org.apache.myfaces.shared_impl.webapp.webxml.WebXml;
035:
036: import javax.faces.FactoryFinder;
037: import javax.faces.context.ExternalContext;
038: import javax.faces.event.PhaseListener;
039: import javax.faces.lifecycle.LifecycleFactory;
040: import javax.servlet.ServletContext;
041: import javax.servlet.jsp.JspApplicationContext;
042: import javax.servlet.jsp.JspFactory;
043: import java.util.Iterator;
044: import java.util.List;
045:
046: /**
047: * @author Mathias Broekelmann (latest modification by $Author: baranda $)
048: * @version $Revision: 532019 $ $Date: 2007-04-24 19:34:36 +0200 (Di, 24 Apr 2007) $
049: */
050: public class DefaultFacesInitializer implements FacesInitializer {
051: private static final Log log = LogFactory
052: .getLog(DefaultFacesInitializer.class);
053: private JspFactory _jspFactory;
054:
055: protected ELResolverBuilder createResolverBuilderForJSP(
056: RuntimeConfig runtimeConfig) {
057: return new ResolverBuilderForJSP(runtimeConfig);
058: }
059:
060: public void initFaces(ServletContext servletContext) {
061: try {
062: log.trace("Initializing MyFaces");
063:
064: // Load the configuration
065: ExternalContext externalContext = new ServletExternalContextImpl(
066: servletContext, null, null);
067:
068: // parse web.xml
069: WebXml webXml = WebXml.getWebXml(externalContext);
070: if (webXml == null) {
071: log
072: .warn("Couldn't find web.xml. Abort initializing MyFaces.");
073: return;
074: }
075: if (webXml.getFacesServletMappings().isEmpty()) {
076: log
077: .warn("No mappings of FacesServlet found. Abort initializing MyFaces.");
078: return;
079: }
080:
081: // TODO: this Class.forName will be removed when Tomcat fixes a bug
082: // also, we should then be able to remove jasper.jar from the deployment
083: try {
084: Class
085: .forName("org.apache.jasper.compiler.JspRuntimeContext");
086: } catch (ClassNotFoundException e) {
087: // ignore
088: } catch (Exception e) {
089: log.debug(e.getMessage(), e);
090: }
091:
092: JspFactory jspFactory = getJspFactory();
093: if (log.isDebugEnabled()) {
094: log.debug("jspfactory = " + jspFactory);
095: }
096: JspApplicationContext appCtx = jspFactory
097: .getJspApplicationContext(servletContext);
098:
099: appCtx.addELContextListener(new FacesELContextListener());
100:
101: RuntimeConfig runtimeConfig = RuntimeConfig
102: .getCurrentInstance(externalContext);
103: runtimeConfig.setExpressionFactory(appCtx
104: .getExpressionFactory());
105:
106: ApplicationImpl.setInitializingRuntimeConfig(runtimeConfig);
107:
108: // And configure everything
109: new FacesConfigurator(externalContext).configure();
110:
111: validateFacesConfigIfNecessary(servletContext,
112: externalContext);
113:
114: // configure the el resolver for jsp
115: configureResolverForJSP(appCtx, runtimeConfig);
116:
117: if (!"false".equals(servletContext
118: .getInitParameter(StateUtils.USE_ENCRYPTION)))
119: StateUtils.initSecret(servletContext);
120:
121: log.info("ServletContext '"
122: + servletContext.getRealPath("/")
123: + "' initialized.");
124: } catch (Exception ex) {
125: log.error("Error initializing MyFaces: " + ex.getMessage(),
126: ex);
127: }
128: }
129:
130: protected JspFactory getJspFactory() {
131: if (_jspFactory == null) {
132: return JspFactory.getDefaultFactory();
133: }
134: return _jspFactory;
135: }
136:
137: /**
138: * @param jspFactory the jspFactory to set
139: */
140: public void setJspFactory(JspFactory jspFactory) {
141: _jspFactory = jspFactory;
142: }
143:
144: /**
145: * Register a phase listener to every lifecycle. This listener will lazy fill the el resolver for jsp as soon as the
146: * first lifecycle is executed. This is necessarry to allow a faces application further setup after MyFaces has been
147: * initialized. When the first request is processed no further configuation of the el resolvers is allowed.
148: *
149: * @param appCtx
150: * @param runtimeConfig
151: */
152: private void configureResolverForJSP(JspApplicationContext appCtx,
153: RuntimeConfig runtimeConfig) {
154: FacesCompositeELResolver facesCompositeELResolver = new FacesCompositeELResolver(
155: Scope.JSP);
156: appCtx.addELResolver(facesCompositeELResolver);
157: PhaseListener resolverForJSPInitializer = new ResolverForJSPInitializer(
158: createResolverBuilderForJSP(runtimeConfig),
159: facesCompositeELResolver);
160:
161: LifecycleFactory factory = (LifecycleFactory) FactoryFinder
162: .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
163: for (Iterator<String> iter = factory.getLifecycleIds(); iter
164: .hasNext();) {
165: factory.getLifecycle(iter.next()).addPhaseListener(
166: resolverForJSPInitializer);
167: }
168: }
169:
170: protected void validateFacesConfigIfNecessary(
171: ServletContext servletContext,
172: ExternalContext externalContext) {
173: if ("true"
174: .equals(servletContext
175: .getInitParameter(FacesConfigValidator.VALIDATE_CONTEXT_PARAM))) {
176: List<String> list = FacesConfigValidator.validate(
177: externalContext, servletContext.getRealPath("/"));
178:
179: Iterator<String> iterator = list.iterator();
180:
181: while (iterator.hasNext())
182: log.warn(iterator.next());
183:
184: }
185: }
186:
187: public void destroyFaces(ServletContext servletContext) {
188: // TODO is it possible to make a real cleanup?
189: }
190:
191: }
|