package com.tsi.comm.server;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

@Slf4j
public class AppNameInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(ConfigurableApplicationContext context) {
        try {
            String applicationName = "";
            String poolName = "";

            final Environment env = context.getEnvironment();
            final String appName = env.getProperty("spring.application.name", TsiCommServerApplication.APPLICATION_NAME);
            final String serverId = env.getProperty("application.cvim-server.server-id", "0");
            applicationName = appName + "-" + serverId;
            poolName = "pool-" + appName + "-" + serverId;
//            System.setProperty("application.process-name", applicationName);
            System.setProperty("spring.application.name", applicationName);
            System.setProperty("spring.datasource.hikari.pool-name", poolName);
        } catch (Exception e) {
            log.error("Error during context initialization", e);
        }
    }
}
