kim.sesame.framework.web.context.UserContext Maven / Gradle / Ivy
package kim.sesame.framework.web.context;
import kim.sesame.framework.web.entity.IUser;
import java.util.List;
/**
* UserContext
*
* @author johnny
* date : 2017-11-07 13:25
* Description:用户上下文
*/
public class UserContext {
private final static ThreadLocal USER_THREAD_LOCAL = new ThreadLocal<>();
private final static ThreadLocal SESSIONID = new ThreadLocal<>();
private final static ThreadLocal USER_ROLE = new ThreadLocal<>();
//构造器私有化
private UserContext() {
}
//单例
private static class ContextHolder {
private final static UserContext userContext = new UserContext();
}
public static UserContext getUserContext() {
return ContextHolder.userContext;
}
/*-------------------------------------------------------*/
public IUser getUser() {
return USER_THREAD_LOCAL.get();
}
public T getUser(Class objectType) {
return (T) USER_THREAD_LOCAL.get();
}
public void setUser(IUser user) {
USER_THREAD_LOCAL.set(user);
}
/*-------------------------------------------------------*/
public String getUserSessionId(){
return SESSIONID.get();
}
public void setUserSessionId(String sessionId){
SESSIONID.set(sessionId);
}
/*-------------------------------------------------------*/
public List getUserRole(){
return USER_ROLE.get();
}
public T getUserRole(Class objectType){
return (T) USER_ROLE.get();
}
public void setUserRole(List roles){
USER_ROLE.set(roles);
}
/*-------------------------------------------------------*/
public void clean() {
USER_THREAD_LOCAL.remove();
SESSIONID.remove();
USER_ROLE.remove();
}
}