/*
 * Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package monasca.common.middleware;

import com.google.common.cache.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;


public class TokenCache {

  private final LoadingCache<String, String> cache;
  private final Config appConfig = Config.getInstance();
  private AuthClientFactory factory;
  private static final Logger logger = LoggerFactory
    .getLogger(TokenCache.class);


  public TokenCache(final long maxSize, final long timeToExpire) {
    factory = appConfig.getFactory();

    cache = CacheBuilder.newBuilder().maximumSize(maxSize)
      .expireAfterWrite(timeToExpire, TimeUnit.SECONDS)
      .build(new CacheLoader<String, String>() {
        public String load(String key) throws TException, ClientProtocolException {

          String value = null;
          AuthClient client = null;

          try {
            client = factory.getClient();
            value = client.validateTokenForServiceEndpointV3(key);
          } finally {
            if (client != null)
              factory.recycle(client);
          }
          return value;
        }
      });
  }

  public String getToken(String key) throws ClientProtocolException {
    String value = null;
    try {
      value = cache.get(key);
    } catch (ExecutionException e) {
      logger.info("Failed to get token", e);
      throw new ClientProtocolException(e.getMessage(), e);
    }
    return value;
  }

  public void put(String key, String value) {
    cache.put(key, value);
  }

}

@@ -1,13 +1,17 @@
 /*%0A *
+ (C)
  Copyrig
@@ -17,24 +17,25 @@
 ght 
-(c) 
 2014
+,2016
  Hewlett
 -Pac
@@ -34,17 +34,17 @@
 lett
--
+ 
 Packard 
 Deve
@@ -39,16 +39,27 @@
 Packard 
+Enterprise 
 Developm
@@ -66,25 +66,13 @@
 ent 
-Company, L.P.
+LP
 %0A *
- 
 %0A * 
@@ -245,17 +245,16 @@
 se at%0A *
- 
 %0A * http
@@ -294,17 +294,16 @@
 E-2.0%0A *
- 
 %0A * Unle
@@ -1608,16 +1608,53 @@
  = null;
+%0A          boolean discarded = false;
 %0A%0A      
@@ -1768,24 +1768,391 @@
 intV3(key);%0A
+          %7D catch (ClientProtocolException e) %7B%0A            factory.discard(client);%0A            logger.warn(%22Validate token failed with ClientProtocolException. Discarding AuthClient connection from pool to create a new connection.%22);%0A            logger.warn(%22ClientProtocolException %22 + e.getMessage() + %22 %22 + e);%0A            discarded = true;%0A            throw e;%0A
           %7D 
@@ -2191,16 +2191,30 @@
  != null
+ && !discarded
 )%0A      

Reconnect to auth url in case of ClientProtocolException