2019-06-02 05:03:08 +03:00
|
|
|
package pro.gravit.launchserver.manangers;
|
2018-12-26 14:54:24 +03:00
|
|
|
|
2019-06-03 10:58:10 +03:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Objects;
|
|
|
|
|
2019-06-02 05:03:08 +03:00
|
|
|
import pro.gravit.launchserver.Reloadable;
|
|
|
|
import pro.gravit.utils.helper.LogHelper;
|
|
|
|
import pro.gravit.utils.helper.VerifyHelper;
|
2018-12-26 14:54:24 +03:00
|
|
|
|
|
|
|
public class ReloadManager {
|
2019-01-15 06:35:39 +03:00
|
|
|
private final HashMap<String, Reloadable> RELOADABLES = new HashMap<>();
|
|
|
|
|
|
|
|
public void registerReloadable(String name, Reloadable reloadable) {
|
2019-03-28 08:07:54 +03:00
|
|
|
VerifyHelper.putIfAbsent(RELOADABLES, name.toLowerCase(), Objects.requireNonNull(reloadable, "adapter"),
|
|
|
|
String.format("Reloadable has been already registered: '%s'", name.toLowerCase()));
|
2018-12-26 14:54:24 +03:00
|
|
|
}
|
2019-05-15 14:11:22 +03:00
|
|
|
|
|
|
|
public Reloadable unregisterReloadable(String name) {
|
2019-04-27 13:17:10 +03:00
|
|
|
return RELOADABLES.remove(name);
|
|
|
|
}
|
2019-01-15 06:35:39 +03:00
|
|
|
|
|
|
|
public void reloadAll() {
|
|
|
|
RELOADABLES.forEach((k, v) -> {
|
2018-12-26 14:54:24 +03:00
|
|
|
try {
|
|
|
|
v.reload();
|
|
|
|
} catch (Exception e) {
|
|
|
|
LogHelper.error(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-01-15 06:35:39 +03:00
|
|
|
|
2018-12-26 14:54:24 +03:00
|
|
|
public void reload(String name) throws Exception {
|
2019-03-28 08:07:54 +03:00
|
|
|
RELOADABLES.get(name.toLowerCase()).reload();
|
2018-12-26 14:54:24 +03:00
|
|
|
}
|
2019-01-15 06:35:39 +03:00
|
|
|
|
|
|
|
public void printReloadables() {
|
2018-12-26 15:10:56 +03:00
|
|
|
LogHelper.info("Print reloadables");
|
2019-01-15 06:35:39 +03:00
|
|
|
RELOADABLES.forEach((k, v) -> LogHelper.subInfo(k));
|
2018-12-26 15:10:56 +03:00
|
|
|
LogHelper.info("Found %d reloadables", RELOADABLES.size());
|
|
|
|
}
|
2018-12-26 14:54:24 +03:00
|
|
|
}
|