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.context;
20  
21  import java.lang.reflect.Field;
22  
23  import javax.faces.FacesException;
24  import javax.faces.context.ExternalContext;
25  import javax.faces.context.FacesContext;
26  import javax.faces.context.FacesContextFactory;
27  import javax.faces.lifecycle.Lifecycle;
28  import javax.servlet.ServletContext;
29  import javax.servlet.ServletRequest;
30  import javax.servlet.ServletResponse;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.myfaces.context.servlet.FacesContextImpl;
35  import org.apache.myfaces.context.servlet.ServletExternalContextImpl;
36  
37  /**
38   * DOCUMENT ME!
39   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
40   * @version $Revision: 827879 $ $Date: 2009-10-20 22:10:03 -0500 (Tue, 20 Oct 2009) $
41   */
42  public class FacesContextFactoryImpl
43          extends FacesContextFactory implements ReleaseableFacesContextFactory
44  {
45      private static final Log log = LogFactory.getLog(FacesContextFactoryImpl.class);
46      
47      /**
48       * This var is assigned as the same as javax.faces.context.ExternalContext._firstInstance,
49       * and since it is a static reference and does not change, we can cache it here safely.
50       */
51      private final ThreadLocal<ExternalContext> _firstExternalContextInstance;
52  
53      @SuppressWarnings("unchecked")
54      public FacesContextFactoryImpl()
55      {
56          super();
57          ThreadLocal<ExternalContext> firstExternalContextInstance = null;
58          try
59          {
60              Field externalContextFirstInstance = ExternalContext.class.getDeclaredField("_firstInstance");
61              externalContextFirstInstance.setAccessible(true);
62              
63              if (externalContextFirstInstance != null)
64              {
65                  if (firstExternalContextInstance == null)
66                  {
67                      firstExternalContextInstance = 
68                          (ThreadLocal<ExternalContext>) externalContextFirstInstance.get(null);
69                  }
70              }
71          }
72          catch (SecurityException e)
73          {
74              // It could happen, but we can ignore it.
75              if (log.isDebugEnabled())
76                  log.debug("Cannot access private field _firstInstance from ExternalContext ",e);
77          }
78          catch (Exception e)
79          {
80              //It should not happen if we have only myfaces on classpath
81              if (log.isDebugEnabled())
82                  log.debug("Cannot found private field _firstInstance from ExternalContext ",e);
83          }
84          
85          _firstExternalContextInstance = firstExternalContextInstance;
86                  
87      }
88  
89      public FacesContext getFacesContext(Object context,
90                                          Object request,
91                                          Object response,
92                                          Lifecycle lifecycle)
93              throws FacesException
94      {
95          if (context == null) {
96              throw new NullPointerException("context");
97          }
98          if (request == null) {
99              throw new NullPointerException("request");
100         }
101         if (response == null) {
102             throw new NullPointerException("response");
103         }
104         if (lifecycle == null) {
105             throw new NullPointerException("lifecycle");
106         }
107 
108         FacesContext facesContext = null;
109         
110         //We need this instance to be set later.
111         ReleaseableExternalContext externalContext = new ServletExternalContextImpl((ServletContext)context,
112                     (ServletRequest) request, (ServletResponse) response);
113         
114         // MyfacesGenericPortlet was replaced by jsr 301 portlet bridge,
115         // and the bundled inside myfaces 1.2 sources does not work because
116         // there was changes from 1.1 to 1.2 (viewhandler), so we 
117         // can comment this code.
118         // Note all portlet code was removed in version 2.0, but it is
119         // not necessary to remove from this branch.
120         // else if (context instanceof PortletContext)
121         //{
122         //    externalContext = new PortletExternalContextImpl(context,
123         //            request, response);
124         //}
125         
126         facesContext = new FacesContextImpl(externalContext,this);
127         
128         if (facesContext != null)
129         {
130             if (_firstExternalContextInstance != null)
131             {
132                 // Initialize the firstExternalContext that old jsf 1.1 of 
133                 // ExternalContext should fall when call jsf 1.2 methods.
134                 _firstExternalContextInstance.set((ExternalContext) externalContext);
135             }            
136             return facesContext;
137         }
138         
139         throw new FacesException("Unsupported context type " + context.getClass().getName());
140     }
141     
142     public void release()
143     {
144         if (_firstExternalContextInstance != null)
145         {
146             _firstExternalContextInstance.remove();
147         }
148     }    
149 }