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.managedproperty;
21  
22  import java.lang.reflect.ParameterizedType;
23  import javax.enterprise.context.Dependent;
24  import javax.enterprise.context.spi.CreationalContext;
25  import javax.enterprise.inject.spi.BeanManager;
26  import javax.enterprise.util.AnnotationLiteral;
27  import javax.faces.FacesException;
28  import javax.faces.annotation.ManagedProperty;
29  import javax.faces.context.FacesContext;
30  import org.apache.myfaces.cdi.util.AbstractDynamicProducer;
31  import org.apache.myfaces.shared.util.ClassUtils;
32  
33  /**
34   *
35   */
36  public class ManagedPropertyProducer extends AbstractDynamicProducer<Object>
37  {
38      class ManagedPropertyAnnotationLiteral extends AnnotationLiteral<ManagedProperty> implements ManagedProperty
39      {
40          private static final long serialVersionUID = 1L;
41          
42          private String value;
43  
44          public ManagedPropertyAnnotationLiteral(String value)
45          {
46              this.value = value;
47          }
48  
49          @Override
50          public String value()
51          {
52              return value;
53          }
54      }
55      
56      public ManagedPropertyProducer(BeanManager beanManager, ManagedPropertyInfo typeInfo)
57      {
58          super(beanManager);
59          
60          Class<?> beanClass;
61          // Need to check for ParameterizedType to support types such as List<String>
62          if (typeInfo.getType() instanceof ParameterizedType) 
63          {
64              beanClass = ClassUtils.simpleClassForName(
65                      ((ParameterizedType) typeInfo.getType()).getRawType().getTypeName());
66          }
67          else 
68          {
69              // need to use simpleJavaTypeToClass to support Arrays and primitive types
70              beanClass = ClassUtils.simpleJavaTypeToClass(typeInfo.getType().getTypeName());
71          }
72  
73          super.id(typeInfo.getType() + "_" + typeInfo.getExpression())
74                  .scope(Dependent.class)
75                  .qualifiers(new ManagedPropertyAnnotationLiteral(typeInfo.getExpression()))
76                  .types(typeInfo.getType(), Object.class)
77                  .beanClass(beanClass)
78                  .create(e -> createManagedProperty(e, typeInfo));
79      }
80  
81      protected Object createManagedProperty(CreationalContext<Object> cc, ManagedPropertyInfo typeInfo)
82      {
83          FacesContext facesContext = FacesContext.getCurrentInstance();
84          if (facesContext == null)
85          {
86              throw new FacesException("@ManagedProperty(\"" + typeInfo.getExpression()
87                      + "\") can only be resolved in a JSF request!");
88          }
89          
90          return facesContext.getApplication().evaluateExpressionGet(
91                  facesContext, typeInfo.getExpression(), getBeanClass());
92      }
93  }