package org.jgrades.logging.job;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import org.jgrades.logging.utils.InternalProperties;
import org.slf4j.LoggerFactory;

import java.io.File;

public class LoggerContextReloader {
    private JoranConfigurator configurator;

    public void reload() {
        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
        context.reset();
        configurator = new JoranConfigurator();
        configurator.setContext(context);

        File externalXmlFile = new File(InternalProperties.XML_FILE);
        if(externalXmlFile.exists()){
            setConfiguration(externalXmlFile.getAbsolutePath());
        } else{
            setDefaultConfiguration();
        }
    }

    private void setConfiguration(String configXmlFilePath){
        try {
            configurator.doConfigure(configXmlFilePath);
        } catch (JoranException e) {
            setDefaultConfiguration();
        }
    }

    private void setDefaultConfiguration(){
        setConfiguration(InternalProperties.ONLY_CONSOLE_XML_FILE);
    }
}

@@ -329,53 +329,8 @@
 r %7B%0A
-    private JoranConfigurator configurator;%0A%0A
     
@@ -460,32 +460,50 @@
 eset();%0A        
+JoranConfigurator 
 configurator = n
@@ -693,115 +693,113 @@
     
-setConfiguration(externalXmlFile.getAbsolutePath());%0A        %7D else%7B%0A            setDefaultConfiguration();
+try %7B%0A                configurator.doConfigure(externalXmlFile);%0A            %7D catch (JoranException e) %7B
 %0A   
@@ -807,184 +807,83 @@
     
-%7D%0A
     
-%7D%0A%0A
     
-private void setConfiguration(String configXmlFilePath)%7B%0A        try %7B%0A            configurator.doConfigure(configXmlFilePath);%0A        %7D catch (JoranException e) 
+setDefaultConfiguration(configurator);%0A            %7D%0A        %7D else
 %7B%0A  
@@ -916,16 +916,28 @@
 uration(
+configurator
 );%0A     
@@ -993,78 +993,212 @@
 ion(
-)%7B%0A        setConfiguration(InternalProperties.ONLY_CONSOLE_XML_FILE);
+JoranConfigurator configurator)%7B%0A        try %7B%0A            configurator.doConfigure(InternalProperties.ONLY_CONSOLE_XML_FILE);%0A        %7D catch (JoranException e) %7B%0A            // not possible...%0A        %7D
 %0A   

revert LoggerContextReloader