1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public abstract class DynaAction extends Action {
70
71
72
73
74 public final static Class[] PARAMETERS = new Class[] {ActionForm.class, HttpServletRequest.class, HttpServletResponse.class, ActionMapping.class};
75
76
77
78
79 private final Map methods = new FastHashMap();
80
81
82
83
84 private final Map locales = new HashMap();
85
86
87
88
89 public DynaAction() {
90 Method[] methods = this.getClass().getMethods();
91 for (int i = 0; i < methods.length; i++) {
92 if (Arrays.equals(methods[i].getParameterTypes(), PARAMETERS)) {
93 this.methods.put(methods[i].getName(), methods[i]);
94 }
95 }
96 ((FastHashMap)this.methods).setFast(true);
97 }
98
99
100
101
102
103
104 protected ActionForward execute(ActionForm form, HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws Exception {
105 if (logger.isDebugEnabled()) {
106 logger.debug("execute(" + mapping.getPath() + ") - start");
107 }
108 String method = mapping.getParameter();
109 if (mapping == null) {
110 throw new ActionException("action.parameter.null");
111 }
112 if (method.startsWith("@")) {
113
114 method = method.substring(1);
115
116 method = request.getParameter(method);
117 if (!methods.containsKey(method)) {
118
119
120 Map lookup = null;
121 Locale locale = request.getLocale();
122 boolean exists = true;
123 synchronized (locales) {
124 lookup = (Map)this.locales.get(locale);
125 if (lookup == null) {
126 exists = false;
127 lookup = new HashMap();
128 locales.put(locale, lookup);
129 }
130 }
131 if (!exists) {
132 synchronized (lookup) {
133
134
135
136
137
138
139 ModuleConfig module = (ModuleConfig)request.getAttribute(Globals.MODULE_KEY);
140 MessageResourcesConfig[] messages = module.findMessageResourcesConfigs();
141
142 for (int i = 0; i < messages.length; i++) {
143 MessageResources resources = this.getResources(request, messages[i].getKey());
144
145 Iterator names = this.methods.keySet().iterator();
146 while (names.hasNext()) {
147 String name = (String)names.next();
148 String message = resources.getMessage(locale, name);
149 if ((message != null) && !lookup.containsKey(message)) {
150
151
152 if (name.indexOf('.') > -1) {
153
154 name = name.substring(name.lastIndexOf('.') + 1);
155 }
156 if (methods.containsKey(name)) {
157 lookup.put(message, name);
158 }
159 }
160 }
161 }
162 }
163 }
164
165 method = (String)lookup.get(method);
166 }
167 }
168
169 if (logger.isTraceEnabled()) {
170 logger.trace(" method = " + method);
171 }
172 try {
173 Object forward = ((Method)methods.get(method)).invoke(this, new Object[] {form, request, response, mapping});
174 if (forward instanceof String) {
175 return mapping.findForward((String)forward);
176 } else {
177 return (ActionForward)forward;
178 }
179 } catch (InvocationTargetException ite) {
180 Throwable cause = ite.getTargetException();
181 if (cause instanceof Exception) {
182 throw (Exception)cause;
183 } else {
184 logger.debug("execute( " + mapping.getPath() + ") - error", cause);
185 throw new ActionException("action.error.method", cause);
186 }
187 } catch (Exception e) {
188 logger.debug("execute( " + mapping.getPath() + ") - error", e);
189 throw new ActionException("action.error.method.unknown", e);
190 }
191 }
192
193
194
195
196
197
198 public final String forward(ActionForm form, HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) {
199 return "success";
200 }
201 }