KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > interceptor > action > ActionInterceptorContext


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.interceptor.action;
19
20 import org.apache.beehive.netui.pageflow.PageFlowController;
21 import org.apache.beehive.netui.pageflow.internal.InternalConstants;
22 import org.apache.beehive.netui.pageflow.interceptor.request.RequestInterceptorContext;
23 import org.apache.beehive.netui.util.logging.Logger;
24 import org.apache.beehive.netui.util.config.ConfigUtil;
25 import org.apache.beehive.netui.util.config.bean.PageflowActionInterceptors;
26 import org.apache.struts.action.ActionForward;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30 import javax.servlet.ServletContext JavaDoc;
31 import javax.servlet.ServletRequest JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import org.apache.beehive.netui.util.internal.concurrent.InternalConcurrentHashMap;
39 import java.io.Serializable JavaDoc;
40
41
42 /**
43  * Context passed to {@link ActionInterceptor} methods.
44  */

45 public class ActionInterceptorContext
46         extends RequestInterceptorContext
47 {
48     private static final String JavaDoc ACTIVE_INTERCEPTOR_CONTEXT_ATTR = InternalConstants.ATTR_PREFIX + "interceptorContext";
49     private static final String JavaDoc CACHE_ATTR = InternalConstants.ATTR_PREFIX + "actionInterceptorConfig";
50     
51     private static final Logger _log = Logger.getInstance( ActionInterceptorContext.class );
52     
53     private PageFlowController _pageFlow;
54     private InterceptorForward _originalForward;
55     private String JavaDoc _actionName;
56
57     
58     public ActionInterceptorContext( HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
59                                      ServletContext JavaDoc servletContext, PageFlowController controller,
60                                      InterceptorForward originalForward, String JavaDoc actionName )
61     {
62         super( request, response, servletContext );
63         _pageFlow = controller;
64         _originalForward = originalForward;
65         _actionName = actionName;
66     }
67
68     /**
69      * Get the page flow on which the action is being raised.
70      */

71     public PageFlowController getPageFlow()
72     {
73         return _pageFlow;
74     }
75
76     /**
77      *
78      * Get a wrapper for the original URI from the action that was intercepted. This value will be <code>null</code>
79      * if the interceptor was run before the action, or if the action itself returned <code>null</code>.
80      */

81     public InterceptorForward getOriginalForward()
82     {
83         return _originalForward;
84     }
85
86     /**
87      * Get the name of the action being raised.
88      */

89     public String JavaDoc getActionName()
90     {
91         return _actionName;
92     }
93
94     /**
95      * Set an {@link InterceptorForward} that changes the destination URI of the intercepted action. If the
96      * InterceptorForward points to a nested page flow, then {@link ActionInterceptor#afterNestedIntercept} will be
97      * called before the nested page flow returns to the original page flow.
98      */

99     public void setOverrideForward( InterceptorForward fwd, ActionInterceptor interceptor )
100     {
101         setResultOverride( fwd, interceptor );
102         
103         //
104
// If there was no original forward (i.e., this is happening before the action was invoked), create a
105
// pseudo-forward out of the original request.
106
//
107
if ( _originalForward == null ) _originalForward = new OriginalForward( getRequest() );
108         
109         //
110
// Store this context in the request.
111
//
112
getRequest().setAttribute( ACTIVE_INTERCEPTOR_CONTEXT_ATTR, this );
113     }
114
115     public ActionInterceptor getOverridingActionInterceptor()
116     {
117         return ( ActionInterceptor ) super.getOverridingInterceptor();
118     }
119
120     public InterceptorForward getInterceptorForward()
121     {
122         return ( InterceptorForward ) getResultOverride();
123     }
124    
125     public boolean hasInterceptorForward()
126     {
127         return hasResultOverride();
128     }
129     
130     public static ActionInterceptorContext getActiveContext( ServletRequest JavaDoc request, boolean consume )
131     {
132         ActionInterceptorContext context =
133                 ( ActionInterceptorContext ) request.getAttribute( ACTIVE_INTERCEPTOR_CONTEXT_ATTR );
134         if ( consume ) request.removeAttribute( ACTIVE_INTERCEPTOR_CONTEXT_ATTR );
135         return context;
136     }
137    
138     
139     private static class OriginalForward extends InterceptorForward
140     {
141         private Map JavaDoc _savedAttrs;
142     
143     
144         public OriginalForward( HttpServletRequest JavaDoc request )
145         {
146             super( request );
147             saveRequestAttrs( request );
148         }
149     
150         private void saveRequestAttrs( ServletRequest JavaDoc request )
151         {
152             _savedAttrs = new HashMap JavaDoc();
153         
154             for ( Enumeration JavaDoc e = request.getAttributeNames(); e.hasMoreElements(); )
155             {
156                 String JavaDoc attrName = ( String JavaDoc ) e.nextElement();
157                 Object JavaDoc attrVal = request.getAttribute( attrName );
158             
159                 if ( attrVal instanceof Serializable JavaDoc )
160                 {
161                     _savedAttrs.put( attrName, attrVal );
162                 }
163                 else
164                 {
165                     if ( _log.isWarnEnabled() )
166                     {
167                         _log.warn( "Dropping non-serializable request attribute " + attrName + " (" + attrVal + ")." );
168                     }
169                 }
170             }
171         }
172     
173         public void rehydrateRequest( ServletRequest JavaDoc request )
174         {
175             if ( _savedAttrs != null )
176             {
177                 for ( Iterator JavaDoc i = _savedAttrs.entrySet().iterator(); i.hasNext(); )
178                 {
179                     Map.Entry JavaDoc entry = ( Map.Entry JavaDoc ) i.next();
180                 
181                     String JavaDoc attrName = ( String JavaDoc ) entry.getKey();
182                     if ( request.getAttribute( attrName ) == null )
183                     {
184                         request.setAttribute( attrName, entry.getValue() );
185                     }
186                 }
187             }
188         }
189     }
190     
191     public List JavaDoc/*< Interceptor >*/ getActionInterceptors()
192     {
193         ServletContext JavaDoc servletContext = getServletContext();
194         InternalConcurrentHashMap/*< String, HashMap< String, ArrayList< Interceptor > > >*/ cache =
195                 ( InternalConcurrentHashMap ) servletContext.getAttribute( CACHE_ATTR );
196         
197         if ( cache == null )
198         {
199             //
200
// Don't have to synchronize here. If by some chance two initial requests come in at the same time,
201
// one of the caches will get overwritten in the ServletContext, but it will just get recreated the
202
// next time.
203
//
204
cache = new InternalConcurrentHashMap/*< String, HashMap< String, ArrayList< Interceptor > > >*/();
205             servletContext.setAttribute( CACHE_ATTR, cache );
206         }
207         
208         String JavaDoc modulePath = getPageFlow().getModulePath();
209         String JavaDoc actionName = getActionName();
210         HashMap JavaDoc/*< String, ArrayList< Interceptor > >*/ cacheByPageFlow = ( HashMap JavaDoc ) cache.get( modulePath );
211         if ( cacheByPageFlow != null )
212         {
213             List JavaDoc/*< Interceptor >*/ interceptors = ( List JavaDoc ) cacheByPageFlow.get( actionName );
214             if ( interceptors != null ) return interceptors;
215         }
216         
217         //
218
// We didn't find it in the cache -- build it.
219
//
220
if ( cacheByPageFlow == null ) cacheByPageFlow = new HashMap JavaDoc/*< String, ArrayList< Interceptor > >*/();
221         PageflowActionInterceptors config = ConfigUtil.getConfig().getPageflowActionInterceptors();
222         ArrayList JavaDoc/*< Interceptor >*/ interceptorsList = new ArrayList JavaDoc/*< Interceptor >*/();
223         
224         if ( config == null )
225         {
226             cacheByPageFlow.put( actionName, interceptorsList );
227             cache.put( modulePath, cacheByPageFlow );
228             return interceptorsList;
229         }
230         
231         //
232
// Global interceptors.
233
//
234
PageflowActionInterceptors.Global globalInterceptors = config.getGlobal();
235         
236         if ( globalInterceptors != null )
237         {
238             addInterceptors( globalInterceptors.getActionInterceptorArray(), interceptorsList, ActionInterceptor.class );
239             addSimpleInterceptors( globalInterceptors.getSimpleActionInterceptorArray(), interceptorsList );
240         }
241         
242         //
243
// Per-pageflow and per-action interceptors.
244
//
245
String JavaDoc pageFlowURI = getPageFlow().getURI();
246         PageflowActionInterceptors.PerPageflow[] perPageFlowInterceptorsConfig = config.getPerPageflowArray();
247         
248         if ( perPageFlowInterceptorsConfig != null )
249         {
250             for ( int i = 0; i < perPageFlowInterceptorsConfig.length; i++ )
251             {
252                 PageflowActionInterceptors.PerPageflow ppfi = perPageFlowInterceptorsConfig[i];
253                 
254                 if ( ppfi != null && pageFlowURI.equals( ppfi.getPageflowUri() ) )
255                 {
256                     //
257
// This is a matching page flow -- add per-pageflow interceptors.
258
//
259
addInterceptors( perPageFlowInterceptorsConfig[i].getActionInterceptorArray(), interceptorsList,
260                                      ActionInterceptor.class );
261                     addSimpleInterceptors( perPageFlowInterceptorsConfig[i].getSimpleActionInterceptorArray(),
262                                            interceptorsList );
263                     
264                     PageflowActionInterceptors.PerPageflow.PerAction[] perActionConfigs =
265                             perPageFlowInterceptorsConfig[i].getPerActionArray();
266                     
267                     if ( perActionConfigs != null )
268                     {
269                         for ( int j = 0; j < perActionConfigs.length; j++ )
270                         {
271                             PageflowActionInterceptors.PerPageflow.PerAction perActionConfig = perActionConfigs[j];
272                             
273                             if ( perActionConfig != null && actionName.equals( perActionConfig.getActionName() ) )
274                             {
275                                 //
276
// This is a matching action -- add per-action interceptors.
277
//
278
addInterceptors( perActionConfig.getActionInterceptorArray(), interceptorsList,
279                                                  ActionInterceptor.class );
280                                 addSimpleInterceptors( perActionConfig.getSimpleActionInterceptorArray(),
281                                                        interceptorsList );
282                             }
283                         }
284                     }
285                 }
286             }
287         }
288         
289         cacheByPageFlow.put( actionName, interceptorsList );
290         cache.put( modulePath, cacheByPageFlow );
291         return interceptorsList;
292     }
293     
294     private static void addSimpleInterceptors( org.apache.beehive.netui.util.config.bean.SimpleActionInterceptor[] configBeans,
295                                                List JavaDoc/*< Interceptor >*/ interceptorsList )
296     {
297         for ( int i = 0; i < configBeans.length; i++ )
298         {
299             org.apache.beehive.netui.util.config.bean.SimpleActionInterceptor configBean = configBeans[i];
300             String JavaDoc path = configBean.getInterceptPath();
301             boolean afterAction = configBean.getAfterAction();
302             SimpleActionInterceptorConfig config = new SimpleActionInterceptorConfig( path, afterAction );
303             interceptorsList.add( new SimpleActionInterceptor( config ) );
304         }
305     }
306
307     public void setOriginalForward( ActionForward origFwd )
308     {
309         _originalForward = origFwd != null ? new InterceptorForward( origFwd, getServletContext(), _pageFlow ) : null;
310     }
311     
312     public static void init( ServletContext JavaDoc servletContext )
313     {
314         // TODO: move some of the lazy-load logic in getActionInterceptors into here.
315
}
316 }
317
Popular Tags