/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2013 Geosparc nv, http://www.geosparc.com/, Belgium.
 *
 * The program is available in open source according to the GNU Affero
 * General Public License. All contributions in this program are covered
 * by the Geomajas Contributors License Agreement. For full licensing
 * details, see LICENSE.txt in the project root.
 */
package org.geomajas.plugin.deskmanager.security;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import org.geomajas.plugin.deskmanager.security.role.DeskmanagerAuthentication;
import org.geomajas.security.Authentication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Create and manage/cache authentication tokens.
 * 
 * @author Oliver May
 */
@Component
public class AuthenticationTokenService {

	private final Logger log = LoggerFactory.getLogger(AuthenticationTokenService.class);

	private Map<String, DeskmanagerAuthentication> tokens = 
			new ConcurrentHashMap<String, DeskmanagerAuthentication>();

	public Authentication getAuthentication(String token) {
		log.debug("Getting authentication for token {}", token);
		return tokens.get(token);
	}

	public void logout(String token) {
		tokens.remove(token);
	}

	public String login(DeskmanagerAuthentication authentication) {
		String token = getToken();
		return login(token, authentication);
	}

	private String login(String token, DeskmanagerAuthentication authentication) {
		log.debug("Registering token {} for authentication {}", token, authentication);
		if (null == token) {
			return login(authentication);
		}
		tokens.put(token, authentication);
		return token;
	}

	@SuppressWarnings("unused")
	@Scheduled(fixedDelay = 3600000)
	private void cleanUp() {
		List<String> toBeRemoved = new ArrayList<String>();
		for (Entry<String, DeskmanagerAuthentication> token : tokens.entrySet()) {
			log.debug("Checking validity of authentication token {} {}", token.getKey(), token.getValue());
			if (token.getValue().getInvalidAfter().before(new Date())) {
				log.info("Authentication token {} expired, removing. {}", token.getKey(), token.getValue());
				toBeRemoved.add(token.getKey());
			}
		}
		for (String key : toBeRemoved) {
			logout(key);
		}
	}

	public String getToken() {
		return "gdm." + UUID.randomUUID().toString();
	}

}

@@ -484,183 +484,12 @@
 til.
-ArrayList;%0Aimport java.util.Date;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Map.Entry;%0Aimport java.util.UUID;%0Aimport java.util.concurrent.ConcurrentHashMap
+UUID
 ;%0A%0Ai
@@ -604,32 +604,74 @@
 Authentication;%0A
+import org.geomajas.service.CacheService;%0A
 import org.slf4j
@@ -742,18 +742,21 @@
 ork.
-scheduling
+beans.factory
 .ann
@@ -763,23 +763,23 @@
 otation.
-Schedul
+Autowir
 ed;%0Aimpo
@@ -960,16 +960,66 @@
 ervice %7B
+%0A%09%0A%09@Autowired%0A%09private CacheService cacheService;
 %0A%0A%09priva
@@ -1104,130 +1104,8 @@
 );%0A%0A
-%09private Map%3CString, DeskmanagerAuthentication%3E tokens = %0A%09%09%09new ConcurrentHashMap%3CString, DeskmanagerAuthentication%3E();%0A%0A
 %09pub
@@ -1229,83 +1229,207 @@
 urn 
-tokens.get(token);%0A%09%7D%0A%0A%09public void logout(String token) %7B%0A%09%09tokens.remove(
+cacheService.get(AuthenticationTokenService.class.toString(), token, Authentication.class);%0A%09%7D%0A%0A%09public void logout(String token) %7B%0A%09%09cacheService.remove(AuthenticationTokenService.class.toString(), 
 toke
@@ -1801,643 +1801,109 @@
 %7D%0A%09%09
-tokens.put(token, authentication);%0A%09%09return token;%0A%09%7D%0A%0A%09@SuppressWarnings(%22unused%22)%0A%09@Scheduled(fixedDelay = 3600000)%0A%09private void cleanUp() %7B%0A%09%09List%3CString%3E toBeRemoved = new ArrayList%3CString%3E();%0A%09%09for (Entry%3CString, DeskmanagerAuthentication%3E token : tokens.entrySet()) %7B%0A%09%09%09log.debug(%22Checking validity of authentication token %7B%7D %7B%7D%22, token.getKey(), token.getValue());%0A%09%09%09if (token.getValue().getInvalidAfter().before(new Date())) %7B%0A%09%09%09%09log.info(%22Authentication token %7B%7D expired, removing. %7B%7D%22, token.getKey(), token.getValue());%0A%09%09%09%09toBeRemoved.add(token.getKey());%0A%09%09%09%7D%0A%09%09%7D%0A%09%09for (String key : toBeRemoved) %7B%0A%09%09%09logout(key);%0A%09%09%7D
+cacheService.put(AuthenticationTokenService.class.toString(), token, authentication);%0A%09%09return token;
 %0A%09%7D%0A

Move to geomajas cache service