KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.logging.Logger;
21 import org.apache.beehive.netui.util.config.ConfigUtil;
22 import org.apache.beehive.netui.util.config.bean.PageflowConfig;
23 import org.apache.beehive.netui.util.config.bean.DefaultSharedFlowRefs;
24 import org.apache.beehive.netui.util.config.bean.SharedFlowRef;
25 import org.apache.beehive.netui.pageflow.internal.InternalUtils;
26 import org.apache.beehive.netui.pageflow.config.PageFlowControllerConfig;
27 import org.apache.beehive.netui.pageflow.handler.ReloadableClassHandler;
28 import org.apache.beehive.netui.pageflow.handler.Handlers;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32 import javax.servlet.ServletContext JavaDoc;
33 import javax.servlet.ServletException JavaDoc;
34 import javax.servlet.ServletRequest JavaDoc;
35
36 import org.apache.struts.config.ModuleConfig;
37 import org.apache.struts.config.ControllerConfig;
38 import org.apache.struts.action.ActionServlet;
39
40 import java.io.IOException JavaDoc;
41 import java.util.Map JavaDoc;
42 import java.util.LinkedHashMap JavaDoc;
43 import java.util.Iterator JavaDoc;
44
45
46 /**
47  * Factory for creating {@link FlowController}s - user {@link PageFlowController}s and {@link SharedFlowController}s.
48  */

