package io.gisbi.listener;


import io.gisbi.utils.ConfigUtils;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.util.Objects;

public class EhCacheStartListener implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        Resource resource = new ClassPathResource("application.yml");
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(resource);
        String property = Objects.requireNonNull(factory.getObject()).getProperty("gisbi.login_timeout", "480");
        System.setProperty("gisbi.login_timeout", property);

        String ehcache = ConfigUtils.getConfig("gisbi.path.ehcache", "/opt/gisbi/cache");
        System.setProperty("gisbi.path.ehcache", ehcache);
    }
}
