Avalon Land

Blog EntryJSR-168 Portal + JSF: Portlet PreferencesOct 9, '07 5:26 AM
for everyone
all portals conformed with JSR-168 specification, has standard mechanism for access and manage portlets preferences through the javax.portlet.PortletPreferences interface implementation. this article consider to portlet work with preferences in portlet powered by JavaServer Faces technology.

quick solution

the common interface for connect JSF technology with external environment is the javax.faces.context.FacesContext class. Are portlet preferences, portlet request type information, portlet mode and view settings store inside the javax.portlet.PortletRequest interface implementations. A portlet name-space information store inside the javax.portlet.PortletRespose interface implementations. for collaboration between a JSF portlet and a portal specific external environment entities implement following class:
public final class PorletFacesUtils {     
    private PortletFacesUtils() {
    }     
    
    public static PortletRequest getPortletRequest(FacesContext context) throws Exception {        
        Object request = context.getExternalContext().getRequest();
        if (request instanceof PortletRequest) {             
            return (PortletRequest) request;
        } else {
            throws new Exception(“Portlet run outside the portal environment or portlet request class is not generic”);
        }
    }

    public static PortletRequest getPortletResponse(FacesContext context) throws Exception {
        Object response = context.getExternalContext().getResponse();
        if (request instanceof PortletResponse) {
            return (PortletResponse) response;
        } else {
            throws new Exception(“Portlet run outside the portal environment or portlet response class is not generic”);
        }
    }

    public static PortletPreferences getPortletRequest(FacesContext context) throws Exception {
        PortletRequest request = getPortletRequest(context);
        return request.getPortletPreferences();
    }
}
thous, we are implement bridge between a JSF-application and a portal environment specific classes. this class may use for work with portlet preferences and settings, also class may extends for get another portal environment specific classes (javax.portlet.PortletSession, javax.portlet.PortletContext, etc.).
risks
some JSF components frameworks (for example ICEfaces) using a custom portal request and response wrappers. this wrappers could not extends the standard interfaces and throw exceptions in process with utility class. in this case is necessary to implement the portal environment adapter for each custom wrapper implementation. following I'll told about that.

flexible solution
flexible implementation provides a possibility to simplify different JSF component libraries, portal implementations and JSF implementation vendors switching. solution based on utility class functionality described above.
so, implement the common interface of portal environment adapter:
public interface PortletFacesAdapter extends Serializable {

    PortletRequest getPortletRequest(FacesContext context);

    PortletResponse getPortletResponse(FacesContext context);

    PortletPreferences getPortletPreferences(FacesContext context);
}


place the utility class functionality to generic implementation of the adapter interface:
public class GenericPortletFacesAdapter {
    public PortletRequest getPortletRequest(FacesContext context) throws Exception {
        Object request = context.getExternalContext().getRequest();
        if (request instanceof PortletRequest) {
            return (PortletRequest) request;
        } else {
            throws new Exception(“Portlet run outside the portal environment or portlet request class is not generic”);
        }
    }

    public PortletRequest getPortletResponse(FacesContext context) throws Exception {
        Object response = context.getExternalContext().getResponse();
        if (request instanceof PortletResponse) {
            return (PortletResponse) response;
        } else {
            throws new Exception(“Portlet run outside the portal environment or portlet response class is not generic”);
        }
    }
    public PortletPreferences getPortletRequest(FacesContext context) throws Exception {
        PortletRequest request = getPortletRequest(context);
        return request.getPortletPreferences();
    }
}
now we need to implement JSF managed bean for adapters support:
public class TestBean extends AdaptableBean {
}
and configure portlet for use GenericPortletFacesAdapter inside this class:
<faces-config>
    <managed-bean>
        <managed-bean-name>portletAdapter</managed-bean-name>
        <managed-bean-class>GenericPortletFacesAdapter</managed-bean-class>
        <managed-bean-scope>application</managed-bean-scope>
    </managed-bean>
    <managed-bean>
        <managed-bean-name>testBean</managed-bean-name>
        <managed-bean-class>TestBean</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
        <managed-property>
            <property-name>adapter</property-name>
            <value>portletAdapter</value>
        </managed-property>
    </managed-bean>
</faces-config>
linksJava Portlet APIJava ServerFaces API

Add a Comment
   
© 2008 Multiply, Inc.    About · Blog · Terms · Privacy · Corp Info · Contact Us · Help