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