View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.config.impl.digester;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.LinkedHashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import javax.faces.render.RenderKitFactory;
30  
31  import org.apache.myfaces.config.FacesConfigDispenser;
32  import org.apache.myfaces.config.element.Behavior;
33  import org.apache.myfaces.config.element.ClientBehaviorRenderer;
34  import org.apache.myfaces.config.element.ManagedBean;
35  import org.apache.myfaces.config.element.NavigationRule;
36  import org.apache.myfaces.config.element.Renderer;
37  import org.apache.myfaces.config.element.Application;
38  import org.apache.myfaces.config.element.Converter;
39  import org.apache.myfaces.config.element.FacesConfig;
40  import org.apache.myfaces.config.element.Factory;
41  import org.apache.myfaces.config.element.LocaleConfig;
42  import org.apache.myfaces.config.element.NamedEvent;
43  import org.apache.myfaces.config.element.RenderKit;
44  import org.apache.myfaces.config.element.ResourceBundle;
45  import org.apache.myfaces.config.element.SystemEventListener;
46  
47  /**
48   * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller</a>
49   */
50  public class DigesterFacesConfigDispenserImpl extends FacesConfigDispenser
51  {
52      /**
53       * 
54       */
55      private static final long serialVersionUID = 3550379003287939559L;
56      // Factories
57      private List<String> applicationFactories = new ArrayList<String>();
58      private List<String> exceptionHandlerFactories = new ArrayList<String>();
59      private List<String> externalContextFactories = new ArrayList<String>();
60      private List<String> facesContextFactories = new ArrayList<String>();
61      private List<String> lifecycleFactories = new ArrayList<String>();
62      private List<String> ViewDeclarationLanguageFactories = new ArrayList<String>();
63      private List<String> partialViewContextFactories = new ArrayList<String>();
64      private List<String> renderKitFactories = new ArrayList<String>();
65      private List<String> tagHandlerDelegateFactories = new ArrayList<String>();
66      private List<String> visitContextFactories = new ArrayList<String>();
67      private List<String> faceletCacheFactories = new ArrayList<String>();
68      
69      private String defaultRenderKitId;
70      private String messageBundle;
71      private String partialTraversal;
72      private String facesVersion;
73      
74      private LocaleConfig localeConfig;
75  
76      private Map<String, String> components = new HashMap<String, String>();
77      private Map<String, String> converterByClass = new HashMap<String, String>();
78      private Map<String, String> converterById = new HashMap<String, String>();
79      private Map<String, String> validators = new HashMap<String, String>();
80      private List<Behavior> behaviors = new ArrayList<Behavior>();
81      
82      private Map<String, Converter> converterConfigurationByClassName = new HashMap<String, Converter>();
83      
84      private Map<String, org.apache.myfaces.config.impl.digester.elements.RenderKit> renderKits = new LinkedHashMap<String, org.apache.myfaces.config.impl.digester.elements.RenderKit>();
85      
86      private List<String> actionListeners = new ArrayList<String>();
87      private List<String> elResolvers = new ArrayList<String>();
88      private List<String> lifecyclePhaseListeners = new ArrayList<String>();
89      private List<String> navigationHandlers = new ArrayList<String>();
90      private List<String> propertyResolver = new ArrayList<String>();
91      private List<String> resourceHandlers = new ArrayList<String>();
92      private List<String> stateManagers = new ArrayList<String>();
93      private List<String> variableResolver = new ArrayList<String>();
94      private List<String> viewHandlers = new ArrayList<String>();
95      private List<String> defaultValidatorIds = new ArrayList<String>();
96      private List<String> defaultAnnotatedValidatorIds = new ArrayList<String>();
97      
98      private List<ManagedBean> managedBeans = new ArrayList<ManagedBean>();
99      
100     private List<NavigationRule> navigationRules = new ArrayList<NavigationRule>();
101     private List<ResourceBundle> resourceBundles = new ArrayList<ResourceBundle>();
102 
103     private List<SystemEventListener> systemEventListeners = new ArrayList<SystemEventListener>();
104     
105     private List<NamedEvent> namedEvents = new ArrayList<NamedEvent>();
106     
107     /**
108      * Add another unmarshalled faces config object.
109      * 
110      * @param config
111      *            unmarshalled faces config object
112      */
113     public void feed(FacesConfig config)
114     {
115         for (Factory factory : config.getFactories())
116         {
117             applicationFactories.addAll(factory.getApplicationFactory());
118             exceptionHandlerFactories.addAll(factory.getExceptionHandlerFactory());
119             externalContextFactories.addAll(factory.getExternalContextFactory());
120             facesContextFactories.addAll(factory.getFacesContextFactory());
121             lifecycleFactories.addAll(factory.getLifecycleFactory());
122             ViewDeclarationLanguageFactories.addAll(factory.getViewDeclarationLanguageFactory());
123             partialViewContextFactories.addAll(factory.getPartialViewContextFactory());
124             renderKitFactories.addAll(factory.getRenderkitFactory());
125             tagHandlerDelegateFactories.addAll(factory.getTagHandlerDelegateFactory());
126             visitContextFactories.addAll(factory.getVisitContextFactory());
127         }
128 
129         components.putAll(config.getComponents());
130         validators.putAll(config.getValidators());
131         behaviors.addAll (config.getBehaviors());
132         
133         for (Application application : config.getApplications())
134         {
135             if (!application.getDefaultRenderkitId().isEmpty())
136             {
137                 defaultRenderKitId =
138                         application.getDefaultRenderkitId().get(application.getDefaultRenderkitId().size() - 1);
139             }
140 
141             if (!application.getMessageBundle().isEmpty())
142             {
143                 messageBundle = application.getMessageBundle().get(application.getMessageBundle().size() - 1);
144             }
145 
146             if (!application.getLocaleConfig().isEmpty())
147             {
148                 localeConfig = application.getLocaleConfig().get(application.getLocaleConfig().size() - 1);
149             }
150             
151             if (!application.getPartialTraversal().isEmpty())
152             {
153                 partialTraversal = application.getPartialTraversal().get (application.getPartialTraversal().size() - 1);
154             }
155             
156             actionListeners.addAll(application.getActionListener());
157             navigationHandlers.addAll(application.getNavigationHandler());
158             resourceHandlers.addAll(application.getResourceHandler());
159             viewHandlers.addAll(application.getViewHandler());
160             stateManagers.addAll(application.getStateManager());
161             propertyResolver.addAll(application.getPropertyResolver());
162             variableResolver.addAll(application.getVariableResolver());
163             resourceBundles.addAll(application.getResourceBundle());
164             elResolvers.addAll(application.getElResolver());
165 
166             // Jsf 2.0 spec section 3.5.3 says this: 
167             // ".... Any configuration resource that declares a list of default 
168             // validators overrides any list provided in a previously processed
169             // configuration resource. If an empty <default-validators/> element 
170             // is found in a configuration resource, the list
171             // of default validators must be cleared....."
172             if (application.isDefaultValidatorsPresent())
173             {
174                 // we have a <default-validators> element, so any existing
175                 // default validators should be removed
176                 defaultValidatorIds.clear();
177                 
178                 // now add all default-validator entries (could be zero)
179                 defaultValidatorIds.addAll(application.getDefaultValidatorIds());
180             }
181             else
182             {
183                 //If isDefaultValidatorsPresent() is false, and there are still 
184                 //default validators, it means they were added using annotations, so
185                 //they are not affected by the empty entry according to section 3.5.3
186                 defaultAnnotatedValidatorIds.addAll(application.getDefaultValidatorIds());
187             }
188             
189             systemEventListeners.addAll(application.getSystemEventListeners());
190         }
191 
192         for (Converter converter : config.getConverters())
193         {
194             if (converter.getConverterId() != null)
195             {
196                 converterById.put(converter.getConverterId(),converter
197                         .getConverterClass());
198             }
199             if (converter.getForClass() != null)
200             {
201                 converterByClass.put(converter.getForClass(),converter
202                         .getConverterClass());
203             }
204 
205             converterConfigurationByClassName.put(converter.getConverterClass(), converter);
206         }
207 
208         for (RenderKit renderKit : config.getRenderKits())
209         {
210             String renderKitId = renderKit.getId();
211 
212             if (renderKitId == null)
213             {
214                 renderKitId = RenderKitFactory.HTML_BASIC_RENDER_KIT;
215             }
216 
217             org.apache.myfaces.config.impl.digester.elements.RenderKit existing = renderKits.get(renderKitId);
218 
219             if (existing == null)
220             {
221                 existing = new org.apache.myfaces.config.impl.digester.elements.RenderKit();
222                 existing.merge(renderKit);
223                 renderKits.put(renderKitId, existing);
224                 //renderKits.put(renderKitId, renderKit);
225             }
226             else
227             {
228                 existing.merge(renderKit);
229             }
230         }
231 
232         lifecyclePhaseListeners.addAll(config.getLifecyclePhaseListener());
233         managedBeans.addAll(config.getManagedBeans());
234         navigationRules.addAll(config.getNavigationRules());
235         facesVersion = config.getVersion();
236         namedEvents.addAll(config.getNamedEvents());
237     }
238 
239     /**
240      * Add another ApplicationFactory class name
241      * 
242      * @param factoryClassName
243      *            a class name
244      */
245     public void feedApplicationFactory(String factoryClassName)
246     {
247         applicationFactories.add(factoryClassName);
248     }
249 
250     public void feedExceptionHandlerFactory(String factoryClassName)
251     {
252         exceptionHandlerFactories.add(factoryClassName);
253     }
254 
255     public void feedExternalContextFactory(String factoryClassName)
256     {
257         externalContextFactories.add(factoryClassName);
258     }
259 
260     /**
261      * Add another FacesContextFactory class name
262      * 
263      * @param factoryClassName
264      *            a class name
265      */
266     public void feedFacesContextFactory(String factoryClassName)
267     {
268         facesContextFactories.add(factoryClassName);
269     }
270 
271     /**
272      * Add another LifecycleFactory class name
273      * 
274      * @param factoryClassName
275      *            a class name
276      */
277     public void feedLifecycleFactory(String factoryClassName)
278     {
279         lifecycleFactories.add(factoryClassName);
280     }
281 
282     public void feedViewDeclarationLanguageFactory(String factoryClassName)
283     {
284         ViewDeclarationLanguageFactories.add(factoryClassName);
285     }
286 
287     public void feedPartialViewContextFactory(String factoryClassName)
288     {
289         partialViewContextFactories.add(factoryClassName);
290     }
291 
292     /**
293      * Add another RenderKitFactory class name
294      * 
295      * @param factoryClassName
296      *            a class name
297      */
298     public void feedRenderKitFactory(String factoryClassName)
299     {
300         renderKitFactories.add(factoryClassName);
301     }
302 
303     public void feedTagHandlerDelegateFactory(String factoryClassName)
304     {
305         tagHandlerDelegateFactories.add(factoryClassName);
306     }
307 
308     public void feedVisitContextFactory(String factoryClassName)
309     {
310         visitContextFactories.add(factoryClassName);
311     }
312 
313     /**
314      * @return Collection over ApplicationFactory class names
315      */
316     public Collection<String> getApplicationFactoryIterator()
317     {
318         return applicationFactories;
319     }
320 
321     public Collection<String> getExceptionHandlerFactoryIterator()
322     {
323         return exceptionHandlerFactories;
324     }
325 
326     public Collection<String> getExternalContextFactoryIterator()
327     {
328         return externalContextFactories;
329     }
330 
331     /**
332      * @return Collection over FacesContextFactory class names
333      */
334     public Collection<String> getFacesContextFactoryIterator()
335     {
336         return facesContextFactories;
337     }
338 
339     /**
340      * @return Collection over LifecycleFactory class names
341      */
342     public Collection<String> getLifecycleFactoryIterator()
343     {
344         return lifecycleFactories;
345     }
346 
347     public Collection<String> getViewDeclarationLanguageFactoryIterator()
348     {
349         return ViewDeclarationLanguageFactories;
350     }
351 
352     public Collection<String> getPartialViewContextFactoryIterator()
353     {
354         return partialViewContextFactories;
355     }
356 
357     /**
358      * @return Collection over RenderKit factory class names
359      */
360     public Collection<String> getRenderKitFactoryIterator()
361     {
362         return renderKitFactories;
363     }
364 
365     public Collection<String> getTagHandlerDelegateFactoryIterator()
366     {
367         return tagHandlerDelegateFactories;
368     }
369 
370     public Collection<String> getVisitContextFactoryIterator()
371     {
372         return visitContextFactories;
373     }
374 
375     /**
376      * @return Collection over ActionListener class names
377      */
378     public Collection<String> getActionListenerIterator()
379     {
380         return new ArrayList<String>(actionListeners);
381     }
382 
383     /**
384      * @return the default render kit id
385      */
386     public String getDefaultRenderKitId()
387     {
388         return defaultRenderKitId;
389     }
390 
391     /**
392      * @return Collection over message bundle names
393      */
394     public String getMessageBundle()
395     {
396         return messageBundle;
397     }
398 
399     /**
400      * @return Collection over NavigationHandler class names
401      */
402     public Collection<String> getNavigationHandlerIterator()
403     {
404         return new ArrayList<String>(navigationHandlers);
405     }
406 
407     /**
408      * @return the partial traversal class name
409      */
410     public String getPartialTraversal ()
411     {
412         return partialTraversal;
413     }
414     
415     /**
416      * @return Collection over ResourceHandler class names
417      */
418     public Collection<String> getResourceHandlerIterator()
419     {
420         return new ArrayList<String>(resourceHandlers);
421     }
422 
423     /**
424      * @return Collection over ViewHandler class names
425      */
426     public Collection<String> getViewHandlerIterator()
427     {
428         return new ArrayList<String>(viewHandlers);
429     }
430 
431     /**
432      * @return Collection over StateManager class names
433      */
434     public Collection<String> getStateManagerIterator()
435     {
436         return new ArrayList<String>(stateManagers);
437     }
438 
439     /**
440      * @return Collection over PropertyResolver class names
441      */
442     public Collection<String> getPropertyResolverIterator()
443     {
444         return new ArrayList<String>(propertyResolver);
445     }
446 
447     /**
448      * @return Collection over VariableResolver class names
449      */
450     public Collection<String> getVariableResolverIterator()
451     {
452 
453         return new ArrayList<String>(variableResolver);
454     }
455 
456     /**
457      * @return the default locale name
458      */
459     public String getDefaultLocale()
460     {
461         if (localeConfig != null)
462         {
463             return localeConfig.getDefaultLocale();
464         }
465         return null;
466     }
467 
468     /**
469      * @return Collection over supported locale names
470      */
471     public Collection<String> getSupportedLocalesIterator()
472     {
473         List<String> locale;
474         if (localeConfig != null)
475         {
476             locale = localeConfig.getSupportedLocales();
477         }
478         else
479         {
480             locale = Collections.emptyList();
481         }
482 
483         return locale;
484     }
485 
486     /**
487      * @return Collection over all defined component types
488      */
489     public Collection<String> getComponentTypes()
490     {
491         return components.keySet();
492     }
493 
494     /**
495      * @return component class that belongs to the given component type
496      */
497     public String getComponentClass(String componentType)
498     {
499         return components.get(componentType);
500     }
501 
502     /**
503      * @return Collection over all defined converter ids
504      */
505     public Collection<String> getConverterIds()
506     {
507         return converterById.keySet();
508     }
509 
510     /**
511      * @return Collection over all classes with an associated converter
512      */
513     public Collection<String> getConverterClasses()
514     {
515         return converterByClass.keySet();
516     }
517 
518     public Collection<String> getConverterConfigurationByClassName()
519     {
520         return converterConfigurationByClassName.keySet();
521     }
522 
523     public Converter getConverterConfiguration(String converterClassName)
524     {
525         return converterConfigurationByClassName.get(converterClassName);
526     }
527 
528     /**
529      * @return converter class that belongs to the given converter id
530      */
531     public String getConverterClassById(String converterId)
532     {
533         return converterById.get(converterId);
534     }
535 
536     /**
537      * @return converter class that is associated with the given class name
538      */
539     public String getConverterClassByClass(String className)
540     {
541         return converterByClass.get(className);
542     }
543 
544     /**
545      * @return Collection over all defined default validator ids
546      */
547     public Collection<String> getDefaultValidatorIds ()
548     {
549         List<String> allDefaultValidatorIds = new ArrayList<String>();
550         allDefaultValidatorIds.addAll(defaultAnnotatedValidatorIds);
551         allDefaultValidatorIds.addAll(defaultValidatorIds);
552         return allDefaultValidatorIds;
553     }
554     
555     /**
556      * @return Collection over all defined validator ids
557      */
558     public Collection<String> getValidatorIds()
559     {
560         return validators.keySet();
561     }
562 
563     /**
564      * @return validator class name that belongs to the given validator id
565      */
566     public String getValidatorClass(String validatorId)
567     {
568         return validators.get(validatorId);
569     }
570 
571     /**
572      * @return Collection over {@link org.apache.myfaces.config.element.ManagedBean ManagedBean}s
573      */
574     public Collection<ManagedBean> getManagedBeans()
575     {
576         return managedBeans;
577     }
578 
579     /**
580      * @return Collection over {@link org.apache.myfaces.config.element.NavigationRule NavigationRule}s
581      */
582     public Collection<NavigationRule> getNavigationRules()
583     {
584         return navigationRules;
585     }
586 
587     /**
588      * @return Collection over all defined renderkit ids
589      */
590     public Collection<String> getRenderKitIds()
591     {
592         return renderKits.keySet();
593     }
594 
595     /**
596      * @return renderkit class name for given renderkit id
597      */
598     public Collection<String> getRenderKitClasses(String renderKitId)
599     {
600         return renderKits.get(renderKitId).getRenderKitClasses();
601     }
602 
603     /**
604      * @return Iterator over {@link org.apache.myfaces.config.element.ClientBehaviorRenderer ClientBehaviorRenderer}s for the given renderKitId
605      */
606     public Collection<ClientBehaviorRenderer> getClientBehaviorRenderers (String renderKitId)
607     {
608         return renderKits.get (renderKitId).getClientBehaviorRenderers();
609     }
610     
611     /**
612      * @return Collection over {@link org.apache.myfaces.config.element.Renderer Renderer}s for the given renderKitId
613      */
614     public Collection<Renderer> getRenderers(String renderKitId)
615     {
616         return renderKits.get(renderKitId).getRenderer();
617     }
618 
619     /**
620      * @return Collection over {@link javax.faces.event.PhaseListener} implementation class names
621      */
622     public Collection<String> getLifecyclePhaseListeners()
623     {
624         return lifecyclePhaseListeners;
625     }
626 
627     public Collection<ResourceBundle> getResourceBundles()
628     {
629         return resourceBundles;
630     }
631 
632     public Collection<String> getElResolvers()
633     {
634         return elResolvers;
635     }
636 
637     public Collection<SystemEventListener> getSystemEventListeners()
638     {        
639         return systemEventListeners;
640     }
641     
642     public Collection<Behavior> getBehaviors ()
643     {
644         return behaviors;
645     }
646     
647     public String getFacesVersion ()
648     {
649         return facesVersion;
650     }
651     
652     public Collection<NamedEvent> getNamedEvents()
653     {
654         return namedEvents;
655     }
656 }