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  package org.apache.myfaces.trinidad.bean;
20  
21  import java.io.Serializable;
22  
23  import javax.el.ELException;
24  import javax.el.ValueExpression;
25  
26  import javax.faces.component.StateHolder;
27  import javax.faces.context.FacesContext;
28  import javax.faces.el.EvaluationException;
29  import javax.faces.el.ValueBinding;
30  
31  public class ValueExpressionValueBinding extends ValueBinding
32  {
33    /**
34     * Given a ValueExpression <code>ve</code>, return a ValueBinding.
35     * The returned ValueBinding will implement StateHolder and Serializable interfaces if
36     * <code>ve</code> implements these interfaces.
37     * @param ve  The ValueExpression
38     * @return a ValueBinding equivalent to the ValueExpression
39     */
40    public static ValueBinding getValueBinding(ValueExpression ve)
41    {
42      // if we previously wrapped a ValueBinding, unwrap it and return it, otherwise create the
43      // correct subclass of ValueBinding
44      if (ve instanceof ValueBindingValueExpression)
45        return ((ValueBindingValueExpression)ve).getValueBinding();
46      else if (ve instanceof StateHolder)
47      {
48        if (ve instanceof Serializable)
49          return new SerializableStateHolderValueExpressionValueBinding(ve);
50        else
51          return new StateHolderValueExpressionValueBinding(ve);      
52      }
53      else if (ve instanceof Serializable)
54      {
55        return new SerializableValueExpressionValueBinding(ve);
56      }
57      else
58      {
59        return new ValueExpressionValueBinding(ve);      
60      }    
61    }
62    
63    @SuppressWarnings("deprecation")
64    private ValueExpressionValueBinding(ValueExpression ve)
65    {
66      if (ve == null)
67        throw new NullPointerException();
68      
69      _ve = ve;
70    }
71  
72    public ValueExpression getValueExpression()
73    {
74      return _ve;
75    }
76    
77    @SuppressWarnings("deprecation")
78    public Object getValue(FacesContext facesContext)
79    {
80      try
81      {
82        return _ve.getValue(facesContext.getELContext());
83      }
84      // Convert EL exceptions into EvaluationExceptions
85      catch (ELException ee)
86      {
87        throw new EvaluationException(ee.getMessage(), ee.getCause());
88      }    
89    }
90  
91    @SuppressWarnings("deprecation")
92    public void setValue(FacesContext facesContext, Object object)
93    {
94      try
95      {
96        _ve.setValue(facesContext.getELContext(), object);
97      }
98      // Convert EL exceptions into EvaluationExceptions
99      catch (ELException ee)
100     {
101       throw new EvaluationException(ee.getMessage(), ee.getCause());
102     }    
103   }
104 
105   @SuppressWarnings("deprecation")
106   public boolean isReadOnly(FacesContext facesContext)
107   {
108     try
109     {
110       return _ve.isReadOnly(facesContext.getELContext());
111     }
112     // Convert EL exceptions into EvaluationExceptions
113     catch (ELException ee)
114     {
115       throw new EvaluationException(ee.getMessage(), ee.getCause());
116     }    
117   }
118 
119   @SuppressWarnings("deprecation")
120   public Class getType(FacesContext facesContext)
121   {
122     try
123     {
124       return _ve.getType(facesContext.getELContext());
125     }
126     // Convert EL exceptions into EvaluationExceptions
127     catch (ELException ee)
128     {
129       throw new EvaluationException(ee.getMessage(), ee.getCause());
130     }
131   }
132 
133   public boolean equals(Object o)
134   {
135     if (o == this)
136       return true;
137     
138     if (!(o instanceof ValueExpressionValueBinding))
139       return false;
140       
141     ValueExpressionValueBinding that = (ValueExpressionValueBinding) o;
142     return that._ve.equals(_ve);
143   }
144 
145   public int hashCode()
146   {
147     return _ve.hashCode();
148   }
149   
150   public String toString()
151   {
152     return super.toString() + ", expression=" + _ve;
153   }
154 
155   private static class SerializableValueExpressionValueBinding extends ValueExpressionValueBinding
156                                                                implements Serializable                                                 
157   {
158     public SerializableValueExpressionValueBinding(ValueExpression ve)
159     {
160       super(ve);
161     }
162   }
163   
164   private static class StateHolderValueExpressionValueBinding extends ValueExpressionValueBinding
165                                                               implements StateHolder
166   {
167     public StateHolderValueExpressionValueBinding(ValueExpression ve)
168     {
169       super(ve);
170       _stateHolder = (StateHolder)ve;
171     }
172     
173     public Object saveState(FacesContext facesContext)
174     {
175       return _stateHolder.saveState(facesContext);
176     }
177 
178     public void restoreState(FacesContext facesContext, Object object)
179     {
180       _stateHolder.restoreState(facesContext, object);
181     }
182 
183     public boolean isTransient()
184     {
185       return _stateHolder.isTransient();
186     }
187 
188     public void setTransient(boolean b)
189     {
190       _stateHolder.setTransient(b);
191     }
192     
193     private final StateHolder _stateHolder;
194   }
195   
196   private static class SerializableStateHolderValueExpressionValueBinding extends 
197                                                              StateHolderValueExpressionValueBinding
198                                                               implements Serializable
199   {
200     public SerializableStateHolderValueExpressionValueBinding(ValueExpression ve)
201     {
202       super(ve);
203     }
204   }
205 
206   private final ValueExpression _ve;
207 }