49 public class FlowControllerFactory
50         extends Factory
51 {
52     private static final Logger _log = Logger.getInstance( FlowControllerFactory.class );
53     
54     private ReloadableClassHandler _rch;
55     
56     protected FlowControllerFactory( ServletContext JavaDoc servletContext )
57     {
58         super( servletContext );
59         _rch = Handlers.get( servletContext ).getReloadableClassHandler();
60     }
61     
62     /**
63      * Get a FlowControllerFactory.
64      *
65      * @param servletContext the current ServletContext.
66      * @return a FlowControllerFactory for the given ServletContext. It may or may not be a cached instance.
67      */

68     public static FlowControllerFactory get( ServletContext JavaDoc servletContext )
69     {
70         return new FlowControllerFactory( servletContext );
71     }
72     
73     /**
74      * Get the page flow instance that should be associated with the given request. If it doesn't exist, create it.
75      * If one is created, the page flow stack (for nesting) will be cleared or pushed, and the new instance will be
76      * stored as the current page flow.
77      *
78      * @param context a {@link RequestContext} object which contains the current request and response.
79      * @return the {@link PageFlowController} for the request, or <code>null</code> if none was found.
80      */

81     public PageFlowController getPageFlowForRequest( RequestContext context )
82             throws InstantiationException JavaDoc, IllegalAccessException JavaDoc
83     {
84         String JavaDoc servletPath = InternalUtils.getDecodedServletPath( context.getHttpRequest() );
85         return getPageFlowForPath( context, servletPath );
86     }
87     
88     /**
89      * Get the page flow instance that should be associated with the given path. If it doesn't exist, create it.
90      * If one is created, the page flow stack (for nesting) will be cleared or pushed, and the new instance will be
91      * stored as the current page flow.
92      *
93      * @param context a {@link RequestContext} object which contains the current request and response.
94      * @param path a <strong>webapp-relative</strong> path. The path should not contain the webapp context path.
95      * @return the {@link PageFlowController} for the given path, or <code>null</code> if none was found.
96      */

97     public PageFlowController getPageFlowForPath( RequestContext context, String JavaDoc path )
98         throws InstantiationException JavaDoc, IllegalAccessException JavaDoc
99     {
100         HttpServletRequest JavaDoc request = context.getHttpRequest();
101         HttpServletResponse JavaDoc response = context.getHttpResponse();
102         PageFlowController cur = PageFlowUtils.getCurrentPageFlow( request );
103         String JavaDoc parentDir = PageFlowUtils.getModulePathForRelativeURI( path );
104         
105         //
106
// Reinitialize transient data that may have been lost on session failover.
107
//
108
if ( cur != null ) cur.reinitialize( request, response, getServletContext() );
109         
110         //
111
// If there's no current PageFlow, or if the current PageFlowController has a module path that
112
// is incompatible with the current request URI, then create the appropriate PageFlowController.
113
//
114
if ( cur == null || ! PageFlowUtils.getModulePathForRelativeURI( cur.getURI() ).equals( parentDir ) )
115         {
116             try
117             {
118                 String JavaDoc className = InternalUtils.getFlowControllerClassName( parentDir, request, getServletContext() );
119                 return className != null ? createPageFlow( context, className ) : null;
120             }
121             catch ( ClassNotFoundException JavaDoc e )
122             {
123                 if ( _log.isInfoEnabled() ) _log.info( "No page flow exists for path " + path );
124                 return null;
125             }
126         }
127         
128         return cur;
129     }
130     
131     /**
132      * Create a page flow of the given type. The page flow stack (for nesting) will be cleared or pushed, and the new
133      * instance will be stored as the current page flow.
134      *
135      * @param context a {@link RequestContext} object which contains the current request and response.
136      * @param pageFlowClassName the type name of the desired page flow.
137      * @return the newly-created {@link PageFlowController}, or <code>null</code> if none was found.
138      */

139     public PageFlowController createPageFlow( RequestContext context, String JavaDoc pageFlowClassName )
140         throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc
141     {
142         Class JavaDoc pageFlowClass = getFlowControllerClass( pageFlowClassName );
143         return createPageFlow( context, pageFlowClass );
144     }
145     
146     /**
147      * Create a {@link PageFlowController} of the given type. The PageFlowController stack (for
148      * nesting) will be cleared or pushed, and the new instance will be stored as the current
149      * PageFlowController.
150      *
151      * @param context a {@link RequestContext} object which contains the current request and response.
152      * @param pageFlowClass the type of the desired PageFlowController.
153      * @return the newly-created PageFlowController, or <code>null</code> if none was found.
154      */

155     public PageFlowController createPageFlow( RequestContext context, Class JavaDoc pageFlowClass )
156             throws InstantiationException JavaDoc, IllegalAccessException JavaDoc
157     {
158         if ( ! PageFlowController.class.isAssignableFrom( pageFlowClass ) ) return null;
159         
160         //
161
// First check if this is a request for a "long lived" page flow. If so, try
162
// PageFlowUtils.getCurrentPageFlow again, with the longLived flag.
163
//
164
HttpServletRequest JavaDoc request = context.getHttpRequest();
165         HttpServletResponse JavaDoc response = context.getHttpResponse();
166         ServletContext JavaDoc servletContext = getServletContext();
167         PageFlowController retVal = null;
168         String JavaDoc modulePath = InternalUtils.inferModulePathFromClassName( pageFlowClass.getName() );
169         ModuleConfig mc = ensureModule( modulePath, request, servletContext );
170         
171         if ( mc == null )
172         {
173             _log.error( "Struts module " + modulePath + " not found for " + pageFlowClass.getName()
174                         + "; cannot create page flow.");
175             return null;
176         }
177         
178         if ( InternalUtils.isLongLived( mc ) )
179         {
180             retVal = PageFlowUtils.getLongLivedPageFlow( modulePath, request );
181             
182             if ( _log.isDebugEnabled() )
183             {
184                 if ( retVal != null )
185                 {
186                     _log.debug( "Using long lived PageFlowController of type " + pageFlowClass.getName() );
187                 }
188             }
189         }
190         
191         //
192
// First, see if this is a nested page flow that's already on the stack. Unless "renesting" is explicitly
193
// enabled, we don't want to allow another instance of this page flow to be nested. This is a common
194
// browser back-button problem:
195
// 1) request nested page flow A
196
// 2) request nested page flow B
197
// 3) press back button, and execute an action on A.
198
//
199
// This logic does not deal with immediate self-nesting (A->A), which is taken care of in
200
// PageFlowController.forwardTo(). Nested page flows can only self-nest by forwarding to the .jpf URI, not
201
// indirectly by executing actions on themselves (think about it -- that would be a disaster).
202
//
203
boolean createdNew = false;
204         boolean isNestable = InternalUtils.isNestable( mc );
205         PageFlowStack pfStack = PageFlowStack.get( request, false );
206         
207         if ( isNestable && pfStack != null )
208         {
209             PageflowConfig options = ConfigUtil.getConfig().getPageflowConfig();
210             
211             if ( options == null || ! options.getEnableSelfNesting() )
212             {
213                 int lastIndexOfJpfClass = pfStack.lastIndexOf( request, pageFlowClass );
214                 
215                 if ( lastIndexOfJpfClass != -1 )
216                 {
217                     retVal = pfStack.popUntil( request, lastIndexOfJpfClass );
218                     retVal.persistInSession( request, response );
219                     return retVal;
220                 }
221             }
222         }
223         
224         //
225
// OK, if it's not an existing long lived page flow, and if this wasn't a nested page flow already on the
226
// stack, then create a new instance.
227
//
228
if ( retVal == null )
229         {
230             if ( _log.isDebugEnabled() )
231             {
232                 _log.debug( "Creating PageFlowController of type " + pageFlowClass.getName() );
233             }
234             
235             retVal = ( PageFlowController ) pageFlowClass.newInstance();
236             createdNew = true;
237         }
238         
239         //
240
// Store the previous PageFlowController on the nesting stack (if this one is nestable),
241
// or destroy the nesting stack.
242
//
243
if ( isNestable )
244         {
245             //
246
// Call create() on the newly-created page flow.
247
//
248
if ( createdNew ) retVal.create( request, response, servletContext );
249             PageFlowController current = PageFlowUtils.getCurrentPageFlow( request );
250             
251             if ( current != null )
252             {
253                 if ( _log.isDebugEnabled() )
254                 {
255                     _log.debug( "Pushing PageFlowController " + current + " onto the nesting stack" );
256                 }
257                 
258                 if ( pfStack == null ) pfStack = PageFlowStack.get( request, true );
259                 pfStack.push( current, request );
260             }
261             
262             retVal.reinitialize( request, response, servletContext );
263             retVal.persistInSession( request, response );
264         }
265         else
266         {
267             //
268
// Going to a non-nested pageflow. Blow away the pageflow stack.
269
//
270
if ( pfStack != null )
271             {
272                 if ( _log.isDebugEnabled() )
273                 {
274                     _log.debug( "Destroying the PageFlowController stack." );
275                 }
276                 
277                 //
278
// Start popping page flows until 1) there are none left on the stack, or 2) we find
279
// one of the type we're returning. If (2), we'll use that one (this means that executing
280
// an action on a nesting page flow while in a nested one will not destroy the nesting
281
// page flow only to create a new instance of it).
282
//
283
PageFlowController onStackAlready = pfStack.popUntil( request, retVal.getClass() );
284                 
285                 if ( onStackAlready != null )
286                 {
287                     if ( _log.isDebugEnabled() )
288                     {
289                         _log.debug( "Found a page flow of type " + retVal.getClass() + " in the stack; "
290                                     + "using that instance and stopping destruction of the nesting stack." );
291                     }
292                     
293                     retVal = onStackAlready;
294                     retVal.persistInSession( request, response );
295                 }
296                 else
297                 {
298                     //
299
// We're actually using the newly-created page flow, so call create() on it.
300
// Note that we make the call to persistInSession *before* create, so the previous flow's
301
// onDestroy() gets called before the new one's onCreate().
302
//
303
retVal.reinitialize( request, response, servletContext );
304                     retVal.persistInSession( request, response );
305                     retVal.create( request, response, servletContext );
306                 }
307             }
308             else
309             {
310                 //
311
// We're actually using the newly-created page flow, so call create() on it (*after* persisting
312
// in the session so the previous page flow's onDestroy() gets called before the new one's
313
// onCreate()).
314
//
315
retVal.reinitialize( request, response, servletContext );
316                 retVal.persistInSession( request, response );
317                 if ( createdNew ) retVal.create( request, response, servletContext );
318             }
319         }
320         
321         return retVal;
322     }
323     
324     /**
325      * Create a {@link SharedFlowController} of the given type.
326      *
327      * @param context a {@link RequestContext} object which contains the current request and response.
328      * @param sharedFlowClassName the type name of the desired SharedFlowController.
329      * @return the newly-created SharedFlowController, or <code>null</code> if none was found.
330      */

331     public SharedFlowController createSharedFlow( RequestContext context, String JavaDoc sharedFlowClassName )
332             throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc
333     {
334         Class JavaDoc sharedFlowClass = getFlowControllerClass( sharedFlowClassName );
335         return createSharedFlow( context, sharedFlowClass );
336     }
337     
338     /**
339      * Create a {@link SharedFlowController} of the given type.
340      *
341      * @param context a {@link RequestContext} object which contains the current request and response.
342      * @param sharedFlowClass the type of the desired SharedFlowController.
343      * @return the newly-created SharedFlowController, or <code>null</code> if none was found.
344      */

345     public SharedFlowController createSharedFlow( RequestContext context, Class JavaDoc sharedFlowClass )
346             throws InstantiationException JavaDoc, IllegalAccessException JavaDoc
347     {
348         assert SharedFlowController.class.isAssignableFrom( sharedFlowClass ) : sharedFlowClass.getName();
349         
350         if ( _log.isDebugEnabled() )
351         {
352             _log.debug( "Creating SharedFlowController of type " + sharedFlowClass.getName() );
353         }
354         
355         SharedFlowController retVal = ( SharedFlowController ) sharedFlowClass.newInstance();
356         HttpServletRequest JavaDoc request = context.getHttpRequest();
357         HttpServletResponse JavaDoc response = context.getHttpResponse();
358         retVal.create( request, response, getServletContext() );
359         
360         if ( _log.isDebugEnabled() )
361         {
362             _log.debug( "Storing " + retVal + " in the session..." );
363         }
364         
365         retVal.persistInSession( request, response );
366         return retVal;
367     }
368     
369     /**
370      * Get the map of shared flows for the given request. The map is derived from the shared flows
371      * that are declared (through the <code>sharedFlowRefs</code> attribute of
372      * {@link org.apache.beehive.netui.pageflow.annotations.Jpf.Controller &#64;Jpf.Controller}) in the page flow for the request.
373      *
374      * @param context a {@link RequestContext} object which contains the current request and response.
375      * @return a Map of shared-flow-name (String) to {@link SharedFlowController}.
376      * @throws ClassNotFoundException if a declared shared flow class could not be found.
377      * @throws InstantiationException if a declared shared flow class could not be instantiated.
378      * @throws IllegalAccessException if a declared shared flow class was not accessible.
379      */

380     public Map JavaDoc/*< String, SharedFlowController >*/ getSharedFlowsForRequest( RequestContext context )
381             throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc
382     {
383         String JavaDoc path = InternalUtils.getDecodedServletPath( context.getHttpRequest() );
384         return getSharedFlowsForPath( context, path );
385     }
386     
387     /**
388      * Get the map of shared flows for the given path. The map is derived from the shared flows
389      * that are declared (through the <code>sharedFlowRefs</code> attribute of
390      * {@link org.apache.beehive.netui.pageflow.annotations.Jpf.Controller &#64;Jpf.Controller}) in the page flow for the path.
391      *
392      * @param context a {@link RequestContext} object which contains the current request and response.
393      * @param path a <strong>webapp-relative</strong> path. The path should not contain the webapp context path.
394      * @return a Map of shared-flow-name (String) to {@link SharedFlowController}.
395      * @throws ClassNotFoundException if a declared shared flow class could not be found.
396      * @throws InstantiationException if a declared shared flow class could not be instantiated.
397      * @throws IllegalAccessException if a declared shared flow class was not accessible.
398      */

399     public Map JavaDoc/*< String, SharedFlowController >*/ getSharedFlowsForPath( RequestContext context, String JavaDoc path )
400         throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc
401     {
402         String JavaDoc parentDir = PageFlowUtils.getModulePathForRelativeURI( path );
403         HttpServletRequest JavaDoc request = context.getHttpRequest();
404         HttpServletResponse JavaDoc response = context.getHttpResponse();
405         ModuleConfig mc = InternalUtils.ensureModuleConfig( parentDir, request, getServletContext() );
406         LinkedHashMap JavaDoc/*< String, SharedFlowController >*/ sharedFlows = getDefaultSharedFlows( context );
407         
408         if ( mc != null )
409         {
410             ControllerConfig cc = mc.getControllerConfig();
411             
412             if ( cc instanceof PageFlowControllerConfig )
413             {
414                 Map JavaDoc/*< String, String >*/ sharedFlowTypes = ( ( PageFlowControllerConfig ) cc ).getSharedFlowTypes();
415                 
416                 if ( sharedFlowTypes != null && sharedFlowTypes.size() > 0 )
417                 {
418                     if ( sharedFlows == null ) sharedFlows = new LinkedHashMap JavaDoc/*< String, SharedFlowController >*/();
419                     
420                     for ( Iterator JavaDoc/*<Map.Entry>*/ i = sharedFlowTypes.entrySet().iterator(); i.hasNext(); )
421                     {
422                         Map.Entry JavaDoc entry = ( Map.Entry JavaDoc ) i.next();
423                         String JavaDoc name = ( String JavaDoc ) entry.getKey();
424                         String JavaDoc type = ( String JavaDoc ) entry.getValue();
425                         addSharedFlow( context, name, type, sharedFlows );
426                     }
427                     
428                     return sharedFlows;
429                 }
430             }
431         }
432         
433         //
434
// Legacy behavior: if there's no shared flow for the request, initialize a GlobalApp instance.
435
//
436
SharedFlowController ga = PageFlowUtils.getGlobalApp( request );
437         
438         if ( ga != null )
439         {
440             ga.reinitialize( request, response, getServletContext() );
441         }
442         else
443         {
444             getGlobalApp( request, response, getServletContext() );
445         }
446         
447         return sharedFlows;
448     }
449     
450     LinkedHashMap JavaDoc/*< String, SharedFlowController >*/ getDefaultSharedFlows( RequestContext context )
451             throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc
452     {
453         DefaultSharedFlowRefs defaultRefs = ConfigUtil.getConfig().getDefaultSharedFlowRefs();
454         
455         if ( defaultRefs != null )
456         {
457             SharedFlowRef[] refs = defaultRefs.getSharedFlowRefArray();
458            
459             if ( refs.length > 0 )
460             {
461                 LinkedHashMap JavaDoc/*< String, SharedFlowController >*/ sharedFlows = new LinkedHashMap JavaDoc();
462                 
463                 for ( int i = 0; i < refs.length; i++ )
464                 {
465                     SharedFlowRef ref = refs[i];
466                     if ( _log.isInfoEnabled() )
467                     {
468                         _log.info( "Shared flow of type " + ref.getType() + " is a default shared flow reference "
469                                     + "with name " + ref.getName() );
470                     }
471                     addSharedFlow( context, ref.getName(), ref.getType(), sharedFlows );
472                 }
473                 
474                 return sharedFlows;
475             }
476         }
477         
478         return null;
479     }
480     
481     private void addSharedFlow( RequestContext context, String JavaDoc name, String JavaDoc type, LinkedHashMap JavaDoc sharedFlows )
482             throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc
483     {
484         HttpServletRequest JavaDoc request = context.getHttpRequest();
485         SharedFlowController sf = PageFlowUtils.getSharedFlow( type, request );
486         
487         //
488
// Reinitialize transient data that may have been lost on session failover.
489
//
490
if ( sf != null )
491         {
492             sf.reinitialize( request, context.getHttpResponse(), getServletContext() );
493         }
494         else
495         {
496             sf = createSharedFlow( context, type );
497         }
498         
499         if ( ! ( sf instanceof GlobalApp ) ) sharedFlows.put( name, sf );
500     }
501     
502     /**
503      * Get a FlowController class. By default, this loads the class using the thread context class loader.
504      * @param className the name of the {@link FlowController} class to load.
505      * @return the loaded {@link FlowController} class.
506      * @throws ClassNotFoundException if the requested class could not be found.
507      */

508     public Class JavaDoc getFlowControllerClass( String JavaDoc className )
509         throws ClassNotFoundException JavaDoc
510     {
511         return _rch.loadClass( className );
512     }
513     
514     /**
515      * Get the page flow instance that should be associated with the given request. If it doesn't exist, create it.
516      * If one is created, the page flow stack (for nesting) will be cleared or pushed, and the new instance will be
517      * stored as the current page flow.
518      * @deprecated Use {@link #getPageFlowForRequest(RequestContext)} instead.
519      *
520      * @param request the current HttpServletRequest.
521      * @param response the current HttpServletResponse.
522      * @param servletContext the current ServletContext.
523      * @return the {@link PageFlowController} for the request, or <code>null</code> if none was found.
524      */

525     public static PageFlowController getPageFlowForRequest( HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
526                                                             ServletContext JavaDoc servletContext )
527     {
528         return getPageFlowForRelativeURI( request, response, InternalUtils.getDecodedServletPath( request ), servletContext );
529     }
530     
531     /**
532      * Get the page flow instance that should be associated with the given URI. If it doesn't exist, create it.
533      * If one is created, the page flow stack (for nesting) will be cleared or pushed, and the new instance will be
534      * stored as the current page flow.
535      * @deprecated Use {@link #getPageFlowForPath(RequestContext, String)} instead. The URI must be stripped of the
536      * webapp context path before being passed.
537      *
538      * @param request the current HttpServletRequest.
539      * @param response the current HttpServletResponse.
540      * @param uri a server-relative URI. The URI should contain the webapp context path.
541      * @param servletContext the current ServletContext.
542      * @return the {@link PageFlowController} for the given URI, or <code>null</code> if none was found.
543      */

544     public static PageFlowController getPageFlowForURI( HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
545                                                         String JavaDoc uri, ServletContext JavaDoc servletContext )
546     {
547         return getPageFlowForRelativeURI( request, response, PageFlowUtils.getRelativeURI( request, uri, null ),
548                                           servletContext );
549     }
550     
551     /**
552      * Get the page flow instance that should be associated with the given path. If it doesn't exist, create it.
553      * If one is created, the page flow stack (for nesting) will be cleared or pushed, and the new instance will be
554      * stored as the current page flow.
555      * @deprecated Use {@link #getPageFlowForPath(RequestContext, String)} instead.
556      *
557      * @param request the current HttpServletRequest.
558      * @param response the current HttpServletResponse.
559      * @param path a <strong>webapp-relative</strong> path. The path should not contain the webapp context path.
560      * @param servletContext the current ServletContext.
561      * @return the {@link PageFlowController} for the given path, or <code>null</code> if none was found.
562      */

563     public static PageFlowController getPageFlowForRelativeURI( HttpServletRequest JavaDoc request,
564                                                                 HttpServletResponse JavaDoc response, String JavaDoc path,
565                                                                 ServletContext JavaDoc servletContext )
566     {
567         try
568         {
569             return get( servletContext ).getPageFlowForPath( new RequestContext( request, response ), path );
570         }
571         catch ( InstantiationException JavaDoc e )
572         {
573             _log.error( "Could not instantiate PageFlowController for request " + request.getRequestURI(), e );
574             return null;
575         }
576         catch ( IllegalAccessException JavaDoc e )
577         {
578             _log.error( "Could not instantiate PageFlowController for request " + request.getRequestURI(), e );
579             return null;
580         }
581     }
582     
583     /**
584      * Create a page flow of the given type. The page flow stack (for nesting) will be cleared or pushed, and the new
585      * instance will be stored as the current page flow.
586      * @deprecated Use {@link #createPageFlow(RequestContext, String)} instead.
587      *
588      * @param request the current HttpServletRequest.
589      * @param response the current HttpServletResponse.
590      * @param pageFlowClassName the type name of the desired page flow.
591      * @param servletContext the current ServletContext.
592      * @return the newly-created {@link PageFlowController}, or <code>null</code> if none was found.
593      */

594     public static PageFlowController getPageFlow( String JavaDoc pageFlowClassName, HttpServletRequest JavaDoc request,
595                                                   HttpServletResponse JavaDoc response, ServletContext JavaDoc servletContext )
596     {
597         try
598         {
599             return get( servletContext ).createPageFlow( new RequestContext( request, response ), pageFlowClassName );
600         }
601         catch ( ClassNotFoundException JavaDoc e)
602         {
603             if ( _log.isErrorEnabled() ) _log.error( "Requested page flow class " + pageFlowClassName + " not found." );
604             return null;
605         }
606         catch ( InstantiationException JavaDoc e )
607         {
608             _log.error( "Could not instantiate PageFlowController of type " + pageFlowClassName, e );
609             return null;
610         }
611         catch ( IllegalAccessException JavaDoc e )
612         {
613             _log.error( "Could not instantiate PageFlowController of type " + pageFlowClassName, e );
614             return null;
615         }
616     }
617     
618     /**
619      * Get or create the current user session's GlobalApp instance (from the Global.app file).
620      * @deprecated Global.app is deprecated; use shared flows and {@link #getSharedFlowsForRequest(RequestContext)}.
621      *
622      * @param request the current HttpServletRequest.
623      * @param response the current HttpServletResponse.
624      * @return the current session's {@link GlobalApp} instance, or a new one (based on Global.app) if none is found.
625      * If Global.app does not exist in the current webapp, <code>null</code> is returned.
626      */

627     public static GlobalApp getGlobalApp( HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
628                                           ServletContext JavaDoc servletContext )
629     {
630         GlobalApp current = PageFlowUtils.getGlobalApp( request );
631         if ( current != null ) return current;
632         
633         try
634         {
635             try
636             {
637                 FlowControllerFactory factory = get( servletContext );
638                 SharedFlowController sf =
639                     factory.createSharedFlow( new RequestContext( request, response ), PageFlowConstants.GLOBALAPP_CLASSNAME );
640                 
641                 if ( ! ( sf instanceof GlobalApp ) )
642                 {
643                     _log.error( "Class " + PageFlowConstants.GLOBALAPP_CLASSNAME + " is not an instance of "
644                                 + GlobalApp.class.getName() );
645                     return null;
646                 }
647                 
648                 return ( GlobalApp ) sf;
649             }
650             catch ( InstantiationException JavaDoc e )
651             {
652                 _log.error( "Could not instantiate Global.app.", e );
653                 return null;
654             }
655             catch ( IllegalAccessException JavaDoc e )
656             {
657                 _log.error( "Could not instantiate Global.app", e );
658                 return null;
659             }
660         }
661         catch ( ClassNotFoundException JavaDoc e )
662         {
663             // Ignore -- this is expected if there's no (legacy) Global.app.
664
return null;
665         }
666     }
667     
668     private static ModuleConfig ensureModule( String JavaDoc modulePath, ServletRequest JavaDoc request,
669                                               ServletContext JavaDoc servletContext )
670     {
671         ModuleConfig mc = InternalUtils.getModuleConfig( modulePath, servletContext );
672         
673         if ( mc == null )
674         {
675             ActionServlet as = InternalUtils.getActionServlet( servletContext );
676                 
677             if ( as instanceof AutoRegisterActionServlet )
678             {
679                 AutoRegisterActionServlet das = ( AutoRegisterActionServlet ) as;
680                 
681                 try
682                 {
683                     mc = das.ensureModuleRegistered( modulePath, request );
684                 }
685                 catch ( IOException JavaDoc e )
686                 {
687                     _log.error( "Could not register Struts module for path " + modulePath, e );
688                 }
689                 catch ( ServletException JavaDoc e )
690                 {
691                     _log.error( "Could not register Struts module for path " + modulePath, e.getRootCause() );
692                 }
693             }
694         }
695         
696         return mc;
697     }
698     
699     /**
700      * Create a page flow of the given type. The page flow stack (for nesting) will be cleared or pushed, and the new
701      * instance will be stored as the current page flow.
702      * @deprecated Use {@link #createPageFlow(RequestContext, Class)} instead.
703      *
704      * @param request the current HttpServletRequest.
705      * @param response the current HttpServletResponse.
706      * @param pageFlowClass the type of the desired page flow.
707      * @param servletContext the current ServletContext.
708      * @return the newly-created {@link PageFlowController}, or <code>null</code> if none was found.
709      */

710     public static PageFlowController getPageFlow( Class JavaDoc pageFlowClass, HttpServletRequest JavaDoc request,
711                                                   HttpServletResponse JavaDoc response, ServletContext JavaDoc servletContext )
712     {
713         try
714         {
715             FlowControllerFactory factory = get( servletContext );
716             return factory.createPageFlow( new RequestContext( request, response ), pageFlowClass );
717         }
718         catch ( InstantiationException JavaDoc e )
719         {
720             _log.error( "Could not instantiate PageFlowController of type " + pageFlowClass.getName(), e );
721             return null;
722         }
723         catch ( IllegalAccessException JavaDoc e )
724         {
725             _log.error( "Could not instantiate PageFlowController of type " + pageFlowClass.getName(), e );
726             return null;
727         }
728     }
729 }
730
Popular Tags