Author: pete.muir(a)jboss.org
Date: 2009-04-01 07:48:55 -0400 (Wed, 01 Apr 2009)
New Revision: 10266
Modified:
trunk/src/main/org/jboss/seam/contexts/ServletLifecycle.java
trunk/ui/src/main/java/org/jboss/seam/ui/facelet/RendererRequest.java
Log:
JBSEAM-3555
Modified: trunk/src/main/org/jboss/seam/contexts/ServletLifecycle.java
===================================================================
--- trunk/src/main/org/jboss/seam/contexts/ServletLifecycle.java 2009-04-01 07:34:13 UTC
(rev 10265)
+++ trunk/src/main/org/jboss/seam/contexts/ServletLifecycle.java 2009-04-01 11:48:55 UTC
(rev 10266)
@@ -6,6 +6,7 @@
*/
package org.jboss.seam.contexts;
+import java.lang.ref.WeakReference;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@@ -23,7 +24,7 @@
/**
* Methods for setup and teardown of Seam contexts at the
* beginning and end of servlet requests.
- *
+ *
* @author Gavin King
* @author <a href="mailto:theute@jboss.org">Thomas Heute</a>
*/
@@ -40,7 +41,7 @@
return servletContext;
}
- public static void beginRequest(HttpServletRequest request)
+ public static void beginRequest(HttpServletRequest request)
{
log.debug( ">>> Begin web request" );
Contexts.eventContext.set( new EventContext( new ServletRequestMap(request) ) );
@@ -112,7 +113,7 @@
Contexts.startup(ScopeType.APPLICATION);
Events.instance().raiseEvent("org.jboss.seam.postInitialization");
-
+
// Clean up contexts used during initialization
Contexts.destroy( Contexts.getConversationContext() );
Contexts.conversationContext.set(null);
@@ -120,12 +121,17 @@
Contexts.eventContext.set(null);
Contexts.sessionContext.set(null);
Contexts.applicationContext.set(null);
-
+
log.debug("<<< End initialization");
}
- public static void beginApplication(ServletContext context)
+ public static void beginApplication(ServletContext context)
{
+ // caching the classloader to servletContext
+ WeakReference<ClassLoader> ref = new
WeakReference<ClassLoader>(Thread.currentThread().getContextClassLoader());
+ context.setAttribute("seam.context.classLoader",ref);
+ log.info("Cached the context classloader in servletContext as
'seam.context.classLoader'");
+
servletContext = context;
Lifecycle.beginApplication( new ServletApplicationMap(context) );
}
Modified: trunk/ui/src/main/java/org/jboss/seam/ui/facelet/RendererRequest.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/facelet/RendererRequest.java 2009-04-01
07:34:13 UTC (rev 10265)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/facelet/RendererRequest.java 2009-04-01
11:48:55 UTC (rev 10266)
@@ -2,12 +2,16 @@
import java.io.IOException;
import java.io.StringWriter;
+import java.lang.ref.WeakReference;
import java.net.URL;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
+import javax.servlet.ServletContext;
import org.jboss.seam.core.ResourceLoader;
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
import org.jboss.seam.jsf.DelegatingFacesContext;
import org.jboss.seam.mock.MockHttpServletRequest;
import org.jboss.seam.mock.MockHttpServletResponse;
@@ -19,61 +23,78 @@
public class RendererRequest
{
-
+ private static final LogProvider log = Logging.getLogProvider(RendererRequest.class);
+
private FacesContext originalFacesContext;
private FacesContext facesContext;
-
+
private MockHttpServletRequest request;
private MockHttpServletResponse response;
-
+
private StringWriter writer;
-
+
private String viewId;
-
+
+ private ClassLoader originalClassLoader;
+
public RendererRequest(String viewId)
{
this.viewId = viewId;
}
-
+
private void init()
{
request = new MockHttpServletRequest(HttpSessionManager.instance());
response = new MockHttpServletResponse();
-
+
// 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));
+
+ // Set the context classloader to the cached one
+ originalClassLoader = Thread.currentThread().getContextClassLoader();
+ ServletContext ctx = request.getSession().getServletContext();
+ WeakReference<ClassLoader> ref =
(WeakReference<ClassLoader>)ctx.getAttribute("seam.context.classLoader");
+ if (ref == null || ref.get() == null) {
+ log.warn("Failed to bootstrap context classloader. Facelets may not work
properly from MDBs");
+ } else {
+ Thread.currentThread().setContextClassLoader(ref.get());
+ }
}
-
+
private void cleanup()
{
+ if (originalClassLoader != null) {
+ Thread.currentThread().setContextClassLoader(originalClassLoader);
+ originalClassLoader = null;
+ }
+
facesContext.release();
DelegatingFacesContext.setCurrentInstance(originalFacesContext);
-
-
+
originalFacesContext = null;
facesContext = null;
request = null;
response = null;
}
-
+
public void run() throws IOException
{
init();
renderFacelet(facesContext, faceletForViewId(viewId));
cleanup();
}
-
+
public String getOutput()
{
return writer.getBuffer().toString();
Show replies by date