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<?, ?> transientData = new ConcurrentHashMap<>();
039
040    /**
041     * Create a new session.
042     */
043    @SuppressWarnings("PMD.ShortVariable")
044    public InMemorySession(String id) {
045        this.id = id;
046        createdAt = Instant.now();
047        lastUsedAt = createdAt;
048    }
049
050    /**
051     * Returns the session id.
052     * 
053     * @return the id
054     */
055    @SuppressWarnings("PMD.ShortMethodName")
056    public String id() {
057        return id;
058    }
059
060    /**
061     * Returns the creation time stamp.
062     * 
063     * @return the creation time stamp
064     */
065    public Instant createdAt() {
066        return createdAt;
067    }
068
069    /**
070     * Returns the last used (referenced in request) time stamp. 
071     * 
072     * @return the last used timestamp
073     */
074    public Instant lastUsedAt() {
075        return lastUsedAt;
076    }
077
078    /**
079     * Updates the last used time stamp.
080     */
081    public void updateLastUsedAt() {
082        this.lastUsedAt = Instant.now();
083    }
084
085    /*
086     * (non-Javadoc)
087     * 
088     * @see org.jgrapes.http.Session#transientData()
089     */
090    @Override
091    public Map<?, ?> transientData() {
092        return transientData;
093    }
094
095    /*
096     * (non-Javadoc)
097     * 
098     * @see java.lang.Object#hashCode()
099     */
100    @Override
101    public int hashCode() {
102        return id.hashCode();
103    }
104
105    /*
106     * (non-Javadoc)
107     * 
108     * @see java.lang.Object#equals(java.lang.Object)
109     */
110    @Override
111    public boolean equals(Object obj) {
112        if (obj == null) {
113            return false;
114        }
115        if (getClass() != obj.getClass()) {
116            return false;
117        }
118        InMemorySession other = (InMemorySession) obj;
119        if (id == null) {
120            if (other.id != null) {
121                return false;
122            }
123        } else if (!id.equals(other.id)) {
124            return false;
125        }
126        return true;
127    }
128
129    /*
130     * (non-Javadoc)
131     * 
132     * @see java.lang.Object#toString()
133     */
134    @Override
135    public String toString() {
136        StringBuilder builder = new StringBuilder(50);
137        builder.append("InMemorySession [");
138        if (id != null) {
139            builder.append("id=");
140            builder.append(id);
141            builder.append(", ");
142        }
143        if (createdAt != null) {
144            builder.append("createdAt=");
145            builder.append(createdAt);
146            builder.append(", ");
147        }
148        if (lastUsedAt != null) {
149            builder.append("lastUsedAt=");
150            builder.append(lastUsedAt);
151        }
152        builder.append(']');
153        return builder.toString();
154    }
155
156}