package gp.e3.autheo.client.internal.token;

import gp.e3.autheo.authentication.infrastructure.validators.StringValidator;
import gp.e3.autheo.client.exceptions.InvalidStateException;
import gp.e3.autheo.client.filter.TokenDTO;
import redis.clients.jedis.Jedis;

public enum TokenHandlerSingleton {
	
	INSTANCE;
	
	public static final String NIL = "nil";
	public static final String OK = "OK";
	
	private String tokenValue; 
	private Jedis redisClient;

	private boolean singletonHasBeenInitialized() {
		
		return (this.tokenValue != null && redisClient != null);
	}
	
	public void initializeSingleton(Jedis redisClient, String tokenValue) {
		
		if (!singletonHasBeenInitialized()) {
			
			this.tokenValue = tokenValue;
			this.redisClient = redisClient;
		}
	}
	
	public TokenDTO getInternalApiTokenFromRedis(String sellerId) throws InvalidStateException {
		
		TokenDTO tokenDTO = null;
		
		if (singletonHasBeenInitialized()) {
			
			String tokenToString = redisClient.get(sellerId);
			
			if (StringValidator.isValidString(tokenToString) && !tokenToString.equalsIgnoreCase(NIL)) {
				tokenDTO = TokenDTO.buildTokenDTOFromTokenToString(tokenToString);
			}
			
		} else {
			
			String errorMessage = "The singleton has not been initialized by first time.";
			throw new InvalidStateException(errorMessage);
		}
		
		return tokenDTO;
	}
}
@@ -254,16 +254,54 @@
 s.Jedis;
+%0Aimport redis.clients.jedis.JedisPool;
 %0A%0Apublic
@@ -431,37 +431,8 @@
 ;%0A%09%0A
-%09private String tokenValue; %0A
 %09pri
@@ -441,28 +441,30 @@
 te Jedis
+Pool
  redis
-Client
+Pool
 ;%0A%0A%09priv
@@ -524,46 +524,17 @@
 rn (
-this.tokenValue != null && redisClient
+redisPool
  != 
@@ -587,39 +587,22 @@
 edis
- redisClient, String tokenValue
+Pool jedisPool
 ) %7B%0A
@@ -655,71 +655,29 @@
 %0A%09%09%09
-this.tokenValue = tokenValue;%0A%09%09%09this.redisClient = r
+redisPool = j
 edis
-Client
+Pool
 ;%0A%09%09
@@ -850,24 +850,72 @@
 ed()) %7B%0A%09%09%09%0A
+%09%09%09Jedis redisClient = redisPool.getResource();%0A
 %09%09%09String to
@@ -955,16 +955,58 @@
 lerId);%0A
+%09%09%09redisPool.returnResource(redisClient);%0A
 %09%09%09%0A%09%09%09i

Add redis pool to the TokenHandlerSingleton class.