001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package javax.faces.webapp;
030:
031: import java.io.*;
032: import java.lang.reflect.*;
033: import java.util.logging.*;
034:
035: import javax.faces.*;
036: import javax.faces.application.*;
037: import javax.faces.context.*;
038: import javax.faces.lifecycle.*;
039:
040: import javax.servlet.*;
041: import javax.servlet.http.*;
042:
043: public final class FacesServlet implements Servlet {
044: private static final Logger log = Logger
045: .getLogger(FacesServlet.class.getName());
046:
047: public static final String CONFIG_FILES_ATTR = "javax.faces.CONFIG_FILES";
048: public static final String LIFECYCLE_ID_ATTR = "javax.faces.LIFECYCLE_ID";
049:
050: private ServletConfig _config;
051: private ServletContext _webApp;
052:
053: private FacesContextFactory _facesContextFactory;
054: private Lifecycle _lifecycle;
055:
056: public FacesServlet() {
057: }
058:
059: public ServletConfig getServletConfig() {
060: return _config;
061: }
062:
063: public String getServletInfo() {
064: return "javax.faces.webapp.FacesServlet";
065: }
066:
067: public void init(ServletConfig config) throws ServletException {
068: _config = config;
069: _webApp = config.getServletContext();
070:
071: try {
072: Class cl = Class
073: .forName("com.caucho.jsf.webapp.FacesServletImpl");
074:
075: Servlet servlet = (Servlet) cl.newInstance();
076:
077: Method init = cl.getMethod("init",
078: new Class[] { ServletConfig.class });
079:
080: init.invoke(servlet, config);
081: } catch (ClassNotFoundException e) {
082: log.log(Level.FINER, e.toString(), e);
083: } catch (RuntimeException e) {
084: throw e;
085: } catch (InvocationTargetException e) {
086: if (e.getCause() instanceof RuntimeException)
087: throw (RuntimeException) e.getCause();
088: else if (e.getCause() instanceof ServletException)
089: throw (ServletException) e.getCause();
090: else
091: throw new ServletException(e);
092: } catch (Exception e) {
093: throw new ServletException(e);
094: }
095:
096: _facesContextFactory = (FacesContextFactory) FactoryFinder
097: .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
098:
099: LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder
100: .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
101:
102: String name = null;
103:
104: name = config.getInitParameter("javax.faces.LIFECYCLE_ID");
105:
106: if (name == null)
107: name = _webApp.getInitParameter("javax.faces.LIFECYCLE_ID");
108:
109: if (name == null)
110: name = LifecycleFactory.DEFAULT_LIFECYCLE;
111:
112: _lifecycle = lifecycleFactory.getLifecycle(name);
113: }
114:
115: public void service(ServletRequest request, ServletResponse response)
116: throws IOException, ServletException {
117: HttpServletRequest req = (HttpServletRequest) request;
118: HttpServletResponse res = (HttpServletResponse) response;
119:
120: FacesContext oldContext = FacesContext.getCurrentInstance();
121:
122: FacesContext context = null;
123:
124: try {
125: context = _facesContextFactory.getFacesContext(_webApp,
126: req, res, _lifecycle);
127:
128: _lifecycle.execute(context);
129: _lifecycle.render(context);
130: } catch (FacesException e) {
131: throw new ServletException(e);
132: } finally {
133: if (context != null)
134: context.release();
135:
136: FacesContext.setCurrentInstance(oldContext);
137: }
138: }
139:
140: public void destroy() {
141: }
142: }
|