KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > application > ApplicationImpl


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.application;
17
18 import org.apache.myfaces.application.jsp.JspStateManagerImpl;
19 import org.apache.myfaces.application.jsp.JspViewHandlerImpl;
20 import org.apache.myfaces.el.MethodBindingImpl;
21 import org.apache.myfaces.el.PropertyResolverImpl;
22 import org.apache.myfaces.el.ValueBindingImpl;
23 import org.apache.myfaces.el.VariableResolverImpl;
24 import org.apache.myfaces.util.BiLevelCacheMap;
25 import org.apache.myfaces.util.ClassUtils;
26 import org.apache.myfaces.config.impl.digester.elements.Property;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.commons.beanutils.BeanUtils;
31
32 import javax.faces.FacesException;
33 import javax.faces.application.Application;
34 import javax.faces.application.NavigationHandler;
35 import javax.faces.application.StateManager;
36 import javax.faces.application.ViewHandler;
37 import javax.faces.component.UIComponent;
38 import javax.faces.context.FacesContext;
39 import javax.faces.convert.Converter;
40 import javax.faces.el.*;
41 import javax.faces.event.ActionListener;
42 import javax.faces.validator.Validator;
43 import java.util.*;
44
45
46 /**
47  * DOCUMENT ME!
48  * @author Manfred Geiler (latest modification by $Author: matzew $)
49  * @author Anton Koinov
50  * @author Thomas Spiegl
51  * @version $Revision: 1.37 $ $Date: 2005/03/10 10:26:00 $
52  * $Log: ApplicationImpl.java,v $
53  * Revision 1.37 2005/03/10 10:26:00 matzew
54  * Patch for the ApplicationImpl form Mike Traum (MYFACES-122).
55  * Also changed behaivor of "createConverter(java.lang.String converterId)"
56  *
57  * Revision 1.36 2005/03/04 00:28:45 mmarinschek
58  * Changes in configuration due to missing Attribute/Property classes for the converter; not building in the functionality yet except for part of the converter properties
59  *
60  * Revision 1.35 2004/12/03 08:46:09 manolito
61  * MYFACES-45 / ApplicationImpl does not correctly traverse a Class' hierarchy to create a Converter
62  *
63  * Revision 1.34 2004/10/13 11:50:59 matze
64  * renamed packages to org.apache
65  *
66  * Revision 1.33 2004/09/08 07:45:05 mwessendorf
67  * set DefaultRenderKitId = "HTML_BASIC"
68  *
69  * Revision 1.32 2004/08/10 10:57:37 manolito
70  * fixed StackOverflow in ClassUtils and cleaned up ClassUtils methods
71  *
72  * Revision 1.31 2004/07/01 22:05:14 mwessendorf
73  * ASF switch
74  *
75  * Revision 1.30 2004/06/14 12:55:23 manolito
76  * Added missing CVS Log comment
77  *
78  */

