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  
20  package org.apache.myfaces.cdi.model;
21  
22  import java.lang.reflect.Type;
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.AfterBeanDiscovery;
28  import javax.enterprise.inject.spi.AfterDeploymentValidation;
29  import javax.enterprise.inject.spi.Annotated;
30  import javax.enterprise.inject.spi.AnnotatedType;
31  import javax.enterprise.inject.spi.BeanManager;
32  import javax.enterprise.inject.spi.BeforeBeanDiscovery;
33  import javax.enterprise.inject.spi.Extension;
34  import javax.enterprise.inject.spi.ProcessManagedBean;
35  import javax.faces.model.DataModel;
36  import javax.faces.model.FacesDataModel;
37  import org.apache.myfaces.cdi.util.CDIUtils;
38  import org.apache.myfaces.shared.util.ClassUtils;
39  
40  /**
41   *
42   */
43  public class FacesDataModelExtension implements Extension
44  {
45      private Set<DataModelInfo> types = new HashSet<DataModelInfo>();
46  
47      void beforeBeanDiscovery(
48          @Observes final BeforeBeanDiscovery event, BeanManager beanManager)
49      {
50          AnnotatedType beanHolder = beanManager.createAnnotatedType(FacesDataModelClassBeanHolder.class);
51          event.addAnnotatedType(beanHolder, beanHolder.getJavaClass().getName());
52      }
53  
54      public <T> void collect(@Observes ProcessManagedBean<T> event)
55      {
56          if (event.getAnnotatedBeanClass().isAnnotationPresent(FacesDataModel.class))
57          {
58              Annotated annotated = event.getAnnotatedBeanClass();
59              
60              Type type = annotated.getBaseType();
61  
62              FacesDataModel conv = (FacesDataModel) annotated.getAnnotation(FacesDataModel.class);
63              
64              boolean hasValue = conv.forClass() != null;
65              if (hasValue)
66              {
67                  types.add(new DataModelInfo(type, conv.forClass()));
68              }
69          }
70      }
71      
72      public void afterBean(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager)
73      {
74          for (DataModelInfo typeInfo : types)
75          {
76              afterBeanDiscovery.addBean(new DynamicDataModelProducer(beanManager, typeInfo));
77          }
78      }
79      
80      public void afterDeploymentValidation(@Observes AfterDeploymentValidation adv, BeanManager beanManager)
81      {
82          FacesDataModelClassBeanHolder holder = CDIUtils.lookup(beanManager, FacesDataModelClassBeanHolder.class);
83          for (DataModelInfo typeInfo : types)
84          {
85              holder.addFacesDataModel(typeInfo.getForClass(), 
86                      ClassUtils.simpleClassForName(typeInfo.getType().getTypeName()));
87          }
88          // Initialize unmodifiable wrapper
89          Map<Class<?>,Class<? extends DataModel>> map = holder.getClassInstanceToDataModelWrapperClassMap();
90      }
91  
92  }