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