Clover coverage report - Code Coverage for tapestry release 4.0.1
Coverage timestamp: Fri Mar 31 2006 09:12:14 EST
file stats: LOC: 102   Methods: 7
NCLOC: 55   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SessionScopeManager.java 100% 100% 100% 100%
coverage
 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 attributes. The
 23    * HttpSession is created as necessary.
 24    *
 25    * @author Howard M. Lewis Ship
 26    * @since 4.0
 27    */
 28    public class SessionScopeManager implements StateObjectPersistenceManager
 29    {
 30    private WebRequest _request;
 31   
 32    private String _applicationId;
 33   
 34  18 private String buildKey(String objectName)
 35    {
 36    // For Portlets, the application id is going to be somewhat redundant, since
 37    // the Portlet API keeps portlets seperate anyway.
 38   
 39  18 return "state:" + _applicationId + ":" + objectName;
 40    }
 41   
 42    /**
 43    * Returns the session for the current request, creating it if necessary.
 44    */
 45   
 46  14 private WebSession getSession()
 47    {
 48  14 return _request.getSession(true);
 49    }
 50   
 51  6 public boolean exists(String objectName)
 52    {
 53  6 WebSession session = _request.getSession(false);
 54   
 55  6 if (session == null)
 56  2 return false;
 57   
 58  4 return session.getAttribute(buildKey(objectName)) != null;
 59    }
 60   
 61  8 public Object get(String objectName, StateObjectFactory factory)
 62    {
 63  8 String key = buildKey(objectName);
 64  8 WebSession session = getSession();
 65   
 66  8 Object result = session.getAttribute(key);
 67  8 if (result == null)
 68    {
 69  6 result = factory.createStateObject();
 70  4 session.setAttribute(key, result);
 71    }
 72   
 73  6 return result;
 74    }
 75   
 76  8 public void store(String objectName, Object stateObject)
 77    {
 78  8 if (stateObject instanceof SessionStoreOptimized)
 79    {
 80  4 SessionStoreOptimized optimized = (SessionStoreOptimized) stateObject;
 81   
 82  4 if (!optimized.isStoreToSessionNeeded())
 83  2 return;
 84    }
 85   
 86  6 String key = buildKey(objectName);
 87   
 88  6 WebSession session = getSession();
 89   
 90  6 session.setAttribute(key, stateObject);
 91    }
 92   
 93  16 public void setApplicationId(String applicationName)
 94    {
 95  16 _applicationId = applicationName;
 96    }
 97   
 98  18 public void setRequest(WebRequest request)
 99    {
 100  18 _request = request;
 101    }
 102    }