Control Life Cycle

 <  Day Day Up  >  

As you saw in Chapter 3, "WebLogic Workshop Application Development Basics," controls are specially annotated Java objects. As with any Java object, controls have a well-defined life cycle. They are created, perform their function, and are then destroyed . Controls differ from other objects in that you can catch life cycle events at various stages and perform work. Figure 8.1 shows the minimum life cycle events of all controls. Table 8.1 describes the details of each life cycle method. Other control types, such as asynchronous controls, add other events. Every control has an associated Context object.

Figure 8.1. The control life cycle.

graphics/08fig01.gif

State 1: Looking at the control life cycle, you can see that controls start off in state 1, which is the state that exists before a control has been instantiated . As required by the application, WebLogic Workshop then creates an instance of the control, and the developer-coded onCreate() method is called. After an in-memory instance of the control exists, any child or encapsulated controls are created. Encapsulated controls are any controls that the control might use to get its job done.

State 2: In state 2, a control is ready for use. All child controls have been instantiated and are ready. A control moves from state 2 to state 3 when the first developer-written business method is called. However, specialized initialization code might need to be executed. Developers can add this specialized code in the onAcquire() method.

State 3: Controls stay in state 3 while business methods are executed. When the control is no longer needed, it moves to state 4, but not without first calling the onRelease() method, which can release any resources obtained in onAcquire() .

State 4: State 4 is then reached and the control instance can be destroyed normally.

Table 8.1. Control Life Cycle Event Callback Methods

METHOD

CALLED WHEN

DESCRIPTION

void onCreate()

After all encapsulated controls have been initialized

The onCreate() handler method is called after all encapsulated controls have been prepared and are ready for use. In addition, the Context object, which manages all life cycle events, is ready. You can use encapsulated controls at this point.

void onAcquire()

Called before any developer-written methods are executed

The onAcquire() handler method is used to define the beginning and ending boundaries of a control's developer-written method calls. onAcquire() is called before any developer-written method is executed and can be used to prepare common resources without needing specialized initialization methods.

void onRelease()

Called before control is destroyed

The onRelease() handler method defines the end of a control's life cycle and can be used to perform specialized cleanup.

void onException (Exception e, String methodName, Object[] methodArgs)

Called before an exception is propagated upstream

The onException() handler method is used to trap and examine unexpected exceptions. The offending method is provided along with its arguments. Specialized handling, which takes place before external exception handling, can be done at this point, such as logging a special exception notice.

You take advantage of life cycle events by adding a Context object to your custom Java control and then adding handler methods, such as onCreate() , onAcquire() , and onRelease() . The shaded portion of the following code shows the definition of the required Context object:

 public class LifeCycleImpl implements LifeCycle, ControlSource{     static final long serialVersionUID = 1L;  /**   * @common:context   */   com.bea.control.ControlContext context;  

To handle life cycle events, you write handler methods specific to the event being handled. The following code shows adding an onCreate() handler:

 public void context_onCreate() {     // your code here... } 

LIFE CYCLE CALLBACK CONTAINERS

The life cycle documentation packaged with the Control Developer's Kit differs slightly from that published online at http://edocs.bea.com. Both resources describe the various callback methods; however, the Control Developer's Kit documentation notes that control life cycle callbacks can be used only in conversational Web services. Testing indicates that life cycle callbacks from within other containers, such as Page Flow or controls, work fine and the eDocs documentation makes no reference to this restriction. Typically, these differences are a result of the online documentation being updated constantly and the packaged documentation being a snapshot at a given moment. The best rule is to always look online first.


Handler methods are actually callbacks to com.bea.control.ControlContext instances. To implement them, add the instance variable name to the beginning of the method. In the onCreate() example, using the context variable, you write handlers by adding context to the handler method name ”in this case, context_onCreate() . Refer back to Table 8.1 to see the method signature of the most important life cycle handler methods.

 <  Day Day Up  >  


BEA WebLogic Workshop 8.1 Kick Start
BEA WebLogic Workshop 8.1 Kick Start: Simplifying Java Web Applications and J2EE
ISBN: 0672326221
EAN: 2147483647
Year: 2004
Pages: 138

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net