KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > context > request > AbstractRequestAttributesScope


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc get(String JavaDoc name, ObjectFactory objectFactory) {
40         RequestAttributes attributes = RequestContextHolder.currentRequestAttributes();
41         Object JavaDoc scopedObject = attributes.getAttribute(name, getScope());
42         if (scopedObject == null) {
43             scopedObject = objectFactory.getObject();
44             attributes.setAttribute(name, scopedObject, getScope());
45         }
46         return scopedObject;
47     }
48
49     public Object JavaDoc remove(String JavaDoc name) {
50         RequestAttributes attributes = RequestContextHolder.currentRequestAttributes();
51         Object JavaDoc scopedObject = attributes.getAttribute(name, getScope());
52         if (scopedObject != null) {
53             attributes.removeAttribute(name, getScope());
54             return scopedObject;
55         }
56         else {
57             return null;
58         }
59     }
60
61     public void registerDestructionCallback(String JavaDoc name, Runnable JavaDoc callback) {
62         RequestAttributes attributes = RequestContextHolder.currentRequestAttributes();
63         attributes.registerDestructionCallback(name, callback, getScope());
64     }
65
66
67     /**
68      * Template method that determines the actual target scope.
69      * @return the target scope, in the form of an appropriate
70      * {@link RequestAttributes} constant
71      * @see RequestAttributes#SCOPE_REQUEST
72      * @see RequestAttributes#SCOPE_SESSION
73      * @see RequestAttributes#SCOPE_GLOBAL_SESSION
74      */

75     protected abstract int getScope();
76
77 }
78
Popular Tags