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.converter;
21  
22  import java.util.logging.Level;
23  import java.util.logging.Logger;
24  import javax.enterprise.context.Dependent;
25  import javax.enterprise.context.spi.CreationalContext;
26  import javax.enterprise.inject.Typed;
27  import javax.enterprise.inject.spi.BeanManager;
28  import javax.faces.FacesException;
29  import javax.faces.convert.Converter;
30  import org.apache.myfaces.cdi.util.AbstractDynamicProducer;
31  import org.apache.myfaces.shared.util.ClassUtils;
32  
33  /**
34   *
35   */
36  @Typed
37  public class FacesConverterProducer extends AbstractDynamicProducer<Converter>
38  {
39      public FacesConverterProducer(BeanManager beanManager, FacesConverterInfo typeInfo)
40      {
41          super(beanManager);
42          
43          String forClass = typeInfo.getForClass() == null ? "" : 
44                  ((typeInfo.getForClass() == Object.class) ? "" : typeInfo.getForClass().getName());
45          String converterId = typeInfo.getConverterId() == null ? "" : typeInfo.getConverterId();
46          String id = "" + typeInfo.getType() + "_" + forClass + "_" + converterId;
47  
48          FacesConverterAnnotationLiteral literal = new FacesConverterAnnotationLiteral(
49                          typeInfo.getForClass() == null ? Object.class : typeInfo.getForClass(), 
50                          typeInfo.getConverterId() == null ? "" : typeInfo.getConverterId(), true);
51  
52          super.id(id)
53                  .scope(Dependent.class)
54                  .qualifiers(literal)
55                  .types(typeInfo.getType(), Object.class)
56                  .beanClass(ClassUtils.simpleClassForName(typeInfo.getType().getTypeName()))
57                  .create(e -> createConverter(e));
58      }
59  
60      protected Converter createConverter(CreationalContext<Converter> cc)
61      {
62          Class<? extends Converter> converterClass = (Class<? extends Converter>) getBeanClass();        
63          Converter converter = null;
64          try
65          {
66              converter = converterClass.newInstance();
67          }
68          catch (Exception ex)
69          {
70              Logger.getLogger(FacesConverterProducer.class.getName()).log(
71                      Level.SEVERE, "Could not instantiate converter " + converterClass.getName(), ex);
72              throw new FacesException("Could not instantiate converter: " + converterClass.getName(), ex);
73              
74          }
75          return converter;
76      }
77  }