View Javadoc

1   /*
2    * Copyright 2004 Carlos Sanchez.
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  package net.sf.oness.common.webapp.controller.action;
17  
18  import java.util.Enumeration;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.Map;
22  import java.util.ResourceBundle;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.struts.action.ActionForm;
28  import org.apache.struts.action.ActionForward;
29  import org.apache.struts.action.ActionMapping;
30  import org.apache.struts.action.DynaActionForm;
31  import org.apache.struts.actions.LookupDispatchAction;
32  
33  /***
34   * An abstract Action that dispatches to a method based in the start of the
35   * action path.
36   * 
37   * @author Carlos Sanchez
38   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible </a>
39   * @version $Revision: 1.4 $
40   */
41  public abstract class AutoDispatchAction extends LookupDispatchAction {
42  
43      private Map keyMethodMap = null;
44  
45      private SpringActionSupport springActionSupport = null;
46  
47      /***
48       * Gets a bean from Spring configuration
49       * 
50       * @param name
51       *            bean name
52       * @return the bean
53       */
54      public Object getBean(String name) {
55          if (springActionSupport == null)
56              springActionSupport = new SpringActionSupport(servlet);
57          return springActionSupport.getBean(name);
58      }
59  
60      /***
61       * Provides the mapping from resource key to method name.
62       * 
63       * Reads the values form the ResourceBundle LookupMethods
64       * 
65       * @return Resource key / method name map.
66       */
67      public Map getKeyMethodMap() {
68          if (keyMethodMap == null) {
69  
70              Map map = new HashMap();
71  
72              ResourceBundle methods = ResourceBundle
73                      .getBundle(AutoDispatchAction.class.getPackage().getName()
74                              + ".LookupMethods");
75  
76              Enumeration keys = methods.getKeys();
77              while (keys.hasMoreElements()) {
78                  String key = (String) keys.nextElement();
79                  map.put(key, methods.getString(key));
80              }
81  
82              keyMethodMap = map;
83          }
84  
85          return keyMethodMap;
86      }
87  
88      /***
89       * Override the execute method in LookupDispatchAction to parse URLs and
90       * forward to methods without parameters.
91       * 
92       * If the action mapping parameter is present it will be used.
93       * 
94       * If not, it will check if the action path contains any of the values from
95       * the Map returned by getKeyMethodMap() and will forward to the method
96       * called after that value. If more that one match the result is not
97       * specified.
98       * 
99       * Also will forward to unspecified method when no parameter is present.
100      * 
101      * For example if the values from getKeyMethodMap() are edit=edit
102      * update=update view=view create=create other=another
103      * 
104      * The following map between paths and methods will be used
105      * 
106      * <ul>
107      * <li>edit*.do -> edit method</li>
108      * <li>update*.do -> update method</li>
109      * <li>view*.do -> view method</li>
110      * <li>create*.do -> create method</li>
111      * <li>find*.do -> find method</li>
112      * <li>other*.do -> another method</li>
113      * </ul>
114      * 
115      * @param mapping
116      *            The ActionMapping used to select this instance
117      * @param request
118      *            The HTTP request we are processing
119      * @param response
120      *            The HTTP response we are creating
121      * @param form
122      *            The optional ActionForm bean for this request (if any)
123      * @return Describes where and how control should be forwarded.
124      * @exception Exception
125      *                if an error occurs
126      */
127     public ActionForward execute(ActionMapping mapping, ActionForm form,
128             HttpServletRequest request, HttpServletResponse response)
129             throws Exception {
130 
131         if (isCancelled(request)) {
132             ActionForward af = cancelled(mapping, form, request, response);
133             if (af != null) {
134                 return af;
135             }
136         }
137 
138         /* Identify the request parameter containing the method name */
139         String parameter = mapping.getParameter();
140 
141         String keyName = (parameter == null) ? null : request
142                 .getParameter(parameter);
143 
144         /*
145          * If there is no parameter set in the action mapping get the dispatcher
146          * method from the name of the action path
147          */
148         if ((keyName == null) || (keyName.length() == 0)) {
149             Map map = getKeyMethodMap();
150             Iterator iter = map.keySet().iterator();
151             while (iter.hasNext()) {
152                 String element = (String) iter.next();
153 
154                 /* If it matches call the corresponding method */
155                 if (mapping.getPath().indexOf(element) > -1) {
156                     return dispatchMethod(mapping, form, request, response,
157                             (String) map.get(element));
158                 }
159             }
160 
161             return this.unspecified(mapping, form, request, response);
162         }
163 
164         // Identify the string to lookup
165         String methodName = getMethodName(mapping, form, request, response,
166                 parameter);
167 
168         return dispatchMethod(mapping, form, request, response, methodName);
169     }
170 
171     /***
172      * Get a property value from a DynaActionForm
173      * 
174      * @param actionForm
175      *            a DynaActionForm
176      * @param name
177      *            name of the property
178      * @return
179      */
180     protected Object getDynaProperty(ActionForm actionForm, String name) {
181         return ((DynaActionForm) actionForm).get(name);
182     }
183 
184 }