001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2017-2018 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.http;
020
021import java.io.Serializable;
022import java.time.Instant;
023import java.util.Map;
024import java.util.concurrent.ConcurrentHashMap;
025
026/**
027 * Implements the {@link Session} interface using a {@link ConcurrentHashMap}.
028 */
029@SuppressWarnings("serial")
030public class InMemorySession
031        extends ConcurrentHashMap<Serializable, Serializable>
032        implements Session {
033
034    @SuppressWarnings("PMD.ShortVariable")
035    private final String id;
036    private final Instant createdAt;
037    private Instant lastUsedAt;
038    private final Map<Object, Object> transientData = new ConcurrentHashMap<>();
039    private boolean beingDiscarded;
040
041    /**
042     * Create a new session.
043     */
044    @SuppressWarnings("PMD.ShortVariable")
045    public InMemorySession(String id) {
046        this.id = id;
047        createdAt = Instant.now();
048        lastUsedAt = createdAt;
049    }
050
051    /**
052     * Returns the session id.
053     * 
054     * @return the id
055     */
056    @SuppressWarnings("PMD.ShortMethodName")
057    @Override
058    public String id() {
059        return id;
060    }
061
062    /**
063     * Returns the creation time stamp.
064     * 
065     * @return the creation time stamp
066     */
067    @Override
068    public Instant createdAt() {
069        return createdAt;
070    }
071
072    /**
073     * Returns the last used (referenced in request) time stamp. 
074     * 
075     * @return the last used timestamp
076     */
077    @Override
078    public Instant lastUsedAt() {
079        return lastUsedAt;
080    }
081
082    /**
083     * Updates the last used time stamp.
084     */
085    @Override
086    public void updateLastUsedAt() {
087        this.lastUsedAt = Instant.now();
088    }
089
090    /*
091     * (non-Javadoc)
092     * 
093     * @see org.jgrapes.http.Session#transientData()
094     */
095    @Override
096    public Map<Object, Object> transientData() {
097        return transientData;
098    }
099
100    /*
101     * (non-Javadoc)
102     * 
103     * @see java.lang.Object#hashCode()
104     */
105    @Override
106    public int hashCode() {
107        return id.hashCode();
108    }
109
110    /**
111     * Marks the session as to be removed.
112     *
113     * @return false, if called before (session is already being discarded)
114     */
115    public boolean setBeingDiscarded() {
116        boolean result = !beingDiscarded;
117        beingDiscarded = true;
118        return result;
119    }
120
121    /*
122     * (non-Javadoc)
123     * 
124     * @see java.lang.Object#equals(java.lang.Object)
125     */
126    @Override
127    public boolean equals(Object obj) {
128        if (obj == null) {
129            return false;
130        }
131        if (getClass() != obj.getClass()) {
132            return false;
133        }
134        @SuppressWarnings("PMD.LooseCoupling")
135        InMemorySession other = (InMemorySession) obj;
136        if (id == null) {
137            if (other.id != null) {
138                return false;
139            }
140        } else if (!id.equals(other.id)) {
141            return false;
142        }
143        return true;
144    }
145
146    /*
147     * (non-Javadoc)
148     * 
149     * @see java.lang.Object#toString()
150     */
151    @Override
152    public String toString() {
153        StringBuilder builder = new StringBuilder(50);
154        builder.append("InMemorySession [");
155        if (id != null) {
156            builder.append("id=").append(id).append(", ");
157        }
158        if (createdAt != null) {
159            builder.append("createdAt=").append(createdAt).append(", ");
160        }
161        if (lastUsedAt != null) {
162            builder.append("lastUsedAt=").append(lastUsedAt);
163        }
164        builder.append(']');
165        return builder.toString();
166    }
167
168}