01: /*
02: * Copyright 2005-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
05: * in compliance with the License. You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software distributed under the License
10: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11: * or implied. See the License for the specific language governing permissions and limitations under
12: * the License.
13: */
14:
15: package org.strecks.injection.handler;
16:
17: import javax.servlet.http.HttpServletRequest;
18:
19: import org.strecks.context.ActionContext;
20: import org.strecks.injection.factory.AnnotationFactoryUtils;
21: import org.strecks.util.Assert;
22:
23: /**
24: * Injection handler to find named attribute from <code>HttpServletRequest</code>, i.e.
25: * <i>request</i> scope
26: * @author Phil Zoio
27: */
28: public class RequestAttributeInjectionHandler implements
29: InjectionHandler {
30:
31: private String attributeName;
32:
33: /**
34: * Whether to create instance of class if not present. Will only work if declared type
35: * represents a class and can be instantiated using the <code>Class.newInstance()</code>
36: * method
37: */
38: private Class autoCreateClass;
39:
40: public RequestAttributeInjectionHandler(String attributeName,
41: Class autoCreateClass) {
42: super ();
43: Assert.notNull(attributeName);
44: this .attributeName = attributeName;
45: this .autoCreateClass = autoCreateClass;
46: }
47:
48: public String getAttributeName() {
49: return attributeName;
50: }
51:
52: public Object getValue(ActionContext injectionContext) {
53:
54: HttpServletRequest request = injectionContext.getRequest();
55:
56: Object attribute = request.getAttribute(attributeName);
57:
58: if (attribute == null && autoCreateClass != null) {
59: attribute = AnnotationFactoryUtils
60: .maybeAutoCreate(autoCreateClass);
61: request.setAttribute(attributeName, attribute);
62: }
63: return attribute;
64: }
65:
66: }
|