[seam-commits] Seam SVN: r8195 - in trunk: ui/src/main/java/org/jboss/seam/ui/facelet and 1 other directory.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Thu May 15 09:25:37 EDT 2008


Author: pete.muir at jboss.org
Date: 2008-05-15 09:25:37 -0400 (Thu, 15 May 2008)
New Revision: 8195

Added:
   trunk/src/main/org/jboss/seam/mock/MockFacesContextFactory.java
   trunk/src/main/org/jboss/seam/mock/MockLifecycleFactory.java
   trunk/ui/src/main/java/org/jboss/seam/ui/facelet/MockHttpSessionManager.java
   trunk/ui/src/main/java/org/jboss/seam/ui/facelet/MockServletContextManager.java
   trunk/ui/src/main/java/org/jboss/seam/ui/facelet/RendererFacesContextFactory.java
   trunk/ui/src/main/java/org/jboss/seam/ui/facelet/RendererRequest.java
Modified:
   trunk/src/main/org/jboss/seam/mock/AbstractSeamTest.java
   trunk/src/main/org/jboss/seam/mock/MockFacesContext.java
   trunk/src/main/org/jboss/seam/mock/MockHttpServletRequest.java
   trunk/src/main/org/jboss/seam/mock/MockLifecycle.java
   trunk/ui/src/main/java/org/jboss/seam/ui/facelet/FaceletsRenderer.java
Log:
first commit for JBSEAM-2915

Modified: trunk/src/main/org/jboss/seam/mock/AbstractSeamTest.java
===================================================================
--- trunk/src/main/org/jboss/seam/mock/AbstractSeamTest.java	2008-05-15 11:02:44 UTC (rev 8194)
+++ trunk/src/main/org/jboss/seam/mock/AbstractSeamTest.java	2008-05-15 13:25:37 UTC (rev 8195)
@@ -12,6 +12,7 @@
 
 import javax.el.ELResolver;
 import javax.el.ValueExpression;
+import javax.faces.FactoryFinder;
 import javax.faces.application.Application;
 import javax.faces.application.FacesMessage;
 import javax.faces.component.UIViewRoot;
@@ -504,6 +505,7 @@
                }
             });
             seamFilter.destroy();
+            facesContext.release();
             afterRequest();
             return conversationId;
          }
@@ -938,6 +940,7 @@
       application = new SeamApplication(new MockApplication());
       conversationViewRootAttributes = new HashMap<String, Map>();
       seamFilter = createSeamFilter();
