Developing with ATG: Part V

9 01 2008

In this edition of the Developing with ATG series we’ll discuss the component life-cycle and scoping. We’ll first discuss the life-cycle of a component in nucleus and follow up with a discussion on component scopes.

The life-cycle of a nucleus component is pretty simple: the component is started and the component is stopped. If a component needs to do any work when started or stopped then it should implement the atg.nucleus.ServiceListenerinterface. A ServiceListener must implement the startService() method and the stopService() method.  When the startService() method is called it is passed an instance of atg.nucleus.ServiceEvent. The ServiceEventcontains information about the components configuration as well as a reference to the instance of atg.nucleus.Nucleus which started the component. These are often great opportunities for the component to create/release required resources. 

So now lets introduce you to scope. Nucleus has three scopes a component can belong to: request, session or global. Up to this point we’ve been discussing globally scoped components but in this edition we introduce request and session scoped components.

Globally scoped components are created when the component is resolved by name and destroyed when nucleus shuts down. There are a lot of important globally scoped components in nucleus. Many of them were described in Developing with ATG: Part III. Globally scoped component live the longest of all the scopes.

A session scoped component differs from a globally scoped component in that these components are created in the context of an HTTP session. When the component is resolved by name it will be associated with the current HTTP session. When the HTTP session is destroyed the component is stopped and taken out of service.

One example of a session scoped component is the /atg/userprofiling/Profile component. This component represents the user associated with the session. If one needs to reference the current user then all one needs to do is resolve this component by name using a name resolver. The following code demonstrates how one can accomplish this.

Profile profile = 
(profile) nameResolver.resolveName("/atg/userprofiling/Profile"); 
String login = 
(Stirng)profile.getPropertyValue("login");

You can even reference a session scoped component from another session (or request) scoped components property file. The following example demonstrates how you would configure a custom component that a reference the session scoped Profile component.

$class=acme.MyCustomComponent 
$scope=session 
profile=/atg/userprofiling/Profile

A request scope component has the shortest lifecycle of any of the scopes. A request scoped component only lives as long as the HTTP request it is being served in. Once the request has be serviced all of the request scoped components are destroyed for that request are destroyed.  The /OriginatingRequest component is an example of a request scoped component. This component implements the javax.servlet.http.HttpServletRequest interface and represents the current request being serviced.

To specify the scope of a component set the $scope metadata property in your components property file. The value of this property can be either global, session or request. If left unspecified the components scope will be global. 

Finally here are some rules you need to keep in mind when creating application that mix components of various scopes. Globally scoped component can only reference other globally scoped components. Session scoped components can only reference other session scoped components (in the same session) or globally scoped components. Finally request scoped components can only reference other request scoped components (in the same request) or sessions scoped components (in the same session as the request) or globally scoped components.  If you think about this for a little its actually fairly sensible.

By the way Nucleus is one of the only component models I am aware of that has support for request and session scoped components. The only other component model I know of that does something like this is Spring 2.0. If you know of any other drop me a line. 

Advertisements

Actions

Information

One response

25 01 2008
Devon

Seam, from JBoss, has Global, Session, Conversation, Request, Page, and Event component scopes. Conversation scope is an amazing thing, and something I really miss on ATG.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: