KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > madvoc > injector > ActionFieldInjector


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.madvoc.injector;
4
5 import jodd.madvoc.ActionRequest;
6 import jodd.madvoc.WebApplication;
7 import jodd.madvoc.interceptor.ScopeType;
8 import jodd.madvoc.meta.In;
9 import jodd.madvoc.meta.Out;
10 import jodd.introspector.DefaultIntrospector;
11 import jodd.introspector.ClassDescriptor;
12 import jodd.bean.BeanUtil;
13 import jodd.servlet.upload.MultipartRequestWrapper;
14 import jodd.servlet.upload.FileUpload;
15 import jodd.util.ReflectUtil;
16
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.http.HttpSession JavaDoc;
19 import javax.servlet.http.HttpServletResponse JavaDoc;
20 import javax.servlet.ServletContext JavaDoc;
21 import java.lang.reflect.Field JavaDoc;
22 import java.util.*;
23
24 /**
25  * Injects servlet object parameters and attributes in the annotated action fields.
26  */

27 public class ActionFieldInjector implements ActionInjector {
28
29     public ActionFieldInjector() {
30     }
31
32     /**
33      * Scope parameters for each class attribute.
34      */

35     static class ScopeParams {
36         ScopeType scope;
37         InjectInfo[] injectInfos;
38         OutjectInfo[] outjectInfos;
39     }
40     static class InjectInfo {
41         Field JavaDoc field;
42         String JavaDoc name;
43         String JavaDoc target;
44         boolean create;
45     }
46     static class OutjectInfo {
47         Field JavaDoc field;
48         String JavaDoc name;
49         String JavaDoc target;
50     }
51
52     protected ScopeParams[] sp;
53
54     /**
55      * Injects and outjects parameters and attributes from various scopes.
56      */

57     public void inject(ActionRequest request) {
58         if (sp == null) {
59             sp = inspect(request.getAction());
60         }
61         for (ScopeParams scopeParam : sp) {
62             if (scopeParam.injectInfos == null) {
63                 continue;
64             }
65             switch (scopeParam.scope) {
66                 case PARAM: injectRequestParam(request, scopeParam.injectInfos); break;
67                 case CONTEXT: injectContextScopes(request, scopeParam.injectInfos); break;
68                 default: injectAttributes(request, scopeParam.injectInfos, scopeParam.scope); break;
69             }
70         }
71     }
72
73     public void outject(ActionRequest request) {
74         if (sp == null) {
75             sp = inspect(request.getAction());
76         }
77         for (ScopeParams scopeParam : sp) {
78             if (scopeParam.outjectInfos == null) {
79                 continue;
80             }
81             outjectAttributes(request, scopeParam.outjectInfos, scopeParam.scope);
82         }
83     }
84
85
86     // ---------------------------------------------------------------- inspect
87

88     /**
89      * Inspects all fields of action class.
90      */

91     @SuppressWarnings JavaDoc({"unchecked"})
92     protected ScopeParams[] inspect(Object JavaDoc action) {
93         ClassDescriptor cd = DefaultIntrospector.lookup(action.getClass());
94         EnumMap<ScopeType, List[]> scopes = new EnumMap<ScopeType, List[]>(ScopeType.class);
95         Field JavaDoc[] fields = cd.getAllFields(true);
96
97         for(Field JavaDoc field : fields) {
98             In in = field.getAnnotation(In.class);
99             if (in != null) {
100                 ScopeType scope = in.scope();
101                 InjectInfo ii = new InjectInfo();
102                 if (in.value().trim().length() > 0) {
103                     ii.name = in.value().trim();
104                     ii.target = field.getName();
105                 } else {
106                     ii.name = field.getName();
107                     ii.target = null;
108                 }
109                 ii.field = field;
110                 ii.create = in.create();
111
112                 List[] lists = scopes.get(scope);
113                 if (lists == null) {
114                     lists = new List[2];
115                     scopes.put(scope, lists);
116                 }
117                 List listIn = lists[0];
118                 if (listIn == null) {
119                     listIn = lists[0] = new ArrayList();
120                 }
121                 listIn.add(ii);
122             }
123
124             Out out = field.getAnnotation(Out.class);
125             if (out != null) {
126                 ScopeType scope = out.scope();
127                 OutjectInfo oi = new OutjectInfo();
128                 if (out.value().trim().length() > 0) {
129                     oi.name = out.value().trim();
130                     oi.target = field.getName();
131                 } else {
132                     oi.name = field.getName();
133                     oi.target = null;
134                 }
135                 oi.field = field;
136
137                 List[] lists = scopes.get(scope);
138                 if (lists == null) {
139                     lists = new List[2];
140                     scopes.put(scope, lists);
141                 }
142                 List listOut = lists[1];
143                 if (listOut == null) {
144                     listOut = lists[1] = new ArrayList();
145                 }
146                 listOut.add(oi);
147             }
148         }
149
150         ScopeParams[] scopeParams = new ScopeParams[scopes.size()];
151         int i = 0;
152         for (ScopeType scope : scopes.keySet()) {
153             scopeParams[i] = new ScopeParams();
154             scopeParams[i].scope = scope;
155             List<InjectInfo> listIn = scopes.get(scope)[0];
156             if (listIn != null) {
157                 scopeParams[i].injectInfos = listIn.toArray(new InjectInfo[listIn.size()]);
158             }
159             List<OutjectInfo> listOut = scopes.get(scope)[1];
160             if (listOut != null) {
161                 scopeParams[i].outjectInfos = listOut.toArray(new OutjectInfo[listOut.size()]);
162             }
163             i++;
164         }
165         return scopeParams;
166     }
167
168
169     // ---------------------------------------------------------------- inject
170

171     /**
172      * Injects http servlet request parameters. It recognizes multipart request
173      * and injects {@link jodd.servlet.upload.FileUpload}s.
174      */

175     protected void injectRequestParam(ActionRequest actionRequest, InjectInfo[] injectInfos) {
176         HttpServletRequest JavaDoc servletRequest = actionRequest.getHttpServletRequest();
177         Enumeration paramNames = servletRequest.getParameterNames();
178         while (paramNames.hasMoreElements()) {
179             String JavaDoc paramName = (String JavaDoc) paramNames.nextElement();
180             for (InjectInfo ii : injectInfos) {
181                 if (matchParamName(ii.name, paramName) == true) {
182                     String JavaDoc[] paramValues = servletRequest.getParameterValues(paramName);
183                     String JavaDoc name = getPropertyName(ii, paramName);
184                     if (ii.create == true) {
185                         BeanUtil.setDeclaredPropertyForcedSilent(actionRequest.getAction(), name, (paramValues.length == 1 ? paramValues[0] : paramValues));
186                     } else {
187                         BeanUtil.setDeclaredPropertySilent(actionRequest.getAction(), name, (paramValues.length == 1 ? paramValues[0] : paramValues));
188                     }
189                 }
190             }
191         }
192         if ((servletRequest instanceof MultipartRequestWrapper) == false) {
193             return;
194         }
195         MultipartRequestWrapper multipartRequest = (MultipartRequestWrapper) servletRequest;
196         if (multipartRequest.isMultipart() == false) {
197             return;
198         }
199         paramNames = multipartRequest.getFileParameterNames();
200         while (paramNames.hasMoreElements()) {
201             String JavaDoc paramName = (String JavaDoc) paramNames.nextElement();
202             for (InjectInfo ii : injectInfos) {
203                 if (matchParamName(ii.name, paramName) == true) {
204                     FileUpload[] paramValues = multipartRequest.getFiles(paramName);
205                     String JavaDoc name = getPropertyName(ii, paramName);
206                     if (ii.create == true) {
207                         BeanUtil.setDeclaredPropertyForcedSilent(actionRequest.getAction(), name, (paramValues.length == 1 ? paramValues[0] : paramValues));
208                 } else {
209                         BeanUtil.setDeclaredPropertySilent(actionRequest.getAction(), name, (paramValues.length == 1 ? paramValues[0] : paramValues));
210                     }
211                 }
212             }
213         }
214
215
216
217     }
218
219     /**
220      * Injects request, session and application attributes.
221      */

222     @SuppressWarnings JavaDoc({"ConstantConditions"})
223     protected void injectAttributes(ActionRequest actionRequest, InjectInfo[] injectInfos, ScopeType scope) {
224         HttpServletRequest JavaDoc servletRequest = null;
225         HttpSession JavaDoc servletSession = null;
226         ServletContext JavaDoc servletContext = null;
227         Enumeration attributeNames;
228         switch (scope) {
229             case REQUEST: servletRequest = actionRequest.getHttpServletRequest(); attributeNames = servletRequest.getAttributeNames(); break;
230             case SESSION: servletSession = actionRequest.getHttpServletRequest().getSession(); attributeNames = servletSession.getAttributeNames(); break;
231             case APPLICATION: servletContext = actionRequest.getWebApplication().getServletContext(); attributeNames = servletContext.getAttributeNames(); break;
232             default: return;
233         }
234         while (attributeNames.hasMoreElements()) {
235             String JavaDoc attrName = (String JavaDoc) attributeNames.nextElement();
236             for (InjectInfo ii : injectInfos) {
237                 if (matchParamName(ii.name, attrName) == true) {
238                     Object JavaDoc attrValue = null;
239                     switch (scope) {
240                         case REQUEST: attrValue = servletRequest.getAttribute(attrName); break;
241                         case SESSION: attrValue = servletSession.getAttribute(attrName); break;
242                         case APPLICATION: attrValue = servletContext.getAttribute(attrName); break;
243                     }
244                     String JavaDoc name = getPropertyName(ii, attrName);
245                     if (ii.create == true) {
246                         BeanUtil.setDeclaredPropertyForcedSilent(actionRequest.getAction(), name, attrValue);
247                     } else {
248                         BeanUtil.setDeclaredPropertySilent(actionRequest.getAction(), name, attrValue);
249                     }
250                 }
251             }
252         }
253     }
254
255     /**
256      * Inject context scope objects.
257      */

258     protected void injectContextScopes(ActionRequest actionRequest, InjectInfo[] injectInfos) {
259         for (InjectInfo ii : injectInfos) {
260             Class JavaDoc fieldType = ii.field.getType();
261             Object JavaDoc value = null;
262             if (ReflectUtil.isSubclass(fieldType, HttpServletRequest JavaDoc.class)) {
263                 value = actionRequest.getHttpServletRequest();
264             } else if (ReflectUtil.isSubclass(fieldType, HttpServletResponse JavaDoc.class)) {
265                 value = actionRequest.getHttpServletResponse();
266             } else if (ReflectUtil.isSubclass(fieldType, HttpSession JavaDoc.class)) {
267                 value = actionRequest.getHttpServletRequest().getSession();
268             } else if (ReflectUtil.isSubclass(fieldType, ServletContext JavaDoc.class)) {
269                 value = actionRequest.getHttpServletRequest().getSession().getServletContext();
270             } else if (ReflectUtil.isSubclass(fieldType, WebApplication.class)) {
271                 value = actionRequest.getWebApplication();
272             }
273             if (value != null) {
274                 BeanUtil.setDeclaredProperty(actionRequest.getAction(), ii.field.getName(), value);
275             }
276         }
277     }
278
279
280
281     // ---------------------------------------------------------------- outject
282

283     /**
284      * Outjects attributes from action to various scopes..
285      */

286     @SuppressWarnings JavaDoc({"ConstantConditions"})
287     protected void outjectAttributes(ActionRequest actionRequest, OutjectInfo[] outjectInfos, ScopeType scope) {
288         HttpServletRequest JavaDoc servletRequest = null;
289         HttpSession JavaDoc servletSession = null;
290         ServletContext JavaDoc servletContext = null;
291         switch (scope) {
292             case PARAM:
293             case REQUEST: servletRequest = actionRequest.getHttpServletRequest(); break;
294             case SESSION: servletSession = actionRequest.getHttpServletRequest().getSession(); break;
295             case APPLICATION: servletContext = actionRequest.getWebApplication().getServletContext(); break;
296             default: return;
297         }
298         for (OutjectInfo oi : outjectInfos) {
299             Object JavaDoc value;
300             if (oi.target == null) {
301                 value = BeanUtil.getDeclaredProperty(actionRequest.getAction(), oi.name);
302             } else {
303                 value = BeanUtil.getDeclaredProperty(actionRequest.getAction(), oi.target);
304             }
305             switch (scope) {
306                 case PARAM:
307                 case REQUEST: servletRequest.setAttribute(oi.name, value); break;
308                 case SESSION: servletSession.setAttribute(oi.name, value); break;
309                 case APPLICATION: servletContext.setAttribute(oi.name, value); break;
310             }
311         }
312     }
313
314
315     // ---------------------------------------------------------------- util
316

317     /**
318      * Matches if attribute name matches the required field name. If match is positive,
319      * injection or outjection is performed on the field.
320      */

321     protected boolean matchParamName(String JavaDoc required, String JavaDoc param) {
322         if (param.startsWith(required) == false) {
323             return false;
324         }
325         int requiredLen = required.length();
326         if (param.length() >= requiredLen + 1) {
327             if ((param.charAt(requiredLen) != '.') && (param.charAt(requiredLen) != '[')) {
328                 return false;
329             }
330         }
331         return true;
332     }
333
334     /**
335      * Returns real property name.
336      */

337     protected String JavaDoc getPropertyName(InjectInfo ii, String JavaDoc attrName) {
338         if (ii.target == null) {
339             return attrName;
340         }
341         return ii.target + attrName.substring(ii.name.length());
342     }
343
344
345 }
346
Popular Tags