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.orchestra.conversation.jsf.components.facelets;
21  
22  import java.io.IOException;
23  
24  import javax.el.ELException;
25  import javax.faces.FacesException;
26  import javax.faces.application.Application;
27  import javax.faces.component.EditableValueHolder;
28  import javax.faces.component.UIComponent;
29  import javax.faces.context.FacesContext;
30  import javax.faces.convert.Converter;
31  import javax.faces.view.facelets.FaceletContext;
32  import javax.faces.view.facelets.FaceletException;
33  import javax.faces.view.facelets.TagAttribute;
34  import javax.faces.view.facelets.TagConfig;
35  import javax.faces.view.facelets.TagHandler;
36  
37  import org.apache.myfaces.orchestra.lib.jsf.SerializableConverter;
38  
39  public class ConverterTagHandler extends TagHandler
40  {
41      private final TagAttribute beanName;
42      private final TagAttribute useWrapper;
43  
44      public ConverterTagHandler(TagConfig config)
45      {
46          super(config);
47          beanName = getRequiredAttribute("beanName"); // NON-NLS
48          useWrapper = getAttribute("useWrapper"); // NON-NLS
49      }
50  
51      public void apply(FaceletContext faceletContext, UIComponent parent)
52      throws IOException, FacesException, FaceletException, ELException
53      {
54          if (parent.getParent() == null)
55          {
56              if (parent instanceof EditableValueHolder)
57              {
58                  Converter converter = createConverter(beanName.getValue());
59  
60                  if (useWrapper == null || !"false".equals(useWrapper.getValue()) &&
61                      !(converter instanceof SerializableConverter))
62                  {
63                      // Needed to check if it is already of the specified type in case the
64                      // managed-bean framework has been configured to auto-wrap Converter
65                      // instances already (eg via a Spring BeanPostProcessor or equivalent).
66                      // This isn't the case, so wrap it now.
67                      converter = new SerializableConverter(beanName.getValue(), converter);
68                  }
69  
70                  ((EditableValueHolder) parent).setConverter(converter);
71              }
72              else
73              {
74                  throw new FacesException("parent is not an EditableValueHolder");
75              }
76          }
77      }
78  
79      /**
80       * Override this method in order to customise the bean instance.
81       */
82      protected static Converter createConverter(String beanName)
83      {
84          FacesContext facesContext = FacesContext.getCurrentInstance();
85          Application application = facesContext.getApplication();
86          Object converter = application.getVariableResolver().resolveVariable(facesContext, beanName);
87          return (Converter) converter;
88      }
89  }