Source Code Cross Referenced for ApplicationImpl.java in  » J2EE » myfaces-core-1.2.0 » org » apache » myfaces » application » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » myfaces core 1.2.0 » org.apache.myfaces.application 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004 The Apache Software Foundation.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package org.apache.myfaces.application;
017:
018:        import java.lang.reflect.Constructor;
019:        import java.util.ArrayList;
020:        import java.util.Collection;
021:        import java.util.Collections;
022:        import java.util.HashMap;
023:        import java.util.Iterator;
024:        import java.util.Locale;
025:        import java.util.Map;
026:        import java.util.MissingResourceException;
027:
028:        import javax.el.CompositeELResolver;
029:        import javax.el.ELContext;
030:        import javax.el.ELContextListener;
031:        import javax.el.ELException;
032:        import javax.el.ELResolver;
033:        import javax.el.ExpressionFactory;
034:        import javax.el.MethodExpression;
035:        import javax.el.ValueExpression;
036:        import javax.faces.FacesException;
037:        import javax.faces.application.Application;
038:        import javax.faces.application.NavigationHandler;
039:        import javax.faces.application.StateManager;
040:        import javax.faces.application.ViewHandler;
041:        import javax.faces.component.UIComponent;
042:        import javax.faces.component.UIViewRoot;
043:        import javax.faces.context.FacesContext;
044:        import javax.faces.convert.Converter;
045:        import javax.faces.el.MethodBinding;
046:        import javax.faces.el.PropertyResolver;
047:        import javax.faces.el.ReferenceSyntaxException;
048:        import javax.faces.el.ValueBinding;
049:        import javax.faces.el.VariableResolver;
050:        import javax.faces.event.ActionListener;
051:        import javax.faces.validator.Validator;
052:
053:        import org.apache.commons.beanutils.BeanUtils;
054:        import org.apache.commons.logging.Log;
055:        import org.apache.commons.logging.LogFactory;
056:        import org.apache.myfaces.application.jsp.JspStateManagerImpl;
057:        import org.apache.myfaces.application.jsp.JspViewHandlerImpl;
058:        import org.apache.myfaces.config.RuntimeConfig;
059:        import org.apache.myfaces.config.impl.digester.elements.Property;
060:        import org.apache.myfaces.config.impl.digester.elements.ResourceBundle;
061:        import org.apache.myfaces.el.PropertyResolverImpl;
062:        import org.apache.myfaces.el.VariableResolverToApplicationELResolverAdapter;
063:        import org.apache.myfaces.el.convert.MethodExpressionToMethodBinding;
064:        import org.apache.myfaces.el.convert.ValueBindingToValueExpression;
065:        import org.apache.myfaces.el.convert.ValueExpressionToValueBinding;
066:        import org.apache.myfaces.el.unified.ELResolverBuilder;
067:        import org.apache.myfaces.el.unified.ResolverBuilderForFaces;
068:        import org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver;
069:        import org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.Scope;
070:        import org.apache.myfaces.shared_impl.util.ClassUtils;
071:
072:        /**
073:         * DOCUMENT ME!
074:         * 
075:         * @author Manfred Geiler (latest modification by $Author: mbr $)
076:         * @author Anton Koinov
077:         * @author Thomas Spiegl
078:         * @author Stan Silvert
079:         * @version $Revision: 524598 $ $Date: 2007-04-01 16:19:55 +0200 (So, 01 Apr 2007) $
080:         */
081:        @SuppressWarnings("deprecation")
082:        public class ApplicationImpl extends Application {
083:            private static final Log log = LogFactory
084:                    .getLog(ApplicationImpl.class);
085:
086:            private final static VariableResolver VARIABLERESOLVER = new VariableResolverToApplicationELResolverAdapter();
087:
088:            private final static PropertyResolver PROPERTYRESOLVER = new PropertyResolverImpl();
089:
090:            // recives the runtime config instance during initializing
091:            private final static ThreadLocal<RuntimeConfig> initializingRuntimeConfig = new ThreadLocal<RuntimeConfig>();
092:
093:            // ~ Instance fields
094:            // ----------------------------------------------------------------------------
095:
096:            private Collection<Locale> _supportedLocales = Collections
097:                    .emptySet();
098:            private Locale _defaultLocale;
099:            private String _messageBundle;
100:
101:            private ViewHandler _viewHandler;
102:            private NavigationHandler _navigationHandler;
103:            private ActionListener _actionListener;
104:            private String _defaultRenderKitId;
105:            private StateManager _stateManager;
106:
107:            private ArrayList<ELContextListener> _elContextListeners;
108:
109:            // components, converters, and validators can be added at runtime--must
110:            // synchronize
111:            private final Map<String, Class> _converterIdToClassMap = Collections
112:                    .synchronizedMap(new HashMap<String, Class>());
113:            private final Map<Class, String> _converterClassNameToClassMap = Collections
114:                    .synchronizedMap(new HashMap<Class, String>());
115:            private final Map<String, org.apache.myfaces.config.impl.digester.elements.Converter> _converterClassNameToConfigurationMap = Collections
116:                    .synchronizedMap(new HashMap<String, org.apache.myfaces.config.impl.digester.elements.Converter>());
117:            private final Map<String, Class> _componentClassMap = Collections
118:                    .synchronizedMap(new HashMap<String, Class>());
119:            private final Map<String, Class> _validatorClassMap = Collections
120:                    .synchronizedMap(new HashMap<String, Class>());
121:
122:            private final RuntimeConfig _runtimeConfig;
123:
124:            private ELResolver elResolver;
125:
126:            private ELResolverBuilder resolverBuilderForFaces;
127:
128:            // ~ Constructors
129:            // -------------------------------------------------------------------------------
130:
131:            public ApplicationImpl() {
132:                this (internalGetRuntimeConfig());
133:            }
134:
135:            private static RuntimeConfig internalGetRuntimeConfig() {
136:                if (initializingRuntimeConfig.get() == null) {
137:                    throw new IllegalStateException(
138:                            "The runtime config instance which is created while initialize myfaces "
139:                                    + "must be set through ApplicationImpl.setInitializingRuntimeConfig");
140:                }
141:                return initializingRuntimeConfig.get();
142:            }
143:
144:            ApplicationImpl(RuntimeConfig runtimeConfig) {
145:                if (runtimeConfig == null) {
146:                    throw new IllegalArgumentException(
147:                            "runtimeConfig must mot be null");
148:                }
149:                // set default implementation in constructor
150:                // pragmatic approach, no syncronizing will be needed in get methods
151:                _viewHandler = new JspViewHandlerImpl();
152:                _navigationHandler = new NavigationHandlerImpl();
153:                _actionListener = new ActionListenerImpl();
154:                _defaultRenderKitId = "HTML_BASIC";
155:                _stateManager = new JspStateManagerImpl();
156:                _elContextListeners = new ArrayList<ELContextListener>();
157:                _runtimeConfig = runtimeConfig;
158:
159:                if (log.isTraceEnabled())
160:                    log.trace("New Application instance created");
161:            }
162:
163:            public static void setInitializingRuntimeConfig(RuntimeConfig config) {
164:                initializingRuntimeConfig.set(config);
165:            }
166:
167:            // ~ Methods
168:            // ------------------------------------------------------------------------------------
169:
170:            @Override
171:            public void addELResolver(ELResolver resolver) {
172:                if (FacesContext.getCurrentInstance() != null) {
173:                    throw new IllegalStateException(
174:                            "It is illegal to add a resolver after the first request is processed");
175:                }
176:                if (resolver != null) {
177:                    _runtimeConfig.addApplicationElResolver(resolver);
178:                }
179:            }
180:
181:            @Override
182:            public ELResolver getELResolver() {
183:                // we don't need synchronization here since it is ok to have multiple instances of the elresolver
184:                if (elResolver == null) {
185:                    elResolver = createFacesResolver();
186:                }
187:                return elResolver;
188:            }
189:
190:            private ELResolver createFacesResolver() {
191:                CompositeELResolver resolver = new FacesCompositeELResolver(
192:                        Scope.Faces);
193:                getResolverBuilderForFaces().build(resolver);
194:                return resolver;
195:            }
196:
197:            protected ELResolverBuilder getResolverBuilderForFaces() {
198:                if (resolverBuilderForFaces == null) {
199:                    resolverBuilderForFaces = new ResolverBuilderForFaces(
200:                            _runtimeConfig);
201:                }
202:                return resolverBuilderForFaces;
203:            }
204:
205:            public void setResolverBuilderForFaces(ELResolverBuilder factory) {
206:                resolverBuilderForFaces = factory;
207:            }
208:
209:            @Override
210:            public java.util.ResourceBundle getResourceBundle(
211:                    FacesContext facesContext, String name)
212:                    throws FacesException, NullPointerException {
213:
214:                checkNull(facesContext, "facesContext");
215:                checkNull(name, "name");
216:
217:                String bundleName = getBundleName(facesContext, name);
218:
219:                if (bundleName == null) {
220:                    return null;
221:                }
222:
223:                Locale locale = Locale.getDefault();
224:
225:                UIViewRoot viewRoot = facesContext.getViewRoot();
226:                if (viewRoot != null && viewRoot.getLocale() != null) {
227:                    locale = viewRoot.getLocale();
228:                }
229:
230:                try {
231:                    return getResourceBundle(bundleName, locale,
232:                            getClassLoader());
233:                } catch (MissingResourceException e) {
234:                    throw new FacesException(
235:                            "Could not load resource bundle for name '" + name
236:                                    + "': " + e.getMessage(), e);
237:                }
238:            }
239:
240:            ClassLoader getClassLoader() {
241:                return Thread.currentThread().getContextClassLoader();
242:            }
243:
244:            String getBundleName(FacesContext facesContext, String name) {
245:                ResourceBundle bundle = getRuntimeConfig(facesContext)
246:                        .getResourceBundle(name);
247:                return bundle != null ? bundle.getBaseName() : null;
248:            }
249:
250:            java.util.ResourceBundle getResourceBundle(String name,
251:                    Locale locale, ClassLoader loader)
252:                    throws MissingResourceException {
253:                return java.util.ResourceBundle.getBundle(name, locale, loader);
254:            }
255:
256:            RuntimeConfig getRuntimeConfig(FacesContext facesContext) {
257:                return RuntimeConfig.getCurrentInstance(facesContext
258:                        .getExternalContext());
259:            }
260:
261:            FacesContext getFaceContext() {
262:                return FacesContext.getCurrentInstance();
263:            }
264:
265:            @Override
266:            public UIComponent createComponent(
267:                    ValueExpression componentExpression,
268:                    FacesContext facesContext, String componentType)
269:                    throws FacesException, NullPointerException {
270:
271:                checkNull(componentExpression, "componentExpression");
272:                checkNull(facesContext, "facesContext");
273:                checkNull(componentType, "componentType");
274:
275:                ELContext elContext = facesContext.getELContext();
276:
277:                try {
278:                    Object retVal = componentExpression.getValue(elContext);
279:
280:                    UIComponent createdComponent;
281:
282:                    if (retVal instanceof  UIComponent) {
283:                        createdComponent = (UIComponent) retVal;
284:                    } else {
285:                        createdComponent = createComponent(componentType);
286:                        componentExpression.setValue(elContext,
287:                                createdComponent);
288:                    }
289:
290:                    return createdComponent;
291:                } catch (FacesException e) {
292:                    throw e;
293:                } catch (Exception e) {
294:                    throw new FacesException(e);
295:                }
296:            }
297:
298:            @Override
299:            public ExpressionFactory getExpressionFactory() {
300:                return _runtimeConfig.getExpressionFactory();
301:            }
302:
303:            @Override
304:            public Object evaluateExpressionGet(FacesContext context,
305:                    String expression, Class expectedType) throws ELException {
306:                ELContext elContext = context.getELContext();
307:                return getExpressionFactory().createValueExpression(elContext,
308:                        expression, expectedType).getValue(elContext);
309:            }
310:
311:            @Override
312:            public void addELContextListener(ELContextListener listener) {
313:
314:                synchronized (_elContextListeners) {
315:                    _elContextListeners.add(listener);
316:                }
317:            }
318:
319:            @Override
320:            public void removeELContextListener(ELContextListener listener) {
321:                synchronized (_elContextListeners) {
322:                    _elContextListeners.remove(listener);
323:                }
324:            }
325:
326:            @Override
327:            public ELContextListener[] getELContextListeners() {
328:                // this gets called on every request, so I can't afford to synchronize
329:                // I just have to trust that toArray() with do the right thing if the
330:                // list is changing (not likely)
331:                return _elContextListeners.toArray(new ELContextListener[0]);
332:            }
333:
334:            @Override
335:            public void setActionListener(ActionListener actionListener) {
336:                checkNull(actionListener, "actionListener");
337:
338:                _actionListener = actionListener;
339:                if (log.isTraceEnabled())
340:                    log.trace("set actionListener = "
341:                            + actionListener.getClass().getName());
342:            }
343:
344:            @Override
345:            public ActionListener getActionListener() {
346:                return _actionListener;
347:            }
348:
349:            @Override
350:            public Iterator<String> getComponentTypes() {
351:                return _componentClassMap.keySet().iterator();
352:            }
353:
354:            @Override
355:            public Iterator<String> getConverterIds() {
356:                return _converterIdToClassMap.keySet().iterator();
357:            }
358:
359:            @Override
360:            public Iterator<Class> getConverterTypes() {
361:                return _converterClassNameToClassMap.keySet().iterator();
362:            }
363:
364:            @Override
365:            public void setDefaultLocale(Locale locale) {
366:                checkNull(locale, "locale");
367:
368:                _defaultLocale = locale;
369:                if (log.isTraceEnabled())
370:                    log.trace("set defaultLocale = " + locale.getCountry()
371:                            + " " + locale.getLanguage());
372:            }
373:
374:            @Override
375:            public Locale getDefaultLocale() {
376:                return _defaultLocale;
377:            }
378:
379:            @Override
380:            public void setMessageBundle(String messageBundle) {
381:                checkNull(messageBundle, "messageBundle");
382:
383:                _messageBundle = messageBundle;
384:                if (log.isTraceEnabled())
385:                    log.trace("set MessageBundle = " + messageBundle);
386:            }
387:
388:            @Override
389:            public String getMessageBundle() {
390:                return _messageBundle;
391:            }
392:
393:            @Override
394:            public void setNavigationHandler(NavigationHandler navigationHandler) {
395:                checkNull(navigationHandler, "navigationHandler");
396:
397:                _navigationHandler = navigationHandler;
398:                if (log.isTraceEnabled())
399:                    log.trace("set NavigationHandler = "
400:                            + navigationHandler.getClass().getName());
401:            }
402:
403:            @Override
404:            public NavigationHandler getNavigationHandler() {
405:                return _navigationHandler;
406:            }
407:
408:            /**
409:             * @deprecated
410:             */
411:            @Deprecated
412:            @Override
413:            public void setPropertyResolver(PropertyResolver propertyResolver) {
414:                checkNull(propertyResolver, "propertyResolver");
415:
416:                if (getFaceContext() != null) {
417:                    throw new IllegalStateException(
418:                            "propertyResolver must be defined before request processing");
419:                }
420:
421:                _runtimeConfig.setPropertyResolver(propertyResolver);
422:
423:                if (log.isTraceEnabled())
424:                    log.trace("set PropertyResolver = "
425:                            + propertyResolver.getClass().getName());
426:            }
427:
428:            /**
429:             * @deprecated
430:             */
431:            @Deprecated
432:            @Override
433:            public PropertyResolver getPropertyResolver() {
434:                return PROPERTYRESOLVER;
435:            }
436:
437:            @Override
438:            public void setSupportedLocales(Collection<Locale> locales) {
439:                checkNull(locales, "locales");
440:
441:                _supportedLocales = locales;
442:                if (log.isTraceEnabled())
443:                    log.trace("set SupportedLocales");
444:            }
445:
446:            @Override
447:            public Iterator<Locale> getSupportedLocales() {
448:                return _supportedLocales.iterator();
449:            }
450:
451:            @Override
452:            public Iterator<String> getValidatorIds() {
453:                return _validatorClassMap.keySet().iterator();
454:            }
455:
456:            /**
457:             * @deprecated
458:             */
459:            @Deprecated
460:            @Override
461:            public void setVariableResolver(VariableResolver variableResolver) {
462:                checkNull(variableResolver, "variableResolver");
463:
464:                if (getFaceContext() != null) {
465:                    throw new IllegalStateException(
466:                            "variableResolver must be defined before request processing");
467:                }
468:
469:                _runtimeConfig.setVariableResolver(variableResolver);
470:
471:                if (log.isTraceEnabled())
472:                    log.trace("set VariableResolver = "
473:                            + variableResolver.getClass().getName());
474:            }
475:
476:            /**
477:             * @deprecated
478:             */
479:            @Deprecated
480:            @Override
481:            public VariableResolver getVariableResolver() {
482:                return VARIABLERESOLVER;
483:            }
484:
485:            @Override
486:            public void setViewHandler(ViewHandler viewHandler) {
487:                checkNull(viewHandler, "viewHandler");
488:
489:                _viewHandler = viewHandler;
490:                if (log.isTraceEnabled())
491:                    log.trace("set ViewHandler = "
492:                            + viewHandler.getClass().getName());
493:            }
494:
495:            @Override
496:            public ViewHandler getViewHandler() {
497:                return _viewHandler;
498:            }
499:
500:            @Override
501:            public void addComponent(String componentType,
502:                    String componentClassName) {
503:                checkNull(componentType, "componentType");
504:                checkEmpty(componentType, "componentType");
505:                checkNull(componentClassName, "componentClassName");
506:                checkEmpty(componentClassName, "componentClassName");
507:
508:                try {
509:                    _componentClassMap.put(componentType, ClassUtils
510:                            .simpleClassForName(componentClassName));
511:                    if (log.isTraceEnabled())
512:                        log.trace("add Component class = " + componentClassName
513:                                + " for type = " + componentType);
514:                } catch (Exception e) {
515:                    log.error("Component class " + componentClassName
516:                            + " not found", e);
517:                }
518:            }
519:
520:            @Override
521:            public void addConverter(String converterId, String converterClass) {
522:                checkNull(converterId, "converterId");
523:                checkEmpty(converterId, "converterId");
524:                checkNull(converterClass, "converterClass");
525:                checkEmpty(converterClass, "converterClass");
526:
527:                try {
528:                    _converterIdToClassMap.put(converterId, ClassUtils
529:                            .simpleClassForName(converterClass));
530:                    if (log.isTraceEnabled())
531:                        log.trace("add Converter id = " + converterId
532:                                + " converterClass = " + converterClass);
533:                } catch (Exception e) {
534:                    log.error("Converter class " + converterClass
535:                            + " not found", e);
536:                }
537:            }
538:
539:            @Override
540:            public void addConverter(Class targetClass, String converterClass) {
541:                checkNull(targetClass, "targetClass");
542:                checkNull(converterClass, "converterClass");
543:                checkEmpty(converterClass, "converterClass");
544:
545:                try {
546:                    _converterClassNameToClassMap.put(targetClass,
547:                            converterClass);
548:                    if (log.isTraceEnabled())
549:                        log.trace("add Converter for class = " + targetClass
550:                                + " converterClass = " + converterClass);
551:                } catch (Exception e) {
552:                    log.error("Converter class " + converterClass
553:                            + " not found", e);
554:                }
555:            }
556:
557:            public void addConverterConfiguration(
558:                    String converterClassName,
559:                    org.apache.myfaces.config.impl.digester.elements.Converter configuration) {
560:                checkNull(converterClassName, "converterClassName");
561:                checkEmpty(converterClassName, "converterClassName");
562:                checkNull(configuration, "configuration");
563:
564:                _converterClassNameToConfigurationMap.put(converterClassName,
565:                        configuration);
566:            }
567:
568:            @Override
569:            public void addValidator(String validatorId, String validatorClass) {
570:                checkNull(validatorId, "validatorId");
571:                checkEmpty(validatorId, "validatorId");
572:                checkNull(validatorClass, "validatorClass");
573:                checkEmpty(validatorClass, "validatorClass");
574:
575:                try {
576:                    _validatorClassMap.put(validatorId, ClassUtils
577:                            .simpleClassForName(validatorClass));
578:                    if (log.isTraceEnabled())
579:                        log.trace("add Validator id = " + validatorId
580:                                + " class = " + validatorClass);
581:                } catch (Exception e) {
582:                    log.error("Validator class " + validatorClass
583:                            + " not found", e);
584:                }
585:            }
586:
587:            @Override
588:            public UIComponent createComponent(String componentType)
589:                    throws FacesException {
590:                checkNull(componentType, "componentType");
591:                checkEmpty(componentType, "componentType");
592:
593:                Class componentClass;
594:                synchronized (_componentClassMap) {
595:                    componentClass = _componentClassMap.get(componentType);
596:                }
597:                if (componentClass == null) {
598:                    log.error("Undefined component type " + componentType);
599:                    throw new FacesException("Undefined component type "
600:                            + componentType);
601:                }
602:
603:                try {
604:                    return (UIComponent) componentClass.newInstance();
605:                } catch (Exception e) {
606:                    log.error(
607:                            "Could not instantiate component componentType = "
608:                                    + componentType, e);
609:                    throw new FacesException(
610:                            "Could not instantiate component componentType = "
611:                                    + componentType, e);
612:                }
613:            }
614:
615:            /**
616:             * @deprecated Use createComponent(ValueExpression, FacesContext, String) instead.
617:             */
618:            @Deprecated
619:            @Override
620:            public UIComponent createComponent(ValueBinding valueBinding,
621:                    FacesContext facesContext, String componentType)
622:                    throws FacesException {
623:
624:                checkNull(valueBinding, "valueBinding");
625:                checkNull(facesContext, "facesContext");
626:                checkNull(componentType, "componentType");
627:                checkEmpty(componentType, "componentType");
628:
629:                ValueExpression valExpression = new ValueBindingToValueExpression(
630:                        valueBinding);
631:
632:                return createComponent(valExpression, facesContext,
633:                        componentType);
634:            }
635:
636:            @Override
637:            public Converter createConverter(String converterId) {
638:                checkNull(converterId, "converterId");
639:                checkEmpty(converterId, "converterId");
640:
641:                Class converterClass = _converterIdToClassMap.get(converterId);
642:
643:                try {
644:                    Converter converter = (Converter) converterClass
645:                            .newInstance();
646:
647:                    setConverterProperties(converterClass, converter);
648:
649:                    return converter;
650:                } catch (Exception e) {
651:                    log.error("Could not instantiate converter "
652:                            + converterClass, e);
653:                    throw new FacesException(
654:                            "Could not instantiate converter: "
655:                                    + converterClass, e);
656:                }
657:            }
658:
659:            @Override
660:            public Converter createConverter(Class targetClass) {
661:                checkNull(targetClass, "targetClass");
662:
663:                return internalCreateConverter(targetClass);
664:            }
665:
666:            private Converter internalCreateConverter(Class targetClass) {
667:                // Locate a Converter registered for the target class itself.
668:                String converterClassName = _converterClassNameToClassMap
669:                        .get(targetClass);
670:
671:                // Locate a Converter registered for interfaces that are
672:                // implemented by the target class (directly or indirectly).
673:                if (converterClassName == null) {
674:                    Class interfaces[] = targetClass.getInterfaces();
675:                    if (interfaces != null) {
676:                        for (int i = 0, len = interfaces.length; i < len; i++) {
677:                            // search all superinterfaces for a matching converter,
678:                            // create it
679:                            Converter converter = internalCreateConverter(interfaces[i]);
680:                            if (converter != null) {
681:                                return converter;
682:                            }
683:                        }
684:                    }
685:                }
686:
687:                if (converterClassName != null) {
688:                    try {
689:                        Class converterClass = ClassUtils
690:                                .simpleClassForName(converterClassName);
691:
692:                        Converter converter = null;
693:                        try {
694:                            // look for a constructor that takes a single Class object
695:                            // See JSF 1.2 javadoc for Converter
696:                            Constructor constructor = converterClass
697:                                    .getConstructor(new Class[] { Class.class });
698:                            converter = (Converter) constructor
699:                                    .newInstance(new Object[] { targetClass });
700:                        } catch (Exception e) {
701:                            // if there is no matching constructor use no-arg
702:                            // constructor
703:                            converter = (Converter) converterClass
704:                                    .newInstance();
705:                        }
706:
707:                        setConverterProperties(converterClass, converter);
708:
709:                        return converter;
710:                    } catch (Exception e) {
711:                        log.error("Could not instantiate converter "
712:                                + converterClassName, e);
713:                        throw new FacesException(
714:                                "Could not instantiate converter: "
715:                                        + converterClassName, e);
716:                    }
717:                }
718:
719:                // locate converter for primitive types
720:                if (targetClass == Long.TYPE) {
721:                    return internalCreateConverter(Long.class);
722:                } else if (targetClass == Boolean.TYPE) {
723:                    return internalCreateConverter(Boolean.class);
724:                } else if (targetClass == Double.TYPE) {
725:                    return internalCreateConverter(Double.class);
726:                } else if (targetClass == Byte.TYPE) {
727:                    return internalCreateConverter(Byte.class);
728:                } else if (targetClass == Short.TYPE) {
729:                    return internalCreateConverter(Short.class);
730:                } else if (targetClass == Integer.TYPE) {
731:                    return internalCreateConverter(Integer.class);
732:                } else if (targetClass == Float.TYPE) {
733:                    return internalCreateConverter(Float.class);
734:                } else if (targetClass == Character.TYPE) {
735:                    return internalCreateConverter(Character.class);
736:                }
737:
738:                // Locate a Converter registered for the superclass (if any) of the
739:                // target class,
740:                // recursively working up the inheritance hierarchy.
741:                Class super Clazz = targetClass.getSuperclass();
742:
743:                return super Clazz != null ? internalCreateConverter(super Clazz)
744:                        : null;
745:
746:            }
747:
748:            private void setConverterProperties(Class converterClass,
749:                    Converter converter) {
750:                org.apache.myfaces.config.impl.digester.elements.Converter converterConfig = _converterClassNameToConfigurationMap
751:                        .get(converterClass.getName());
752:
753:                if (converterConfig != null) {
754:
755:                    Iterator it = converterConfig.getProperties();
756:
757:                    while (it.hasNext()) {
758:                        Property property = (Property) it.next();
759:
760:                        try {
761:                            BeanUtils.setProperty(converter, property
762:                                    .getPropertyName(), property
763:                                    .getDefaultValue());
764:                        } catch (Throwable th) {
765:                            log.error("Initializing converter : "
766:                                    + converterClass.getName()
767:                                    + " with property : "
768:                                    + property.getPropertyName()
769:                                    + " and value : "
770:                                    + property.getDefaultValue() + " failed.");
771:                        }
772:                    }
773:                }
774:            }
775:
776:            // Note: this method used to be synchronized in the JSF 1.1 version. Why?
777:            /**
778:             * @deprecated
779:             */
780:            @Deprecated
781:            @Override
782:            public MethodBinding createMethodBinding(String reference,
783:                    Class[] params) throws ReferenceSyntaxException {
784:                checkNull(reference, "reference");
785:                checkEmpty(reference, "reference");
786:
787:                // TODO: this check should be performed by the expression factory. It is a requirement of the TCK
788:                if (!(reference.startsWith("#{") && reference.endsWith("}"))) {
789:                    throw new ReferenceSyntaxException(
790:                            "Invalid method reference: '" + reference + "'");
791:                }
792:
793:                if (params == null)
794:                    params = new Class[0];
795:
796:                MethodExpression methodExpression;
797:
798:                try {
799:                    methodExpression = getExpressionFactory()
800:                            .createMethodExpression(threadELContext(),
801:                                    reference, Object.class, params);
802:                } catch (ELException e) {
803:                    throw new ReferenceSyntaxException(e);
804:                }
805:
806:                return new MethodExpressionToMethodBinding(methodExpression);
807:            }
808:
809:            @Override
810:            public Validator createValidator(String validatorId)
811:                    throws FacesException {
812:                checkNull(validatorId, "validatorId");
813:                checkEmpty(validatorId, "validatorId");
814:
815:                Class validatorClass = _validatorClassMap.get(validatorId);
816:                if (validatorClass == null) {
817:                    String message = "Unknown validator id '" + validatorId
818:                            + "'.";
819:                    log.error(message);
820:                    throw new FacesException(message);
821:                }
822:
823:                try {
824:                    return (Validator) validatorClass.newInstance();
825:                } catch (Exception e) {
826:                    log.error("Could not instantiate validator "
827:                            + validatorClass, e);
828:                    throw new FacesException(
829:                            "Could not instantiate validator: "
830:                                    + validatorClass, e);
831:                }
832:            }
833:
834:            /**
835:             * @deprecated
836:             */
837:            @Override
838:            public ValueBinding createValueBinding(String reference)
839:                    throws ReferenceSyntaxException {
840:                checkNull(reference, "reference");
841:                checkEmpty(reference, "reference");
842:
843:                ValueExpression valueExpression;
844:
845:                try {
846:                    valueExpression = getExpressionFactory()
847:                            .createValueExpression(threadELContext(),
848:                                    reference, Object.class);
849:                } catch (ELException e) {
850:                    throw new ReferenceSyntaxException(e);
851:                }
852:
853:                return new ValueExpressionToValueBinding(valueExpression);
854:            }
855:
856:            // gets the elContext from the current FacesContext()
857:            private ELContext threadELContext() {
858:                return getFaceContext().getELContext();
859:            }
860:
861:            @Override
862:            public String getDefaultRenderKitId() {
863:                return _defaultRenderKitId;
864:            }
865:
866:            @Override
867:            public void setDefaultRenderKitId(String defaultRenderKitId) {
868:                _defaultRenderKitId = defaultRenderKitId;
869:            }
870:
871:            @Override
872:            public StateManager getStateManager() {
873:                return _stateManager;
874:            }
875:
876:            @Override
877:            public void setStateManager(StateManager stateManager) {
878:                _stateManager = stateManager;
879:            }
880:
881:            private void checkNull(Object param, String paramName) {
882:                if (param == null) {
883:                    throw new NullPointerException(paramName
884:                            + " can not be null.");
885:                }
886:            }
887:
888:            private void checkEmpty(String param, String paramName) {
889:                if (param.length() == 0) {
890:                    throw new NullPointerException("String " + paramName
891:                            + " can not be empty.");
892:                }
893:            }
894:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.