Coverage Report - org.apache.tapestry.engine.state.SessionScopeManager
 
Classes in this File Line Coverage Branch Coverage Complexity
SessionScopeManager
0%
0/25
0%
0/10
1.857
 
 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.SessionStoreOptimized;
 18  
 import org.apache.tapestry.web.WebRequest;
 19  
 import org.apache.tapestry.web.WebSession;
 20  
 
 21  
 /**
 22  
  * Manager for the 'session' scope; state objects are stored as HttpSession
 23  
  * attributes. The HttpSession is created as necessary.
 24  
  * 
 25  
  * @author Howard M. Lewis Ship
 26  
  * @since 4.0
 27  
  */
 28  0
 public class SessionScopeManager implements StateObjectPersistenceManager
 29  
 {
 30  
 
 31  
     private WebRequest _request;
 32  
 
 33  
     private String _applicationId;
 34  
 
 35  
     private String buildKey(String objectName)
 36  
     {
 37  
         // For Portlets, the application id is going to be somewhat redundant,
 38  
         // since
 39  
         // the Portlet API keeps portlets seperate anyway.
 40  
 
 41  0
         return "state:" + _applicationId + ":" + objectName;
 42  
     }
 43  
 
 44  
     /**
 45  
      * Returns the session for the current request, creating it if necessary.
 46  
      */
 47  
 
 48  
     private WebSession getSession()
 49  
     {
 50  0
         return _request.getSession(true);
 51  
     }
 52  
 
 53  
     public boolean exists(String objectName)
 54  
     {
 55  0
         WebSession session = _request.getSession(false);
 56  
 
 57  0
         if (session == null) return false;
 58  
 
 59  0
         return session.getAttribute(buildKey(objectName)) != null;
 60  
     }
 61  
 
 62  
     public Object get(String objectName, StateObjectFactory factory)
 63  
     {
 64  0
         String key = buildKey(objectName);
 65  0
         WebSession session = getSession();
 66  
 
 67  0
         Object result = session.getAttribute(key);
 68  0
         if (result == null)
 69  
         {
 70  0
             result = factory.createStateObject();
 71  0
             session.setAttribute(key, result);
 72  
         }
 73  
 
 74  0
         return result;
 75  
     }
 76  
 
 77  
     public void store(String objectName, Object stateObject)
 78  
     {
 79  0
         if (stateObject instanceof SessionStoreOptimized)
 80  
         {
 81  0
             SessionStoreOptimized optimized = (SessionStoreOptimized) stateObject;
 82  
 
 83  0
             if (!optimized.isStoreToSessionNeeded())
 84  0
                 return;
 85  
         }
 86  
 
 87  0
         String key = buildKey(objectName);
 88  
 
 89  0
         WebSession session = getSession();
 90  
 
 91  0
         session.setAttribute(key, stateObject);
 92  0
     }
 93  
 
 94  
     public void setApplicationId(String applicationName)
 95  
     {
 96  0
         _applicationId = applicationName;
 97  0
     }
 98  
 
 99  
     public void setRequest(WebRequest request)
 100  
     {
 101  0
         _request = request;
 102  0
     }
 103  
 }