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.MethodExpression;
28  import javax.el.MethodInfo;
29  import javax.el.ValueExpression;
30  import javax.faces.FacesWrapper;
31  import javax.faces.context.FacesContext;
32  import javax.faces.view.Location;
33  
34  /**
35   * A MethodExpression that contains the original MethodExpression and
36   * the Location of the facelet file from which the MethodExpression was
37   * created. This is needed when the current composite component (cc) 
38   * has to be resolved by the MethodExpression, because #{cc} refers to the
39   * composite component which is implemented in the file the MethodExpression
40   * comes from and not the one currently on top of the composite component stack.
41   * 
42   * This MethodExpression implementation passes through all methods to the delegate
43   * MethodExpression, but saves the related composite component in a FacesContext attribute 
44   * before the invocation of the method on the delegate and removes it afterwards.
45   * 
46   * @author Jakob Korherr (latest modification by $Author: lu4242 $)
47   * @version $Revision: 1150175 $ $Date: 2011-07-23 12:37:25 -0500 (Sat, 23 Jul 2011) $
48   */
49  public class LocationMethodExpression extends MethodExpression 
50      implements FacesWrapper<MethodExpression>, Externalizable, LocationAware
51  {
52  
53      private static final long serialVersionUID = 1634644578979226893L;
54      
55      private Location location;
56      private MethodExpression delegate;
57      
58      public LocationMethodExpression()
59      {
60          super();
61      }
62      
63      public LocationMethodExpression(Location location, MethodExpression delegate)
64      {
65          this.location = location;
66          this.delegate = delegate;
67      }
68      
69      public Location getLocation()
70      {
71          return location;
72      }
73      
74      @Override
75      public MethodInfo getMethodInfo(ELContext context)
76      {
77          FacesContext facesContext = (FacesContext) context.getContext(FacesContext.class);
78          CompositeComponentELUtils.saveCompositeComponentForResolver(facesContext, location);
79          try
80          {
81              return delegate.getMethodInfo(context);
82          }
83          finally
84          {
85              CompositeComponentELUtils.removeCompositeComponentForResolver(facesContext);
86          }
87      }
88  
89      @Override
90      public Object invoke(ELContext context, Object[] params)
91      {
92          FacesContext facesContext = (FacesContext) context.getContext(FacesContext.class);
93          CompositeComponentELUtils.saveCompositeComponentForResolver(facesContext, location);
94          try
95          {
96              return delegate.invoke(context, params);
97          }
98          finally
99          {
100             CompositeComponentELUtils.removeCompositeComponentForResolver(facesContext);
101         }
102     }
103 
104     @Override
105     public boolean equals(Object obj)
106     {
107         return delegate.equals(obj);
108     }
109 
110     @Override
111     public String getExpressionString()
112     {
113         return delegate.getExpressionString();
114     }
115 
116     @Override
117     public int hashCode()
118     {
119         return delegate.hashCode();
120     }
121 
122     @Override
123     public boolean isLiteralText()
124     {
125         return delegate.isLiteralText();
126     }
127 
128     public MethodExpression getWrapped()
129     {
130         return delegate;
131     }
132     
133     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
134     {
135         this.delegate = (MethodExpression) in.readObject();
136         this.location = (Location) in.readObject();
137     }
138 
139     public void writeExternal(ObjectOutput out) throws IOException
140     {
141         out.writeObject(this.delegate);
142         out.writeObject(this.location);
143     }
144 
145 }