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.ServletContext;
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>ServletContext</code>
25: * @author Phil Zoio
26: */
27: public class ContextAttributeInjectionHandler implements
28: InjectionHandler {
29:
30: private String attributeName;
31:
32: private Class autoCreateClass;
33:
34: public ContextAttributeInjectionHandler(String attributeName,
35: Class autoCreateClass) {
36: super ();
37: Assert.notNull(attributeName);
38: this .attributeName = attributeName;
39: this .autoCreateClass = autoCreateClass;
40: }
41:
42: public String getAttributeName() {
43: return attributeName;
44: }
45:
46: public Object getValue(ActionContext injectionContext) {
47:
48: ServletContext context = injectionContext.getContext();
49:
50: Object attribute = context.getAttribute(attributeName);
51:
52: if (attribute == null && autoCreateClass != null) {
53: attribute = AnnotationFactoryUtils
54: .maybeAutoCreate(autoCreateClass);
55: context.setAttribute(attributeName, attribute);
56: }
57: return attribute;
58: }
59:
60: }
|