<#assign licenseFirst = "/*">
<#assign licensePrefix = " * ">
<#assign licenseLast = " */">
<#include "../Licenses/license-${project.license}.txt">

package ${PACKAGENAME};

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.graalvm.visualvm.core.datasource.DataSource;



public class ${TEMPLATENAME}Provider {
    private static class Singleton {
        final private static ${TEMPLATENAME}Provider INSTANCE = new ${TEMPLATENAME}Provider();
    }

    final private ScheduledExecutorService discoveryProcess = Executors.newSingleThreadScheduledExecutor();

    private ${TEMPLATENAME}Provider() {}

    private void startup() {
        // TODO adjust the refresh interval
        discoveryProcess.schedule(new Runnable() {

            @Override
            public void run() {
                discoverDataSources();
            }
        }, 5, TimeUnit.SECONDS);
    }
    
    private void shutdown() {
        discoveryProcess.shutdown();
    }

    private void discoverDataSources() {
        // TODO handle the datasource discovery
        // call <parent datasource>.getRepository().addDataSource()/ addDataSources() to add discovered datasources
        // call <parent datasource>.getRepository().removeDataSource()/removeDataSources() to remove discovered datasources
    }
    
    public static ${TEMPLATENAME}Provider getInstance() {
        return Singleton.INSTANCE;
    }

    /**
     * TODO Call this method from the module Installer#restored()
     **/
    public static void install() {
        Singleton.INSTANCE.startup();
    }
    
    /**
     * TODO Call this method from the module Installer#uninstalled()
     **/
    public static void uninstall() {
        Singleton.INSTANCE.shutdown();
    }
}

