1 17 package org.alfresco.web.app.servlet; 18 19 import javax.faces.FactoryFinder; 20 import javax.faces.component.UIViewRoot; 21 import javax.faces.context.FacesContext; 22 import javax.faces.context.FacesContextFactory; 23 import javax.faces.el.ValueBinding; 24 import javax.faces.lifecycle.Lifecycle; 25 import javax.faces.lifecycle.LifecycleFactory; 26 import javax.portlet.PortletContext; 27 import javax.portlet.PortletRequest; 28 import javax.portlet.PortletResponse; 29 import javax.servlet.ServletContext ; 30 import javax.servlet.ServletRequest ; 31 import javax.servlet.ServletResponse ; 32 33 36 public final class FacesHelper 37 { 38 41 private FacesHelper() 42 { 43 } 44 45 55 public static FacesContext getFacesContext(ServletRequest request, ServletResponse response, ServletContext context) 56 { 57 return getFacesContextImpl(request, response, context); 58 } 59 60 70 public static FacesContext getFacesContext(PortletRequest request, PortletResponse response, PortletContext context) 71 { 72 return getFacesContextImpl(request, response, context); 73 } 74 75 85 private static FacesContext getFacesContextImpl(Object request, Object response, Object context) 86 { 87 FacesContext facesContext = FacesContext.getCurrentInstance(); 88 if (facesContext != null) return facesContext; 89 90 FacesContextFactory contextFactory = (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY); 91 LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY); 92 Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE); 93 94 facesContext = contextFactory.getFacesContext(context, request, response, lifecycle); 96 97 InnerFacesContext.setFacesContextAsCurrent(facesContext); 99 100 UIViewRoot view = facesContext.getApplication().getViewHandler().createView(facesContext, "/jsp/root"); 102 facesContext.setViewRoot(view); 103 104 return facesContext; 105 } 106 107 115 public static Object getManagedBean(FacesContext fc, String name) 116 { 117 ValueBinding vb = fc.getApplication().createValueBinding("#{" + name + "}"); 118 return vb.getValue(fc); 119 } 120 121 125 private abstract static class InnerFacesContext extends FacesContext 126 { 127 protected static void setFacesContextAsCurrent(FacesContext facesContext) 128 { 129 FacesContext.setCurrentInstance(facesContext); 130 } 131 } 132 } 133 | Popular Tags |