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 com.caucho.jsf.webapp;
030:
031: import java.io.*;
032: import java.lang.reflect.*;
033: import java.net.*;
034: import java.util.*;
035: import java.util.logging.*;
036:
037: import javax.faces.*;
038: import javax.faces.application.*;
039: import javax.faces.context.*;
040: import javax.faces.event.*;
041: import javax.faces.lifecycle.*;
042: import javax.faces.webapp.*;
043:
044: import javax.servlet.*;
045: import javax.servlet.http.*;
046:
047: import com.caucho.config.*;
048: import com.caucho.jsf.application.*;
049: import com.caucho.jsf.cfg.*;
050: import com.caucho.vfs.*;
051:
052: public class FacesServletImpl extends GenericServlet {
053: private static final Logger log = Logger
054: .getLogger(FacesServletImpl.class.getName());
055:
056: private static final String FACES_SCHEMA = "com/caucho/jsf/cfg/jsf.rnc";
057:
058: private ConfigException _configException;
059:
060: private ArrayList<PhaseListener> _phaseListenerList = new ArrayList<PhaseListener>();
061:
062: public FacesServletImpl() {
063: }
064:
065: public void init(ServletConfig config) throws ServletException {
066: Object factoryObj;
067: String factory;
068:
069: initFactory(FactoryFinder.APPLICATION_FACTORY,
070: "com.caucho.jsf.application.ApplicationFactoryImpl");
071:
072: initFactory(FactoryFinder.LIFECYCLE_FACTORY,
073: "com.caucho.jsf.lifecycle.LifecycleFactoryImpl");
074:
075: initFactory(FactoryFinder.RENDER_KIT_FACTORY,
076: "com.caucho.jsf.render.RenderKitFactoryImpl");
077:
078: initFactory(FactoryFinder.FACES_CONTEXT_FACTORY,
079: "com.caucho.jsf.context.FacesContextFactoryImpl");
080:
081: ApplicationFactory appFactory = (ApplicationFactory) FactoryFinder
082: .getFactory(FactoryFinder.APPLICATION_FACTORY);
083:
084: Application app = appFactory.getApplication();
085:
086: if (app == null) {
087: app = new ApplicationImpl();
088: appFactory.setApplication(app);
089: }
090:
091: ClassLoader loader = Thread.currentThread()
092: .getContextClassLoader();
093: try {
094: Enumeration e = loader
095: .getResources("META-INF/faces-config.xml");
096: while (e != null && e.hasMoreElements()) {
097: URL url = (URL) e.nextElement();
098: initPath(app, Vfs.lookup(url.toString()));
099: }
100: } catch (IOException e) {
101: throw ConfigException.create(e);
102: }
103:
104: String path = config.getServletContext().getInitParameter(
105: FacesServlet.CONFIG_FILES_ATTR);
106:
107: String[] paths = null;
108:
109: if (path != null)
110: paths = path.split("\\s*,\\s*");
111:
112: for (int i = 0; paths != null && i < paths.length; i++)
113: initPath(app, Vfs.lookup("./" + paths[i]));
114:
115: initPath(app, Vfs.lookup("WEB-INF/faces-config.xml"));
116:
117: if (app.getViewHandler() == null)
118: app.setViewHandler(new JspViewHandler());
119:
120: if (app.getStateManager() == null)
121: app.setStateManager(new SessionStateManager());
122:
123: LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder
124: .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
125:
126: Iterator iter = lifecycleFactory.getLifecycleIds();
127: while (iter.hasNext()) {
128: Lifecycle lifecycle = lifecycleFactory
129: .getLifecycle((String) iter.next());
130:
131: for (PhaseListener listener : _phaseListenerList) {
132: lifecycle.addPhaseListener(listener);
133: }
134: }
135: }
136:
137: private static void initFactory(String factoryName,
138: String defaultName) {
139: Object factoryObj = null;
140: String factory;
141:
142: try {
143: factoryObj = FactoryFinder.getFactory(factoryName);
144: } catch (FacesException e) {
145: }
146:
147: if (factoryObj == null) {
148: factory = getServiceFactory(factoryName);
149:
150: if (factory == null || "".equals(factory))
151: factory = defaultName;
152:
153: FactoryFinder.setFactory(factoryName, factory);
154: }
155: }
156:
157: private static String getServiceFactory(String factoryName) {
158: try {
159: ClassLoader loader = Thread.currentThread()
160: .getContextClassLoader();
161:
162: InputStream is = loader
163: .getResourceAsStream("META-INF/services/"
164: + factoryName);
165:
166: try {
167: if (is != null) {
168: BufferedReader reader = new BufferedReader(
169: new InputStreamReader(is));
170: String line = reader.readLine();
171:
172: if (line != null) {
173: if (line.indexOf('#') >= 0)
174: line = line.substring(0, line.indexOf('#'));
175:
176: return line.trim();
177: }
178: }
179: } catch (Exception e) {
180: log.log(Level.WARNING, e.toString(), e);
181: } finally {
182: is.close();
183: }
184: } catch (Exception e) {
185: }
186:
187: return null;
188: }
189:
190: private void initPath(Application app, Path facesPath)
191: throws ServletException {
192: if (facesPath.canRead() && !facesPath.isDirectory()) {
193: try {
194: FacesConfig facesConfig = new FacesConfig();
195:
196: Config config = new Config();
197:
198: config.setEL(false);
199:
200: config.configure(facesConfig, facesPath, FACES_SCHEMA);
201:
202: if (app instanceof ApplicationImpl) {
203: ApplicationImpl appImpl = (ApplicationImpl) app;
204:
205: // jsf/4409
206: NavigationHandlerImpl navigationHandler = appImpl
207: .getDefaultNavigationHandler();
208:
209: List<NavigationRule> navigationRules = facesConfig
210: .getNavigationRules();
211:
212: for (NavigationRule navigationRule : navigationRules) {
213: navigationHandler.addRule(navigationRule);
214: }
215:
216: facesConfig.configure(appImpl);
217:
218: facesConfig
219: .configurePhaseListeners(_phaseListenerList);
220:
221: for (ManagedBeanConfig bean : facesConfig
222: .getManagedBeans()) {
223: appImpl.addManagedBean(bean.getName(), bean);
224: }
225:
226: ApplicationConfig appConfig = facesConfig
227: .getApplication();
228:
229: if (appConfig != null) {
230: appConfig.configure(appImpl);
231:
232: for (ResourceBundleConfig bundle : appConfig
233: .getResourceBundleList()) {
234: appImpl.addResourceBundle(bundle.getVar(),
235: bundle);
236: }
237: }
238: }
239: } catch (ConfigException e) {
240: _configException = e;
241:
242: throw e;
243: } catch (RuntimeException e) {
244: throw e;
245: } catch (Exception e) {
246: throw new ServletException(e);
247: }
248: }
249: }
250:
251: public void service(ServletRequest request, ServletResponse response)
252: throws IOException, ServletException {
253: throw new UnsupportedOperationException();
254: }
255:
256: public void destroy() {
257: }
258: }
|