001    // Copyright 2006, 2007, 2008, 2009, 2011 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.tapestry5.internal.services;
016    
017    import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
018    import org.apache.tapestry5.ioc.internal.util.InternalUtils;
019    import org.apache.tapestry5.services.Session;
020    import org.apache.tapestry5.services.SessionPersistedObjectAnalyzer;
021    
022    import javax.servlet.http.HttpServletRequest;
023    import javax.servlet.http.HttpSession;
024    import java.util.Collections;
025    import java.util.Enumeration;
026    import java.util.List;
027    import java.util.Map;
028    
029    /**
030     * A thin wrapper around {@link HttpSession}.
031     */
032    public class SessionImpl implements Session
033    {
034        private final HttpServletRequest request;
035        private final HttpSession session;
036    
037        private boolean invalidated = false;
038    
039        public SessionImpl(HttpServletRequest request, HttpSession session)
040        {
041            this.request = request;
042            this.session = session;
043        }
044    
045        public Object getAttribute(String name)
046        {
047            return session.getAttribute(name);
048        }
049    
050        public List<String> getAttributeNames()
051        {
052            return InternalUtils.toList(session.getAttributeNames());
053        }
054    
055        public void setAttribute(String name, Object value)
056        {
057            session.setAttribute(name, value);
058        }
059    
060        public List<String> getAttributeNames(String prefix)
061        {
062            List<String> result = CollectionFactory.newList();
063    
064            Enumeration e = session.getAttributeNames();
065            while (e.hasMoreElements())
066            {
067                String name = (String) e.nextElement();
068    
069                if (name.startsWith(prefix)) result.add(name);
070            }
071    
072            Collections.sort(result);
073    
074            return result;
075        }
076    
077        public int getMaxInactiveInterval()
078        {
079            return session.getMaxInactiveInterval();
080        }
081    
082        public void invalidate()
083        {
084            invalidated = true;
085    
086            session.invalidate();
087        }
088    
089        public boolean isInvalidated()
090        {
091            if (invalidated) return true;
092    
093            // The easy case is when the session was invalidated through the Tapestry Session
094            // object. The hard case is when the HttpSession was invalidated outside of Tapestry,
095            // in which case, request.getSession() will return a new HttpSession instance (or null)
096    
097            invalidated = request.getSession(false) != session;
098    
099            return invalidated;
100        }
101    
102        public void setMaxInactiveInterval(int seconds)
103        {
104            session.setMaxInactiveInterval(seconds);
105        }
106    
107        public void restoreDirtyObjects()
108        {
109    
110        }
111    }