01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.web.context.request;
18:
19: import org.springframework.beans.factory.ObjectFactory;
20: import org.springframework.beans.factory.config.Scope;
21:
22: /**
23: * Abstract {@link Scope} implementation that reads from a particular scope
24: * in the current thread-bound {@link RequestAttributes} object.
25: *
26: * <p>Subclasses simply need to implement {@link #getScope()} to instruct
27: * this class which {@link RequestAttributes} scope to read attributes from.
28: *
29: * <p>Subclasses may wish to override the {@link #get} and {@link #remove}
30: * methods to add synchronization around the call back into this super class.
31: *
32: * @author Rod Johnson
33: * @author Juergen Hoeller
34: * @author Rob Harrop
35: * @since 2.0
36: */
37: public abstract class AbstractRequestAttributesScope implements Scope {
38:
39: public Object get(String name, ObjectFactory objectFactory) {
40: RequestAttributes attributes = RequestContextHolder
41: .currentRequestAttributes();
42: Object scopedObject = attributes.getAttribute(name, getScope());
43: if (scopedObject == null) {
44: scopedObject = objectFactory.getObject();
45: attributes.setAttribute(name, scopedObject, getScope());
46: }
47: return scopedObject;
48: }
49:
50: public Object remove(String name) {
51: RequestAttributes attributes = RequestContextHolder
52: .currentRequestAttributes();
53: Object scopedObject = attributes.getAttribute(name, getScope());
54: if (scopedObject != null) {
55: attributes.removeAttribute(name, getScope());
56: return scopedObject;
57: } else {
58: return null;
59: }
60: }
61:
62: public void registerDestructionCallback(String name,
63: Runnable callback) {
64: RequestAttributes attributes = RequestContextHolder
65: .currentRequestAttributes();
66: attributes.registerDestructionCallback(name, callback,
67: getScope());
68: }
69:
70: /**
71: * Template method that determines the actual target scope.
72: * @return the target scope, in the form of an appropriate
73: * {@link RequestAttributes} constant
74: * @see RequestAttributes#SCOPE_REQUEST
75: * @see RequestAttributes#SCOPE_SESSION
76: * @see RequestAttributes#SCOPE_GLOBAL_SESSION
77: */
78: protected abstract int getScope();
79:
80: }
|