Getting a FacesContext from a GWT service

I'm working on a project where we needed to integrate JSF and GWT. To be more specific, we needed to have access to JSF beans in the GWT services.

Because I missed the methods getThreadLocalRequest() and getThreadLocalResponse() in RemoteServiceServlet, I had to search quite a bit before i found an answer to this problem. So to save others some time i post the class i came up with.

Most of the code is taken from an article at ocpsoft.com which also points out how important it is to release the context when you're done with it. The big difference are the lines,
UIViewRoot view = facesContext.getApplication().getViewHandler().createView(facesContext, "gwtViewID");
facesContext.setViewRoot(view);
, which sets the view root. If no view root is set you can't use, for instance MessageBundles.

The getBean methods are only convenience methods for retrieving beans by name.

/**
 * A substitute for RemoteServiceServlet that has access to the FacesContext.
  */
@SuppressWarnings("serial")
public class FacesContextAwareRemoteServiceServlet extends RemoteServiceServlet {
 protected  T getBean(final Class type) {
  String name = type.getSimpleName();
  name = name.substring(0, 1).toLowerCase().concat(name.substring(1));
  return getBean(name, type);
 }

 @SuppressWarnings("unchecked")
 protected  T getBean(final String name, final Class type) {
  ELContext el = getFacesContext().getELContext();
  return (T) el.getELResolver().getValue(el, null, name);
 }

 /**
  * Returns the current FacesContext.
  */
 protected FacesContext getFacesContext() {
  return getFacesContext(getThreadLocalRequest(), getThreadLocalResponse());
 }

 /**
  * Should be called when the FacesContext aren't needed any more.
  */
 protected void releaseFacesContext() {
  FacesContext facesContext = FacesContext.getCurrentInstance();
  if (facesContext != null) {
   facesContext.release();
   InnerFacesContext.setFacesContextAsCurrentInstance(null);
  }
 }

 private FacesContext getFacesContext(ServletRequest request, ServletResponse response) {
  FacesContext facesContext = FacesContext.getCurrentInstance();
  if (facesContext != null) {
   return facesContext;
  }

  FacesContextFactory contextFactory = (FacesContextFactory) FactoryFinder
    .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
  LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder
    .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
  Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);

  ServletContext servletContext = ((HttpServletRequest) request).getSession().getServletContext();
  facesContext = contextFactory.getFacesContext(servletContext, request, response, lifecycle);

  InnerFacesContext.setFacesContextAsCurrentInstance(facesContext);
  
  UIViewRoot view = facesContext.getApplication().getViewHandler().createView(facesContext, "gwtViewID");
  facesContext.setViewRoot(view);

  return facesContext;
 }

 private abstract static class InnerFacesContext extends FacesContext {
  protected static void setFacesContextAsCurrentInstance(final FacesContext facesContext) {
   FacesContext.setCurrentInstance(facesContext);
  }
 }
}
Get from github

2 comments:

longge said...

http://uggloveshoes.vox.com/
http://uggshoe.vox.com/
http://uggloveshoes.webs.com/apps/blog/
http://warmugg.webs.com/apps/blog/
http://uggloveshoes.weebly.com/
http://ugggg.weebly.com/
http://uggloveshoes.wordpress.com/
http://uggloveboots.wordpress.com/
http://pulse.yahoo.com/_AYKQMFYOZ26I3OTTDFMBU4QBDQ/blog/
http://pulse.yahoo.com/_RDDQ2DFI4RDOV4Y2L47DLPM6C4/blog/

123 said...

http://remind.com.br/projetos/criar-blog-gratis/replicahandbags69/
http://remind.com.br/projetos/criar-blog-gratis/ureplicawatches/
http://remind.com.br/projetos/criar-blog-gratis/ureplicawatches1987/
http://replbags.livejournal.com/
http://replcawatche.livejournal.com/
http://repldbags.livejournal.com/
http://replica69bags.insanejournal.com/
http://replicabag242.livejournal.com/
http://replicacheap.21classes.com/weblogCategory/1951uut1lrg4t
http://replicacheap.ayubd.com/