package org.onetwo.plugins.security.common;

import java.util.concurrent.TimeUnit;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.onetwo.common.log.MyLoggerFactory;
import org.onetwo.common.utils.SessionStorer;
import org.onetwo.common.utils.UserDetail;
import org.onetwo.plugins.security.server.SsoServerConfig;
import org.slf4j.Logger;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;

public class MemorySessionStorer implements SessionStorer {
	
	private static final Logger logger = MyLoggerFactory.getLogger(MemorySessionStorer.class);
	
//	private Map<String, UserDetail> users = LangUtils.newHashMap();
	private Cache<String, UserDetail> users;

	@Resource
	private SsoServerConfig ssoServerConfig;
	
	public MemorySessionStorer(){
	}
	
	@PostConstruct
	public void init(){
		long timeout = ssoServerConfig.getTimeout();
		users = CacheBuilder.newBuilder()
		.expireAfterAccess(timeout, TimeUnit.MINUTES)
//		.expireAfterWrite(timeout, TimeUnit.SECONDS)
		//TODO don't work
		.removalListener(new RemovalListener<String, UserDetail>(){

			@Override
			public void onRemoval(RemovalNotification<String, UserDetail> notification) {
				logger.info("====>>user[{}] has remove..", notification.getValue().getUserId());
			}
			
		})
		.build();
	}
	
	public void addUser(String token, UserDetail userDetail){
		users.put(token, userDetail);
	}

	@Override
	public UserDetail getUser(String token) {
		return users.getIfPresent(token);
	}

	@Override
	public UserDetail removeUser(String token) {
		UserDetail user = getUser(token);
		users.invalidate(token);
		return user;
	}
	
}

@@ -197,16 +197,55 @@
 actory;%0A
+import org.onetwo.common.utils.Assert;%0A
 import o
@@ -1520,16 +1520,41 @@
 etail)%7B%0A
+%09%09Assert.notNull(token);%0A
 %09%09users.
@@ -1577,16 +1577,16 @@
 etail);%0A
-
 %09%7D%0A%0A%09@Ov
@@ -1640,24 +1640,41 @@
  %7B%0A%09%09return 
+token==null?null:
 users.getIfP
@@ -1749,16 +1749,50 @@
 oken) %7B%0A
+%09%09if(token==null)%0A%09%09%09return null;%0A
 %09%09UserDe
@@ -1811,32 +1811,50 @@
 getUser(token);%0A
+%09%09if(user!=null)%0A%09
 %09%09users.invalida

fix sessionstore npe