Coverage Report - org.apache.tapestry.record.SessionPropertyPersistenceStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
SessionPropertyPersistenceStrategy
0%
0/25
0%
0/4
1.5
SessionPropertyPersistenceStrategy$1
0%
0/4
N/A
1.5
SessionPropertyPersistenceStrategy$2
0%
0/3
N/A
1.5
 
 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.record;
 16  
 
 17  
 import java.util.ArrayList;
 18  
 import java.util.Collection;
 19  
 import java.util.Collections;
 20  
 
 21  
 import org.apache.hivemind.util.Defense;
 22  
 import org.apache.tapestry.engine.ServiceEncoding;
 23  
 import org.apache.tapestry.web.WebRequest;
 24  
 import org.apache.tapestry.web.WebSession;
 25  
 
 26  
 /**
 27  
  * The most basic {@link org.apache.tapestry.record.PropertyPersistenceStrategy},
 28  
  * which stores properties in the HttpSession as attributes.
 29  
  * 
 30  
  * @author Howard M. Lewis Ship
 31  
  * @since 4.0
 32  
  */
 33  0
 public class SessionPropertyPersistenceStrategy implements
 34  
         PropertyPersistenceStrategy
 35  
 {
 36  
 
 37  
     public static final String STRATEGY_ID = "session";
 38  
 
 39  
     // Really, the name of the servlet; used as a prefix on all
 40  
     // HttpSessionAttribute keys
 41  
     // to keep things straight if multiple Tapestry apps are deployed
 42  
     // in the same WAR.
 43  
 
 44  
     private String _applicationId;
 45  
 
 46  
     private WebRequest _request;
 47  
 
 48  
     public void store(String pageName, String idPath, String propertyName, Object newValue)
 49  
     {
 50  0
         Defense.notNull(pageName, "pageName");
 51  0
         Defense.notNull(propertyName, "propertyName");
 52  
 
 53  0
         WebSession session = _request.getSession(true);
 54  
 
 55  0
         String attributeName = RecordUtils.buildChangeKey(STRATEGY_ID, _applicationId, pageName, idPath, propertyName);
 56  
 
 57  0
         session.setAttribute(attributeName, newValue);
 58  0
     }
 59  
 
 60  
     public Collection getStoredChanges(String pageName)
 61  
     {
 62  0
         Defense.notNull(pageName, "pageName");
 63  
 
 64  0
         WebSession session = _request.getSession(false);
 65  
 
 66  0
         if (session == null) 
 67  0
             return Collections.EMPTY_LIST;
 68  
 
 69  0
         final Collection result = new ArrayList();
 70  
 
 71  0
         WebSessionAttributeCallback callback = new WebSessionAttributeCallback()
 72  0
         {
 73  
             public void handleAttribute(WebSession sess, String name)
 74  
             {
 75  0
                 PropertyChange change = RecordUtils.buildChange(name, sess.getAttribute(name));
 76  
 
 77  0
                 result.add(change);
 78  0
             }
 79  
         };
 80  
 
 81  0
         RecordUtils.iterateOverMatchingAttributes(STRATEGY_ID, _applicationId, pageName, session, callback);
 82  
 
 83  0
         return result;
 84  
     }
 85  
 
 86  
     public void discardStoredChanges(String pageName)
 87  
     {
 88  0
         WebSession session = _request.getSession(false);
 89  
 
 90  0
         if (session == null) return;
 91  
 
 92  0
         WebSessionAttributeCallback callback = new WebSessionAttributeCallback()
 93  0
         {
 94  
             public void handleAttribute(WebSession sess, String name)
 95  
             {
 96  0
                 sess.setAttribute(name, null);
 97  0
             }
 98  
         };
 99  
 
 100  0
         RecordUtils.iterateOverMatchingAttributes(STRATEGY_ID, _applicationId,
 101  
                 pageName, session, callback);
 102  0
     }
 103  
 
 104  
     /**
 105  
      * Does nothing; session persistence does not make use of query parameters.
 106  
      */
 107  
 
 108  
     public void addParametersForPersistentProperties(ServiceEncoding encoding,
 109  
             boolean post)
 110  
     {
 111  0
     }
 112  
 
 113  
     public void setApplicationId(String applicationName)
 114  
     {
 115  0
         _applicationId = applicationName;
 116  0
     }
 117  
 
 118  
     public void setRequest(WebRequest request)
 119  
     {
 120  0
         _request = request;
 121  0
     }
 122  
 }