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.lang.reflect.Type;
23  
24  import javax.enterprise.inject.spi.BeanManager;
25  import javax.enterprise.util.TypeLiteral;
26  import javax.faces.FacesWrapper;
27  import javax.faces.component.PartialStateHolder;
28  import javax.faces.component.UIComponent;
29  import javax.faces.context.FacesContext;
30  import javax.faces.convert.Converter;
31  import javax.faces.convert.ConverterException;
32  import org.apache.myfaces.cdi.util.CDIUtils;
33  
34  /**
35   *
36   */
37  public class FacesConverterCDIWrapper implements PartialStateHolder, Converter, FacesWrapper<Converter>
38  {
39      private transient Converter delegate;
40      
41      //private Class<? extends Converter> converterClass;
42      private Class<?> forClass;
43      private String converterId;
44      private boolean _transient;
45      private static final Type CONVERTER_TYPE = new TypeLiteral<Converter<?>>() { 
46          private static final long serialVersionUID = 1L; 
47      }.getType(); 
48  
49      public FacesConverterCDIWrapper()
50      {
51      }
52  
53      public FacesConverterCDIWrapper(Class<? extends Converter> converterClass, Class<?> forClass, String converterId)
54      {
55          //this.converterClass = converterClass;
56          this.forClass = forClass;
57          this.converterId = converterId;
58      }
59  
60      @Override
61      public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException
62      {
63          return getWrapped().getAsObject(context, component, value);
64      }
65  
66      @Override
67      public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException
68      {
69          return getWrapped().getAsString(context, component, value);
70      }
71  
72      @Override
73      public Converter getWrapped()
74      {
75          if (delegate == null)
76          {
77  
78              BeanManager  beanManager = CDIUtils.getBeanManager(FacesContext.getCurrentInstance().getExternalContext());
79              FacesConverterAnnotationLiteral qualifier;
80  
81              if (converterId != null)
82              {
83                  qualifier = new FacesConverterAnnotationLiteral(Object.class, converterId, true);
84                  delegate = (Converter) CDIUtils.getInstance(beanManager, CONVERTER_TYPE, true, qualifier);
85  
86                  if( delegate == null )
87                  {
88                      delegate = (Converter) CDIUtils.getInstance(beanManager, Converter.class, true, qualifier);
89                  }
90              }
91              else if (forClass != null)
92              {
93                  qualifier = new FacesConverterAnnotationLiteral(forClass, "", true);
94                  delegate = (Converter) CDIUtils.getInstance(beanManager, CONVERTER_TYPE, true, qualifier);
95  
96                  if( delegate == null )
97                  {  
98                      delegate = (Converter) CDIUtils.getInstance(beanManager, Converter.class, true, qualifier);
99                  }
100             }
101         }
102         return delegate;
103     }
104     
105     @Override
106     public Object saveState(FacesContext context)
107     {
108         if (!initialStateMarked())
109         {
110             Object values[] = new Object[2];
111             //values[0] = converterClass;
112             values[0] = forClass;
113             values[1] = converterId;
114             return values;
115         }
116         return null;
117     }
118 
119     @Override
120     public void restoreState(FacesContext context, Object state)
121     {
122         if (state != null)
123         {
124             Object values[] = (Object[])state;
125             //converterClass = (Class)values[0];            
126             forClass = (Class)values[0];
127             converterId = (String)values[1];
128         }
129     }
130 
131     @Override
132     public boolean isTransient()
133     {
134         return _transient;
135     }
136 
137     @Override
138     public void setTransient(boolean newTransientValue)
139     {
140         _transient = newTransientValue;
141     }
142 
143     private boolean _initialStateMarked = false;
144 
145     public void clearInitialState()
146     {
147         _initialStateMarked = false;
148     }
149 
150     public boolean initialStateMarked()
151     {
152         return _initialStateMarked;
153     }
154 
155     public void markInitialState()
156     {
157         _initialStateMarked = true;
158     }
159 }