Search in sources :

Example 1 with DataSave

use of me.shadorc.shadbot.data.annotation.DataSave in project Shadbot by Shadorc.

the class StatsManager method save.

@DataSave(filePath = FILE_NAME, initialDelay = 10, period = 10, unit = TimeUnit.MINUTES)
public static void save() throws JSONException, IOException {
    JSONObject mainObj = new JSONObject();
    Reflections reflections = new Reflections(StatsManager.class.getPackage().getName(), new MethodAnnotationsScanner());
    for (Method jsonMethod : reflections.getMethodsAnnotatedWith(StatsJSON.class)) {
        StatsJSON annotation = jsonMethod.getAnnotation(StatsJSON.class);
        try {
            mainObj.put(annotation.name(), jsonMethod.invoke(null));
        } catch (Exception err) {
            LogUtils.error(err, String.format("An error occurred while saving statistics %s.", jsonMethod.getDeclaringClass().getSimpleName()));
        }
    }
    try (FileWriter writer = new FileWriter(FILE)) {
        writer.write(mainObj.toString(Config.JSON_INDENT_FACTOR));
    }
}
Also used : JSONObject(org.json.JSONObject) MethodAnnotationsScanner(org.reflections.scanners.MethodAnnotationsScanner) FileWriter(java.io.FileWriter) StatsJSON(me.shadorc.shadbot.data.stats.annotation.StatsJSON) Method(java.lang.reflect.Method) IOException(java.io.IOException) JSONException(org.json.JSONException) Reflections(org.reflections.Reflections) DataSave(me.shadorc.shadbot.data.annotation.DataSave)

Example 2 with DataSave

use of me.shadorc.shadbot.data.annotation.DataSave in project Shadbot by Shadorc.

the class DataManager method init.

public static boolean init() {
    LogUtils.infof("Initializing data files...");
    if (!SAVE_DIR.exists() && !SAVE_DIR.mkdir()) {
        LogUtils.error("The save folder could not be created.");
        return false;
    }
    Reflections reflections = new Reflections(DataManager.class.getPackage().getName(), new MethodAnnotationsScanner());
    for (Method initMethod : reflections.getMethodsAnnotatedWith(DataInit.class)) {
        try {
            initMethod.invoke(null);
        } catch (Exception err) {
            LogUtils.error(err, String.format("An error occurred while initializing data %s.", initMethod.getDeclaringClass().getSimpleName()));
            return false;
        }
        // Search for save() method and if found, schedule it
        for (Method method : initMethod.getDeclaringClass().getMethods()) {
            if (method.isAnnotationPresent(DataSave.class)) {
                DataSave annotation = method.getAnnotation(DataSave.class);
                Runnable saveTask = () -> {
                    try {
                        LogUtils.infof("Saving %s...", initMethod.getDeclaringClass().getSimpleName());
                        method.invoke(null);
                        LogUtils.infof("%s saved.", initMethod.getDeclaringClass().getSimpleName());
                    } catch (Exception err) {
                        LogUtils.error(err, String.format("An error occurred while saving %s.", annotation.filePath()));
                    }
                };
                SAVE_TASKS.add(saveTask);
                SCHEDULED_EXECUTOR.scheduleAtFixedRate(saveTask, annotation.initialDelay(), annotation.period(), annotation.unit());
            }
        }
        LogUtils.infof("%s initialized.", initMethod.getDeclaringClass().getSimpleName());
    }
    LogUtils.infof("Data files initialized.");
    return true;
}
Also used : MethodAnnotationsScanner(org.reflections.scanners.MethodAnnotationsScanner) Method(java.lang.reflect.Method) DataSave(me.shadorc.shadbot.data.annotation.DataSave) Reflections(org.reflections.Reflections)

Aggregations

Method (java.lang.reflect.Method)2 DataSave (me.shadorc.shadbot.data.annotation.DataSave)2 Reflections (org.reflections.Reflections)2 MethodAnnotationsScanner (org.reflections.scanners.MethodAnnotationsScanner)2 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 StatsJSON (me.shadorc.shadbot.data.stats.annotation.StatsJSON)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1