package com.sun.identity.saml2.common;

import com.sun.identity.common.SystemConfigurationUtil;
import com.sun.identity.saml2.plugins.JMQSAML2Repository;

public class SAML2Repository {
   private static final String DEFAULT_REPOSITORY_CLASS = "com.sun.identity.saml2.plugins.DefaultJMQSAML2Repository";
   private static final String REPOSITORY_CLASS_PROPERTY = "com.sun.identity.saml2.plugins.SAML2RepositoryImpl";
   private static final String REPOSITORY_CLASS = SystemConfigurationUtil.getProperty(
      "com.sun.identity.saml2.plugins.SAML2RepositoryImpl", "com.sun.identity.saml2.plugins.DefaultJMQSAML2Repository"
   );
   private static JMQSAML2Repository saml2Repository;

   public static JMQSAML2Repository getInstance() throws SAML2Exception {
      if (saml2Repository == null) {
         throw new SAML2Exception(SAML2Utils.BUNDLE.getString("nullSAML2Repository"));
      } else {
         return saml2Repository;
      }
   }

   static {
      try {
         saml2Repository = (JMQSAML2Repository)Class.forName(REPOSITORY_CLASS).getDeclaredConstructor().newInstance();
      } catch (Exception e) {
         SAML2Utils.DEBUG.error("Failed to instantiate JMQSAML2Repository", e);
         saml2Repository = null;
      }
   }
}
