package com.nuliji.tools.shiro.session;

import java.io.Serializable;
import java.util.Collection;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * Created by gaojie on 2017/11/7.
 */
public class CustomSessionDao extends AbstractSessionDAO {
    private static Logger logger = LoggerFactory.getLogger(CustomSessionDao.class);
    private SessionRepository sessionRepository;

    @Override
    public void delete(Session session) {
        if (session == null) {
            logger.error("session or session id is null");
            return;
        }
        Serializable id = session.getId();
        if (id != null)
            getSessionRepository().deleteSession(id);
    }
    @Override
    public Collection<Session> getActiveSessions() {
        return getSessionRepository().getAllSessions();
    }
    @Override
    public void update(Session session) throws UnknownSessionException {
        getSessionRepository().saveSession(session);
    }
    @Override
    protected Serializable doCreate(Session session) {
        Serializable sessionId = this.generateSessionId(session);
        this.assignSessionId(session, sessionId);
        getSessionRepository().saveSession(session);
        return sessionId;
    }
    @Override
    protected Session doReadSession(Serializable sessionId) {
        return getSessionRepository().getSession(sessionId);
    }

    public SessionRepository getSessionRepository() {
        return sessionRepository;
    }

    public void setSessionRepository(SessionRepository sessionRepository) {
        this.sessionRepository = sessionRepository;
    }
}