JSF web application developers typically call FacesContext.getCurrentInstance() in order to obtain the ThreadLocal
singleton instance associated with the current request. While JSF portlet developers can certainly do the
same, it's easier to call PortletFacesContext.getInstance() which returns an application-scoped singleton instance.
Example 6.28. Obtaining the PortletFacesContext Singleton Instance
public class SessionScopedManagedBean {
private PortletFacesContext portletFacesContext = PortletFacesContext.getInstance();
public List<DlFileEntry> getDocuments() {
List<DLFileEntry> documents;
try {
documents = DLFileEntryLocalServiceUtil.getFileEntries(folderId);
}
catch (Exception e) {
LOG.error(e.getMessage(), e);
// Don't have to call PortletFacesContext.getInstance() first since
// a reference to it was obtained when the bean was created.
portletFacesContext.addGlobalUnexpectedErrorMessage();
}
return documents;
}
}
PortletFacesContext is an abstract class that extends the edoras framework's ExtFacesContext abstract class, which in turn extends the JSF FacesContext abstract class. Since PortletFacesContext ultimately extends FacesContext, it supplies all the same method signatures and can therefore do anything that FacesContext can do. Both PortletFacesContext and ExtFacesContext implement the delegation design pattern for methods defined by FacesContext by first calling FacesContext.getCurrentInstance() and then delegating to corresponding methods. The benefit of using this technique is that JSF portlet developers only have to call PortletFacesContext.getInstance() once, and can save the singleton object reference for future use.