0001: /*
0002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
0003: *
0004: * This file is part of Resin(R) Open Source
0005: *
0006: * Each copy or derived work must preserve the copyright notice and this
0007: * notice unmodified.
0008: *
0009: * Resin Open Source is free software; you can redistribute it and/or modify
0010: * it under the terms of the GNU General Public License version 2
0011: * as published by the Free Software Foundation.
0012: *
0013: * Resin Open Source is distributed in the hope that it will be useful,
0014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
0016: * of NON-INFRINGEMENT. See the GNU General Public License for more
0017: * details.
0018: *
0019: * You should have received a copy of the GNU General Public License
0020: * along with Resin Open Source; if not, write to the
0021: *
0022: * Free Software Foundation, Inc.
0023: * 59 Temple Place, Suite 330
0024: * Boston, MA 02111-1307 USA
0025: *
0026: * @author Scott Ferguson
0027: */
0028:
0029: package com.caucho.jsf.application;
0030:
0031: import com.caucho.config.Config;
0032: import com.caucho.jsf.cfg.ManagedBeanConfig;
0033: import com.caucho.jsf.cfg.ResourceBundleConfig;
0034: import com.caucho.jsf.context.FacesELContext;
0035: import com.caucho.jsf.el.FacesContextELResolver;
0036: import com.caucho.jsf.el.FacesJspELResolver;
0037: import com.caucho.jsf.el.JsfResourceBundleELResolver;
0038: import com.caucho.jsf.el.MethodBindingAdapter;
0039: import com.caucho.jsf.el.ValueBindingAdapter;
0040: import com.caucho.jsf.el.ValueExpressionAdapter;
0041: import com.caucho.server.webapp.WebApp;
0042: import com.caucho.util.L10N;
0043:
0044: import javax.el.*;
0045: import javax.faces.FacesException;
0046: import javax.faces.application.Application;
0047: import javax.faces.application.NavigationHandler;
0048: import javax.faces.application.StateManager;
0049: import javax.faces.application.ViewHandler;
0050: import javax.faces.component.*;
0051: import javax.faces.component.html.*;
0052: import javax.faces.context.FacesContext;
0053: import javax.faces.convert.*;
0054: import javax.faces.el.MethodBinding;
0055: import javax.faces.el.PropertyResolver;
0056: import javax.faces.el.ReferenceSyntaxException;
0057: import javax.faces.el.ValueBinding;
0058: import javax.faces.el.VariableResolver;
0059: import javax.faces.event.ActionListener;
0060: import javax.faces.validator.DoubleRangeValidator;
0061: import javax.faces.validator.LengthValidator;
0062: import javax.faces.validator.LongRangeValidator;
0063: import javax.faces.validator.Validator;
0064: import javax.servlet.jsp.JspApplicationContext;
0065: import javax.servlet.jsp.JspFactory;
0066: import java.lang.reflect.Constructor;
0067: import java.util.ArrayList;
0068: import java.util.Collection;
0069: import java.util.HashMap;
0070: import java.util.Iterator;
0071: import java.util.Locale;
0072: import java.util.ResourceBundle;
0073:
0074: public class ApplicationImpl extends Application {
0075: private static final L10N L = new L10N(ApplicationImpl.class);
0076:
0077: private ActionListener _actionListener;
0078: private StateManager _stateManager;
0079: private ViewHandler _viewHandler;
0080: private NavigationHandler _navigationHandler;
0081: final private NavigationHandlerImpl _defaultNavigationHandler;
0082:
0083: private PropertyResolver _propertyResolver;
0084: private VariableResolver _variableResolver;
0085:
0086: private ExpressionFactory _jsfExpressionFactory;
0087:
0088: private FacesContextELResolver _elResolver;
0089:
0090: private JsfResourceBundleELResolver _bundleResolver = new JsfResourceBundleELResolver();
0091:
0092: private ArrayList<Locale> _locales;
0093: private Locale _defaultLocale;
0094:
0095: private ArrayList<ELContextListener> _elContextListenerList = new ArrayList<ELContextListener>();
0096:
0097: private ELContextListener[] _elContextListeners;
0098:
0099: private HashMap<String, String> _componentClassNameMap = new HashMap<String, String>();
0100:
0101: private HashMap<String, Class> _componentClassMap = new HashMap<String, Class>();
0102:
0103: private HashMap<String, String> _validatorClassMap = new HashMap<String, String>();
0104:
0105: private HashMap<String, String> _converterIdNameMap = new HashMap<String, String>();
0106:
0107: private HashMap<String, Class> _converterIdMap = new HashMap<String, Class>();
0108:
0109: private HashMap<Class, String> _converterClassNameMap = new HashMap<Class, String>();
0110:
0111: private HashMap<Class, Class> _converterClassMap = new HashMap<Class, Class>();
0112:
0113: private String _defaultRenderKitId = "HTML_BASIC";
0114:
0115: private String _messageBundle;
0116:
0117: private boolean _isInit;
0118:
0119: public ApplicationImpl() {
0120: WebApp webApp = WebApp.getLocal();
0121:
0122: JspFactory jspFactory = JspFactory.getDefaultFactory();
0123:
0124: JspApplicationContext appContext = jspFactory
0125: .getJspApplicationContext(webApp);
0126:
0127: _jsfExpressionFactory = appContext.getExpressionFactory();
0128:
0129: ELResolver[] customResolvers = new ELResolver[0];
0130: _elResolver = new FacesContextELResolver(customResolvers,
0131: _bundleResolver);
0132:
0133: setViewHandler(new JspViewHandler());
0134: setStateManager(new SessionStateManager());
0135:
0136: appContext.addELResolver(new FacesJspELResolver(this ));
0137:
0138: addComponent(UIColumn.COMPONENT_TYPE,
0139: "javax.faces.component.UIColumn");
0140:
0141: addComponent(UIData.COMPONENT_TYPE,
0142: "javax.faces.component.UIData");
0143:
0144: addComponent(UIGraphic.COMPONENT_TYPE,
0145: "javax.faces.component.UIGraphic");
0146:
0147: addComponent(UIInput.COMPONENT_TYPE,
0148: "javax.faces.component.UIInput");
0149:
0150: addComponent(UIMessage.COMPONENT_TYPE,
0151: "javax.faces.component.UIMessage");
0152:
0153: addComponent(UIMessages.COMPONENT_TYPE,
0154: "javax.faces.component.UIMessages");
0155:
0156: addComponent(UINamingContainer.COMPONENT_TYPE,
0157: "javax.faces.component.UINamingContainer");
0158:
0159: addComponent(UIOutput.COMPONENT_TYPE,
0160: "javax.faces.component.UIOutput");
0161:
0162: addComponent(UIParameter.COMPONENT_TYPE,
0163: "javax.faces.component.UIParameter");
0164:
0165: addComponent(UISelectBoolean.COMPONENT_TYPE,
0166: "javax.faces.component.UISelectBoolean");
0167:
0168: addComponent(UISelectOne.COMPONENT_TYPE,
0169: "javax.faces.component.UISelectOne");
0170:
0171: addComponent(UISelectMany.COMPONENT_TYPE,
0172: "javax.faces.component.UISelectMany");
0173:
0174: addComponent(UISelectItem.COMPONENT_TYPE,
0175: "javax.faces.component.UISelectItem");
0176:
0177: addComponent(UISelectItems.COMPONENT_TYPE,
0178: "javax.faces.component.UISelectItems");
0179:
0180: addComponent(UIViewRoot.COMPONENT_TYPE,
0181: "javax.faces.component.UIViewRoot");
0182:
0183: addComponent(HtmlCommandButton.COMPONENT_TYPE,
0184: "javax.faces.component.html.HtmlCommandButton");
0185:
0186: addComponent(HtmlCommandLink.COMPONENT_TYPE,
0187: "javax.faces.component.html.HtmlCommandLink");
0188:
0189: addComponent(HtmlDataTable.COMPONENT_TYPE,
0190: "javax.faces.component.html.HtmlDataTable");
0191:
0192: addComponent(HtmlGraphicImage.COMPONENT_TYPE,
0193: "javax.faces.component.html.HtmlGraphicImage");
0194:
0195: addComponent(HtmlInputHidden.COMPONENT_TYPE,
0196: "javax.faces.component.html.HtmlInputHidden");
0197:
0198: addComponent(HtmlInputSecret.COMPONENT_TYPE,
0199: "javax.faces.component.html.HtmlInputSecret");
0200:
0201: addComponent(HtmlInputText.COMPONENT_TYPE,
0202: "javax.faces.component.html.HtmlInputText");
0203:
0204: addComponent(HtmlInputTextarea.COMPONENT_TYPE,
0205: "javax.faces.component.html.HtmlInputTextarea");
0206:
0207: addComponent(HtmlMessage.COMPONENT_TYPE,
0208: "javax.faces.component.html.HtmlMessage");
0209:
0210: addComponent(HtmlMessages.COMPONENT_TYPE,
0211: "javax.faces.component.html.HtmlMessages");
0212:
0213: addComponent(HtmlOutputFormat.COMPONENT_TYPE,
0214: "javax.faces.component.html.HtmlOutputFormat");
0215:
0216: addComponent(HtmlOutputLabel.COMPONENT_TYPE,
0217: "javax.faces.component.html.HtmlOutputLabel");
0218:
0219: addComponent(HtmlOutputLink.COMPONENT_TYPE,
0220: "javax.faces.component.html.HtmlOutputLink");
0221:
0222: addComponent(HtmlOutputText.COMPONENT_TYPE,
0223: "javax.faces.component.html.HtmlOutputText");
0224:
0225: addComponent(HtmlPanelGrid.COMPONENT_TYPE,
0226: "javax.faces.component.html.HtmlPanelGrid");
0227:
0228: addComponent(HtmlPanelGroup.COMPONENT_TYPE,
0229: "javax.faces.component.html.HtmlPanelGroup");
0230:
0231: addComponent(HtmlForm.COMPONENT_TYPE,
0232: "javax.faces.component.html.HtmlForm");
0233:
0234: addComponent(HtmlSelectBooleanCheckbox.COMPONENT_TYPE,
0235: "javax.faces.component.html.HtmlSelectBooleanCheckbox");
0236:
0237: addComponent(HtmlSelectManyCheckbox.COMPONENT_TYPE,
0238: "javax.faces.component.html.HtmlSelectManyCheckbox");
0239:
0240: addComponent(HtmlSelectManyListbox.COMPONENT_TYPE,
0241: "javax.faces.component.html.HtmlSelectManyListbox");
0242:
0243: addComponent(HtmlSelectManyMenu.COMPONENT_TYPE,
0244: "javax.faces.component.html.HtmlSelectManyMenu");
0245:
0246: addComponent(HtmlSelectOneListbox.COMPONENT_TYPE,
0247: "javax.faces.component.html.HtmlSelectOneListbox");
0248:
0249: addComponent(HtmlSelectOneMenu.COMPONENT_TYPE,
0250: "javax.faces.component.html.HtmlSelectOneMenu");
0251:
0252: addComponent(HtmlSelectOneRadio.COMPONENT_TYPE,
0253: "javax.faces.component.html.HtmlSelectOneRadio");
0254:
0255: addConverter(BooleanConverter.CONVERTER_ID,
0256: BooleanConverter.class.getName());
0257: addConverter(boolean.class, BooleanConverter.class.getName());
0258: addConverter(Boolean.class, BooleanConverter.class.getName());
0259:
0260: addConverter(CharacterConverter.CONVERTER_ID,
0261: CharacterConverter.class.getName());
0262: addConverter(char.class, CharacterConverter.class.getName());
0263: addConverter(Character.class, CharacterConverter.class
0264: .getName());
0265:
0266: addConverter(ByteConverter.CONVERTER_ID, ByteConverter.class
0267: .getName());
0268: addConverter(byte.class, ByteConverter.class.getName());
0269: addConverter(Byte.class, ByteConverter.class.getName());
0270: addConverter(Byte.TYPE, ByteConverter.class.getName());
0271:
0272: addConverter(ShortConverter.CONVERTER_ID, ShortConverter.class
0273: .getName());
0274: addConverter(short.class, ShortConverter.class.getName());
0275: addConverter(Short.class, ShortConverter.class.getName());
0276:
0277: addConverter(IntegerConverter.CONVERTER_ID,
0278: IntegerConverter.class.getName());
0279: addConverter(int.class, IntegerConverter.class.getName());
0280: addConverter(Integer.class, IntegerConverter.class.getName());
0281:
0282: addConverter(LongConverter.CONVERTER_ID, LongConverter.class
0283: .getName());
0284: addConverter(long.class, LongConverter.class.getName());
0285: addConverter(Long.class, LongConverter.class.getName());
0286:
0287: addConverter(FloatConverter.CONVERTER_ID, FloatConverter.class
0288: .getName());
0289: addConverter(float.class, FloatConverter.class.getName());
0290: addConverter(Float.class, FloatConverter.class.getName());
0291:
0292: addConverter(DoubleConverter.CONVERTER_ID,
0293: DoubleConverter.class.getName());
0294: addConverter(double.class, DoubleConverter.class.getName());
0295: addConverter(Double.class, DoubleConverter.class.getName());
0296:
0297: addConverter(DateTimeConverter.CONVERTER_ID,
0298: DateTimeConverter.class.getName());
0299:
0300: addConverter(NumberConverter.CONVERTER_ID,
0301: NumberConverter.class.getName());
0302:
0303: addConverter(BigDecimalConverter.CONVERTER_ID,
0304: BigDecimalConverter.class.getName());
0305: addConverter(java.math.BigDecimal.class,
0306: BigDecimalConverter.class.getName());
0307:
0308: addConverter(BigIntegerConverter.CONVERTER_ID,
0309: BigIntegerConverter.class.getName());
0310: addConverter(java.math.BigInteger.class,
0311: BigIntegerConverter.class.getName());
0312:
0313: addConverter(EnumConverter.CONVERTER_ID, EnumConverter.class
0314: .getName());
0315: addConverter(Enum.class, EnumConverter.class.getName());
0316:
0317: addValidator(DoubleRangeValidator.VALIDATOR_ID,
0318: DoubleRangeValidator.class.getName());
0319: addValidator(LengthValidator.VALIDATOR_ID,
0320: LengthValidator.class.getName());
0321: addValidator(LongRangeValidator.VALIDATOR_ID,
0322: LongRangeValidator.class.getName());
0323:
0324: _defaultNavigationHandler = new NavigationHandlerImpl();
0325: }
0326:
0327: public void addManagedBean(String name,
0328: ManagedBeanConfig managedBean) {
0329: _elResolver.addManagedBean(name, managedBean);
0330: }
0331:
0332: public void addResourceBundle(String name,
0333: ResourceBundleConfig bundle) {
0334: _bundleResolver.addBundle(name, bundle);
0335: }
0336:
0337: public ActionListener getActionListener() {
0338: if (_actionListener == null)
0339: _actionListener = new ActionListenerImpl();
0340:
0341: return _actionListener;
0342: }
0343:
0344: public void setActionListener(ActionListener listener) {
0345: if (listener == null)
0346: throw new NullPointerException();
0347:
0348: _actionListener = listener;
0349: }
0350:
0351: public Locale getDefaultLocale() {
0352: return _defaultLocale;
0353: }
0354:
0355: public void setDefaultLocale(Locale locale) {
0356: if (locale == null)
0357: throw new NullPointerException();
0358:
0359: _defaultLocale = locale;
0360: }
0361:
0362: public String getDefaultRenderKitId() {
0363: return _defaultRenderKitId;
0364: }
0365:
0366: public void setDefaultRenderKitId(String renderKitId) {
0367: _defaultRenderKitId = renderKitId;
0368: }
0369:
0370: public String getMessageBundle() {
0371: return _messageBundle;
0372: }
0373:
0374: public void setMessageBundle(String bundle) {
0375: _messageBundle = bundle;
0376: }
0377:
0378: @Override
0379: public ResourceBundle getResourceBundle(FacesContext context,
0380: String name) {
0381: return null;
0382: }
0383:
0384: public NavigationHandler getNavigationHandler() {
0385: if (_navigationHandler == null)
0386: return _defaultNavigationHandler;
0387:
0388: return _navigationHandler;
0389: }
0390:
0391: public void setNavigationHandler(NavigationHandler handler) {
0392: if (handler == null)
0393: throw new NullPointerException();
0394:
0395: _navigationHandler = handler;
0396: }
0397:
0398: public NavigationHandlerImpl getDefaultNavigationHandler() {
0399: return _defaultNavigationHandler;
0400: }
0401:
0402: @Deprecated
0403: public PropertyResolver getPropertyResolver() {
0404: if (_propertyResolver == null)
0405: _propertyResolver = new PropertyResolverAdapter(
0406: getELResolver());
0407:
0408: return _propertyResolver;
0409: }
0410:
0411: @Deprecated
0412: public void setPropertyResolver(PropertyResolver resolver) {
0413: _propertyResolver = resolver;
0414: }
0415:
0416: @Deprecated
0417: public VariableResolver getVariableResolver() {
0418: if (_variableResolver == null)
0419: _variableResolver = new VariableResolverAdapter(
0420: getELResolver());
0421:
0422: return _variableResolver;
0423: }
0424:
0425: @Deprecated
0426: public void setVariableResolver(VariableResolver resolver) {
0427: _variableResolver = resolver;
0428: }
0429:
0430: /**
0431: * @Since 1.2
0432: */
0433: public void addELResolver(ELResolver resolver) {
0434: if (_isInit)
0435: throw new IllegalStateException(
0436: L
0437: .l("Can't add ELResolver after Application has been initialized"));
0438: _elResolver.addELResolver(resolver);
0439: }
0440:
0441: /**
0442: * @Since 1.2
0443: */
0444: public void addELContextListener(ELContextListener listener) {
0445: _elContextListenerList.add(listener);
0446: _elContextListeners = null;
0447: }
0448:
0449: /**
0450: * @Since 1.2
0451: */
0452: public void removeELContextListener(ELContextListener listener) {
0453: _elContextListenerList.remove(listener);
0454: _elContextListeners = null;
0455: }
0456:
0457: /**
0458: * @Since 1.2
0459: */
0460: public ELContextListener[] getELContextListeners() {
0461: synchronized (_elContextListenerList) {
0462: if (_elContextListeners == null) {
0463: _elContextListeners = new ELContextListener[_elContextListenerList
0464: .size()];
0465:
0466: _elContextListenerList.toArray(_elContextListeners);
0467: }
0468: }
0469:
0470: return _elContextListeners;
0471: }
0472:
0473: /**
0474: * @Since 1.2
0475: */
0476: public ExpressionFactory getExpressionFactory() {
0477: return _jsfExpressionFactory;
0478: }
0479:
0480: @Override
0481: public ELResolver getELResolver() {
0482: return _elResolver;
0483: }
0484:
0485: public ViewHandler getViewHandler() {
0486: return _viewHandler;
0487: }
0488:
0489: public void setViewHandler(ViewHandler handler) {
0490: if (handler == null)
0491: throw new NullPointerException();
0492:
0493: _viewHandler = handler;
0494: }
0495:
0496: public StateManager getStateManager() {
0497: return _stateManager;
0498: }
0499:
0500: public void setStateManager(StateManager manager) {
0501: _stateManager = manager;
0502: }
0503:
0504: public void addComponent(String componentType, String componentClass) {
0505: if (componentType == null)
0506: throw new NullPointerException();
0507:
0508: synchronized (_componentClassNameMap) {
0509: _componentClassNameMap.put(componentType, componentClass);
0510: }
0511: }
0512:
0513: public UIComponent createComponent(String componentType)
0514: throws FacesException {
0515: if (componentType == null)
0516: throw new NullPointerException();
0517:
0518: Class cl = getComponentClass(componentType);
0519:
0520: if (cl == null)
0521: throw new FacesException(L.l(
0522: "'{0}' is an unknown UI componentType to create",
0523: componentType));
0524:
0525: try {
0526: return (UIComponent) cl.newInstance();
0527: } catch (RuntimeException e) {
0528: throw e;
0529: } catch (Exception e) {
0530: throw new FacesException(e);
0531: }
0532: }
0533:
0534: private Class getComponentClass(String name) {
0535: synchronized (_componentClassMap) {
0536: Class cl = _componentClassMap.get(name);
0537:
0538: if (cl != null)
0539: return cl;
0540:
0541: String className = _componentClassNameMap.get(name);
0542:
0543: if (className == null)
0544: throw new FacesException(L.l(
0545: "'{0}' is an unknown component type", name));
0546:
0547: try {
0548: ClassLoader loader = Thread.currentThread()
0549: .getContextClassLoader();
0550:
0551: cl = Class.forName(className, false, loader);
0552:
0553: Config.validate(cl, UIComponent.class);
0554:
0555: _componentClassMap.put(name, cl);
0556:
0557: return cl;
0558: } catch (RuntimeException e) {
0559: throw e;
0560: } catch (Exception e) {
0561: throw new FacesException(e);
0562: }
0563: }
0564: }
0565:
0566: /**
0567: * @Since 1.2
0568: */
0569: public UIComponent createComponent(ValueExpression componentExpr,
0570: FacesContext context, String componentType)
0571: throws FacesException {
0572: if (componentExpr == null || context == null
0573: || componentType == null)
0574: throw new NullPointerException();
0575:
0576: Object value = componentExpr.getValue(context.getELContext());
0577:
0578: if (value instanceof UIComponent)
0579: return (UIComponent) value;
0580:
0581: UIComponent component = createComponent(componentType);
0582:
0583: componentExpr.setValue(context.getELContext(), component);
0584:
0585: return component;
0586: }
0587:
0588: @Deprecated
0589: public UIComponent createComponent(ValueBinding componentBinding,
0590: FacesContext context, String componentType)
0591: throws FacesException {
0592: if (componentBinding == null || context == null
0593: || componentType == null)
0594: throw new NullPointerException();
0595:
0596: return createComponent(new ValueExpressionAdapter(
0597: componentBinding, UIComponent.class), context,
0598: componentType);
0599: }
0600:
0601: public Iterator<String> getComponentTypes() {
0602: return _componentClassNameMap.keySet().iterator();
0603: }
0604:
0605: public void addConverter(String converterId, String converterClass) {
0606: if (converterId == null)
0607: throw new NullPointerException();
0608:
0609: synchronized (_converterIdMap) {
0610: _converterIdNameMap.put(converterId, converterClass);
0611: }
0612: }
0613:
0614: public Converter createConverter(String converterId)
0615: throws FacesException {
0616: if (converterId == null)
0617: throw new NullPointerException();
0618:
0619: Class cl = getConverterIdClass(converterId);
0620:
0621: if (cl == null)
0622: return null;
0623:
0624: try {
0625: return (Converter) cl.newInstance();
0626: } catch (RuntimeException e) {
0627: throw e;
0628: } catch (Exception e) {
0629: throw new FacesException(e);
0630: }
0631: }
0632:
0633: private Class getConverterIdClass(String id) {
0634: synchronized (_converterIdMap) {
0635: Class cl = _converterIdMap.get(id);
0636:
0637: if (cl != null)
0638: return cl;
0639:
0640: String className = _converterIdNameMap.get(id);
0641:
0642: if (className == null)
0643: throw new FacesException(L.l(
0644: "'{0}' is an unknown converter type", id));
0645:
0646: try {
0647: ClassLoader loader = Thread.currentThread()
0648: .getContextClassLoader();
0649:
0650: cl = Class.forName(className, false, loader);
0651:
0652: Config.validate(cl, Converter.class);
0653:
0654: _converterIdMap.put(id, cl);
0655:
0656: return cl;
0657: } catch (RuntimeException e) {
0658: throw e;
0659: } catch (Exception e) {
0660: throw new FacesException(e);
0661: }
0662: }
0663: }
0664:
0665: public Iterator<String> getConverterIds() {
0666: return _converterIdNameMap.keySet().iterator();
0667: }
0668:
0669: public void addConverter(Class type, String converterClass) {
0670: if (type == null)
0671: throw new NullPointerException();
0672:
0673: synchronized (_converterClassMap) {
0674: try {
0675: ClassLoader loader = Thread.currentThread()
0676: .getContextClassLoader();
0677:
0678: Class cl = Class.forName(converterClass, false, loader);
0679:
0680: Config.validate(cl, Converter.class);
0681:
0682: _converterClassMap.put(type, cl);
0683: } catch (RuntimeException e) {
0684: throw e;
0685: } catch (Exception e) {
0686: throw new FacesException(e);
0687: }
0688: }
0689: }
0690:
0691: public Converter createConverter(Class type) throws FacesException {
0692: if (type == null)
0693: throw new NullPointerException();
0694:
0695: Class cl = findConverter(type);
0696:
0697: if (cl == null)
0698: return null;
0699:
0700: try {
0701:
0702: try {
0703: Constructor constructor = cl
0704: .getConstructor(Class.class);
0705:
0706: return (Converter) constructor.newInstance(type);
0707: } catch (NoSuchMethodException ignore) {
0708: }
0709:
0710: return (Converter) cl.newInstance();
0711: } catch (RuntimeException e) {
0712: throw e;
0713: } catch (Exception e) {
0714: throw new FacesException(e);
0715: }
0716: }
0717:
0718: private Class findConverter(Class type) {
0719: if (type == null)
0720: return null;
0721:
0722: Class cl;
0723:
0724: synchronized (_converterClassMap) {
0725: cl = _converterClassMap.get(type);
0726: }
0727:
0728: if (cl != null)
0729: return cl;
0730:
0731: Class[] interfaces = type.getInterfaces();
0732: for (int i = 0; i < interfaces.length; i++) {
0733: cl = findConverter(interfaces[i]);
0734:
0735: if (cl != null)
0736: return cl;
0737: }
0738:
0739: return findConverter(type.getSuperclass());
0740: }
0741:
0742: public Iterator<Class> getConverterTypes() {
0743: return _converterClassMap.keySet().iterator();
0744: }
0745:
0746: @Deprecated
0747: public MethodBinding createMethodBinding(String ref, Class[] param)
0748: throws ReferenceSyntaxException {
0749: ExpressionFactory factory = getExpressionFactory();
0750:
0751: ELResolver elResolver = getELResolver();
0752: FacesContext facesContext = FacesContext.getCurrentInstance();
0753: ELContext elContext = new FacesELContext(facesContext,
0754: elResolver);
0755:
0756: if (param == null)
0757: param = new Class[0];
0758:
0759: if (!ref.startsWith("#{") && !ref.endsWith("}"))
0760: throw new ReferenceSyntaxException(
0761: L
0762: .l(
0763: "'{0}' is an illegal MethodBinding. MethodBindings require #{...} syntax.",
0764: ref));
0765:
0766: try {
0767: MethodExpression expr = factory.createMethodExpression(
0768: elContext, ref, Object.class, param);
0769:
0770: return new MethodBindingAdapter(expr, param);
0771: } catch (ELException e) {
0772: throw new ReferenceSyntaxException(e);
0773: }
0774: }
0775:
0776: public Iterator<Locale> getSupportedLocales() {
0777: if (_locales != null)
0778: return _locales.iterator();
0779: else
0780: return new ArrayList<Locale>().iterator();
0781: }
0782:
0783: public void setSupportedLocales(Collection<Locale> locales) {
0784: _locales = new ArrayList<Locale>(locales);
0785: }
0786:
0787: public void addValidator(String validatorId, String validatorClass) {
0788: if (validatorId == null || validatorClass == null)
0789: throw new NullPointerException();
0790:
0791: _validatorClassMap.put(validatorId, validatorClass);
0792: }
0793:
0794: public Validator createValidator(String validatorId)
0795: throws FacesException {
0796: if (validatorId == null)
0797: throw new NullPointerException();
0798:
0799: try {
0800: String validatorClass = _validatorClassMap.get(validatorId);
0801:
0802: if (validatorClass == null)
0803: throw new FacesException(L.l(
0804: "'{0}' is not a known validator.", validatorId));
0805:
0806: Thread thread = Thread.currentThread();
0807: ClassLoader loader = thread.getContextClassLoader();
0808:
0809: Class cl = Class.forName(validatorClass, false, loader);
0810:
0811: return (Validator) cl.newInstance();
0812: } catch (FacesException e) {
0813: throw e;
0814: } catch (Exception e) {
0815: throw new FacesException(e);
0816: }
0817: }
0818:
0819: public Iterator<String> getValidatorIds() {
0820: return _validatorClassMap.keySet().iterator();
0821: }
0822:
0823: @Override
0824: public ValueBinding createValueBinding(String ref)
0825: throws ReferenceSyntaxException {
0826: ExpressionFactory factory = getExpressionFactory();
0827:
0828: ELResolver elResolver = getELResolver();
0829: FacesContext facesContext = FacesContext.getCurrentInstance();
0830: ELContext elContext = new FacesELContext(facesContext,
0831: getELResolver());
0832:
0833: try {
0834: ValueExpression expr = factory.createValueExpression(
0835: elContext, ref, Object.class);
0836:
0837: ValueBinding binding = new ValueBindingAdapter(expr);
0838:
0839: return binding;
0840: } catch (ELException e) {
0841: throw new ReferenceSyntaxException(e);
0842: }
0843: }
0844:
0845: @Override
0846: public Object evaluateExpressionGet(FacesContext context,
0847: String expression, Class expectedType) {
0848: ExpressionFactory factory = getExpressionFactory();
0849:
0850: ELContext elContext = context.getELContext();
0851:
0852: ValueExpression expr = factory.createValueExpression(elContext,
0853: expression, expectedType);
0854:
0855: return expr.getValue(elContext);
0856: }
0857:
0858: public void initRequest() {
0859: _isInit = true;
0860:
0861: if (_viewHandler == null)
0862: _viewHandler = new JspViewHandler();
0863:
0864: if (_stateManager == null)
0865: _stateManager = new SessionStateManager();
0866: }
0867:
0868: public String toString() {
0869: return "ApplicationImpl[]";
0870: }
0871:
0872: static class PropertyResolverAdapter extends PropertyResolver {
0873: private ELResolver _elResolver;
0874:
0875: PropertyResolverAdapter(ELResolver elResolver) {
0876: _elResolver = elResolver;
0877: }
0878:
0879: public Class getType(Object base, int index)
0880: throws javax.faces.el.PropertyNotFoundException {
0881: if (base == null)
0882: throw new javax.faces.el.PropertyNotFoundException(
0883: "getType() has null base object");
0884:
0885: try {
0886: FacesContext context = FacesContext
0887: .getCurrentInstance();
0888:
0889: return _elResolver.getType(context.getELContext(),
0890: base, index);
0891: } catch (javax.el.PropertyNotFoundException e) {
0892: throw new javax.faces.el.PropertyNotFoundException(e);
0893: }
0894: }
0895:
0896: public Class getType(Object base, Object property)
0897: throws javax.faces.el.PropertyNotFoundException {
0898: if (base == null)
0899: throw new javax.faces.el.PropertyNotFoundException();
0900:
0901: try {
0902: FacesContext context = FacesContext
0903: .getCurrentInstance();
0904:
0905: return _elResolver.getType(context.getELContext(),
0906: base, property);
0907: } catch (javax.el.PropertyNotFoundException e) {
0908: throw new javax.faces.el.PropertyNotFoundException(e);
0909: }
0910: }
0911:
0912: public Object getValue(Object base, int index)
0913: throws javax.faces.el.PropertyNotFoundException {
0914: if (base == null)
0915: throw new javax.faces.el.PropertyNotFoundException(
0916: "getValue() has null base object");
0917: try {
0918: FacesContext context = FacesContext
0919: .getCurrentInstance();
0920:
0921: return _elResolver.getValue(context.getELContext(),
0922: base, index);
0923: } catch (javax.el.PropertyNotFoundException e) {
0924: throw new javax.faces.el.PropertyNotFoundException(e);
0925: }
0926: }
0927:
0928: public Object getValue(Object base, Object property)
0929: throws javax.faces.el.PropertyNotFoundException {
0930: try {
0931: FacesContext context = FacesContext
0932: .getCurrentInstance();
0933:
0934: return _elResolver.getValue(context.getELContext(),
0935: base, property);
0936: } catch (javax.el.PropertyNotFoundException e) {
0937: throw new javax.faces.el.PropertyNotFoundException(e);
0938: }
0939: }
0940:
0941: public boolean isReadOnly(Object base, int index)
0942: throws javax.faces.el.PropertyNotFoundException {
0943: try {
0944: FacesContext context = FacesContext
0945: .getCurrentInstance();
0946:
0947: return _elResolver.isReadOnly(context.getELContext(),
0948: base, index);
0949: } catch (javax.el.PropertyNotFoundException e) {
0950: throw new javax.faces.el.PropertyNotFoundException(e);
0951: }
0952: }
0953:
0954: public boolean isReadOnly(Object base, Object property)
0955: throws javax.faces.el.PropertyNotFoundException {
0956: try {
0957: FacesContext context = FacesContext
0958: .getCurrentInstance();
0959:
0960: return _elResolver.isReadOnly(context.getELContext(),
0961: base, property);
0962: } catch (javax.el.PropertyNotFoundException e) {
0963: throw new javax.faces.el.PropertyNotFoundException(e);
0964: }
0965: }
0966:
0967: public void setValue(Object base, int index, Object value)
0968: throws javax.faces.el.PropertyNotFoundException {
0969: if (base == null)
0970: throw new javax.faces.el.PropertyNotFoundException();
0971:
0972: try {
0973: FacesContext context = FacesContext
0974: .getCurrentInstance();
0975:
0976: _elResolver.setValue(context.getELContext(), base,
0977: index, value);
0978: } catch (javax.el.PropertyNotFoundException e) {
0979: throw new javax.faces.el.PropertyNotFoundException(e);
0980: } catch (javax.el.PropertyNotWritableException e) {
0981: throw new javax.faces.el.PropertyNotFoundException(e);
0982: }
0983: }
0984:
0985: public void setValue(Object base, Object property, Object value)
0986: throws javax.faces.el.PropertyNotFoundException {
0987: try {
0988: FacesContext context = FacesContext
0989: .getCurrentInstance();
0990:
0991: _elResolver.setValue(context.getELContext(), base,
0992: property, value);
0993: } catch (javax.el.PropertyNotFoundException e) {
0994: throw new javax.faces.el.PropertyNotFoundException(e);
0995: } catch (javax.el.PropertyNotWritableException e) {
0996: throw new javax.faces.el.PropertyNotFoundException(e);
0997: }
0998: }
0999: }
1000:
1001: static class VariableResolverAdapter extends VariableResolver {
1002: private ELResolver _elResolver;
1003:
1004: VariableResolverAdapter(ELResolver elResolver) {
1005: _elResolver = elResolver;
1006: }
1007:
1008: public Object resolveVariable(FacesContext context, String value) {
1009: return _elResolver.getValue(context.getELContext(), null,
1010: value);
1011: }
1012: }
1013: }
|