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.util.HashMap;
23  import java.util.HashSet;
24  import java.util.Map;
25  import java.util.Set;
26  import javax.enterprise.event.Observes;
27  import javax.enterprise.inject.spi.AnnotatedType;
28  import javax.enterprise.inject.spi.Extension;
29  import javax.enterprise.inject.spi.ProcessAnnotatedType;
30  import javax.faces.bean.ManagedBean;
31  import javax.faces.component.FacesComponent;
32  import javax.faces.component.behavior.FacesBehavior;
33  import javax.faces.convert.FacesConverter;
34  import javax.faces.render.FacesBehaviorRenderer;
35  import javax.faces.render.FacesRenderer;
36  import javax.faces.validator.FacesValidator;
37  import javax.faces.view.facelets.FaceletsResourceResolver;
38  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
39  import org.apache.myfaces.config.element.NamedEvent;
40  
41  public class CdiAnnotationProviderExtension implements Extension
42  {
43      /**
44       * Defines if CDI should be used for annotation scanning to improve the startup performance.
45       */
46      @JSFWebConfigParam(since="2.2.9")
47      public static final String USE_CDI_FOR_ANNOTATION_SCANNING
48              = "org.apache.myfaces.annotation.USE_CDI_FOR_ANNOTATION_SCANNING";
49  
50      private final Map<Class<? extends Annotation>, Set<Class<?>>> map;
51      private final Class<? extends Annotation>[] annotationsToScan;
52  
53      public CdiAnnotationProviderExtension()
54      {
55          map = new HashMap<Class<? extends Annotation>, Set<Class<?>>>();
56          annotationsToScan = new Class[] {
57              FacesComponent.class,
58              FacesBehavior.class,
59              FacesConverter.class,
60              FacesValidator.class,
61              FacesRenderer.class,
62              ManagedBean.class,
63              NamedEvent.class,
64              FacesBehaviorRenderer.class,
65              FaceletsResourceResolver.class
66          };
67      }
68  
69      <T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> pat)
70      {
71          AnnotatedType<T> type = pat.getAnnotatedType();
72  
73          for (Class<? extends Annotation> annotation : annotationsToScan)
74          {
75              if (type.isAnnotationPresent(annotation))
76              {
77                  Set<Class<?>> set = map.get(annotation);
78                  if (set == null)
79                  {
80                      set = new HashSet<Class<?>>();
81                      map.put(annotation, set);
82                  }
83  
84                  set.add(type.getJavaClass());
85              }
86          }
87      }
88  
89      public Map<Class<? extends Annotation>, Set<Class<?>>> getMap()
90      {
91          return map;
92      }
93  }