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