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.annotation;
20  
21  import java.lang.annotation.Annotation;
22  import java.lang.reflect.Field;
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.Set;
26  import java.util.logging.Level;
27  import java.util.logging.Logger;
28  
29  import javax.faces.FacesException;
30  import javax.faces.bean.ApplicationScoped;
31  import javax.faces.bean.CustomScoped;
32  import javax.faces.bean.ManagedBean;
33  import javax.faces.bean.NoneScoped;
34  import javax.faces.bean.RequestScoped;
35  import javax.faces.bean.SessionScoped;
36  import javax.faces.bean.ViewScoped;
37  import javax.faces.component.FacesComponent;
38  import javax.faces.component.behavior.FacesBehavior;
39  import javax.faces.context.ExternalContext;
40  import javax.faces.convert.FacesConverter;
41  import javax.faces.event.ComponentSystemEvent;
42  import javax.faces.event.NamedEvent;
43  import javax.faces.render.FacesBehaviorRenderer;
44  import javax.faces.render.FacesRenderer;
45  import javax.faces.render.RenderKitFactory;
46  import javax.faces.validator.FacesValidator;
47  
48  import org.apache.myfaces.config.impl.digester.elements.Application;
49  import org.apache.myfaces.config.impl.digester.elements.Behavior;
50  import org.apache.myfaces.config.impl.digester.elements.Converter;
51  import org.apache.myfaces.config.impl.digester.elements.FacesConfig;
52  import org.apache.myfaces.spi.AnnotationProvider;
53  import org.apache.myfaces.spi.AnnotationProviderFactory;
54  
55  /**
56   * Configure all annotations that needs to be defined at startup.
57   * 
58   * <ul>
59   * <li>{@link javax.faces.component.FacesComponent}</li>
60   * <li>{@link javax.faces.convert.FacesConverter}</li>
61   * <li>{@link javax.faces.validator.FacesValidator}</li>
62   * <li>{@link javax.faces.render.FacesRenderer}</li>
63   * <li>{@link javax.faces.bean.ManagedBean}</li>
64   * <li>{@link javax.faces.bean.ManagedProperty}</li>
65   * <li>{@link javax.faces.render.FacesBehaviorRenderer}</li>
66   * </ul>
67   * <p>
68   * Some parts copied from org.apache.shale.tiger.view.faces.LifecycleListener2
69   * </p>
70   * 
71   * @since 2.0
72   * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
73   * @version $Revision: 1455393 $ $Date: 2013-03-11 21:17:52 -0500 (Mon, 11 Mar 2013) $
74   */
75  public class AnnotationConfigurator
76  {
77      //private static final Log log = LogFactory.getLog(AnnotationConfigurator.class);
78      private static final Logger log = Logger.getLogger(AnnotationConfigurator.class.getName());
79  
80      /**
81       * <p>The render kit factory for this application.</p>
82       */
83      private RenderKitFactory rkFactory = null;
84  
85      public AnnotationConfigurator()
86      {
87      }
88  
89      public FacesConfig createFacesConfig(ExternalContext _externalContext, boolean metadataComplete)
90      {
91          if (!metadataComplete)
92          {
93              AnnotationProvider provider = AnnotationProviderFactory.getAnnotationProviderFactory(_externalContext).getAnnotationProvider(_externalContext);
94              Map<Class<? extends Annotation>,Set<Class<?>>> map = provider.getAnnotatedClasses(_externalContext);
95              return createFacesConfig(map);
96          }
97          return null;
98      }
99      /*
100     public void configure(final Application application, 
101             final FacesConfigDispenser<FacesConfig> dispenser,
102             boolean metadataComplete) throws FacesException
103     {
104         AnnotationProvider provider = AnnotationProviderFactory.getAnnotationProviderFactory(_externalContext).createAnnotationProvider(_externalContext);
105         Map<Class<? extends Annotation>,Set<Class<?>>> map = provider.getAnnotatedClasses(_externalContext);
106         configureClasses(application, dispenser, map);
107     }*/
108     
109     protected FacesConfig createFacesConfig(Map< Class<? extends Annotation>, Set<Class<?>> > map)
110     {
111         FacesConfig facesConfig = new FacesConfig();
112         
113         Set<Class<?>> classes = map.get(FacesComponent.class);
114         
115         if (classes != null && !classes.isEmpty())
116         {
117             for (Class<?> clazz : classes)
118             {
119                 FacesComponent comp = (FacesComponent) clazz
120                         .getAnnotation(FacesComponent.class);
121                 if (comp != null)
122                 {
123                     if (log.isLoggable(Level.FINEST))
124                     {
125                         log.finest("addComponent(" + comp.value() + ","
126                                 + clazz.getName() + ")");
127                     }
128                     
129                     facesConfig.addComponent(comp.value(), clazz.getName());
130                 }
131             }
132         }
133         
134         classes = map.get(FacesConverter.class);
135         if (classes != null && !classes.isEmpty())
136         {
137             for (Class<?> clazz : classes)
138             {
139                 FacesConverter conv = (FacesConverter) clazz
140                         .getAnnotation(FacesConverter.class);
141                 if (conv != null)
142                 {
143                     if (log.isLoggable(Level.FINEST))
144                     {
145                         log.finest("addConverter(" + conv.value() + ","
146                                 + clazz.getName() + ")");
147                     }
148                     //If there is a previous entry on Application Configuration Resources,
149                     //the entry there takes precedence
150                     boolean hasForClass = !Object.class.equals(conv.forClass());
151                     boolean hasValue = conv.value().length() > 0;
152                     if (hasForClass || hasValue)
153                     {
154                         Converter converter = new Converter();
155                         if (hasForClass)
156                         {
157                             converter.setForClass(conv.forClass().getName());
158                         }
159                         if (hasValue) {
160                             converter.setConverterId(conv.value());
161                         }
162                         converter.setConverterClass(clazz.getName());
163                         facesConfig.addConverter(converter);
164                     }
165                     else
166                     {   
167                         // TODO MartinKoci MYFACES-3053
168                         throw new FacesException("@FacesConverter must have value, forClass or both. Check annotation @FacesConverter on class: "  + clazz.getName());
169                     }
170                 }                
171             }
172         }
173         
174         classes = map.get(FacesValidator.class);
175         if (classes != null && !classes.isEmpty())
176         {
177             for (Class<?> clazz : classes)
178             {
179                 FacesValidator val = (FacesValidator) clazz
180                 .getAnnotation(FacesValidator.class);
181                 if (val != null)
182                 {
183                     if (log.isLoggable(Level.FINEST))
184                     {
185                         log.finest("addValidator(" + val.value() + "," + clazz.getName()
186                                 + ")");
187                     }
188                     facesConfig.addValidator(val.value(), clazz.getName());
189                     if (val.isDefault())
190                     {
191                         Application app = null;
192                         if(facesConfig.getApplications().isEmpty())
193                         {
194                             app = new Application();
195                         }
196                         else
197                         {
198                             app = (Application) facesConfig.getApplications().get(0);
199                         }
200                         app.addDefaultValidatorId(val.value());
201                     }
202                 }
203             }
204         }
205 
206         classes = map.get(FacesRenderer.class);
207         if (classes != null && !classes.isEmpty())
208         {
209             for (Class<?> clazz : classes)
210             {
211                 FacesRenderer rend = (FacesRenderer) clazz
212                 .getAnnotation(FacesRenderer.class);
213                 if (rend != null)
214                 {
215                     String renderKitId = rend.renderKitId();
216                     if (renderKitId == null)
217                     {
218                         renderKitId = RenderKitFactory.HTML_BASIC_RENDER_KIT;
219                     }
220                     if (log.isLoggable(Level.FINEST))
221                     {
222                         log.finest("addRenderer(" + renderKitId + ", "
223                                 + rend.componentFamily() + ", " + rend.rendererType()
224                                 + ", " + clazz.getName() + ")");
225                     }
226                     
227                     org.apache.myfaces.config.impl.digester.elements.RenderKit renderKit =
228                        (org.apache.myfaces.config.impl.digester.elements.RenderKit) facesConfig.getRenderKit(renderKitId);
229                     if (renderKit == null)
230                     {
231                         renderKit = new org.apache.myfaces.config.impl.digester.elements.RenderKit();
232                         renderKit.setId(renderKitId);
233                         facesConfig.addRenderKit(renderKit);
234                     }
235                     
236                     org.apache.myfaces.config.impl.digester.elements.Renderer renderer =
237                         new org.apache.myfaces.config.impl.digester.elements.Renderer();
238                     renderer.setComponentFamily(rend.componentFamily());
239                     renderer.setRendererClass(clazz.getName());
240                     renderer.setRendererType(rend.rendererType());
241                     renderKit.addRenderer(renderer);
242                 }        
243             }
244         }
245 
246         classes = map.get(ManagedBean.class);
247         if (classes != null && !classes.isEmpty())
248         {
249             for (Class<?> clazz : classes)
250             {
251                 javax.faces.bean.ManagedBean bean = 
252                     (javax.faces.bean.ManagedBean) clazz.getAnnotation(javax.faces.bean.ManagedBean.class);
253         
254                 if (bean != null)
255                 {
256                     if (log.isLoggable(Level.FINE))
257                     {
258                         log.fine("Class '" + clazz.getName() + "' has an @ManagedBean annotation");
259                     }
260                     
261                     org.apache.myfaces.config.impl.digester.elements.ManagedBean mbc =
262                         new org.apache.myfaces.config.impl.digester.elements.ManagedBean();
263                     String beanName = bean.name();
264                     
265                     if ((beanName == null) || beanName.equals ("")) {
266                         int index;
267                         
268                         // Missing name attribute algorithm: take the unqualified name and make the
269                         // first character lowercase.
270                         
271                         beanName = clazz.getName();
272                         index = beanName.lastIndexOf (".");
273                         
274                         if (index != -1) {
275                             beanName = beanName.substring (index + 1);
276                         }
277                         
278                         beanName = Character.toLowerCase (beanName.charAt (0)) +
279                             beanName.substring (1);
280                     }
281                     
282                     mbc.setName(beanName);
283                     mbc.setEager(Boolean.toString(bean.eager()));
284                     mbc.setBeanClass(clazz.getName());
285                     
286                     ApplicationScoped appScoped = (ApplicationScoped) clazz.getAnnotation(ApplicationScoped.class);
287                     if (appScoped != null)
288                     {
289                         mbc.setScope("application");
290                     }
291                     
292                     else
293                     {
294                         NoneScoped noneScoped = (NoneScoped) clazz.getAnnotation(NoneScoped.class);
295                         if (noneScoped != null)
296                         {
297                             mbc.setScope("none");
298                         }
299                         
300                         else
301                         {
302                             RequestScoped requestScoped = (RequestScoped) clazz.getAnnotation(RequestScoped.class);
303                             if (requestScoped != null)
304                             {
305                                 mbc.setScope("request");
306                             }
307                             
308                             else
309                             {
310                                 SessionScoped sessionScoped = (SessionScoped) clazz.getAnnotation(SessionScoped.class);
311                                 if (sessionScoped != null)
312                                 {
313                                     mbc.setScope("session");
314                                 }
315                                 
316                                 else
317                                 {
318                                     ViewScoped viewScoped = (ViewScoped) clazz.getAnnotation(ViewScoped.class);
319                                     if (viewScoped != null)
320                                     {
321                                         mbc.setScope("view");
322                                     }
323                                     
324                                     else {
325                                         CustomScoped customScoped = (CustomScoped) clazz.getAnnotation(CustomScoped.class);
326                                         if (customScoped != null)
327                                         {
328                                             mbc.setScope(customScoped.value());
329                                         }
330                                         
331                                         else
332                                         {
333                                             // No scope annotation means default of "request".
334                                             
335                                             mbc.setScope ("request");
336                                         }
337                                     }
338                                 }
339                             }
340                         }
341                     }
342                     
343                     Field[] fields = fields(clazz);
344                     for (Field field : fields)
345                     {
346                         if (log.isLoggable(Level.FINEST))
347                         {
348                             log.finest("  Scanning field '" + field.getName() + "'");
349                         }
350                         javax.faces.bean.ManagedProperty property = (javax.faces.bean.ManagedProperty) field
351                                 .getAnnotation(javax.faces.bean.ManagedProperty.class);
352                         if (property != null)
353                         {
354                             if (log.isLoggable(Level.FINE))
355                             {
356                                 log.fine("  Field '" + field.getName()
357                                         + "' has a @ManagedProperty annotation");
358                             }
359                             org.apache.myfaces.config.impl.digester.elements.ManagedProperty mpc = 
360                                 new org.apache.myfaces.config.impl.digester.elements.ManagedProperty();
361                             String name = property.name();
362                             if ((name == null) || "".equals(name))
363                             {
364                                 name = field.getName();
365                             }
366                             mpc.setPropertyName(name);
367                             mpc.setPropertyClass(field.getType().getName()); // FIXME - primitives, arrays, etc.
368                             mpc.setValue(property.value());
369                             mbc.addProperty(mpc);
370                             continue;
371                         }
372                     }
373                     facesConfig.addManagedBean(mbc);
374                 }        
375             }
376         }
377 
378         classes = map.get(NamedEvent.class);
379         if (classes != null && !classes.isEmpty())
380         {
381             for (Class<?> clazz : classes)
382             {
383                 NamedEvent namedEvent = (NamedEvent) clazz.getAnnotation (NamedEvent.class);
384         
385                 if (namedEvent != null) {
386                     // Can only apply @NamedEvent to ComponentSystemEvent subclasses.
387                     
388                     if (!ComponentSystemEvent.class.isAssignableFrom (clazz)) {
389                         // Just log this.  We'll catch it later in the runtime.
390                         
391                         if (log.isLoggable(Level.WARNING)) {
392                             log.warning (clazz.getName() + " is annotated with @javax.faces.event.NamedEvent, but does " +
393                                 "not extend javax.faces.event.ComponentSystemEvent");
394                         }
395                     }
396                     // Have to register @NamedEvent annotations with the NamedEventManager class since
397                     // we need to get access to this info later and can't from the dispenser (it's not a
398                     // singleton).
399                     org.apache.myfaces.config.impl.digester.elements.NamedEvent namedEventConfig = 
400                         new org.apache.myfaces.config.impl.digester.elements.NamedEvent();
401                     namedEventConfig.setEventClass(clazz.getName());
402                     namedEventConfig.setShortName(namedEvent.shortName());
403                     facesConfig.addNamedEvent(namedEventConfig);
404                 }
405             }
406         }
407     
408         classes = map.get(FacesBehavior.class);
409         if (classes != null && !classes.isEmpty())
410         {
411             for (Class<?> clazz : classes)
412             {
413                 FacesBehavior facesBehavior = (FacesBehavior) clazz.getAnnotation (FacesBehavior.class);
414         
415                 if (facesBehavior != null) {
416                     // Can only apply @FacesBehavior to Behavior implementors.
417                     
418                     if (!javax.faces.component.behavior.Behavior.class.isAssignableFrom (clazz)) {
419                         // Just log this.  We'll catch it later in the runtime.
420                         
421                         if (log.isLoggable(Level.WARNING)) {
422                             log.warning (clazz.getName() + " is annotated with @javax.faces.component.behavior.FacesBehavior, " +
423                                     "but does not implement javax.faces.component.behavior.Behavior");
424                         }
425                     }
426                     
427                     if (log.isLoggable(Level.FINEST)) {
428                         log.finest ("addBehavior(" + facesBehavior.value() + ", " + clazz.getName() + ")");
429                     }
430                     
431                     Behavior behavior = new Behavior();
432                     behavior.setBehaviorId(facesBehavior.value());
433                     behavior.setBehaviorClass(clazz.getName());
434                     facesConfig.addBehavior(behavior);
435                 }
436                 
437             }
438         }
439     
440         classes = map.get(FacesBehaviorRenderer.class);
441         if (classes != null && !classes.isEmpty())
442         {
443             for (Class<?> clazz : classes)
444             {
445                 FacesBehaviorRenderer facesBehaviorRenderer = (FacesBehaviorRenderer) clazz.getAnnotation (FacesBehaviorRenderer.class);
446         
447                 if (facesBehaviorRenderer != null) {
448                     String renderKitId = facesBehaviorRenderer.renderKitId();
449                     //RenderKit renderKit;
450                     
451                     if (log.isLoggable(Level.FINEST)) {
452                         log.finest ("addClientBehaviorRenderer(" + renderKitId + ", " + facesBehaviorRenderer.rendererType() + ", " +
453                              clazz.getName() + ")");
454                     }
455                     
456                     org.apache.myfaces.config.impl.digester.elements.RenderKit renderKit =
457                         (org.apache.myfaces.config.impl.digester.elements.RenderKit) facesConfig.getRenderKit(renderKitId);
458                     if (renderKit == null)
459                     {
460                         renderKit = new org.apache.myfaces.config.impl.digester.elements.RenderKit();
461                         renderKit.setId(renderKitId);
462                         facesConfig.addRenderKit(renderKit);
463                     }
464                     
465                     org.apache.myfaces.config.impl.digester.elements.ClientBehaviorRenderer cbr = 
466                         new org.apache.myfaces.config.impl.digester.elements.ClientBehaviorRenderer();
467                     cbr.setRendererType(facesBehaviorRenderer.rendererType());
468                     cbr.setRendererClass(clazz.getName());
469                     renderKit.addClientBehaviorRenderer(cbr);
470                 }
471             }
472         }
473         return facesConfig;
474     }
475     
476     /*
477     protected void configureClasses( Application application, 
478             FacesConfigDispenser<FacesConfig> dispenser, 
479             Map< Class<? extends Annotation>, Set<Class<?>> > map)
480     {
481         Set<Class<?>> classes = map.get(FacesComponent.class);
482         
483         if (classes != null && !classes.isEmpty())
484         {
485             for (Class<?> clazz : classes)
486             {
487                 FacesComponent comp = (FacesComponent) clazz
488                         .getAnnotation(FacesComponent.class);
489                 if (comp != null)
490                 {
491                     if (log.isLoggable(Level.FINEST))
492                     {
493                         log.finest("addComponent(" + comp.value() + ","
494                                 + clazz.getName() + ")");
495                     }
496                     
497                     //If there is a previous entry on Application Configuration Resources,
498                     //the entry there takes precedence
499                     if (dispenser.getComponentClass(comp.value()) == null)
500                     {
501                         application.addComponent(comp.value(), clazz.getName());                
502                     }
503                 }
504             }
505         }
506         
507         classes = map.get(FacesConverter.class);
508         if (classes != null && !classes.isEmpty())
509         {
510             for (Class<?> clazz : classes)
511             {
512                 FacesConverter conv = (FacesConverter) clazz
513                         .getAnnotation(FacesConverter.class);
514                 if (conv != null)
515                 {
516                     if (log.isLoggable(Level.FINEST))
517                     {
518                         log.finest("addConverter(" + conv.value() + ","
519                                 + clazz.getName() + ")");
520                     }
521                     //If there is a previous entry on Application Configuration Resources,
522                     //the entry there takes precedence
523                     if (!Object.class.equals(conv.forClass()))
524                     {
525                         application.addConverter(conv.forClass(), clazz.getName());
526                     }
527                     if (dispenser.getConverterClassById(conv.value()) == null &&
528                             conv.value() != null && !"".equals(conv.value()))
529                     {
530                         application.addConverter(conv.value(), clazz.getName());
531                     }
532                 }                
533             }
534         }
535         
536         classes = map.get(FacesValidator.class);
537         if (classes != null && !classes.isEmpty())
538         {
539             for (Class<?> clazz : classes)
540             {
541                 FacesValidator val = (FacesValidator) clazz
542                 .getAnnotation(FacesValidator.class);
543                 if (val != null)
544                 {
545                     if (log.isLoggable(Level.FINEST))
546                     {
547                         log.finest("addValidator(" + val.value() + "," + clazz.getName()
548                                 + ")");
549                     }
550                     //If there is a previous entry on Application Configuration Resources,
551                     //the entry there takes precedence
552                     if (dispenser.getValidatorClass(val.value()) == null)
553                     {
554                         application.addValidator(val.value(), clazz.getName());
555                         if (val.isDefault())
556                         {
557                             application.addDefaultValidatorId(val.value());
558                         }
559                     }
560                 }
561             }
562         }
563 
564         classes = map.get(FacesRenderer.class);
565         if (classes != null && !classes.isEmpty())
566         {
567             for (Class<?> clazz : classes)
568             {
569                 FacesRenderer rend = (FacesRenderer) clazz
570                 .getAnnotation(FacesRenderer.class);
571                 if (rend != null)
572                 {
573                     String renderKitId = rend.renderKitId();
574                     if (renderKitId == null)
575                     {
576                         renderKitId = RenderKitFactory.HTML_BASIC_RENDER_KIT;
577                     }
578                     if (log.isLoggable(Level.FINEST))
579                     {
580                         log.finest("addRenderer(" + renderKitId + ", "
581                                 + rend.componentFamily() + ", " + rend.rendererType()
582                                 + ", " + clazz.getName() + ")");
583                     }
584                     try
585                     {
586                         RenderKit rk = renderKitFactory().getRenderKit(null,
587                                 renderKitId);
588                         if (rk == null)
589                         {
590                             if (log.isLoggable(Level.SEVERE))
591                             {
592                                 log.severe("RenderKit "+renderKitId+" not found when adding @FacesRenderer annotation");
593                             }
594                             throw new FacesException("RenderKit "+renderKitId+" not found when adding @FacesRenderer annotation");
595                         }
596                         rk.addRenderer(rend.componentFamily(), rend.rendererType(),
597                                 (Renderer) clazz.newInstance());
598                     }
599                     catch (Exception e)
600                     {
601                         throw new FacesException(e);
602                     }
603                 }        
604             }
605         }
606 
607         classes = map.get(ManagedBean.class);
608         if (classes != null && !classes.isEmpty())
609         {
610             for (Class<?> clazz : classes)
611             {
612                 javax.faces.bean.ManagedBean bean = 
613                     (javax.faces.bean.ManagedBean) clazz.getAnnotation(javax.faces.bean.ManagedBean.class);
614         
615                 if (bean != null)
616                 {
617                     if (log.isLoggable(Level.FINE))
618                     {
619                         log.fine("Class '" + clazz.getName() + "' has an @ManagedBean annotation");
620                     }
621                     RuntimeConfig runtimeConfig = RuntimeConfig.getCurrentInstance(_externalContext);
622                     org.apache.myfaces.config.impl.digester.elements.ManagedBean mbc =
623                         new org.apache.myfaces.config.impl.digester.elements.ManagedBean();
624                     String beanName = bean.name();
625                     
626                     if ((beanName == null) || beanName.equals ("")) {
627                         int index;
628                         
629                         // Missing name attribute algorithm: take the unqualified name and make the
630                         // first character lowercase.
631                         
632                         beanName = clazz.getName();
633                         index = beanName.lastIndexOf (".");
634                         
635                         if (index != -1) {
636                             beanName = beanName.substring (index + 1);
637                         }
638                         
639                         beanName = Character.toLowerCase (beanName.charAt (0)) +
640                             beanName.substring (1);
641                     }
642                     
643                     mbc.setName(beanName);
644                     mbc.setEager(Boolean.toString(bean.eager()));
645                     mbc.setBeanClass(clazz.getName());
646                     
647                     ApplicationScoped appScoped = (ApplicationScoped) clazz.getAnnotation(ApplicationScoped.class);
648                     if (appScoped != null)
649                     {
650                         mbc.setScope("application");
651                     }
652                     
653                     else
654                     {
655                         NoneScoped noneScoped = (NoneScoped) clazz.getAnnotation(NoneScoped.class);
656                         if (noneScoped != null)
657                         {
658                             mbc.setScope("none");
659                         }
660                         
661                         else
662                         {
663                             RequestScoped requestScoped = (RequestScoped) clazz.getAnnotation(RequestScoped.class);
664                             if (requestScoped != null)
665                             {
666                                 mbc.setScope("request");
667                             }
668                             
669                             else
670                             {
671                                 SessionScoped sessionScoped = (SessionScoped) clazz.getAnnotation(SessionScoped.class);
672                                 if (sessionScoped != null)
673                                 {
674                                     mbc.setScope("session");
675                                 }
676                                 
677                                 else
678                                 {
679                                     ViewScoped viewScoped = (ViewScoped) clazz.getAnnotation(ViewScoped.class);
680                                     if (viewScoped != null)
681                                     {
682                                         mbc.setScope("view");
683                                     }
684                                     
685                                     else {
686                                         CustomScoped customScoped = (CustomScoped) clazz.getAnnotation(CustomScoped.class);
687                                         if (customScoped != null)
688                                         {
689                                             mbc.setScope(customScoped.value());
690                                         }
691                                         
692                                         else
693                                         {
694                                             // No scope annotation means default of "request".
695                                             
696                                             mbc.setScope ("request");
697                                         }
698                                     }
699                                 }
700                             }
701                         }
702                     }
703                     
704                     Field[] fields = fields(clazz);
705                     for (Field field : fields)
706                     {
707                         if (log.isLoggable(Level.FINEST))
708                         {
709                             log.finest("  Scanning field '" + field.getName() + "'");
710                         }
711                         javax.faces.bean.ManagedProperty property = (javax.faces.bean.ManagedProperty) field
712                                 .getAnnotation(javax.faces.bean.ManagedProperty.class);
713                         if (property != null)
714                         {
715                             if (log.isLoggable(Level.FINE))
716                             {
717                                 log.fine("  Field '" + field.getName()
718                                         + "' has a @ManagedProperty annotation");
719                             }
720                             org.apache.myfaces.config.impl.digester.elements.ManagedProperty mpc = 
721                                 new org.apache.myfaces.config.impl.digester.elements.ManagedProperty();
722                             String name = property.name();
723                             if ((name == null) || "".equals(name))
724                             {
725                                 name = field.getName();
726                             }
727                             mpc.setPropertyName(name);
728                             mpc.setPropertyClass(field.getType().getName()); // FIXME - primitives, arrays, etc.
729                             mpc.setValue(property.value());
730                             mbc.addProperty(mpc);
731                             continue;
732                         }
733                     }
734                     runtimeConfig.addManagedBean(mbc.getManagedBeanName(), mbc);
735                 }        
736             }
737         }
738 
739         classes = map.get(NamedEvent.class);
740         if (classes != null && !classes.isEmpty())
741         {
742             for (Class<?> clazz : classes)
743             {
744                 NamedEvent namedEvent = (NamedEvent) clazz.getAnnotation (NamedEvent.class);
745         
746                 if (namedEvent != null) {
747                     // Can only apply @NamedEvent to ComponentSystemEvent subclasses.
748                     
749                     if (!ComponentSystemEvent.class.isAssignableFrom (clazz)) {
750                         // Just log this.  We'll catch it later in the runtime.
751                         
752                         if (log.isLoggable(Level.WARNING)) {
753                             log.warning (clazz.getName() + " is annotated with @javax.faces.event.NamedEvent, but does " +
754                                 "not extend javax.faces.event.ComponentSystemEvent");
755                         }
756                         
757                         return;
758                     }
759                     
760                     // Have to register @NamedEvent annotations with the NamedEventManager class since
761                     // we need to get access to this info later and can't from the dispenser (it's not a
762                     // singleton).
763                     
764                     NamedEventManager.getInstance().addNamedEvent (namedEvent.shortName(),
765                         (Class<? extends ComponentSystemEvent>) clazz);
766                 }
767             }
768         }
769     
770         classes = map.get(FacesBehavior.class);
771         if (classes != null && !classes.isEmpty())
772         {
773             for (Class<?> clazz : classes)
774             {
775                 FacesBehavior facesBehavior = (FacesBehavior) clazz.getAnnotation (FacesBehavior.class);
776         
777                 if (facesBehavior != null) {
778                     // Can only apply @FacesBehavior to Behavior implementors.
779                     
780                     if (!Behavior.class.isAssignableFrom (clazz)) {
781                         // Just log this.  We'll catch it later in the runtime.
782                         
783                         if (log.isLoggable(Level.WARNING)) {
784                             log.warning (clazz.getName() + " is annotated with @javax.faces.component.behavior.FacesBehavior, " +
785                                     "but does not implement javax.faces.component.behavior.Behavior");
786                         }
787                     }
788                     
789                     if (log.isLoggable(Level.FINEST)) {
790                         log.finest ("addBehavior(" + facesBehavior.value() + ", " + clazz.getName() + ")");
791                     }
792                     
793                     application.addBehavior (facesBehavior.value(), clazz.getName());
794                 }
795                 
796             }
797         }
798     
799         classes = map.get(FacesBehaviorRenderer.class);
800         if (classes != null && !classes.isEmpty())
801         {
802             for (Class<?> clazz : classes)
803             {
804                 FacesBehaviorRenderer facesBehaviorRenderer = (FacesBehaviorRenderer) clazz.getAnnotation (FacesBehaviorRenderer.class);
805         
806                 if (facesBehaviorRenderer != null) {
807                     String renderKitId = facesBehaviorRenderer.renderKitId();
808                     RenderKit renderKit;
809                     
810                     if (log.isLoggable(Level.FINEST)) {
811                         log.finest ("addClientBehaviorRenderer(" + renderKitId + ", " + facesBehaviorRenderer.rendererType() + ", " +
812                              clazz.getName() + ")");
813                     }
814                     
815                     try {
816                         ClientBehaviorRenderer clientBehaviorRenderer;
817                         
818                         renderKit = renderKitFactory().getRenderKit (null, renderKitId);
819                         
820                         clientBehaviorRenderer = (ClientBehaviorRenderer) clazz.newInstance();
821                         
822                         renderKit.addClientBehaviorRenderer(facesBehaviorRenderer.rendererType(), clientBehaviorRenderer);
823                     }
824                     
825                     catch (Throwable e) {
826                         throw new FacesException (e);
827                     }
828                 }
829             }
830         }
831         */
832         /*
833         ListenerFor listenerFor = (ListenerFor) clazz.getAnnotation(ListenerFor.class);
834         if (listenerFor != null)
835         {
836             processListenerFor(application, clazz, listenerFor);
837         }
838         
839         ListenersFor listenersFor = (ListenersFor) clazz.getAnnotation(ListenersFor.class);
840         if (listenersFor != null)
841         {
842             for (ListenerFor item : listenersFor.value())
843             {
844                 processListenerFor(application, clazz, item);
845             }
846         }*/
847         
848         // TODO: All annotations scanned at startup must be configured here!
849     /*
850     }
851     */
852 
853     /**
854      * <p>Return an array of all <code>Field</code>s reflecting declared
855      * fields in this class, or in any superclass other than
856      * <code>java.lang.Object</code>.</p>
857      *
858      * @param clazz Class to be analyzed
859      */
860     private Field[] fields(Class<?> clazz) {
861 
862         Map<String,Field> fields = new HashMap<String,Field>();
863         do {
864             for (Field field : clazz.getDeclaredFields()) {
865                 if (!fields.containsKey(field.getName())) {
866                     fields.put(field.getName(), field);
867                 }
868             }
869         } while ((clazz = clazz.getSuperclass()) != Object.class);
870         return (Field[]) fields.values().toArray(new Field[fields.size()]);
871 
872     }
873 
874     /**
875      * <p>Return the <code>RenderKitFactory</code> for this application.</p>
876      */
877     /*
878     private RenderKitFactory renderKitFactory()
879     {
880 
881         if (rkFactory == null)
882         {
883             rkFactory = (RenderKitFactory) FactoryFinder
884                     .getFactory(FactoryFinder.RENDER_KIT_FACTORY);
885         }
886         return rkFactory;
887 
888     }*/
889 }