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.util.Map;
23  import javax.enterprise.context.Dependent;
24  import javax.enterprise.context.spi.CreationalContext;
25  import javax.enterprise.inject.Typed;
26  import javax.enterprise.inject.spi.BeanManager;
27  import javax.faces.model.DataModel;
28  import org.apache.myfaces.cdi.util.AbstractDynamicProducer;
29  import org.apache.myfaces.cdi.util.CDIUtils;
30  
31  /**
32   *
33   */
34  @Typed
35  public class DynamicDataModelProducer extends AbstractDynamicProducer<Map<Class<?>, Class<? extends DataModel>>>
36  {
37      public DynamicDataModelProducer(BeanManager beanManager, DataModelInfo typeInfo)
38      {
39          super(beanManager);
40          
41          FacesDataModelAnnotationLiteral literal = new FacesDataModelAnnotationLiteral(typeInfo.getForClass());
42  
43          super.id("" + typeInfo.getForClass())
44                  .scope(Dependent.class)
45                  .qualifiers(literal)
46                  .types(typeInfo.getType(), Object.class)
47                  .beanClass(Map.class)
48                  .create(e -> createDataModel(e));
49      }
50  
51      protected Map<Class<?>,Class<? extends DataModel>> createDataModel(
52              CreationalContext<Map<Class<?>,Class<? extends DataModel>>> cc)
53      {
54          FacesDataModelClassBeanHolder holder = CDIUtils.lookup(getBeanManager(), FacesDataModelClassBeanHolder.class);
55          return holder.getClassInstanceToDataModelWrapperClassMap();
56      }
57  }