package com.sun.identity.saml2.meta;

import com.airlock.iam.opensso.jaxb.entityconfig.EntityConfigElement;
import com.airlock.iam.opensso.jaxb.metadata.EntityDescriptorElement;
import java.util.Hashtable;

class SAML2MetaCache {
   private static final Hashtable<String, EntityDescriptorElement> DESCRIPTOR_CACHE = new Hashtable<>();
   private static final Hashtable<String, EntityConfigElement> CONFIG_CACHE = new Hashtable<>();

   private SAML2MetaCache() {
   }

   static EntityDescriptorElement getEntityDescriptor(String realm, String entityId) {
      String cacheKey = buildCacheKey(realm, entityId);
      EntityDescriptorElement descriptor = DESCRIPTOR_CACHE.get(cacheKey);
      if (SAML2MetaUtils.DEBUG.messageEnabled()) {
         SAML2MetaUtils.DEBUG.message("SAML2MetaCache.getEntityDescriptor: cacheKey = " + cacheKey + ", found = " + (descriptor != null));
      }

      return descriptor;
   }

   static void putEntityDescriptor(String realm, String entityId, EntityDescriptorElement descriptor) {
      String cacheKey = buildCacheKey(realm, entityId);
      if (descriptor != null) {
         if (SAML2MetaUtils.DEBUG.messageEnabled()) {
            SAML2MetaUtils.DEBUG.message("SAML2MetaCache.putEntityDescriptor: cacheKey = " + cacheKey);
         }

         DESCRIPTOR_CACHE.put(cacheKey, descriptor);
      } else {
         if (SAML2MetaUtils.DEBUG.messageEnabled()) {
            SAML2MetaUtils.DEBUG.message("SAML2MetaCache.putEntityDescriptor: delete cacheEey = " + cacheKey);
         }

         DESCRIPTOR_CACHE.remove(cacheKey);
         CONFIG_CACHE.remove(cacheKey);
      }
   }

   static EntityConfigElement getEntityConfig(String realm, String entityId) {
      String cacheKey = buildCacheKey(realm, entityId);
      EntityConfigElement config = CONFIG_CACHE.get(cacheKey);
      if (SAML2MetaUtils.DEBUG.messageEnabled()) {
         SAML2MetaUtils.DEBUG.message("SAML2MetaCache.getEntityConfig: cacheKey = " + cacheKey + ", found = " + (config != null));
      }

      return config;
   }

   static void putEntityConfig(String realm, String entityId, EntityConfigElement config) {
      String cacheKey = buildCacheKey(realm, entityId);
      if (config != null) {
         if (SAML2MetaUtils.DEBUG.messageEnabled()) {
            SAML2MetaUtils.DEBUG.message("SAML2MetaCache.putEntityConfig: cacheKey = " + cacheKey);
         }

         CONFIG_CACHE.put(cacheKey, config);
      } else {
         if (SAML2MetaUtils.DEBUG.messageEnabled()) {
            SAML2MetaUtils.DEBUG.message("SAML2MetaCache.putEntityConfig: delete cacheKey = " + cacheKey);
         }

         CONFIG_CACHE.remove(cacheKey);
      }
   }

   static void clear() {
      if (SAML2MetaUtils.DEBUG.messageEnabled()) {
         SAML2MetaUtils.DEBUG.message("SAML2MetaCache.clear() called");
      }

      DESCRIPTOR_CACHE.clear();
      CONFIG_CACHE.clear();
   }

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