<commit_before>package com.highway2urhell;

import com.highway2urhell.agent.InstrumentationHolder;
import com.highway2urhell.domain.EntryPathData;
import com.highway2urhell.domain.FilterEntryPath;
import com.highway2urhell.domain.H2hConfig;
import com.highway2urhell.domain.OutputSystem;
import com.highway2urhell.service.*;
import com.highway2urhell.transformer.EntryPointTransformer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class CoreEngine {

    private static CoreEngine instance;

    private Map<String, LeechService> leechPluginRegistry = new HashMap<String, LeechService>();
    private Set<ReporterService> reporterPluginRegistry = new HashSet<ReporterService>();
    protected final transient Logger LOGGER = LoggerFactory.getLogger(this.getClass());
    private final static String H2H_CONFIG = "H2H_CONFIG";
    private final static String JAVA = "Java";
    private final static Integer DEFAULT_TIMER = 2000;
    private H2hConfig config;

    private CoreEngine() {
        // nothing
    }

    public static CoreEngine getInstance() {
        if (instance == null) {
            synchronized (CoreEngine.class) {
                if (instance == null) {
                    instance = new CoreEngine();
                    instance.registerPlugins();
                    instance.runPluginsTriggeredAtStartup();
                    instance.configure();
                    instance.runListener();
                }
            }
        }
        return instance;
    }

    public void runListener(){
        ScheduledExecutorService schExService = Executors.newScheduledThreadPool(1);
        schExService.scheduleAtFixedRate(new Runnable() {

            public void run() {
                LOGGER.debug("Call the H2H-Web server");
                ListenerService.getInstance().callServerH2H();
            }
        }, 5, 10, TimeUnit.SECONDS);
    }

    public void enableEntryPointCoverage(FilterEntryPath filter) throws ClassNotFoundException, UnmodifiableClassException {
        LOGGER.info("enabling entry point coverage");
        Instrumentation instrumentation = InstrumentationHolder.getInstance().getInst();
        if (instrumentation != null) {
            TransformerService ts = new TransformerService();
            Map<String, List<EntryPathData>> mapConvert = ts.transformDataFromLeechPluginForTransformation(leechPluginRegistry.values(), filter);
            if (!config.getPathSend()) {
                //if the initPath are not called
                initPathsRemote();
            }
            instrumentation.addTransformer(new EntryPointTransformer(mapConvert, CoreEngine.getInstance().getConfig().getPerformance()), true);
            ts.transformAllClassScanByH2h(instrumentation, mapConvert.keySet());
        } else {
            LOGGER.error("Instrumentation fail because internal inst is null");
        }
    }

    public void initPathsRemote() {
        if (config.getToken() != null) {
            ThunderExporterService.getInstance().initPathsRemoteApp();
            config.setPathSend(true);
        }
    }

    public void leech() {
        for (ReporterService reporterService : reporterPluginRegistry) {
            for (LeechService leechService : leechPluginRegistry.values()) {
                reporterService.report(leechService.getFrameworkInformations());
            }
        }
    }

    public Collection<LeechService> getLeechServiceRegistered() {
        return leechPluginRegistry.values();
    }

    public LeechService getFramework(String frameworkName) {
        return leechPluginRegistry.get(frameworkName);
    }

    private void registerPlugins() {
        autoDiscoverLeechPlugins();
        autoDiscoverReporterPlugins();
    }

    private void autoDiscoverLeechPlugins() {
        Set<AbstractLeechService> leechServices = PluginUtils.autodiscoverPlugin(AbstractLeechService.class);
        for (AbstractLeechService leechService : leechServices) {
            leechPluginRegistry.put(leechService.getFrameworkInformations().getFrameworkName(), leechService);
        }
    }

    private void autoDiscoverReporterPlugins() {
        reporterPluginRegistry = PluginUtils.autodiscoverPlugin(ReporterService.class);
    }

    private void runPluginsTriggeredAtStartup() {
        for (LeechService leechService : leechPluginRegistry.values()) {
            if (leechService.isTriggeredAtStartup()) {
                leechService.receiveData(null);
            }
        }
    }

    private void configure() {
        // Grab Env
        String rootH2h = System.getProperty(H2H_CONFIG);
        if (rootH2h == null) {
            throw new RuntimeException("Unknow Variable H2H_CONFIG. Please Set H2H_CONFIG to location application deployment.");
        }
        if ("".equals(rootH2h)) {
            throw new RuntimeException("Variable Path H2H_CONFIG. Please Set H2H_CONFIG to location application deployment.");
        }
        parseConfig(rootH2h);
    }

    public void parseConfig(String pathFile) {
        config = new H2hConfig();
        config.setTypeAppz(JAVA);
        Properties prop = new Properties();
        InputStream input = null;
        try {
            input = new FileInputStream(pathFile);
            prop.load(input);
            config.setUrlApplication(prop.getProperty("urlapplication"));
            config.setNameApplication(prop.getProperty("nameapplication"));
            config.setPathH2h(prop.getProperty("pathH2h"));
            config.setPathSource(prop.getProperty("pathSource"));
            config.setDescription(prop.getProperty("description"));
            config.setVersionApp(prop.getProperty("versionApp"));
            config.setToken(prop.getProperty("token"));
            config.setReference(prop.getProperty("reference"));
            String performance = prop.getProperty("performance");
            if (performance != null && performance.equals("true")) {
                config.setPerformance(true);
            } else {
                config.setPerformance(false);
            }
            String timer = prop.getProperty("timer");
            if (timer != null) {<commit_msg>Add Default config if file config.properties is not configured<commit_after>package com.highway2urhell;

import com.highway2urhell.agent.InstrumentationHolder;
import com.highway2urhell.domain.EntryPathData;
import com.highway2urhell.domain.FilterEntryPath;
import com.highway2urhell.domain.H2hConfig;
import com.highway2urhell.domain.OutputSystem;
import com.highway2urhell.service.*;
import com.highway2urhell.transformer.EntryPointTransformer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class CoreEngine {

    private static CoreEngine instance;

    private Map<String, LeechService> leechPluginRegistry = new HashMap<String, LeechService>();
    private Set<ReporterService> reporterPluginRegistry = new HashSet<ReporterService>();
    protected final transient Logger LOGGER = LoggerFactory.getLogger(this.getClass());
    private final static String H2H_CONFIG = "H2H_CONFIG";
    private final static String JAVA = "Java";
    private final static Integer DEFAULT_TIMER = 2000;
    private final static String NO_URL = "NO_URL";
    private final static String NO_SOURCE = "NO_SOURCE";
        
    private H2hConfig config;

    private CoreEngine() {
        // nothing
    }

    public static CoreEngine getInstance() {
        if (instance == null) {
            synchronized (CoreEngine.class) {
                if (instance == null) {
                    instance = new CoreEngine();
                    instance.registerPlugins();
                    instance.runPluginsTriggeredAtStartup();
                    instance.configure();
                    instance.runListener();
                }
            }
        }
        return instance;
    }

    public void runListener(){
        ScheduledExecutorService schExService = Executors.newScheduledThreadPool(1);
        schExService.scheduleAtFixedRate(new Runnable() {

            public void run() {
                LOGGER.debug("Call the H2H-Web server");
                ListenerService.getInstance().callServerH2H();
            }
        }, 5, 10, TimeUnit.SECONDS);
    }

    public void enableEntryPointCoverage(FilterEntryPath filter) throws ClassNotFoundException, UnmodifiableClassException {
        LOGGER.info("enabling entry point coverage");
        Instrumentation instrumentation = InstrumentationHolder.getInstance().getInst();
        if (instrumentation != null) {
            TransformerService ts = new TransformerService();
            Map<String, List<EntryPathData>> mapConvert = ts.transformDataFromLeechPluginForTransformation(leechPluginRegistry.values(), filter);
            if (!config.getPathSend()) {
                //if the initPath are not called
                initPathsRemote();
            }
            instrumentation.addTransformer(new EntryPointTransformer(mapConvert, CoreEngine.getInstance().getConfig().getPerformance()), true);
            ts.transformAllClassScanByH2h(instrumentation, mapConvert.keySet());
        } else {
            LOGGER.error("Instrumentation fail because internal inst is null");
        }
    }

    public void initPathsRemote() {
        if (config.getToken() != null) {
            ThunderExporterService.getInstance().initPathsRemoteApp();
            config.setPathSend(true);
        }
    }

    public void leech() {
        for (ReporterService reporterService : reporterPluginRegistry) {
            for (LeechService leechService : leechPluginRegistry.values()) {
                reporterService.report(leechService.getFrameworkInformations());
            }
        }
    }

    public Collection<LeechService> getLeechServiceRegistered() {
        return leechPluginRegistry.values();
    }

    public LeechService getFramework(String frameworkName) {
        return leechPluginRegistry.get(frameworkName);
    }

    private void registerPlugins() {
        autoDiscoverLeechPlugins();
        autoDiscoverReporterPlugins();
    }

    private void autoDiscoverLeechPlugins() {
        Set<AbstractLeechService> leechServices = PluginUtils.autodiscoverPlugin(AbstractLeechService.class);
        for (AbstractLeechService leechService : leechServices) {
            leechPluginRegistry.put(leechService.getFrameworkInformations().getFrameworkName(), leechService);
        }
    }

    private void autoDiscoverReporterPlugins() {
        reporterPluginRegistry = PluginUtils.autodiscoverPlugin(ReporterService.class);
    }

    private void runPluginsTriggeredAtStartup() {
        for (LeechService leechService : leechPluginRegistry.values()) {
            if (leechService.isTriggeredAtStartup()) {
                leechService.receiveData(null);
            }
        }
    }

    private void configure() {
        // Grab Env
        String rootH2h = System.getProperty(H2H_CONFIG);
        if (rootH2h == null) {
            defaultConfig();
        }else if ("".equals(rootH2h)) {
            defaultConfig();
        }else {
            parseConfig(rootH2h);
        }
    }

    public void defaultConfig(){
        config = new H2hConfig();
        config.setTypeAppz(JAVA);
        config.setUrlApplication(NO_URL);
        config.setNameApplication(String.valueOf(System.currentTimeMillis()));
        config.setPathSource(NO_SOURCE);
        config.setDescription("Default Name.");
        config.setPerformance(false);
        config.setTimer(OutputSystem.REMOTE);
        config.setHigherTime(DEFAULT_TIMER);
        //TODO
        //Hardcoded la valeur de la plateforme --> le jour ou la plateforme fonctionnera ... one day ... one day ..
        //Actuellement on hardcode le local
        config.setUrlH2hWeb("http://localhost:8090/core/api/ThunderEntry");
        //TODO
        //Recuperer le token comme parametre le jour ou la plate fonctionnera ... one day ... one day ..
        config.setToken(null);
     }

    public void parseConfig(String pathFile) {
        config = new H2hConfig();
        config.setTypeAppz(JAVA);
        Properties prop = new Properties();
        InputStream input = null;
        try {
            input = new FileInputStream(pathFile);
            prop.load(input);
            config.setUrlApplication(prop.getProperty("urlapplication"));
            config.setNameApplication(prop.getProperty("nameapplication"));
            config.setPathH2h(prop.getProperty("pathH2h"));
            config.setPathSource(prop.getProperty("pathSource"));
            config.setDescription(prop.getProperty("description"));
            config.setVersionApp(prop.getProperty("versionApp"));
            config.setToken(prop.getProperty("token"));
            config.setReference(prop.getProperty("reference"));
            String performance = prop.getProperty("performance");
            if (performance != null && performance.equals("true")) {
                config.setPerformance(true);
            } else {
                config.setPerformance(false);
            }
            String timer = prop.getProperty("timer");
            if (timer != null) {
{'fraction_non_alphanumeric': 0.09509690483077814, 'fraction_numerical': 0.0054960948799537175, 'mean_word_length': 2.7578804347826087, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3137932', 'n_tokens_mistral': 3618, 'n_tokens_neox': 3517, 'n_words': 697}