title: Understanding FacesContext instantiation details
body: <p>Mojarra 2.2.12</p>
  
  <p>Here is the piece of code taking over the instantiation of the <a href="http://grepcode.com/file/repo1.maven.org/maven2/javax.faces/jsf-api/2.1/javax/faces/webapp/FacesServlet.java" rel="nofollow">FacesContext</a>:</p>
  
  <pre><code>FacesContext context = facesContextFactory.getFacesContext
                (servletConfig.getServletContext(), request, response, lifecycle);
  </code></pre>
  
  <p>The expression is perfectly clear. Once we get a request, we acquire a global information from it and create the <code>FacesContext</code> instance using it. So, the instance is created for each request. But obtaining the intance of <code>facesContextFactory</code> seemed to me much more tricky. </p>
  
  <pre><code>// Acquire our FacesContextFactory instance
  try {
      facesContextFactory = (FacesContextFactory)
          FactoryFinder.getFactory
          (FactoryFinder.FACES_CONTEXT_FACTORY);
  } catch (FacesException e) {
      //others
  }
  </code></pre>
  
  <p>Where </p>
  
  <pre><code>String javax.faces.FactoryFinder.FACES_CONTEXT_FACTORY = "javax.faces.context.FacesContextFactory"
  </code></pre>
  
  <p>JavaDocs for the <code>FactoryFinder</code> describes the so called </p>
  
  <blockquote>
    <p>standard discovery algorithm for all factory objects specified in the
    JavaServer Faces APIs.</p>
  </blockquote>
  
  <p>This is what I was confused by.</p>
  
  <p>Now, Let's consider the the actual method that creates the factory instance: <a href="http://grepcode.com/file/repo1.maven.org/maven2/javax.faces/jsf-api/2.1/javax/faces/FactoryFinder.java#FactoryFinder.FactoryManager.getFactory%28java.lang.ClassLoader%2Cjava.lang.String%29" rel="nofollow">javax.faces.FactoryFinderInstance#getFactory(String factoryName)</a></p>
  
  <pre><code>try {
        factoryOrList = factories.get(factoryName);
        if (!(factoryOrList instanceof List)) {
            return factoryOrList;
        }
  } finally {
        lock.readLock().unlock();
  }
  </code></pre>
  
  <p>The <code>factories</code> field is initialized as follows <a href="http://grepcode.com/file/repo1.maven.org/maven2/com.sun.faces/jsf-api/2.2.8/javax/faces/FactoryFinderInstance.java#FactoryFinderInstance.copyInjectionProviderFromFacesContext%28%29" rel="nofollow">copyInjectionProviderFromFacesContext()</a>: </p>
  
  <pre><code>private void copyInjectionProviderFromFacesContext() {
      InjectionProvider injectionProvider = null;
      FacesContext context = FacesContext.getCurrentInstance(); //USE FACES CONTEXT!!!!!
      if (null != context) {
          injectionProvider = (InjectionProvider) context.getAttributes().get("com.sun.faces.config.ConfigManager_INJECTION_PROVIDER_TASK");
      }
      if (null != injectionProvider) {
          factories.put(INJECTION_PROVIDER_KEY, injectionProvider);
      } else {
          if (LOGGER.isLoggable(Level.SEVERE)) {
              LOGGER.log(Level.SEVERE, "Unable to obtain InjectionProvider from init time FacesContext. Does this container implement the Mojarra Injection SPI?");
          }
      }
  }
  </code></pre>
  
  <p>So, creating used <code>FacesContext</code> instance, but the factory itself is used for creating the <code>FacesContext</code>. Couldn't you explain that cycle?</p>