001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.record;
016    
017    import java.util.ArrayList;
018    import java.util.Collection;
019    import java.util.Collections;
020    
021    import org.apache.hivemind.util.Defense;
022    import org.apache.tapestry.engine.ServiceEncoding;
023    import org.apache.tapestry.web.WebRequest;
024    import org.apache.tapestry.web.WebSession;
025    
026    /**
027     * The most basic {@link org.apache.tapestry.record.PropertyPersistenceStrategy},
028     * which stores properties in the HttpSession as attributes.
029     * 
030     * @author Howard M. Lewis Ship
031     * @since 4.0
032     */
033    public class SessionPropertyPersistenceStrategy implements
034            PropertyPersistenceStrategy
035    {
036    
037        public static final String STRATEGY_ID = "session";
038    
039        // Really, the name of the servlet; used as a prefix on all
040        // HttpSessionAttribute keys
041        // to keep things straight if multiple Tapestry apps are deployed
042        // in the same WAR.
043    
044        private String _applicationId;
045    
046        private WebRequest _request;
047    
048        public void store(String pageName, String idPath, String propertyName, Object newValue)
049        {
050            Defense.notNull(pageName, "pageName");
051            Defense.notNull(propertyName, "propertyName");
052    
053            WebSession session = _request.getSession(true);
054    
055            String attributeName = RecordUtils.buildChangeKey(STRATEGY_ID, _applicationId, pageName, idPath, propertyName);
056    
057            session.setAttribute(attributeName, newValue);
058        }
059    
060        public Collection getStoredChanges(String pageName)
061        {
062            Defense.notNull(pageName, "pageName");
063    
064            WebSession session = _request.getSession(false);
065    
066            if (session == null) 
067                return Collections.EMPTY_LIST;
068    
069            final Collection result = new ArrayList();
070    
071            WebSessionAttributeCallback callback = new WebSessionAttributeCallback()
072            {
073                public void handleAttribute(WebSession sess, String name)
074                {
075                    PropertyChange change = RecordUtils.buildChange(name, sess.getAttribute(name));
076    
077                    result.add(change);
078                }
079            };
080    
081            RecordUtils.iterateOverMatchingAttributes(STRATEGY_ID, _applicationId, pageName, session, callback);
082    
083            return result;
084        }
085    
086        public void discardStoredChanges(String pageName)
087        {
088            WebSession session = _request.getSession(false);
089    
090            if (session == null) return;
091    
092            WebSessionAttributeCallback callback = new WebSessionAttributeCallback()
093            {
094                public void handleAttribute(WebSession sess, String name)
095                {
096                    sess.setAttribute(name, null);
097                }
098            };
099    
100            RecordUtils.iterateOverMatchingAttributes(STRATEGY_ID, _applicationId,
101                    pageName, session, callback);
102        }
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        }
112    
113        public void setApplicationId(String applicationName)
114        {
115            _applicationId = applicationName;
116        }
117    
118        public void setRequest(WebRequest request)
119        {
120            _request = request;
121        }
122    }