Red Hat
Jul 16, 2013
by Ken Finnigan

With the release of GateIn 3.6.0.Final we introduced some new CDI scopes to ease its use within a portlet.  In this first blog on CDI scopes for GateIn, we will cover @PortletLifecycleScoped. This scope can be used with GenericPortlets, Portlet Filters, or JSF Portlets.

 

This scope lives for a single Portlet Request lifecycle and is destroyed on completion, meaning that there is no way to guarantee a subsequent Render will generate the same portlet content. The following table illustrates when the context for the scope is activated (A), destroyed (X), or when its already present (P):

 

 

Portlet TriggerAction StartAction CompleteEvent StartEvent CompleteRender StartRender CompleteResource StartResource Complete
Portlet ActionAPPPPX

Portlet Event

APPX

Portlet Render



AX

Portlet Resource





AX

 

An important distinction is that any Beans created within the scope context as part of a Portlet Action, Portlet Event or Portlet Render are not visible within a Portlet Resource call, and vice versa. Each ResourceRequest will receive a completely empty set of Beans that have been constructed, including calls to any @PostConstruct, but that have had no code executed against them. Any changes to Beans within a ResourceRequest is not retained in any way. This situation is perfect for making Ajax calls from a portlet, so long as the data you're relying on perform the work is accessible from non @PortletLifecycleScoped beans.

 

To begin using the new scope, simply add the following dependency into your portlet project pom.xml:

 

        <dependency>
            <groupId>org.gatein.api</groupId>
            <artifactId>gatein-api</artifactId>
            <version>1.0.0.Final</version>
            <scope>provided</scope>
        </dependency>

 

We mark the dependency as provided since GateIn will automatically link our deployment to the latest API dependency at runtime.

 

With the above dependency we can then create a bean such as:

 

@PortletLifecycleScoped
public class LifecycleBean {
  ...
}

 

As far as CDI is concerned, @PortletLifecycleScoped is a bean scope like all those that it provides. This enables us to inject @RequestScoped, @ConversationScoped, @SessionScoped or @ApplicationScoped beans into a @PortletLifecycleScoped bean, or vice versa.

 

Hope this and the scope we cover in the next blog will make developing CDI portlets with GateIn that much easier!