Refactor exporter into separate classes

This commit is contained in:
Luck 2020-08-13 10:12:36 +01:00
parent e1bac438aa
commit 361a4d66bc
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 69 additions and 72 deletions

View File

@ -27,7 +27,6 @@ package me.lucko.luckperms.common.backup;
import com.google.gson.JsonObject;
import me.lucko.luckperms.common.command.CommandResult;
import me.lucko.luckperms.common.locale.message.Message;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.Track;
@ -76,40 +75,20 @@ import java.util.zip.GZIPOutputStream;
/**
* Handles export operations
*/
public class Exporter implements Runnable {
public abstract class Exporter implements Runnable {
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
private final LuckPermsPlugin plugin;
protected final LuckPermsPlugin plugin;
private final Sender executor;
private final Path filePath;
private final boolean includeUsers;
private final boolean includeGroups;
private final boolean saveFile;
private final String label;
private final ProgressLogger log;
protected final ProgressLogger log;
public Exporter(LuckPermsPlugin plugin, Sender executor, Path filePath, boolean includeUsers, boolean includeGroups, boolean saveFile) {
protected Exporter(LuckPermsPlugin plugin, Sender executor, boolean includeUsers, boolean includeGroups) {
this.plugin = plugin;
this.executor = executor;
this.filePath = filePath;
this.includeUsers = includeUsers;
this.includeGroups = includeGroups;
this.saveFile = saveFile;
this.label = null;
this.log = new ProgressLogger(Message.EXPORT_LOG, Message.EXPORT_LOG_PROGRESS, null);
this.log.addListener(plugin.getConsoleSender());
this.log.addListener(executor);
}
public Exporter(LuckPermsPlugin plugin, Sender executor, boolean includeUsers, boolean includeGroups, boolean saveFile, String label) {
this.plugin = plugin;
this.executor = executor;
this.filePath = null;
this.includeUsers = includeUsers;
this.includeGroups = includeGroups;
this.saveFile = saveFile;
this.label = label;
this.log = new ProgressLogger(Message.EXPORT_LOG, Message.EXPORT_LOG_PROGRESS, null);
this.log.addListener(plugin.getConsoleSender());
@ -137,45 +116,10 @@ public class Exporter implements Runnable {
json.add("users", exportUsers());
}
if (this.saveFile) {
this.log.log("Finished gathering data, writing file...");
try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new GZIPOutputStream(Files.newOutputStream(this.filePath)), StandardCharsets.UTF_8))) {
GsonProvider.prettyPrinting().toJson(json, out);
} catch (IOException e) {
e.printStackTrace();
}
this.log.getListeners().forEach(l -> Message.LOG_EXPORT_SUCCESS.send(l, this.filePath.toFile().getAbsolutePath()));
} else {
post(json, this.executor, this.plugin, label);
}
processOutput(json);
}
public static CommandResult post(JsonObject payload, Sender sender, LuckPermsPlugin plugin, String label) {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
try (Writer writer = new OutputStreamWriter(new GZIPOutputStream(bytesOut), StandardCharsets.UTF_8)) {
GsonProvider.prettyPrinting().toJson(payload, writer);
} catch (IOException e) {
e.printStackTrace();
}
String pasteId;
try {
pasteId = plugin.getBytebin().postContent(bytesOut.toByteArray(), AbstractHttpClient.JSON_TYPE, false).key();
} catch (UnsuccessfulRequestException e) {
Message.EXPORT_HTTP_REQUEST_FAILURE.send(sender, e.getResponse().code(), e.getResponse().message());
return CommandResult.STATE_ERROR;
} catch (IOException e) {
new RuntimeException("Error uploading data to bytebin", e).printStackTrace();
Message.EXPORT_HTTP_UNKNOWN_FAILURE.send(sender);
return CommandResult.STATE_ERROR;
}
Message.EXPORT_CODE.send(sender, pasteId, label, pasteId);
return CommandResult.SUCCESS;
}
protected abstract void processOutput(JsonObject json);
private JsonObject exportGroups() {
JsonObject out = new JsonObject();
@ -275,4 +219,57 @@ public class Exporter implements Runnable {
}
return outJson;
}
public static final class SaveFile extends Exporter {
private final Path filePath;
public SaveFile(LuckPermsPlugin plugin, Sender executor, Path filePath, boolean includeUsers, boolean includeGroups) {
super(plugin, executor, includeUsers, includeGroups);
this.filePath = filePath;
}
@Override
protected void processOutput(JsonObject json) {
this.log.log("Finished gathering data, writing file...");
try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new GZIPOutputStream(Files.newOutputStream(this.filePath)), StandardCharsets.UTF_8))) {
GsonProvider.prettyPrinting().toJson(json, out);
} catch (IOException e) {
e.printStackTrace();
}
this.log.getListeners().forEach(l -> Message.LOG_EXPORT_SUCCESS.send(l, this.filePath.toFile().getAbsolutePath()));
}
}
public static final class WebUpload extends Exporter {
private final String label;
public WebUpload(LuckPermsPlugin plugin, Sender executor, boolean includeUsers, boolean includeGroups, String label) {
super(plugin, executor, includeUsers, includeGroups);
this.label = label;
}
@Override
protected void processOutput(JsonObject json) {
this.log.log("Finished gathering data, uploading data...");
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
try (Writer writer = new OutputStreamWriter(new GZIPOutputStream(bytesOut), StandardCharsets.UTF_8)) {
GsonProvider.prettyPrinting().toJson(json, writer);
} catch (IOException e) {
e.printStackTrace();
}
try {
String pasteId = this.plugin.getBytebin().postContent(bytesOut.toByteArray(), AbstractHttpClient.JSON_TYPE, false).key();
this.log.getListeners().forEach(l -> Message.EXPORT_CODE.send(l, pasteId, this.label, pasteId));
} catch (UnsuccessfulRequestException e) {
this.log.getListeners().forEach(l -> Message.EXPORT_HTTP_REQUEST_FAILURE.send(l, e.getResponse().code(), e.getResponse().message()));
} catch (IOException e) {
new RuntimeException("Error uploading data to bytebin", e).printStackTrace();
this.log.getListeners().forEach(Message.EXPORT_HTTP_UNKNOWN_FAILURE::send);
}
}
}
}

View File

@ -58,10 +58,17 @@ public class ExportCommand extends SingleCommand {
boolean includeUsers = !args.remove("--without-users");
boolean includeGroups = !args.remove("--without-groups");
boolean saveFile = !args.remove("--upload");
boolean upload = args.remove("--upload");
Exporter exporter;
if (saveFile) {
if (upload) {
if (!this.running.compareAndSet(false, true)) {
Message.EXPORT_ALREADY_RUNNING.send(sender);
return CommandResult.STATE_ERROR;
}
exporter = new Exporter.WebUpload(plugin, sender, includeUsers, includeGroups, label);
} else {
Path dataDirectory = plugin.getBootstrap().getDataDirectory();
Path path = dataDirectory.resolve(args.get(0) + ".json.gz");
@ -93,14 +100,7 @@ public class ExportCommand extends SingleCommand {
return CommandResult.STATE_ERROR;
}
exporter = new Exporter(plugin, sender, path, includeUsers, includeGroups, saveFile);
} else {
if (!this.running.compareAndSet(false, true)) {
Message.EXPORT_ALREADY_RUNNING.send(sender);
return CommandResult.STATE_ERROR;
}
exporter = new Exporter(plugin, sender, includeUsers, includeGroups, saveFile, label);
exporter = new Exporter.SaveFile(plugin, sender, path, includeUsers, includeGroups);
}
// Run the exporter in its own thread.