package com.sun.identity.configuration;

import com.sun.identity.common.SystemConfigurationUtil;
import com.sun.identity.shared.configuration.ISystemProperties;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;

public class FedLibSystemProperties implements ISystemProperties {
   private static final String SYSTEM_CONFIG_PROPERTY_NAME = "FederationConfig";
   private static final String FEDLET_HOME_TAG = "@FEDLET_HOME@";
   private static final String FEDLET_HOME_DIR = "com.sun.identity.fedlet.home";
   private static Properties systemConfigProps;

   @Override
   public String get(String key) {
      return systemConfigProps.getProperty(key);
   }

   @Override
   public void initializeProperties(Properties properties) {
      Properties newProps = new Properties();
      newProps.putAll(systemConfigProps);
      newProps.putAll(properties);
      systemConfigProps = newProps;
   }

   private static void initProperties() {
      systemConfigProps = new Properties();

      try {
         ResourceBundle bundle = ResourceBundle.getBundle("FederationConfig");

         for (String key : bundle.keySet()) {
            systemConfigProps.put(key, bundle.getString(key));
         }
      } catch (MissingResourceException mrex) {
         String fedletHomeDir = System.getProperty("com.sun.identity.fedlet.home");
         if (fedletHomeDir == null || fedletHomeDir.trim().isEmpty()) {
            fedletHomeDir = System.getProperty("user.home") + File.separator + "fedlet";
         }

         try (InputStream fis = new FileInputStream(fedletHomeDir + File.separator + "FederationConfig.properties")) {
            Properties props = new Properties();
            props.load(fis);

            for (Object o : props.keySet()) {
               String key = (String)o;
               String value = (String)props.get(key);
               int loc = value.indexOf("@FEDLET_HOME@");
               if (loc == 0) {
                  value = fedletHomeDir.trim() + value.substring("@FEDLET_HOME@".length());
               } else if (loc != -1) {
                  value = value.substring(0, loc) + fedletHomeDir.trim() + value.substring(loc + "@FEDLET_HOME@".length());
               }

               systemConfigProps.put(key, value);
            }
         } catch (MissingResourceException | IOException var11) {
         }
      }
   }

   @Override
   public Collection<String> getServerList() throws Exception {
      return SystemConfigurationUtil.getServerList();
   }

   @Override
   public void initializeProperties(String propertyName, String propertyValue) {
      Properties newProps = new Properties();
      newProps.putAll(systemConfigProps);
      newProps.put(propertyName, propertyValue);
      systemConfigProps = newProps;
   }

   static {
      initProperties();
   }
}
