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) throws IOException, FacesException, FaceletException, ELException
51  	{
52  		if (parent.getParent() == null)
53  		{
54  			if (parent instanceof EditableValueHolder)
55  			{
56  				Converter converter = createConverter(beanName.getValue());
57  
58  				if (useWrapper == null || !"false".equals(useWrapper.getValue()) &&
59  					!(converter instanceof SerializableConverter))
60  				{
61  					// Needed to check if it is already of the specified type in case the
62  					// managed-bean framework has been configured to auto-wrap Converter
63  					// instances already (eg via a Spring BeanPostProcessor or equivalent).
64  					// This isn't the case, so wrap it now.
65  					converter = new SerializableConverter(beanName.getValue(), converter);
66  				}
67  
68  				((EditableValueHolder) parent).setConverter(converter);
69  			}
70  			else
71  			{
72  				throw new FacesException("parent is not an EditableValueHolder");
73  			}
74  		}
75  	}
76  
77  	/**
78  	 * Override this method in order to customise the bean instance.
79  	 */
80  	protected static Converter createConverter(String beanName)
81  	{
82  		FacesContext facesContext = FacesContext.getCurrentInstance();
83  		Application application = facesContext.getApplication();
84  		Object converter = application.getVariableResolver().resolveVariable(facesContext, beanName);
85  		return (Converter) converter;
86  	}
87  }