View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.control;
16  
17  import org.figure8.join.util.LogUtil;
18  import org.figure8.join.core.ContainerContextHandler;
19  import org.figure8.join.core.InvalidParameterException;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.struts.action.Action;
23  import org.apache.struts.action.ActionMapping;
24  import org.apache.struts.tiles.TilesRequestProcessor;
25  import org.springframework.web.struts.DelegatingActionUtils;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import java.io.IOException;
31  /**
32   * This is an extension of Struts <code>RequestProcessor</code> that delegates
33   * the action creation to Spring if a context is available ... Otherwise the default
34   * behaviour implemented into Struts is used (ie. instanciation of action class)
35   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
36   * @version $Revision: 1.2 $
37   */
38  public class DelegatingSpringRequestProcessor extends TilesRequestProcessor{
39  
40     // Static -------------------------------------------------------------------
41  
42     /** Get a commons logger ... */
43     private static Log log = LogUtil.getLog(DelegatingSpringRequestProcessor.class);
44  
45  
46     // Override of TilesRequestProcessor ----------------------------------------
47  
48     /**
49      * Override the base class method to return the delegate action.
50      * @param request The servlet container request wrapper
51      * @param response The servlet container response wrapper
52      * @param mapping The mappings available for this action
53      * @return The corresponding action instance.
54      */
55     protected Action processActionCreate(HttpServletRequest request, HttpServletResponse response,
56                                          ActionMapping mapping) throws IOException{
57        try{
58           // Try returning Spring delegate action.
59           Action delegate = getDelegateAction(mapping);
60           delegate.setServlet(this.servlet);
61           return delegate;
62        }
63        catch (IllegalStateException ise){
64           // Spring context is not available. Trace.
65           log.info("Spring Context is not available for the moment...");
66        }
67        catch (InvalidParameterException ipe){
68           // Requested bean does not exists into context.
69           log.info("Requested Action does not exists into context...");
70        }
71        log.info("Spring delegate Action cannot be found. Apply the default behaviour.");
72        return super.processActionCreate(request, response, mapping);
73     }
74  
75  
76     // Protected ----------------------------------------------------------------
77  
78     /**
79      * Return the delegate Action for the given mapping.
80      * <p>The default implementation determines a bean name from the given
81      * ActionMapping and looks up the corresponding bean in the WebApplicationContext.
82      * @param mapping the Struts ActionMapping
83      * @return the delegate Action
84      * @see #determineActionBeanName
85      */
86     protected Action getDelegateAction(ActionMapping mapping) throws InvalidParameterException{
87        String beanName = determineActionBeanName(mapping);
88        return (Action)ContainerContextHandler.getInstance().getComponent(beanName);
89     }
90  
91     /**
92      * Determine the name of the Action bean, to be looked up in
93      * the WebApplicationContext.
94  	 * <p>The default implementation takes the mapping path and
95      * prepends the module prefix, if any.
96      * @param mapping the Struts ActionMapping
97      * @return the name of the Action bean
98      * @see DelegatingActionUtils#determineActionBeanName
99      * @see ActionMapping#getPath
100     */
101    protected String determineActionBeanName(ActionMapping mapping){
102       return DelegatingActionUtils.determineActionBeanName(mapping);
103    }
104 }