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.web;
016    
017    import java.util.List;
018    
019    import javax.servlet.http.HttpSession;
020    
021    import org.apache.hivemind.util.Defense;
022    import org.apache.tapestry.describe.DescriptionReceiver;
023    
024    /**
025     * Adapts {@link javax.servlet.http.HttpSession}  as
026     * {@link org.apache.tapestry.web.WebSession}.
027     * 
028     * @author Howard M. Lewis Ship
029     * @since 4.0
030     */
031    public class ServletWebSession implements WebSession
032    {
033        private final HttpSession _httpSession;
034    
035        public ServletWebSession(HttpSession session)
036        {
037            Defense.notNull(session, "session");
038    
039            _httpSession = session;
040        }
041    
042        public void describeTo(DescriptionReceiver receiver)
043        {
044            receiver.describeAlternate(_httpSession);
045        }
046    
047        public List getAttributeNames()
048        {
049            return WebUtils.toSortedList(_httpSession.getAttributeNames());
050        }
051    
052        public Object getAttribute(String name)
053        {
054            return _httpSession.getAttribute(name);
055        }
056    
057        public void setAttribute(String name, Object attribute)
058        {
059            if (attribute == null)
060                _httpSession.removeAttribute(name);
061            else
062                _httpSession.setAttribute(name, attribute);
063        }
064    
065        public String getId()
066        {
067            return _httpSession.getId();
068        }
069    
070        public boolean isNew()
071        {
072            return _httpSession.isNew();
073        }
074    
075        public void invalidate()
076        {
077            _httpSession.invalidate();
078        }
079    
080        /** 
081         * {@inheritDoc}
082         */
083        public long getCreationTime()
084        {
085            return _httpSession.getCreationTime();
086        }
087    
088        /** 
089         * {@inheritDoc}
090         */
091        public long getLastAccessedTime()
092        {
093            return _httpSession.getLastAccessedTime();
094        }
095    
096        /** 
097         * {@inheritDoc}
098         */
099        public int getMaxInactiveInterval()
100        {
101            return _httpSession.getMaxInactiveInterval();
102        }
103    
104        /** 
105         * {@inheritDoc}
106         */
107        public void setMaxInactiveInterval(int interval)
108        {
109            _httpSession.setMaxInactiveInterval(interval);
110        }
111    }