+      FactoryFinder.setFactory(FactoryFinder.FACES_CONTEXT_FACTORY, MockFacesContextFactory.class.getName());
       
       for (ELResolver elResolver : getELResolvers())
       {

Modified: trunk/src/main/org/jboss/seam/mock/MockFacesContext.java
===================================================================
--- trunk/src/main/org/jboss/seam/mock/MockFacesContext.java	2008-05-15 11:02:44 UTC (rev 8194)
+++ trunk/src/main/org/jboss/seam/mock/MockFacesContext.java	2008-05-15 13:25:37 UTC (rev 8195)
@@ -199,7 +199,8 @@
    @Override
    public void release()
    {
-      setCurrentInstance(null);      
+      setCurrentInstance(null);
+      MockFacesContextFactory.setFacesContext(null);
    }
 
    @Override
@@ -217,6 +218,8 @@
    public MockFacesContext setCurrent()
    {
       setCurrentInstance(this);
+      
+      MockFacesContextFactory.setFacesContext(this);
       return this;
    }
 

Added: trunk/src/main/org/jboss/seam/mock/MockFacesContextFactory.java
===================================================================
--- trunk/src/main/org/jboss/seam/mock/MockFacesContextFactory.java	                        (rev 0)
+++ trunk/src/main/org/jboss/seam/mock/MockFacesContextFactory.java	2008-05-15 13:25:37 UTC (rev 8195)
@@ -0,0 +1,29 @@
+package org.jboss.seam.mock;
+
+import javax.faces.FacesException;
+import javax.faces.context.FacesContext;
+import javax.faces.context.FacesContextFactory;
+import javax.faces.lifecycle.Lifecycle;
+
+public class MockFacesContextFactory extends FacesContextFactory
+{
+   
+   private static FacesContext facesContext;
+
+   @Override
+   public FacesContext getFacesContext(Object context, Object request, Object response, Lifecycle lifecycle) throws FacesException
+   {
+      return facesContext;
+   }
+   
+   public static void setFacesContext(FacesContext facesContext)
+   {
+      MockFacesContextFactory.facesContext = facesContext;
+   }
+
+   public static FacesContext getFacesContext()
+   {
+      return MockFacesContextFactory.facesContext;
+   }
+   
+}


Property changes on: trunk/src/main/org/jboss/seam/mock/MockFacesContextFactory.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: trunk/src/main/org/jboss/seam/mock/MockHttpServletRequest.java
===================================================================
--- trunk/src/main/org/jboss/seam/mock/MockHttpServletRequest.java	2008-05-15 11:02:44 UTC (rev 8194)
+++ trunk/src/main/org/jboss/seam/mock/MockHttpServletRequest.java	2008-05-15 13:25:37 UTC (rev 8195)
@@ -10,6 +10,7 @@
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.security.Principal;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Enumeration;
 import java.util.HashMap;
@@ -40,7 +41,8 @@
    private String principalName;
    private Set<String> principalRoles;
    private Cookie[] cookies;
-   private String method; 
+   private String method;
+   private Enumeration locales;
    
    public MockHttpServletRequest(HttpSession session)
    {
@@ -61,6 +63,7 @@
       this.method = method;
       // The 1.2 RI NPEs if this header isn't present 
       headers.put("Accept", new String[0]);
+      locales = new IteratorEnumeration(new ArrayList().iterator());
    }
 
    public Map<String, String[]> getParameters()
@@ -341,8 +344,7 @@
 
    public Enumeration getLocales()
    {
-      //TODO
-      return null;
+      return locales;
    }
 
    public boolean isSecure()

Modified: trunk/src/main/org/jboss/seam/mock/MockLifecycle.java
===================================================================
--- trunk/src/main/org/jboss/seam/mock/MockLifecycle.java	2008-05-15 11:02:44 UTC (rev 8194)
+++ trunk/src/main/org/jboss/seam/mock/MockLifecycle.java	2008-05-15 13:25:37 UTC (rev 8195)
@@ -2,6 +2,7 @@
 package org.jboss.seam.mock;
 
 import javax.faces.FacesException;
+import javax.faces.FactoryFinder;
 import javax.faces.context.FacesContext;
 import javax.faces.event.PhaseListener;
 import javax.faces.lifecycle.Lifecycle;
@@ -10,6 +11,12 @@
 {
    
    public static final Lifecycle INSTANCE = new MockLifecycle();
+   
+   public MockLifecycle()
+   {
+      MockLifecycleFactory.setLifecycle(this);
+      FactoryFinder.setFactory(FactoryFinder.LIFECYCLE_FACTORY, MockLifecycleFactory.class.getName());
+   }
 
    @Override
    public void addPhaseListener(PhaseListener pl)

Added: trunk/src/main/org/jboss/seam/mock/MockLifecycleFactory.java
===================================================================
--- trunk/src/main/org/jboss/seam/mock/MockLifecycleFactory.java	                        (rev 0)
+++ trunk/src/main/org/jboss/seam/mock/MockLifecycleFactory.java	2008-05-15 13:25:37 UTC (rev 8195)
@@ -0,0 +1,42 @@
+package org.jboss.seam.mock;
+
+import java.util.Arrays;
+import java.util.Iterator;
+
+import javax.faces.lifecycle.Lifecycle;
+import javax.faces.lifecycle.LifecycleFactory;
+
+public class MockLifecycleFactory extends LifecycleFactory
+{
+   
+   private static Lifecycle lifecycle;
+   
+   public static void setLifecycle(Lifecycle lifecycle)
+   {
+      MockLifecycleFactory.lifecycle = lifecycle;
+   }
+   
+   public static Lifecycle getLifecycle()
+   {
+      return MockLifecycleFactory.lifecycle;
+   }
+
+   @Override
+   public void addLifecycle(String lifecycleId, Lifecycle lifecycle)
+   {
+      throw new IllegalArgumentException("Not supported");
+   }
+
+   @Override
+   public Lifecycle getLifecycle(String lifecycleId)
+   {
+      return lifecycle;
+   }
+
+   @Override
+   public Iterator<String> getLifecycleIds()
+   {
+      return Arrays.asList(DEFAULT_LIFECYCLE).iterator();
+   }
+   
+}


Property changes on: trunk/src/main/org/jboss/seam/mock/MockLifecycleFactory.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: trunk/ui/src/main/java/org/jboss/seam/ui/facelet/FaceletsRenderer.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/facelet/FaceletsRenderer.java	2008-05-15 11:02:44 UTC (rev 8194)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/facelet/FaceletsRenderer.java	2008-05-15 13:25:37 UTC (rev 8195)
@@ -1,29 +1,15 @@
 package org.jboss.seam.ui.facelet;
 
 import java.io.IOException;
-import java.io.StringWriter;
-import java.net.URL;
 
-import javax.faces.component.UIViewRoot;
-import javax.faces.context.FacesContext;
-import javax.faces.context.ResponseWriter;
-
 import org.jboss.seam.ScopeType;
 import org.jboss.seam.annotations.AutoCreate;
 import org.jboss.seam.annotations.Install;
 import org.jboss.seam.annotations.Name;
 import org.jboss.seam.annotations.Scope;
 import org.jboss.seam.annotations.intercept.BypassInterceptors;
-import org.jboss.seam.core.ResourceLoader;
 import org.jboss.seam.faces.Renderer;
-import org.jboss.seam.mock.MockExternalContext;
-import org.jboss.seam.mock.MockFacesContext;
-import org.jboss.seam.ui.util.JSF;
 
-import com.sun.facelets.Facelet;
-import com.sun.facelets.impl.DefaultFaceletFactory;
-import com.sun.facelets.impl.DefaultResourceResolver;
-
 /**
  * 
  * Implementation of Renderer using Facelets
@@ -42,167 +28,25 @@
 @Install(value = true, precedence = Install.BUILT_IN, classDependencies="com.sun.facelets.Facelet")
 public class FaceletsRenderer extends Renderer
 {
-   /**
-    * Sets up and tears down the rendering (FacesContext, ExternalContext) using 
-    * Seam's mock contexts if JSF is not currently running.  Also switches out the
-    * ViewRoot for the duration.  Any output written to the ResponseWriter is
-    * buffered.
-    * 
-    * @author Pete Muir
-    *
-    */
-   protected abstract class RenderingContext 
-   {
-      
-      public RenderingContext(String viewId)
-      {
-         this.viewId = viewId;
-      }
-      
-      private String viewId;
-      
-      private ClassLoader originalClassLoader;
-      private ResponseWriter originalResponseWriter;
-      private UIViewRoot originalViewRoot;
-      private StringWriter writer = new StringWriter();
-      
-      /**
-       * Call to execute the process method inside the RenderingContext
-       */
-      public void run() 
-      {
-         try
-         {
-            init();
-            process();
-         }
-         finally
-         {
-            cleanup();
-         }
-      }
-      
-      private void init()
-      {
-         // Make sure we are using the correct ClassLoader
-         originalClassLoader = Thread.currentThread().getContextClassLoader();
-         Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
-         
-         // If a FacesContext isn't available, set one up
-         if (FacesContext.getCurrentInstance() == null)
-         {
-            MockFacesContext mockFacesContext = new MockFacesContext(new MockExternalContext())
-                     .setCurrent();
-            mockFacesContext.createViewRoot();
-         }
-         
-         FacesContext facesContext = FacesContext.getCurrentInstance();
-         
-         // Wrap the ResponseWriter
-         originalResponseWriter = facesContext.getResponseWriter();
-         facesContext.setResponseWriter(facesContext.getRenderKit().createResponseWriter(writer,
-                  null, null));
-         
-         // Create a new UIViewRoot
-         originalViewRoot = facesContext.getViewRoot();
-         UIViewRoot viewRoot = new UIViewRoot();
-         viewRoot.setRenderKitId(facesContext.getApplication().getViewHandler().calculateRenderKitId(facesContext));
-         viewRoot.setViewId(viewId);
-         viewRoot.setLocale(originalViewRoot.getLocale());
-         facesContext.setViewRoot(viewRoot);
-      }
-      
-      private void cleanup()
-      {
-         FacesContext facesContext = FacesContext.getCurrentInstance();
-         if (originalResponseWriter != null)
-         {
-            facesContext.setResponseWriter(originalResponseWriter);
-         }
-         if (originalViewRoot != null)
-         {
-            facesContext.setViewRoot(originalViewRoot);
-         }
-         Thread.currentThread().setContextClassLoader(originalClassLoader);       
-      }
-      
-      /**
-       * Return any output written to the ResponseWriter as a String
-       */
-      public String getOutput() 
-      {
-         return writer.getBuffer().toString(); 
-      }
-      
-      /**
-       * Override to execute within a RenderingContext
-       */
-      protected abstract void process();
-      
-   }
    
+   
    /**
     * Render the viewId, anything written to the JSF ResponseWriter is
-    * returned
+    * returned 
     */
    @Override
-   public String render(final String viewId)
+   public String render(final String viewId) 
    {
-      RenderingContext context = new RenderingContext(viewId) {
-         
-         @Override
-         public void process()
-         {
-            FacesContext facesContext = FacesContext.getCurrentInstance();
-            try
-            {
-               renderFacelet(facesContext, faceletForURL(resourceURL(viewId)));
-            }
-            catch (IOException e)
-            {
-               throw new RuntimeException("Error rendering view", e);
-            }
-         }
-         
-      };
-      context.run();
-      
-      return context.getOutput();
-   }
-
-   /**
-    * Get a viewId as a URL
-    */
-   protected URL resourceURL(String viewId)
-   {
-
-      URL url = ResourceLoader.instance().getResource(viewId);
-
-      if (url == null)
+      RendererRequest rendererRequest = new RendererRequest(viewId);
+      try
       {
-         throw new IllegalArgumentException("resource doesn't exist: " + viewId);
+         rendererRequest.run();
       }
-
-      return url;
+      catch (IOException e)
+      {
+         throw new RuntimeException("error rendering " + viewId, e);
+      }
+      return rendererRequest.getOutput();
    }
-
-   /**
-    * Get a Facelet for a URL
-    */
-   protected Facelet faceletForURL(URL url) throws IOException
-   {
-      return new DefaultFaceletFactory(FaceletCompiler.instance(), new DefaultResourceResolver())
-               .getFacelet(url);
-   }
-
-   /**
-    * Render a Facelet
-    */
-   protected void renderFacelet(FacesContext facesContext, Facelet facelet) throws IOException
-   {
-      UIViewRoot root = facesContext.getViewRoot();
-      facelet.apply(facesContext, root);
-      JSF.renderChildren(facesContext, root);  
-   }
    
 }

Added: trunk/ui/src/main/java/org/jboss/seam/ui/facelet/MockHttpSessionManager.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/facelet/MockHttpSessionManager.java	                        (rev 0)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/facelet/MockHttpSessionManager.java	2008-05-15 13:25:37 UTC (rev 8195)
@@ -0,0 +1,49 @@
+package org.jboss.seam.ui.facelet;
+
+import static org.jboss.seam.ScopeType.SESSION;
+
+import javax.servlet.http.HttpSession;
+
+import org.jboss.seam.Component;
+import org.jboss.seam.annotations.AutoCreate;
+import org.jboss.seam.annotations.Create;
+import org.jboss.seam.annotations.Install;
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.Unwrap;
+import org.jboss.seam.annotations.intercept.BypassInterceptors;
+import org.jboss.seam.contexts.Contexts;
+import org.jboss.seam.mock.MockHttpSession;
+
+ at Name("org.jboss.seam.ui.facelet.mockHttpSession")
+ at Scope(SESSION)
+ at BypassInterceptors
+ at Install(dependencies="org.jboss.seam.faces.renderer")
+ at AutoCreate
+public class MockHttpSessionManager
+{
+   
+   private HttpSession session;
+   
+   @Create
+   public void create()
+   {
+      this.session = new MockHttpSession(MockServletContextManager.instance());
+   }
+   
+   @Unwrap
+   public HttpSession getSession()
+   {
+      return session;
+   }
+   
+   public static HttpSession instance()
+   {
+      if (!Contexts.isSessionContextActive())
+      {
+         throw new IllegalStateException("Session context is not active");
+      }
+      return (HttpSession) Component.getInstance(MockHttpSessionManager.class, SESSION);
+   }
+
+}


Property changes on: trunk/ui/src/main/java/org/jboss/seam/ui/facelet/MockHttpSessionManager.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: trunk/ui/src/main/java/org/jboss/seam/ui/facelet/MockServletContextManager.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/facelet/MockServletContextManager.java	                        (rev 0)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/facelet/MockServletContextManager.java	2008-05-15 13:25:37 UTC (rev 8195)
@@ -0,0 +1,49 @@
+package org.jboss.seam.ui.facelet;
+
+import static org.jboss.seam.ScopeType.APPLICATION;
+
+import javax.servlet.ServletContext;
+
+import org.jboss.seam.Component;
+import org.jboss.seam.annotations.AutoCreate;
+import org.jboss.seam.annotations.Create;
+import org.jboss.seam.annotations.Install;
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.Unwrap;
+import org.jboss.seam.annotations.intercept.BypassInterceptors;
+import org.jboss.seam.contexts.Contexts;
+import org.jboss.seam.mock.MockServletContext;
+
+ at Name("org.jboss.seam.ui.facelet.mockServletContext")
+ at Scope(APPLICATION)
+ at BypassInterceptors
+ at Install(dependencies="org.jboss.seam.faces.renderer")
+ at AutoCreate
+public class MockServletContextManager
+{
+   
+   private ServletContext servletContext;
+   
+   @Create
+   public void create()
+   {
+      this.servletContext = new MockServletContext();
+   }
+   
+   @Unwrap
+   public ServletContext getServletContext()
+   {
+      return servletContext;
+   }
+   
+   public static ServletContext instance()
+   {
+      if (!Contexts.isApplicationContextActive())
+      {
+         throw new IllegalStateException("Application context is not active");
+      }
+      return (ServletContext) Component.getInstance(MockServletContextManager.class, APPLICATION);
+   }
+
+}


Property changes on: trunk/ui/src/main/java/org/jboss/seam/ui/facelet/MockServletContextManager.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: trunk/ui/src/main/java/org/jboss/seam/ui/facelet/RendererFacesContextFactory.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/facelet/RendererFacesContextFactory.java	                        (rev 0)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/facelet/RendererFacesContextFactory.java	2008-05-15 13:25:37 UTC (rev 8195)
@@ -0,0 +1,75 @@
+package org.jboss.seam.ui.facelet;
+
+import static org.jboss.seam.ScopeType.APPLICATION;
+
+import javax.faces.FactoryFinder;
+import javax.faces.context.FacesContext;
+import javax.faces.lifecycle.Lifecycle;
+import javax.faces.lifecycle.LifecycleFactory;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+import org.jboss.seam.Component;
+import org.jboss.seam.annotations.AutoCreate;
+import org.jboss.seam.annotations.Create;
+import org.jboss.seam.annotations.Destroy;
+import org.jboss.seam.annotations.Install;
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.intercept.BypassInterceptors;
+import org.jboss.seam.contexts.Contexts;
+
+
+/**
+ * A copy of the FacesServlet for use in our Renderer.
+ * 
+ * Shamelessly adapted from the RI
+ * 
+ * @author Pete Muir
+ *
+ */
+
+ at Name("org.jboss.seam.ui.facelet.facesContextFactory")
+ at Scope(APPLICATION)
+ at BypassInterceptors
+ at Install(dependencies="org.jboss.seam.faces.renderer")
+ at AutoCreate
+public class RendererFacesContextFactory
+{
+
+   private javax.faces.context.FacesContextFactory facesContextFactory;
+   private Lifecycle lifecycle;
+   
+   @Create
+   public void create()
+   {
+      // Acquire our FacesContextFactory instance
+      facesContextFactory = (javax.faces.context.FacesContextFactory) FactoryFinder.getFactory (FactoryFinder.FACES_CONTEXT_FACTORY);
+
+      // Acquire our Lifecycle instance
+      LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
+      lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
+   }
+
+   public FacesContext getFacesContext(ServletRequest request, ServletResponse response)
+   {
+      return facesContextFactory.getFacesContext(MockServletContextManager.instance(), request, response, lifecycle);
+   }
+   
+   @Destroy
+   public void destroy()
+   {
+      facesContextFactory = null;
+      lifecycle = null;
+   }
+
+   public static RendererFacesContextFactory instance()
+   {
+      if (!Contexts.isApplicationContextActive())
+      {
+         throw new IllegalStateException("Application context is not active");
+      }
+      return (RendererFacesContextFactory) Component.getInstance(RendererFacesContextFactory.class, APPLICATION);
+   }
+   
+}


Property changes on: trunk/ui/src/main/java/org/jboss/seam/ui/facelet/RendererFacesContextFactory.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: trunk/ui/src/main/java/org/jboss/seam/ui/facelet/RendererRequest.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/facelet/RendererRequest.java	                        (rev 0)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/facelet/RendererRequest.java	2008-05-15 13:25:37 UTC (rev 8195)
@@ -0,0 +1,114 @@
+package org.jboss.seam.ui.facelet;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.net.URL;
+
+import javax.faces.component.UIViewRoot;
+import javax.faces.context.FacesContext;
+
+import org.jboss.seam.core.ResourceLoader;
+import org.jboss.seam.jsf.DelegatingFacesContext;
+import org.jboss.seam.mock.MockHttpServletRequest;
+import org.jboss.seam.mock.MockHttpServletResponse;
+import org.jboss.seam.ui.util.JSF;
+
+import com.sun.facelets.Facelet;
+import com.sun.facelets.impl.DefaultFaceletFactory;
+import com.sun.facelets.impl.DefaultResourceResolver;
+
+public class RendererRequest
+{
+   
+   private FacesContext originalFacesContext;
+   private FacesContext facesContext;
+   
+   private MockHttpServletRequest request;
+   private MockHttpServletResponse response;
+   
+   private ClassLoader originalClassLoader;
+   
+   private StringWriter writer;
+   
+   private String viewId;
+   
+   public RendererRequest(String viewId)
+   {
+      this.viewId = viewId;
+   }
+   
+   private void init()
+   {
+      request = new MockHttpServletRequest(MockHttpSessionManager.instance());
+      response = new MockHttpServletResponse();
+      
+      // Make sure we are using the correct ClassLoader
+      // TODO Is this still necessary
+      originalClassLoader = Thread.currentThread().getContextClassLoader();
+      Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
+      
+      // Generate the FacesContext from the JSF FacesContextFactory
+      originalFacesContext = FacesContext.getCurrentInstance();
+      facesContext = RendererFacesContextFactory.instance().getFacesContext(request, response);
+      DelegatingFacesContext.setCurrentInstance(facesContext);
+      
+      // Create the viewRoot
+      UIViewRoot newRoot = facesContext.getApplication().getViewHandler().createView(facesContext, viewId);
+      facesContext.setViewRoot(newRoot);
+      
+      // Set the responseWriter to write to a buffer
+      writer = new StringWriter();
+      facesContext.setResponseWriter(facesContext.getRenderKit().createResponseWriter(writer,
+      null, null));
+   }
+   
+   private void cleanup()
+   {
+      facesContext.release();
+      DelegatingFacesContext.setCurrentInstance(originalFacesContext);
+      
+      Thread.currentThread().setContextClassLoader(originalClassLoader);
+      
+      originalFacesContext = null;
+      facesContext = null;
+      request = null;
+      response = null;
+      originalClassLoader = null;
+   }
+   
+   public void run() throws IOException
+   {
+      init();
+      renderFacelet(facesContext, faceletForViewId(viewId));
+      cleanup();
+   }
+   
+   public String getOutput()
+   {
+      return writer.getBuffer().toString();
+   }
+
+   /**
+    * Get a Facelet for a URL
+    */
+   protected Facelet faceletForViewId(String viewId) throws IOException
+   {
+      URL url = ResourceLoader.instance().getResource(viewId);
+      if (url == null)
+      {
+         throw new IllegalArgumentException("resource doesn't exist: " + viewId);
+      }
+      return new DefaultFaceletFactory(FaceletCompiler.instance(), new DefaultResourceResolver())
+               .getFacelet(url);
+   }
+
+   /**
+    * Render a Facelet
+    */
+   protected void renderFacelet(FacesContext facesContext, Facelet facelet) throws IOException
+   {
+      UIViewRoot root = facesContext.getViewRoot();
+      facelet.apply(facesContext, root);
+      JSF.renderChildren(facesContext, root);  
+   }
+}


Property changes on: trunk/ui/src/main/java/org/jboss/seam/ui/facelet/RendererRequest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain




More information about the seam-commits mailing list