Clover Coverage Report - SmartWeb
Coverage timestamp: Sun Jun 8 2008 21:20:12 CEST
../../../img/srcFileCovDistChart0.png 29% of files have more coverage
45   182   19   15
26   101   0,42   3
3     6,33  
1    
10,8% of code in this file is excluded from these metrics.
 
  DynaAction       Line # 51 45 19 0% 0.0
 
No Tests
 
1    /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10    * This library 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. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this library; if not, write to the Free Software
17    * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18    *
19    * For further informations on the SmartWeb Framework please visit
20    *
21    * http://smartweb.sourceforge.net
22    */
23   
24    package net.smartlab.web;
25   
26    import java.lang.reflect.InvocationTargetException;
27    import java.lang.reflect.Method;
28    import java.util.Arrays;
29    import java.util.HashMap;
30    import java.util.Iterator;
31    import java.util.Locale;
32    import java.util.Map;
33   
34    import javax.servlet.http.HttpServletRequest;
35    import javax.servlet.http.HttpServletResponse;
36   
37    import org.apache.commons.collections.FastHashMap;
38    import org.apache.struts.Globals;
39    import org.apache.struts.action.ActionForm;
40    import org.apache.struts.action.ActionForward;
41    import org.apache.struts.action.ActionMapping;
42    import org.apache.struts.config.MessageResourcesConfig;
43    import org.apache.struts.config.ModuleConfig;
44    import org.apache.struts.util.MessageResources;
45   
46    /**
47    * TODO documentation
48    *
49    * @author rlogiacco
50    */
 
51    public abstract class DynaAction extends Action {
52   
53    /**
54    * Standard dynamic method parameters.
55    */
56    private final static Class[] PARAMETERS = new Class[] {ActionForm.class, HttpServletRequest.class, HttpServletResponse.class, ActionMapping.class};
57   
58    /**
59    * Mapping of exposed methods.
60    */
61    private final Map methods = new FastHashMap();
62   
63    /**
64    * Mapping of locales to resource messages for reverse lookup.
65    */
66    private final Map locales = new HashMap();
67   
68    /**
69    * Default constructor.
70    */
 
71  0 toggle public DynaAction() {
72  0 Method[] methods = this.getClass().getMethods();
73  0 for (int i = 0; i < methods.length; i++) {
74  0 if (Arrays.equals(methods[i].getParameterTypes(), PARAMETERS)) {
75  0 this.methods.put(methods[i].getName(), methods[i]);
76    }
77    }
78  0 ((FastHashMap)this.methods).setFast(true);
79    }
80   
81    /**
82    * @see net.smartlab.web.Action#execute(org.apache.struts.action.ActionForm,
83    * javax.servlet.http.HttpServletRequest,
84    * javax.servlet.http.HttpServletResponse, ActionMapping)
85    */
 
86  0 toggle protected ActionForward execute(ActionForm form, HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws Exception {
87  0 if (logger.isDebugEnabled()) {
88  0 logger.debug("execute(" + mapping.getPath() + ") - start");
89    }
90  0 String method = mapping.getParameter();
91  0 if (mapping == null) {
92  0 throw new ActionException("action.parameter.null");
93    }
94  0 if (method.startsWith("@")) {
95    // Strip the leading char
96  0 method = method.substring(1);
97    // Dynamically find the method using the request parameter value
98  0 method = request.getParameter(method);
99  0 if (!methods.containsKey(method)) {
100    // Try to reverse resolve using the resource bundle
101    // Based on this request's Locale get the lookupMap
102  0 Map lookup = null;
103  0 Locale locale = request.getLocale();
104  0 boolean exists = true;
105  0 synchronized (locales) {
106  0 lookup = (Map)this.locales.get(locale);
107  0 if (lookup == null) {
108  0 exists = false;
109  0 lookup = new HashMap();
110  0 locales.put(locale, lookup);
111    }
112    }
113  0 if (!exists) {
114  0 synchronized (lookup) {
115  0 method = mapping.getParameter().substring(1);
116    /*
117    * This is the first time this Locale is used so build
118    * the reverse lookup Map. Search for message keys in
119    * all configured MessageResources for the current
120    * module.
121    */
122  0 ModuleConfig module = (ModuleConfig)request.getAttribute(Globals.MODULE_KEY);
123  0 MessageResourcesConfig[] messages = module.findMessageResourcesConfigs();
124    // Look through all module's MessageResources
125  0 for (int i = 0; i < messages.length; i++) {
126  0 MessageResources resources = this.getResources(request, messages[i].getKey());
127    // Look for method name in Messages
128  0 Iterator names = this.methods.keySet().iterator();
129  0 while (names.hasNext()) {
130  0 String name = (String)names.next();
131  0 String message = resources.getMessage(locale, method + '.' + name);
132  0 if ((message != null) && !lookup.containsKey(message)) {
133    // Found method name and haven't added to
134    // Map yet, so add the text
135  0 lookup.put(message, name);
136    }
137    }
138    }
139    }
140    }
141    // Find the method
142  0 method = (String)lookup.get(method);
143    }
144    }
145    // Execute the specified method
146  0 if (logger.isTraceEnabled()) {
147  0 logger.trace(" method = " + method);
148    }
149  0 try {
150  0 Object forward = ((Method)methods.get(method)).invoke(this, new Object[] {form, request, response, mapping});
151  0 if (forward instanceof String) {
152  0 return mapping.findForward((String)forward);
153    } else {
154  0 return (ActionForward)forward;
155    }
156    } catch (InvocationTargetException ite) {
157    Throwable cause = ite.getTargetException();
158    if (cause instanceof Exception) {
159    throw (Exception)cause;
160    } else {
161    logger.error("execute( " + mapping.getPath() + ") - error", cause);
162    throw new ActionException("action.method.exception", cause);
163    }
164    } catch (Exception e) {
165    logger.error("execute( " + mapping.getPath() + ") - error", e);
166    throw new ActionException("action.method.unknown", method);
167    }
168    }
169   
170    /**
171    * TODO documentation
172    *
173    * @param form
174    * @param request
175    * @param response
176    * @param mapping
177    * @return
178    */
 
179  0 toggle public final String forward(ActionForm form, HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) {
180  0 return "success";
181    }
182    }