Clover coverage report - Code Coverage for tapestry release 4.0.3
Coverage timestamp: Fri May 5 2006 21:17:42 CDT
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  27 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  27 return "state:" + _applicationId + ":" + objectName;
 40    }
 41   
 42    /**
 43    * Returns the session for the current request, creating it if necessary.
 44    */
 45   
 46  21 private WebSession getSession()
 47    {
 48  21 return _request.getSession(true);
 49    }
 50   
 51  9 public boolean exists(String objectName)
 52    {
 53  9 WebSession session = _request.getSession(false);
 54   
 55  9 if (session == null)
 56  3 return false;
 57   
 58  6 return session.getAttribute(buildKey(objectName)) != null;
 59    }
 60   
 61  12 public Object get(String objectName, StateObjectFactory factory)
 62    {
 63  12 String key = buildKey(objectName);
 64  12 WebSession session = getSession();
 65   
 66  12 Object result = session.getAttribute(key);
 67  12 if (result == null)
 68    {
 69  9 result = factory.createStateObject();
 70  6 session.setAttribute(key, result);
 71    }
 72   
 73  9 return result;
 74    }
 75   
 76  12 public void store(String objectName, Object stateObject)
 77    {
 78  12 if (stateObject instanceof SessionStoreOptimized)
 79    {
 80  6 SessionStoreOptimized optimized = (SessionStoreOptimized) stateObject;
 81   
 82  6 if (!optimized.isStoreToSessionNeeded())
 83  3 return;
 84    }
 85   
 86  9 String key = buildKey(objectName);
 87   
 88  9 WebSession session = getSession();
 89   
 90  9 session.setAttribute(key, stateObject);
 91    }
 92   
 93  24 public void setApplicationId(String applicationName)
 94    {
 95  24 _applicationId = applicationName;
 96    }
 97   
 98  27 public void setRequest(WebRequest request)
 99    {
 100  27 _request = request;
 101    }
 102    }