KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > engine > state > SessionScopeManager


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.engine.state;
16
17 import org.apache.tapestry.web.WebRequest;
18 import org.apache.tapestry.web.WebSession;
19
20 /**
21  * Manager for the 'session' scope; state objects are stored as HttpSession attributes. The
22  * HttpSession is created as necessary.
23  *
24  * @author Howard M. Lewis Ship
25  * @since 4.0
26  */

27 public class SessionScopeManager implements StateObjectPersistenceManager
28 {
29     private WebRequest _request;
30
31     private String JavaDoc _applicationId;
32
33     private String JavaDoc buildKey(String JavaDoc objectName)
34     {
35         // For Portlets, the application id is going to be somewhat redundant, since
36
// the Portlet API keeps portlets seperate anyway.
37

38         return "state:" + _applicationId + ":" + objectName;
39     }
40
41     /**
42      * Returns the session for the current request, creating it if necessary.
43      */

44
45     private WebSession getSession()
46     {
47         return _request.getSession(true);
48     }
49
50     public boolean exists(String JavaDoc objectName)
51     {
52         WebSession session = _request.getSession(false);
53
54         if (session == null)
55             return false;
56
57         return session.getAttribute(buildKey(objectName)) != null;
58     }
59
60     public Object JavaDoc get(String JavaDoc objectName, StateObjectFactory factory)
61     {
62         String JavaDoc key = buildKey(objectName);
63         WebSession session = getSession();
64
65         Object JavaDoc result = session.getAttribute(key);
66         if (result == null)
67         {
68             result = factory.createStateObject();
69             session.setAttribute(key, result);
70         }
71
72         return result;
73     }
74
75     public void store(String JavaDoc objectName, Object JavaDoc stateObject)
76     {
77         String JavaDoc key = buildKey(objectName);
78
79         WebSession session = getSession();
80
81         session.setAttribute(key, stateObject);
82     }
83
84     public void setApplicationId(String JavaDoc applicationName)
85     {
86         _applicationId = applicationName;
87     }
88
89     public void setRequest(WebRequest request)
90     {
91         _request = request;
92     }
93 }
Popular Tags