package com.sun.identity.cot;

import com.sun.identity.shared.debug.Debug;
import java.util.Hashtable;
import java.util.Map;

public final class COTCache {
   private static final Map<String, CircleOfTrustDescriptor> COT_CACHE = new Hashtable<>();
   private static final Debug DEBUG = COTUtils.DEBUG;

   private COTCache() {
   }

   static CircleOfTrustDescriptor getCircleOfTrust(String realm, String name) {
      String classMethod = "CircleOfDescriptorCache:getCircleOfTrust:";
      String cacheKey = buildCacheKey(realm, name);
      CircleOfTrustDescriptor cotDesc = COT_CACHE.get(cacheKey);
      if (COTUtils.DEBUG.messageEnabled()) {
         COTUtils.DEBUG.message(classMethod + "cacheKey = " + cacheKey + ", found = " + (cotDesc != null));
      }

      return cotDesc;
   }

   static void putCircleOfTrust(String realm, String name, CircleOfTrustDescriptor cotDescriptor) {
      String classMethod = "CircleOfTrustCache:putCircleOfTrust";
      String cacheKey = buildCacheKey(realm, name);
      if (DEBUG.messageEnabled()) {
         DEBUG.message(classMethod + ": cacheKey = " + cacheKey);
      }

      COT_CACHE.put(cacheKey, cotDescriptor);
   }

   static void clear() {
      COT_CACHE.clear();
   }

   private static String buildCacheKey(String realm, String cotName) {
      return realm + "//" + cotName;
   }
}