79 public class ApplicationImpl
80     extends Application
81 {
82     private static final Log log = LogFactory.getLog(ApplicationImpl.class);
83
84     //~ Instance fields ----------------------------------------------------------------------------
85

86     private final Map _valueBindingCache =
87         new BiLevelCacheMap(90)
88         {
89             protected Object JavaDoc newInstance(Object JavaDoc key)
90             {
91                 return new ValueBindingImpl(ApplicationImpl.this, (String JavaDoc) key);
92             }
93         };
94
95     private Collection _supportedLocales = Collections.EMPTY_SET;
96     private Locale _defaultLocale = Locale.getDefault();
97     private String JavaDoc _messageBundle;
98
99     private ViewHandler _viewHandler;
100     private NavigationHandler _navigationHandler;
101     private VariableResolver _variableResolver;
102     private PropertyResolver _propertyResolver;
103     private ActionListener _actionListener;
104     private String JavaDoc _defaultRenderKitId;
105     private StateManager _stateManager;
106
107     // components, converters, and validators can be added at runtime--must synchronize
108
private final Map _converterIdToClassMap = Collections.synchronizedMap(new HashMap());
109     private final Map _converterClassToClassMap = Collections.synchronizedMap(new HashMap());
110     private final Map _converterClassNameToConfigurationMap = Collections.synchronizedMap(new HashMap());
111     private final Map _componentClassMap = Collections.synchronizedMap(new HashMap());
112     private final Map _validatorClassMap = Collections.synchronizedMap(new HashMap());
113
114
115     //~ Constructors -------------------------------------------------------------------------------
116

117     public ApplicationImpl()
118     {
119         // set default implementation in constructor
120
// pragmatic approach, no syncronizing will be needed in get methods
121
_viewHandler = new JspViewHandlerImpl();
122         _navigationHandler = new NavigationHandlerImpl();
123         _variableResolver = new VariableResolverImpl();
124         _propertyResolver = new PropertyResolverImpl();
125         _actionListener = new ActionListenerImpl();
126         _defaultRenderKitId = "HTML_BASIC";
127         _stateManager = new JspStateManagerImpl();
128         if (log.isTraceEnabled()) log.trace("New Application instance created");
129     }
130
131     //~ Methods ------------------------------------------------------------------------------------
132

133     public void setActionListener(ActionListener actionListener)
134     {
135         if (actionListener == null)
136         {
137             log.error("setting actionListener to null is not allowed");
138             throw new NullPointerException JavaDoc("setting actionListener to null is not allowed");
139         }
140         _actionListener = actionListener;
141         if (log.isTraceEnabled()) log.trace("set actionListener = " + actionListener.getClass().getName());
142     }
143
144     public ActionListener getActionListener()
145     {
146         return _actionListener;
147     }
148
149     public Iterator getComponentTypes()
150     {
151         return _componentClassMap.keySet().iterator();
152     }
153
154     public Iterator getConverterIds()
155     {
156         return _converterIdToClassMap.keySet().iterator();
157     }
158
159     public Iterator getConverterTypes()
160     {
161         return _converterClassToClassMap.keySet().iterator();
162     }
163
164     public void setDefaultLocale(Locale locale)
165     {
166         if (locale == null)
167         {
168             log.error("setting locale to null is not allowed");
169             throw new NullPointerException JavaDoc("setting locale to null is not allowed");
170         }
171         _defaultLocale = locale;
172         if (log.isTraceEnabled()) log.trace("set defaultLocale = " + locale.getCountry() + " " + locale.getLanguage());
173     }
174
175     public Locale getDefaultLocale()
176     {
177         return _defaultLocale;
178     }
179
180     public void setMessageBundle(String JavaDoc messageBundle)
181     {
182         if (messageBundle == null)
183         {
184             log.error("setting messageBundle to null is not allowed");
185             throw new NullPointerException JavaDoc("setting messageBundle to null is not allowed");
186         }
187         _messageBundle = messageBundle;
188         if (log.isTraceEnabled()) log.trace("set MessageBundle = " + messageBundle);
189     }
190
191     public String JavaDoc getMessageBundle()
192     {
193         return _messageBundle;
194     }
195
196     public void setNavigationHandler(NavigationHandler navigationHandler)
197     {
198         if (navigationHandler == null)
199         {
200             log.error("setting navigationHandler to null is not allowed");
201             throw new NullPointerException JavaDoc("setting navigationHandler to null is not allowed");
202         }
203         _navigationHandler = navigationHandler;
204         if (log.isTraceEnabled()) log.trace("set NavigationHandler = " + navigationHandler.getClass().getName());
205     }
206
207     public NavigationHandler getNavigationHandler()
208     {
209         return _navigationHandler;
210     }
211
212     public void setPropertyResolver(PropertyResolver propertyResolver)
213     {
214         if (propertyResolver == null)
215         {
216             log.error("setting propertyResolver to null is not allowed");
217             throw new NullPointerException JavaDoc("setting propertyResolver to null is not allowed");
218         }
219         _propertyResolver = propertyResolver;
220         if (log.isTraceEnabled()) log.trace("set PropertyResolver = " + propertyResolver.getClass().getName());
221    }
222
223     public PropertyResolver getPropertyResolver()
224     {
225         return _propertyResolver;
226     }
227
228     public void setSupportedLocales(Collection locales)
229     {
230         if (locales == null)
231         {
232             log.error("setting supportedLocales to null is not allowed");
233             throw new NullPointerException JavaDoc("setting supportedLocales to null is not allowed");
234         }
235         _supportedLocales = locales;
236         if (log.isTraceEnabled()) log.trace("set SupportedLocales");
237     }
238
239     public Iterator getSupportedLocales()
240     {
241         return _supportedLocales.iterator();
242     }
243
244     public Iterator getValidatorIds()
245     {
246         return _validatorClassMap.keySet().iterator();
247     }
248
249     public void setVariableResolver(VariableResolver variableResolver)
250     {
251         if (variableResolver == null)
252         {
253             log.error("setting variableResolver to null is not allowed");
254             throw new NullPointerException JavaDoc("setting variableResolver to null is not allowed");
255         }
256         _variableResolver = variableResolver;
257         if (log.isTraceEnabled()) log.trace("set VariableResolver = " + variableResolver.getClass().getName());
258     }
259
260     public VariableResolver getVariableResolver()
261     {
262         return _variableResolver;
263     }
264
265     public void setViewHandler(ViewHandler viewHandler)
266     {
267         if (viewHandler == null)
268         {
269             log.error("setting viewHandler to null is not allowed");
270             throw new NullPointerException JavaDoc("setting viewHandler to null is not allowed");
271         }
272         _viewHandler = viewHandler;
273         if (log.isTraceEnabled()) log.trace("set ViewHandler = " + viewHandler.getClass().getName());
274     }
275
276     public ViewHandler getViewHandler()
277     {
278         return _viewHandler;
279     }
280
281     public void addComponent(String JavaDoc componentType, String JavaDoc componentClassName)
282     {
283         if ((componentType == null) || (componentType.length() == 0))
284         {
285             log.error("addComponent: componentType = null is not allowed");
286             throw new NullPointerException JavaDoc("addComponent: componentType = null ist not allowed");
287         }
288         if ((componentClassName == null) || (componentClassName.length() == 0))
289         {
290             log.error("addComponent: component = null is not allowed");
291             throw new NullPointerException JavaDoc("addComponent: component = null is not allowed");
292         }
293         
294         try
295         {
296             _componentClassMap.put(componentType, ClassUtils.simpleClassForName(componentClassName));
297             if (log.isTraceEnabled()) log.trace("add Component class = " + componentClassName +
298                                                 " for type = " + componentType);
299         }
300         catch (Exception JavaDoc e)
301         {
302             log.error("Component class " + componentClassName + " not found", e);
303         }
304     }
305
306     public void addConverter(String JavaDoc converterId, String JavaDoc converterClass)
307     {
308         if ((converterId == null) || (converterId.length() == 0))
309         {
310             log.error("addConverter: converterId = null is not allowed");
311             throw new NullPointerException JavaDoc("addConverter: converterId = null ist not allowed");
312         }
313         if ((converterClass == null) || (converterClass.length() == 0))
314         {
315             log.error("addConverter: converterClass = null is not allowed");
316             throw new NullPointerException JavaDoc("addConverter: converterClass = null ist not allowed");
317         }
318
319         
320         try
321         {
322             _converterIdToClassMap.put(converterId, ClassUtils.simpleClassForName(converterClass));
323             if (log.isTraceEnabled()) log.trace("add Converter id = " + converterId +
324                     " converterClass = " + converterClass);
325            }
326         catch (Exception JavaDoc e)
327         {
328             log.error("Converter class " + converterClass + " not found", e);
329         }
330     }
331
332     public void addConverter(Class JavaDoc targetClass, String JavaDoc converterClass)
333     {
334         if ((targetClass == null))
335         {
336             log.error("addConverter: targetClass = null is not allowed");
337             throw new NullPointerException JavaDoc("addConverter: targetClass = null ist not allowed");
338         }
339         if ((converterClass == null) || (converterClass.length() == 0))
340         {
341             log.error("addConverter: converterClass = null is not allowed");
342             throw new NullPointerException JavaDoc("addConverter: converterClass = null ist not allowed");
343         }
344         
345         try
346         {
347             _converterClassToClassMap.put(targetClass, ClassUtils.simpleClassForName(converterClass));
348             if (log.isTraceEnabled()) log.trace("add Converter for class = " + targetClass +
349                     " converterClass = " + converterClass);
350         }
351         catch (Exception JavaDoc e)
352         {
353             log.error("Converter class " + converterClass + " not found", e);
354         }
355     }
356
357     public void addConverterConfiguration(String JavaDoc converterClassName,
358                                           org.apache.myfaces.config.impl.digester.elements.Converter configuration)
359     {
360         if ((converterClassName == null) || (converterClassName.length() == 0))
361         {
362             log.error("addConverterConfiguration: converterClassName = null is not allowed");
363             throw new NullPointerException JavaDoc("addConverterConfiguration: converterClassName = null ist not allowed");
364         }
365         if ((configuration == null))
366         {
367             log.error("addConverterConfiguration: configuration = null is not allowed");
368             throw new NullPointerException JavaDoc("addConverterConfiguration: configuration = null ist not allowed");
369         }
370
371         _converterClassNameToConfigurationMap.put(converterClassName, configuration);
372     }
373
374     public void addValidator(String JavaDoc validatorId, String JavaDoc validatorClass)
375     {
376         if ((validatorId == null) || (validatorId.length() == 0))
377         {
378             log.error("addValidator: validatorId = null is not allowed");
379             throw new NullPointerException JavaDoc("addValidator: validatorId = null ist not allowed");
380         }
381         if ((validatorClass == null) || (validatorClass.length() == 0))
382         {
383             log.error("addValidator: validatorClass = null is not allowed");
384             throw new NullPointerException JavaDoc("addValidator: validatorClass = null ist not allowed");
385         }
386         
387         try
388         {
389             _validatorClassMap.put(validatorId, ClassUtils.simpleClassForName(validatorClass));
390             if (log.isTraceEnabled()) log.trace("add Validator id = " + validatorId +
391                                             " class = " + validatorClass);
392         }
393         catch (Exception JavaDoc e)
394         {
395             log.error("Validator class " + validatorClass + " not found", e);
396         }
397     }
398
399     public UIComponent createComponent(String JavaDoc componentType)
400         throws FacesException
401     {
402         if ((componentType == null) || (componentType.length() == 0))
403         {
404             log.error("createComponent: componentType = null is not allowed");
405             throw new NullPointerException JavaDoc("createComponent: componentType = null is not allowed");
406         }
407
408         Class JavaDoc componentClass;
409         synchronized (_componentClassMap)
410         {
411             componentClass = (Class JavaDoc) _componentClassMap.get(componentType);
412         }
413         if (componentClass == null)
414         {
415             log.error("Undefined component type " + componentType);
416             throw new FacesException("Undefined component type " + componentType);
417         }
418
419         try
420         {
421             return (UIComponent) componentClass.newInstance();
422         }
423         catch (Exception JavaDoc e)
424         {
425             log.error("Could not instantiate component componentType = " + componentType, e);
426             throw new FacesException("Could not instantiate component componentType = " + componentType, e);
427         }
428     }
429
430     public UIComponent createComponent(ValueBinding valueBinding,
431                                        FacesContext facesContext,
432                                        String JavaDoc componentType)
433         throws FacesException
434     {
435         if ((valueBinding == null))
436         {
437             log.error("createComponent: valueBinding = null is not allowed");
438             throw new NullPointerException JavaDoc("createComponent: valueBinding = null ist not allowed");
439         }
440         if ((facesContext == null))
441         {
442             log.error("createComponent: facesContext = null is not allowed");
443             throw new NullPointerException JavaDoc("createComponent: facesContext = null ist not allowed");
444         }
445         if ((componentType == null) || (componentType.length() == 0))
446         {
447             log.error("createComponent: componentType = null is not allowed");
448             throw new NullPointerException JavaDoc("createComponent: componentType = null ist not allowed");
449         }
450
451         Object JavaDoc obj = valueBinding.getValue(facesContext);
452         if (obj instanceof UIComponent)
453         {
454             return (UIComponent) obj;
455         }
456         else
457         {
458             UIComponent component = createComponent(componentType);
459             valueBinding.setValue(facesContext, component);
460             return component;
461         }
462     }
463
464     public Converter createConverter(String JavaDoc converterId)
465     {
466         if ((converterId == null) || (converterId.length() == 0))
467         {
468             log.error("createConverter: converterId = null is not allowed");
469             throw new NullPointerException JavaDoc("createConverter: converterId = null ist not allowed");
470         }
471
472         Class JavaDoc converterClass = (Class JavaDoc) _converterIdToClassMap.get(converterId);
473
474         try
475         {
476             return (Converter) converterClass.newInstance();
477         }
478         catch (Exception JavaDoc e)
479         {
480             log.error("Could not instantiate converter " + converterClass, e);
481             throw new FacesException("Could not instantiate converter: " + converterClass, e);
482         }
483     }
484
485
486     public Converter createConverter(Class JavaDoc targetClass)
487     {
488         if (targetClass == null)
489         {
490             log.error("createConverter: targetClass = null is not allowed");
491             throw new NullPointerException JavaDoc("createConverter: targetClass = null ist not allowed");
492         }
493
494         Converter converter = internalCreateConverter(targetClass);
495
496         return converter;
497     }
498
499
500     private Converter internalCreateConverter(Class JavaDoc targetClass)
501     {
502         // Locate a Converter registered for the target class itself.
503
Class JavaDoc converterClass = (Class JavaDoc)_converterClassToClassMap.get(targetClass);
504
505         //Locate a Converter registered for interfaces that are
506
// implemented by the target class (directly or indirectly).
507
if (converterClass == null)
508         {
509             Class JavaDoc interfaces[] = targetClass.getInterfaces();
510             if (interfaces != null)
511             {
512                 for (int i = 0, len = interfaces.length; i < len; i++)
513                 {
514                     // search all superinterfaces for a matching converter, create it
515
Converter converter = internalCreateConverter(interfaces[i]);
516                     if (converter != null)
517                     {
518                         return converter;
519                     }
520                 }
521             }
522         }
523
524         if (converterClass != null)
525         {
526             try
527             {
528                 Converter converter = (Converter) converterClass.newInstance();
529
530                 org.apache.myfaces.config.impl.digester.elements.Converter converterConfig =
531                         (org.apache.myfaces.config.impl.digester.elements.Converter)
532                             _converterClassNameToConfigurationMap.get(converterClass.getName());
533
534                 Iterator it = converterConfig.getProperties();
535
536                 while (it.hasNext())
537                 {
538                     Property property = (Property) it.next();
539
540                     try
541                     {
542                         BeanUtils.setProperty(converter,property.getPropertyName(),property.getDefaultValue());
543                     }
544                     catch(Throwable JavaDoc th)
545                     {
546                         log.error("Initializing converter : "+converterClass.getName()+" with property : "+
547                                 property.getPropertyName()+" and value : "+property.getDefaultValue()+" failed.");
548                     }
549                 }
550
551                 return converter;
552             }
553             catch (Exception JavaDoc e)
554             {
555                 log.error("Could not instantiate converter " + converterClass, e);
556                 throw new FacesException("Could not instantiate converter: " + converterClass, e);
557             }
558         }
559
560         // locate converter for primitive types
561
if (targetClass == Long.TYPE)
562         {
563             return internalCreateConverter(Long JavaDoc.class);
564         } else if (targetClass == Boolean.TYPE)
565         {
566             return internalCreateConverter(Boolean JavaDoc.class);
567         } else if (targetClass == Double.TYPE)
568         {
569             return internalCreateConverter(Double JavaDoc.class);
570         } else if (targetClass == Byte.TYPE)
571         {
572             return internalCreateConverter(Byte JavaDoc.class);
573         } else if (targetClass == Short.TYPE)
574         {
575             return internalCreateConverter(Short JavaDoc.class);
576         } else if (targetClass == Integer.TYPE)
577         {
578             return internalCreateConverter(Integer JavaDoc.class);
579         } else if (targetClass == Float.TYPE)
580         {
581             return internalCreateConverter(Float JavaDoc.class);
582         } else if (targetClass == Character.TYPE)
583         {
584             return internalCreateConverter(Character JavaDoc.class);
585         }
586
587
588         //Locate a Converter registered for the superclass (if any) of the target class,
589
// recursively working up the inheritance hierarchy.
590
Class JavaDoc superClazz = targetClass.getSuperclass();
591         if (superClazz != null)
592         {
593             return internalCreateConverter(superClazz);
594         }
595         else
596         {
597             return null;
598         }
599
600     }
601
602
603     public synchronized MethodBinding createMethodBinding(String JavaDoc reference, Class JavaDoc[] params)
604         throws ReferenceSyntaxException
605     {
606         if ((reference == null) || (reference.length() == 0))
607         {
608             log.error("createMethodBinding: reference = null is not allowed");
609             throw new NullPointerException JavaDoc("createMethodBinding: reference = null ist not allowed");
610         }
611
612         // We choose to instantiate a new MethodBinding every time as this is much easier
613
// and about as efficient as implementing a cache specifically for MethodBinding,
614
// which is complicated by the need to use a conposite key=(reference, params)
615
// (significant part of MethodBinding is already cached by ValueBinding implicitly)
616
return new MethodBindingImpl(this, reference, params);
617     }
618
619     public Validator createValidator(String JavaDoc validatorId) throws FacesException
620     {
621         if ((validatorId == null) || (validatorId.length() == 0))
622         {
623             log.error("createValidator: validatorId = null is not allowed");
624             throw new NullPointerException JavaDoc("createValidator: validatorId = null ist not allowed");
625         }
626         
627         Class JavaDoc validatorClass = (Class JavaDoc) _validatorClassMap.get(validatorId);
628         if (validatorClass == null)
629         {
630             String JavaDoc message = "Unknown converter id '" + validatorId + "'.";
631             log.error(message);
632             throw new FacesException(message);
633         }
634         
635         try
636         {
637             return (Validator) validatorClass.newInstance();
638         }
639         catch (Exception JavaDoc e)
640         {
641             log.error("Could not instantiate converter " + validatorClass, e);
642             throw new FacesException("Could not instantiate converter: " + validatorClass, e);
643         }
644     }
645
646     public ValueBinding createValueBinding(String JavaDoc reference) throws ReferenceSyntaxException
647     {
648         if ((reference == null) || (reference.length() == 0))
649         {
650             log.error("createValueBinding: reference = null is not allowed");
651             throw new NullPointerException JavaDoc("createValueBinding: reference = null is not allowed");
652         }
653         return (ValueBinding) _valueBindingCache.get(reference);
654     }
655
656
657     public String JavaDoc getDefaultRenderKitId()
658     {
659         return _defaultRenderKitId;
660     }
661
662     public void setDefaultRenderKitId(String JavaDoc defaultRenderKitId)
663     {
664         _defaultRenderKitId = defaultRenderKitId;
665     }
666
667     public StateManager getStateManager()
668     {
669         return _stateManager;
670     }
671
672     public void setStateManager(StateManager stateManager)
673     {
674         _stateManager = stateManager;
675     }
676 }
677
Popular Tags