KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > PageFlowManagedObject


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.pageflow;
19
20 import org.apache.beehive.netui.pageflow.internal.JavaControlUtils;
21 import org.apache.beehive.netui.util.logging.Logger;
22
23 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
28 import javax.servlet.ServletContext JavaDoc;
29 import java.io.Serializable JavaDoc;
30 import java.lang.reflect.Field JavaDoc;
31
32 /**
33  * Base class for Page Flow managed objects (like page flows and JavaServer Faces backing beans).
34  */

35 public abstract class PageFlowManagedObject
36         implements Serializable JavaDoc, HttpSessionBindingListener JavaDoc
37 {
38     private static final Logger _log = Logger.getInstance( PageFlowManagedObject.class );
39     
40     /**
41      * Reference to the current ServletContext.
42      */

43     private transient ServletContext JavaDoc _servletContext;
44
45     
46     /**
47      * Reinitialize the object for a new request. Used by the framework; normally should not be called directly.
48      */

49     public void reinitialize( HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, ServletContext JavaDoc servletContext )
50     {
51         _servletContext = servletContext;
52     }
53
54     /**
55      * Initialize after object creation. This is a framework-invoked method; it should not normally be called directly.
56      */

57     public synchronized void create( HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, ServletContext JavaDoc servletContext )
58         throws Exception JavaDoc
59     {
60         reinitialize( request, response, servletContext );
61         JavaControlUtils.initJavaControls( request, response, servletContext, this );
62         onCreate();
63     }
64
65     /**
66      * Internal destroy method that is invoked when this object is being removed from the session. This is a
67      * framework-invoked method; it should not normally be called directly.
68      */

69     void destroy( HttpSession JavaDoc session )
70     {
71         onDestroy( session );
72         JavaControlUtils.uninitJavaControls( session.getServletContext(), this );
73     }
74     
75     /**
76      * Create-time callback. Occurs after internal initialization (e.g., Control fields) is done.
77      * @throws Exception
78      */

79     protected void onCreate()
80         throws Exception JavaDoc
81     {
82     }
83     
84     /**
85      * Callback that occurs when this object is "destroyed", i.e., removed from the session.
86      * @param session
87      */

88     protected void onDestroy( HttpSession JavaDoc session )
89     {
90     }
91     
92     /**
93      * Callback when this object is added to the user session.
94      */

95     public void valueBound( HttpSessionBindingEvent JavaDoc event )
96     {
97     }
98
99     /**
100      * Callback when this object is removed from the user session. Causes {@link #onDestroy} to be called. This is a
101      * framework-invoked method that should not normally be called indirectly.
102      */

103     public void valueUnbound( HttpSessionBindingEvent JavaDoc event )
104     {
105         destroy( event.getSession() );
106     }
107
108     /**
109      * Remove this instance from the session.
110      */

111     protected abstract void removeFromSession( HttpServletRequest JavaDoc request );
112     
113     /**
114      * Store this object in the user session, in the appropriate place. Used by the framework; normally should not be
115      * called directly.
116      */

117     public abstract void persistInSession( HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response );
118     
119     /**
120      * Ensures that any changes to this object will be replicated in a cluster (for failover),
121      * even if the replication scheme uses a change-detection algorithm that relies on
122      * HttpSession.setAttribute to be aware of changes. Note that this method is used by the framework
123      * and does not need to be called explicitly in most cases.
124      *
125      * @param request the current HttpServletRequest
126      */

127     public abstract void ensureFailover( HttpServletRequest JavaDoc request );
128     
129     /**
130      * Get the current ServletContext.
131      */

132     protected ServletContext JavaDoc getServletContext()
133     {
134         return _servletContext;
135     }
136     
137     /**
138      * Get the display name for this managed object.
139      */

140     public abstract String JavaDoc getDisplayName();
141     
142     /**
143      * Tell whether the given Field is uninitialized.
144      * @return <code>true</code> if the field is non-<code>null</code> and its value is <code>null</code>.
145      */

146     protected boolean fieldIsUninitialized( Field JavaDoc field )
147     {
148         try
149         {
150             return field != null && field.get( this ) == null;
151         }
152         catch ( IllegalAccessException JavaDoc e )
153         {
154             _log.error( "Error initializing field " + field.getName() + " in " + getDisplayName(), e );
155             return false;
156         }
157     }
158     
159     /**
160      * Initialize the given field with an instance. Mainly useful for the error handling.
161      */

162     protected void initializeField( Field JavaDoc field, Object JavaDoc instance )
163     {
164         if ( instance != null )
165         {
166             if ( _log.isTraceEnabled() )
167             {
168                 _log.trace( "Initializing field " + field.getName() + " in " + getDisplayName() + " with " + instance );
169             }
170             
171             try
172             {
173                 field.set( this, instance );
174             }
175             catch ( IllegalArgumentException JavaDoc e )
176             {
177                 _log.error( "Could not set field " + field.getName() + " on " + getDisplayName() +
178                             "; instance is of type " + instance.getClass().getName() + ", field type is "
179                             + field.getType().getName() );
180             }
181             catch ( IllegalAccessException JavaDoc e )
182             {
183                 _log.error( "Error initializing field " + field.getName() + " in " + getDisplayName(), e );
184             }
185         }
186     }
187 }
188
Popular Tags