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