Coverage Report - org.apache.myfaces.context.FacesContextFactoryImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
FacesContextFactoryImpl
0%
0/36
0%
0/22
0
 
 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  0
     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  0
         super();
 57  0
         ThreadLocal<ExternalContext> firstExternalContextInstance = null;
 58  
         try
 59  
         {
 60  0
             Field externalContextFirstInstance = ExternalContext.class.getDeclaredField("_firstInstance");
 61  0
             externalContextFirstInstance.setAccessible(true);
 62  
             
 63  0
             if (externalContextFirstInstance != null)
 64  
             {
 65  0
                 if (firstExternalContextInstance == null)
 66  
                 {
 67  0
                     firstExternalContextInstance = 
 68  
                         (ThreadLocal<ExternalContext>) externalContextFirstInstance.get(null);
 69  
                 }
 70  
             }
 71  
         }
 72  0
         catch (SecurityException e)
 73  
         {
 74  
             // It could happen, but we can ignore it.
 75  0
             if (log.isDebugEnabled())
 76  0
                 log.debug("Cannot access private field _firstInstance from ExternalContext ",e);
 77  
         }
 78  0
         catch (Exception e)
 79  
         {
 80  
             //It should not happen if we have only myfaces on classpath
 81  0
             if (log.isDebugEnabled())
 82  0
                 log.debug("Cannot found private field _firstInstance from ExternalContext ",e);
 83  0
         }
 84  
         
 85  0
         _firstExternalContextInstance = firstExternalContextInstance;
 86  
                 
 87  0
     }
 88  
 
 89  
     public FacesContext getFacesContext(Object context,
 90  
                                         Object request,
 91  
                                         Object response,
 92  
                                         Lifecycle lifecycle)
 93  
             throws FacesException
 94  
     {
 95  0
         if (context == null) {
 96  0
             throw new NullPointerException("context");
 97  
         }
 98  0
         if (request == null) {
 99  0
             throw new NullPointerException("request");
 100  
         }
 101  0
         if (response == null) {
 102  0
             throw new NullPointerException("response");
 103  
         }
 104  0
         if (lifecycle == null) {
 105  0
             throw new NullPointerException("lifecycle");
 106  
         }
 107  
 
 108  0
         FacesContext facesContext = null;
 109  
         
 110  
         //We need this instance to be set later.
 111  0
         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  0
         facesContext = new FacesContextImpl(externalContext,this);
 127  
         
 128  0
         if (facesContext != null)
 129  
         {
 130  0
             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  0
                 _firstExternalContextInstance.set((ExternalContext) externalContext);
 135  
             }            
 136  0
             return facesContext;
 137  
         }
 138  
         
 139  0
         throw new FacesException("Unsupported context type " + context.getClass().getName());
 140  
     }
 141  
     
 142  
     public void release()
 143  
     {
 144  0
         if (_firstExternalContextInstance != null)
 145  
         {
 146  0
             _firstExternalContextInstance.remove();
 147  
         }
 148  0
     }    
 149  
 }