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.cfg;
030:
031: import com.caucho.config.program.ConfigProgram;
032: import java.util.*;
033: import java.util.logging.*;
034: import java.lang.reflect.*;
035:
036: import javax.el.*;
037:
038: import javax.faces.*;
039: import javax.faces.application.*;
040: import javax.faces.component.*;
041: import javax.faces.component.html.*;
042: import javax.faces.context.*;
043: import javax.faces.convert.*;
044: import javax.faces.el.*;
045: import javax.faces.event.*;
046: import javax.faces.validator.*;
047:
048: import javax.xml.bind.annotation.*;
049:
050: import com.caucho.config.*;
051: import com.caucho.util.*;
052:
053: public class ApplicationConfig {
054: private static final Logger log = Logger
055: .getLogger(ApplicationConfig.class.getName());
056: private static final L10N L = new L10N(ApplicationConfig.class);
057:
058: private String _id;
059:
060: private Class _actionListener;
061:
062: private String _defaultRenderKitId;
063:
064: @XmlElement(name="message-bundle")
065: private String _messageBundle;
066:
067: private Class _navigationHandler;
068:
069: private Class _viewHandler;
070:
071: private Class _stateManager;
072:
073: private ArrayList<ELResolver> _elResolverList = new ArrayList<ELResolver>();
074:
075: private Class _propertyResolver;
076:
077: private Class _variableResolver;
078:
079: private ArrayList<ResourceBundleConfig> _resourceBundleList = new ArrayList<ResourceBundleConfig>();
080:
081: private LocaleConfig _localeConfig;
082:
083: public void setId(String id) {
084: _id = id;
085: }
086:
087: public void setDefaultRenderKitId(String id) {
088: _defaultRenderKitId = id;
089: }
090:
091: public void setMessageBundle(String messageBundle) {
092: _messageBundle = messageBundle;
093: }
094:
095: public void setActionListener(Class actionListener)
096: throws ConfigException {
097: Config.validate(actionListener, ActionListener.class);
098:
099: _actionListener = actionListener;
100: }
101:
102: public void setNavigationHandler(Class navigationHandler)
103: throws ConfigException {
104: if (!NavigationHandler.class
105: .isAssignableFrom(navigationHandler))
106: throw new ConfigException(
107: L
108: .l(
109: "navigation-handler '{0}' must extend javax.faces.application.NavigationHandler.",
110: navigationHandler.getName()));
111:
112: Constructor ctor = null;
113:
114: try {
115: ctor = navigationHandler
116: .getConstructor(new Class[] { NavigationHandler.class });
117: } catch (Exception e) {
118: log.log(Level.FINEST, e.toString(), e);
119: }
120:
121: try {
122: if (ctor == null)
123: ctor = navigationHandler.getConstructor(new Class[] {});
124: } catch (Exception e) {
125: log.log(Level.FINEST, e.toString(), e);
126: }
127:
128: if (ctor == null)
129: throw new ConfigException(
130: L
131: .l(
132: "navigation-handler '{0}' must have either a zero-arg constructor or a constructor with a single NavigationHandler argument.",
133: navigationHandler.getName()));
134:
135: _navigationHandler = navigationHandler;
136: }
137:
138: public void setViewHandler(Class viewHandler)
139: throws ConfigException {
140: if (!ViewHandler.class.isAssignableFrom(viewHandler))
141: throw new ConfigException(
142: L
143: .l(
144: "view-handler '{0}' must extend javax.faces.application.ViewHandler.",
145: viewHandler.getName()));
146:
147: Constructor ctor = null;
148:
149: try {
150: ctor = viewHandler
151: .getConstructor(new Class[] { ViewHandler.class });
152: } catch (Exception e) {
153: log.log(Level.FINEST, e.toString(), e);
154: }
155:
156: try {
157: if (ctor == null)
158: ctor = viewHandler.getConstructor(new Class[] {});
159: } catch (Exception e) {
160: log.log(Level.FINEST, e.toString(), e);
161: }
162:
163: if (ctor == null)
164: throw new ConfigException(
165: L
166: .l(
167: "view-handler '{0}' must have either a zero-arg constructor or a constructor with a single ViewHandler argument.",
168: viewHandler.getName()));
169:
170: _viewHandler = viewHandler;
171: }
172:
173: private Class getViewHandler() {
174: return _viewHandler;
175: }
176:
177: public void setStateManager(Class stateManager)
178: throws ConfigException {
179: if (!StateManager.class.isAssignableFrom(stateManager))
180: throw new ConfigException(
181: L
182: .l(
183: "state-manager '{0}' must extend javax.faces.application.StateManager.",
184: stateManager.getName()));
185:
186: Constructor ctor = null;
187:
188: try {
189: ctor = stateManager
190: .getConstructor(new Class[] { StateManager.class });
191: } catch (Exception e) {
192: log.log(Level.FINEST, e.toString(), e);
193: }
194:
195: try {
196: if (ctor == null)
197: ctor = stateManager.getConstructor(new Class[] {});
198: } catch (Exception e) {
199: log.log(Level.FINEST, e.toString(), e);
200: }
201:
202: if (ctor == null)
203: throw new ConfigException(
204: L
205: .l(
206: "state-manager '{0}' must have either a zero-arg constructor or a constructor with a single StateManager argument.",
207: stateManager.getName()));
208:
209: _stateManager = stateManager;
210: }
211:
212: public Class getStateManager() {
213: return _stateManager;
214: }
215:
216: public void setElResolver(Class elResolver) throws ConfigException {
217: Config.validate(elResolver, ELResolver.class);
218:
219: try {
220: _elResolverList.add((ELResolver) elResolver.newInstance());
221: } catch (Exception e) {
222: throw ConfigException.create(e);
223: }
224: }
225:
226: public Class getElResolver() {
227: return null;
228: }
229:
230: public ArrayList<ELResolver> getElResolverList() {
231: return _elResolverList;
232: }
233:
234: public void setPropertyResolver(Class propertyResolver)
235: throws ConfigException {
236: Config.validate(propertyResolver, PropertyResolver.class);
237:
238: _propertyResolver = propertyResolver;
239: }
240:
241: public Class getPropertyResolver() {
242: return _propertyResolver;
243: }
244:
245: public void setVariableResolver(Class variableResolver)
246: throws ConfigException {
247: Config.validate(variableResolver, VariableResolver.class);
248:
249: _variableResolver = variableResolver;
250: }
251:
252: public Class getVariableResolver() {
253: return _variableResolver;
254: }
255:
256: public void setResourceBundle(ResourceBundleConfig bundle)
257: throws ConfigException {
258: _resourceBundleList.add(bundle);
259: }
260:
261: private ResourceBundleConfig getResourceBundle()
262: throws ConfigException {
263: return null;
264: }
265:
266: public ArrayList<ResourceBundleConfig> getResourceBundleList() {
267: return _resourceBundleList;
268: }
269:
270: public void setApplicationExtension(ConfigProgram program) {
271: }
272:
273: public void setLocaleConfig(LocaleConfig config) {
274: _localeConfig = config;
275: }
276:
277: public void configure(Application app) {
278: if (_localeConfig != null)
279: _localeConfig.configure(app);
280:
281: if (_defaultRenderKitId != null)
282: app.setDefaultRenderKitId(_defaultRenderKitId);
283:
284: if (_viewHandler != null) {
285: ViewHandler handler = null;
286:
287: try {
288: Constructor ctor = _viewHandler
289: .getConstructor(new Class[] { ViewHandler.class });
290:
291: ViewHandler oldHandler = app.getViewHandler();
292:
293: handler = (ViewHandler) ctor.newInstance(oldHandler);
294: } catch (NoSuchMethodException e) {
295: } catch (RuntimeException e) {
296: throw e;
297: } catch (Exception e) {
298: throw ConfigException.create(e);
299: }
300:
301: if (handler == null) {
302: try {
303: handler = (ViewHandler) _viewHandler.newInstance();
304: } catch (RuntimeException e) {
305: throw e;
306: } catch (Exception e) {
307: throw ConfigException.create(e);
308: }
309: }
310:
311: if (handler != null) {
312: app.setViewHandler(handler);
313: log.fine(L.l("JSF[] using '{0}' as view-handler",
314: handler));
315: }
316: }
317:
318: if (_navigationHandler != null) {
319: NavigationHandler handler = null;
320:
321: try {
322: Constructor ctor = _navigationHandler
323: .getConstructor(new Class[] { NavigationHandler.class });
324:
325: NavigationHandler oldHandler = app
326: .getNavigationHandler();
327:
328: handler = (NavigationHandler) ctor
329: .newInstance(oldHandler);
330: } catch (NoSuchMethodException e) {
331: } catch (RuntimeException e) {
332: throw e;
333: } catch (Exception e) {
334: throw ConfigException.create(e);
335: }
336:
337: if (handler == null) {
338: try {
339: handler = (NavigationHandler) _navigationHandler
340: .newInstance();
341: } catch (RuntimeException e) {
342: throw e;
343: } catch (Exception e) {
344: throw ConfigException.create(e);
345: }
346: }
347:
348: if (handler != null) {
349: app.setNavigationHandler(handler);
350: log.fine(L.l("JSF[] using '{0}' as navigation-handler",
351: handler));
352: }
353: }
354:
355: if (_stateManager != null) {
356: StateManager manager = null;
357:
358: try {
359: Constructor ctor = _stateManager
360: .getConstructor(new Class[] { StateManager.class });
361:
362: StateManager oldHandler = app.getStateManager();
363:
364: manager = (StateManager) ctor.newInstance(oldHandler);
365: } catch (NoSuchMethodException e) {
366: } catch (RuntimeException e) {
367: throw e;
368: } catch (Exception e) {
369: throw ConfigException.create(e);
370: }
371:
372: if (manager == null) {
373: try {
374: manager = (StateManager) _stateManager
375: .newInstance();
376: } catch (RuntimeException e) {
377: throw e;
378: } catch (Exception e) {
379: throw ConfigException.create(e);
380: }
381: }
382:
383: if (manager != null) {
384: app.setStateManager(manager);
385: log.fine(L.l("JSF[] using '{0}' as state-manager",
386: manager));
387: }
388: }
389:
390: for (int i = 0; i < _elResolverList.size(); i++)
391: app.addELResolver(_elResolverList.get(i));
392: }
393:
394: public static class LocaleConfig {
395: private Locale _defaultLocale;
396: private ArrayList<Locale> _supportedLocales = new ArrayList<Locale>();
397:
398: public void setId(String id) {
399: }
400:
401: public void setDefaultLocale(String locale) {
402: _defaultLocale = LocaleUtil.createLocale(locale);
403: }
404:
405: public void addSupportedLocale(String locale) {
406: _supportedLocales.add(LocaleUtil.createLocale(locale));
407: }
408:
409: public void configure(Application app) {
410: if (_defaultLocale != null) {
411: app.setDefaultLocale(_defaultLocale);
412: }
413:
414: app.setSupportedLocales(_supportedLocales);
415: }
416: }
417: }
|