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.view.facelets.el;
20  
21  import java.io.Externalizable;
22  import java.io.IOException;
23  import java.io.ObjectInput;
24  import java.io.ObjectOutput;
25  
26  import javax.el.ELContext;
27  import javax.el.ValueExpression;
28  import javax.faces.FacesWrapper;
29  import javax.faces.context.FacesContext;
30  import javax.faces.view.Location;
31  
32  /**
33   * A ValueExpression that contains the original ValueExpression and
34   * the Location of the facelet file from which the ValueExpression was
35   * created. This is needed when the current composite component (cc) 
36   * has to be resolved by the ValueExpression, because #{cc} refers to the
37   * composite component which is implemented in the file the ValueExpression
38   * comes from and not the one currently on top of the composite component stack.
39   * 
40   * This ValueExpression implementation passes through all methods to the delegate
41   * ValueExpression, but saves the related composite component in a FacesContext attribute 
42   * before the invocation of the method on the delegate and removes it afterwards. 
43   * 
44   * @author Jakob Korherr (latest modification by $Author: lu4242 $)
45   * @version $Revision: 1142032 $ $Date: 2011-07-01 14:14:25 -0500 (Fri, 01 Jul 2011) $
46   */
47  public class LocationValueExpression extends ValueExpression
48      implements FacesWrapper<ValueExpression>, Externalizable
49  {
50      
51      private static final long serialVersionUID = -5636849184764526288L;
52      
53      // location and delegate need to be available in LocationValueExpressionUEL
54      Location location;
55      ValueExpression delegate;
56      
57      public LocationValueExpression()
58      {
59          super();
60      }
61      
62      public LocationValueExpression(Location location, ValueExpression delegate)
63      {
64          this.location = location;
65          this.delegate = delegate;
66      }
67      
68      public Location getLocation()
69      {
70          return location;
71      }
72      
73      @Override
74      public Class<?> getExpectedType()
75      {
76          return delegate.getExpectedType();
77      }
78  
79      @Override
80      public Class<?> getType(ELContext context)
81      {
82          FacesContext facesContext = (FacesContext) context.getContext(FacesContext.class);
83          CompositeComponentELUtils.saveCompositeComponentForResolver(facesContext, location);
84          try
85          {
86              return delegate.getType(context);
87          }
88          finally
89          {
90              CompositeComponentELUtils.removeCompositeComponentForResolver(facesContext);
91          }
92      }
93  
94      @Override
95      public Object getValue(ELContext context)
96      {
97          FacesContext facesContext = (FacesContext) context.getContext(FacesContext.class);
98          CompositeComponentELUtils.saveCompositeComponentForResolver(facesContext, location);
99          try
100         {
101             return delegate.getValue(context);
102         }
103         finally
104         {
105             CompositeComponentELUtils.removeCompositeComponentForResolver(facesContext);
106         }
107     }
108 
109     @Override
110     public boolean isReadOnly(ELContext context)
111     {
112         FacesContext facesContext = (FacesContext) context.getContext(FacesContext.class);
113         CompositeComponentELUtils.saveCompositeComponentForResolver(facesContext, location);
114         try
115         {
116             return delegate.isReadOnly(context);
117         }
118         finally
119         {
120             CompositeComponentELUtils.removeCompositeComponentForResolver(facesContext);
121         }
122     }
123 
124     @Override
125     public void setValue(ELContext context, Object value)
126     {
127         FacesContext facesContext = (FacesContext) context.getContext(FacesContext.class);
128         CompositeComponentELUtils.saveCompositeComponentForResolver(facesContext, location);
129         try
130         {
131             delegate.setValue(context, value);
132         }
133         finally
134         {
135             CompositeComponentELUtils.removeCompositeComponentForResolver(facesContext);
136         }
137     }
138 
139     @Override
140     public boolean equals(Object obj)
141     {
142         return delegate.equals(obj);
143     }
144 
145     @Override
146     public String getExpressionString()
147     {
148         return delegate.getExpressionString();
149     }
150 
151     @Override
152     public int hashCode()
153     {
154         return delegate.hashCode();
155     }
156 
157     @Override
158     public boolean isLiteralText()
159     {
160         return delegate.isLiteralText();
161     }
162 
163     public ValueExpression getWrapped()
164     {
165         return delegate;
166     }
167     
168     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
169     {
170         this.delegate = (ValueExpression) in.readObject();
171         this.location = (Location) in.readObject();
172     }
173 
174     public void writeExternal(ObjectOutput out) throws IOException
175     {
176         out.writeObject(this.delegate);
177         out.writeObject(this.location);
178     }
179 }