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.orchestra.lib.jsf;
20  
21  import java.lang.reflect.InvocationTargetException;
22  import java.lang.reflect.Method;
23  import java.util.Iterator;
24  
25  import javax.el.ELContext;
26  import javax.faces.application.Application;
27  import javax.faces.application.FacesMessage;
28  import javax.faces.component.UIViewRoot;
29  import javax.faces.context.ExternalContext;
30  import javax.faces.context.FacesContext;
31  import javax.faces.context.ResponseStream;
32  import javax.faces.context.ResponseWriter;
33  import javax.faces.render.RenderKit;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  
39  /**
40   * Convenient class to wrap the current FacesContext.
41   * <p>
42   * A class of this name is provided in JSF1.2, but not in JSF1.1.
43   * <p>
44   * Any methods that do not actually need to be overridden are declared final
45   * in order to improve performance (helps the JVM to optimise away the call).
46   * <p>
47   * Note that whether a newly-created instance immediately becomes the
48   * object that is returned by FacesContext.getCurrentInstance() depends
49   * upon the value of the "install" parameter for the constructor method. 
50   * <p>
51   * This class is copied from the code in MyFaces Core Impl 1.2.x, but
52   * modified to be compatible with JSF1.1.
53   * 
54   * @since 1.1
55   * 
56   * @author Manfred Geiler (latest modification by $Author: skitching $)
57   * @author Anton Koinov
58   * @version $Revision: 633423 $ $Date: 2008-03-04 12:02:45 +0100 (Tue, 04 Mar 2008) $
59   */
60  class FacesContextWrapper extends FacesContext
61  {
62      //~ Instance fields -------------------------------------------------------
63  
64      private final FacesContext _facesContext;
65      private Method methodGetELContext = null;
66  
67      //~ Constructors ----------------------------------------------------------
68  
69      /**
70       * The install parameter controls whether this object will be configured as
71       * the object returned from calls to FacesContext.getCurrentInstance() or not.
72       * <p>
73       * When only overriding the release() method, then install=false is ok as that
74       * is called directly by the FacesServlet on the instance returned by the
75       * FacesContextFactory. However all other methods are invoked on the object
76       * that is returned from FacesContext.getCurrentInstance, so install=true is
77       * needed in order for any other method overrides to have any effect.
78       * <p>
79       * <b>IMPORTANT</b>: install=true should not be used until MYFACES-1820 is fixed.
80       */
81      public FacesContextWrapper(FacesContext facesContext, boolean install)
82      {
83          _facesContext = facesContext;
84          
85          if (install)
86          {
87              FacesContext.setCurrentInstance(this);
88          }
89      }
90  
91      //~ Non-Final Methods -----------------------------------------------------
92  
93      public void release()
94      {
95          _facesContext.release();
96      }
97  
98      //~ Final Methods ---------------------------------------------------------
99  
100     public final Application getApplication()
101     {
102         return _facesContext.getApplication();
103     }
104 
105     public final Iterator getClientIdsWithMessages()
106     {
107         return _facesContext.getClientIdsWithMessages();
108     }
109 
110     public final ExternalContext getExternalContext()
111     {
112         return _facesContext.getExternalContext();
113     }
114 
115     public final FacesMessage.Severity getMaximumSeverity()
116     {
117         return _facesContext.getMaximumSeverity();
118     }
119 
120     public final Iterator getMessages()
121     {
122         return _facesContext.getMessages();
123     }
124 
125     public final Iterator getMessages(String clientId)
126     {
127         return _facesContext.getMessages(clientId);
128     }
129 
130     public final RenderKit getRenderKit()
131     {
132         return _facesContext.getRenderKit();
133     }
134 
135     public final boolean getRenderResponse()
136     {
137         return _facesContext.getRenderResponse();
138     }
139 
140     public final boolean getResponseComplete()
141     {
142         return _facesContext.getResponseComplete();
143     }
144 
145     public final void setResponseStream(ResponseStream responsestream)
146     {
147         _facesContext.setResponseStream(responsestream);
148     }
149 
150     public final ResponseStream getResponseStream()
151     {
152         return _facesContext.getResponseStream();
153     }
154 
155     public final void setResponseWriter(ResponseWriter responsewriter)
156     {
157         _facesContext.setResponseWriter(responsewriter);
158     }
159 
160     public final ResponseWriter getResponseWriter()
161     {
162         return _facesContext.getResponseWriter();
163     }
164 
165     public final void setViewRoot(UIViewRoot viewRoot)
166     {
167         _facesContext.setViewRoot(viewRoot);
168     }
169 
170     public final UIViewRoot getViewRoot()
171     {
172         return _facesContext.getViewRoot();
173     }
174 
175     public final void addMessage(String clientId, FacesMessage message)
176     {
177         _facesContext.addMessage(clientId, message);
178     }
179 
180     public final void renderResponse()
181     {
182         _facesContext.renderResponse();
183     }
184 
185     public final void responseComplete()
186     {
187         _facesContext.responseComplete();
188     }
189 
190     /**
191      * Implement getELContext by delegating call to another instance.
192      * <p>
193      * Note that this method was added in JSF1.2. In order for a JSF1.2
194      * implementation to be backwards-compatible with JSF1.1, the base
195      * class FacesContext therefore has to automatically do the delegation.
196      * Without automatic delegation, any JSF1.1 class that applies the decorator
197      * pattern to a FacesContext will just break in JSF1.2; the getELContext
198      * method is there (inherited from the base class) but does not correctly
199      * delegate.
200      * <p>
201      * Unfortunately, due to a design flaw in JSF1.2 it is simply not possible
202      * for the base class to delegate; the object to delegate to is not known
203      * to the base class! A partial solution that works in most cases is for
204      * the base class to delegate to the "core" instance of FacesContext for
205      * methods that are not overridden; Sun's RI does this correctly but
206      * unfortunately MyFaces 1.2.0-1.2.2 do not. See MYFACES-1820 for details.
207      * <p>
208      * The solution *here* is to require that a javax.el implementation is in
209      * the classpath even when running JSF1.1. It is then possible for this
210      * wrapper to override the method defined in JSF1.2 even when being 
211      * compiled against the JSF1.1 implementation. It is mildly annoying to
212      * have to include javax.el in a JSF environment (ie when it will never
213      * be used) but better than the alternatives. Actually, for at least some
214      * JVMs, classes needed by a method are not loaded unless that method is
215      * actually referenced, so in some cases (including Sun Java 1.4-1.6) the
216      * el library *can* be omitted from the classpath with JSF1.1.
217      */
218     public final ELContext getELContext()
219     {
220     	// Here, we cannot call getELContext on FacesContext as it does not
221     	// exist for JSF1.1; the solution is to use reflection instead. This
222     	// method will never be called unless we are in a JSF1.2 environment
223     	// so the target method will always exist when this is called.
224     	try
225     	{
226     		if (methodGetELContext == null)
227     		{
228     			// Performance optimisation: find method, and cache it for later.
229     			methodGetELContext = FacesContext.class.getDeclaredMethod("getELContext", (Class[]) null);
230     		}
231     		return (ELContext) methodGetELContext.invoke(_facesContext, (Object[]) null);
232     	}
233     	catch(NoSuchMethodException e)
234     	{
235     		// should never happen
236     		Log log = LogFactory.getLog(this.getClass());
237     		log.error("JSF1.2 method invoked in non-JSF-1.2 environment", e);
238     		throw new IllegalStateException("JSF1.2 method invoked in non-JSF-1.2 environment");
239     	}
240     	catch(InvocationTargetException e)
241     	{
242     		// should never happen
243     		Log log = LogFactory.getLog(this.getClass());
244     		log.error("Method getELContext on wrapped instance threw exception", e);
245     		throw new IllegalStateException("Method getELContext on wrapped instance threw exception");
246     	}
247     	catch(IllegalAccessException e)
248     	{
249     		// should never happen
250     		Log log = LogFactory.getLog(this.getClass());
251     		log.error("Method getElContext on wrapped instance is not accessable", e);
252     		throw new IllegalStateException("Method getElContext on wrapped instance is not accessable");
253     	}
254     }
255 }