Fixes some behavior regarding backups
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good

Makes sure to only proceed with backups if a valid path is given
Makes the backup run in its own thread to prevent the software from locking up
Makes sure only one backup can be running at once
This commit is contained in:
Kristian Knarvik 2021-09-27 18:06:33 +02:00
parent 849655bfc6
commit bf77c13072
2 changed files with 29 additions and 11 deletions

View File

@ -33,7 +33,7 @@ import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.getR
*/
public class ServerLauncherGUI extends MessageHandler implements ActionListener, GUI {
private final JLabel lblStatuslabel = new JLabel("Servers are stopped");
private final JLabel lblStatusLabel = new JLabel("Servers are stopped");
private final ServerLauncherController controller;
private Map<String, String> textStrings;
private Tray applicationTray;
@ -101,7 +101,7 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
@Override
public void setStatus(String text) {
this.lblStatuslabel.setText(text);
this.lblStatusLabel.setText(text);
this.logMessage(text);
}
@ -243,11 +243,11 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
panelBasic.add(profiles);
profiles.addActionListener(this);
sl_panel.putConstraint(SpringLayout.NORTH, lblStatuslabel, 6, SpringLayout.SOUTH, addProfileButton);
sl_panel.putConstraint(SpringLayout.SOUTH, lblStatuslabel, -10, SpringLayout.SOUTH, panelBasic);
sl_panel.putConstraint(SpringLayout.WEST, lblStatuslabel, 10, SpringLayout.WEST, panelBasic);
sl_panel.putConstraint(SpringLayout.EAST, lblStatuslabel, -10, SpringLayout.EAST, panelBasic);
panelBasic.add(lblStatuslabel);
sl_panel.putConstraint(SpringLayout.NORTH, lblStatusLabel, 6, SpringLayout.SOUTH, addProfileButton);
sl_panel.putConstraint(SpringLayout.SOUTH, lblStatusLabel, -10, SpringLayout.SOUTH, panelBasic);
sl_panel.putConstraint(SpringLayout.WEST, lblStatusLabel, 10, SpringLayout.WEST, panelBasic);
sl_panel.putConstraint(SpringLayout.EAST, lblStatusLabel, -10, SpringLayout.EAST, panelBasic);
panelBasic.add(lblStatusLabel);
addServerButton = new JButton("Add server");
sl_panel.putConstraint(SpringLayout.NORTH, addServerButton, 0, SpringLayout.NORTH, startServerButton);
@ -314,7 +314,7 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
@Override
public void actionPerformed(ActionEvent e) {
Object actionSource = e.getSource();
//Registers actions on the main tab
//Register actions on the main tab
handleMainTabButtons(actionSource);
}
@ -333,7 +333,8 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
} else if (actionSource == addServerButton) {
addServer();
} else if (actionSource == backupButton) {
CommonFunctions.backup(this);
//Run backup in its own thread to prevent locking up
Executors.newSingleThreadExecutor().execute(() -> CommonFunctions.backup(this));
} else if (actionSource == addProfileButton) {
controller.addProfile(JOptionPane.showInputDialog("Profile name: "));
updateProfiles();

View File

@ -36,6 +36,7 @@ import java.util.Scanner;
public final class CommonFunctions {
private static final String filesDirectory = Main.getApplicationWorkDirectory() + File.separator + "files";
private static boolean backupRunning = false;
/**
* Creates all folders necessary for tests and normal operation
@ -53,7 +54,7 @@ public final class CommonFunctions {
* @param folder <p>The folder to create</p>
* @throws FileNotFoundException <p>If unable to create the folder</p>
*/
protected static void createFolder(File folder) throws FileNotFoundException {
public static void createFolder(File folder) throws FileNotFoundException {
if (!folder.exists()) {
if (!folder.mkdirs()) {
throw new FileNotFoundException("Cannot create necessary directory.");
@ -298,8 +299,21 @@ public final class CommonFunctions {
/**
* Copies all server directories to a folder specified by the user.
*/
public static void backup(GUI gui) {
public synchronized static void backup(GUI gui) {
if (backupRunning) {
return;
} else {
backupRunning = true;
}
//Get the folder to save the backed up files in
File path = gui.askForDirectory("Backup folder");
if (path == null || !path.isDirectory()) {
backupRunning = false;
return;
}
gui.setStatus("Backup running...");
for (Collection collection : Main.getController().getCurrentProfile().getCollections()) {
//Ignore disabled and invalid servers
if (collection.getServer().getPath().equals("") || !collection.getServer().isEnabled()) {
@ -312,6 +326,7 @@ public final class CommonFunctions {
File destinationFolder = new File(path, name);
//Create child folders
if (!destinationFolder.exists() && !destinationFolder.mkdirs()) {
backupRunning = false;
throw new IllegalArgumentException("Unable to create necessary sub-folder in the backup folder");
}
//Backup
@ -319,9 +334,11 @@ public final class CommonFunctions {
CommonFunctions.copyFolder(gui, srcFolder, destinationFolder);
} catch (IOException e) {
e.printStackTrace();
backupRunning = false;
}
}
gui.setStatus("Backup finished");
backupRunning = false;
}
/**