summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsnowleo <schneeleo@gmail.com>2011-08-27 17:50:44 +0200
committersnowleo <schneeleo@gmail.com>2011-08-27 17:50:44 +0200
commit033babd586d0d2319ce0a4d0ed88dc740ea2a59a (patch)
tree2764dda3ec1f0795ff0ee7f788f24289450bd3a9
parentd3302dea00d9edb1fc0896382c2cd4a53e49f382 (diff)
downloadEssentials-033babd586d0d2319ce0a4d0ed88dc740ea2a59a.tar
Essentials-033babd586d0d2319ce0a4d0ed88dc740ea2a59a.tar.gz
Essentials-033babd586d0d2319ce0a4d0ed88dc740ea2a59a.tar.lz
Essentials-033babd586d0d2319ce0a4d0ed88dc740ea2a59a.tar.xz
Essentials-033babd586d0d2319ce0a4d0ed88dc740ea2a59a.zip
Cleanup & output result of backup command
-rw-r--r--Essentials/src/com/earth2me/essentials/Backup.java142
1 files changed, 94 insertions, 48 deletions
diff --git a/Essentials/src/com/earth2me/essentials/Backup.java b/Essentials/src/com/earth2me/essentials/Backup.java
index 6b88eaef9..b7b441b1e 100644
--- a/Essentials/src/com/earth2me/essentials/Backup.java
+++ b/Essentials/src/com/earth2me/essentials/Backup.java
@@ -1,35 +1,45 @@
package com.earth2me.essentials;
+import java.io.BufferedReader;
import java.io.IOException;
+import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.CraftServer;
-public class Backup implements Runnable {
- private static final Logger logger = Logger.getLogger("Minecraft");
- private final CraftServer server;
- private final IEssentials ess;
- private boolean running = false;
- private int taskId = -1;
- private boolean active = false;
- public Backup(IEssentials ess) {
+public class Backup implements Runnable
+{
+ private static final Logger LOGGER = Logger.getLogger("Minecraft");
+ private transient final CraftServer server;
+ private transient final IEssentials ess;
+ private transient boolean running = false;
+ private transient int taskId = -1;
+ private transient boolean active = false;
+
+ public Backup(final IEssentials ess)
+ {
this.ess = ess;
server = (CraftServer)ess.getServer();
- if (server.getOnlinePlayers().length > 0) {
+ if (server.getOnlinePlayers().length > 0)
+ {
startTask();
}
- }
+ }
- void onPlayerJoin() {
+ void onPlayerJoin()
+ {
startTask();
}
-
- private void startTask() {
- if (!running) {
- long interval = ess.getSettings().getBackupInterval()*1200; // minutes -> ticks
- if (interval < 1200) {
+
+ private void startTask()
+ {
+ if (!running)
+ {
+ final long interval = ess.getSettings().getBackupInterval() * 1200; // minutes -> ticks
+ if (interval < 1200)
+ {
return;
}
taskId = ess.scheduleSyncRepeatingTask(this, interval, interval);
@@ -37,48 +47,84 @@ public class Backup implements Runnable {
}
}
- public void run() {
- if (active) return;
+ public void run()
+ {
+ if (active)
+ {
+ return;
+ }
active = true;
final String command = ess.getSettings().getBackupCommand();
- if (command == null || "".equals(command)) {
+ if (command == null || "".equals(command))
+ {
return;
}
- logger.log(Level.INFO, Util.i18n("backupStarted"));
+ LOGGER.log(Level.INFO, Util.i18n("backupStarted"));
final CommandSender cs = server.getServer().console;
server.dispatchCommand(cs, "save-all");
server.dispatchCommand(cs, "save-off");
-
- ess.scheduleAsyncDelayedTask(
- new Runnable() {
-
- public void run() {
- try {
- Process child = Runtime.getRuntime().exec(command);
- child.waitFor();
- } catch (InterruptedException ex) {
- logger.log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- logger.log(Level.SEVERE, null, ex);
- } finally {
- ess.scheduleSyncDelayedTask(
- new Runnable() {
- public void run() {
- server.dispatchCommand(cs, "save-on");
- if (server.getOnlinePlayers().length == 0) {
- running = false;
- if (taskId != -1) {
- server.getScheduler().cancelTask(taskId);
+ ess.scheduleAsyncDelayedTask(
+ new Runnable()
+ {
+ public void run()
+ {
+ try
+ {
+ final ProcessBuilder childBuilder = new ProcessBuilder(command);
+ childBuilder.redirectErrorStream(true);
+ childBuilder.directory(ess.getDataFolder().getParentFile().getParentFile());
+ final Process child = childBuilder.start();
+ final BufferedReader reader = new BufferedReader(new InputStreamReader(child.getInputStream()));
+ try
+ {
+ child.waitFor();
+ String line;
+ do
+ {
+ line = reader.readLine();
+ if (line != null)
+ {
+ LOGGER.log(Level.INFO, line);
+ }
}
+ while (line != null);
+ }
+ finally
+ {
+ reader.close();
}
- active = false;
- logger.log(Level.INFO, Util.i18n("backupFinished"));
}
- });
- }
- }
- });
+ catch (InterruptedException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ catch (IOException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ finally
+ {
+ ess.scheduleSyncDelayedTask(
+ new Runnable()
+ {
+ public void run()
+ {
+ server.dispatchCommand(cs, "save-on");
+ if (server.getOnlinePlayers().length == 0)
+ {
+ running = false;
+ if (taskId != -1)
+ {
+ server.getScheduler().cancelTask(taskId);
+ }
+ }
+ active = false;
+ LOGGER.log(Level.INFO, Util.i18n("backupFinished"));
+ }
+ });
+ }
+ }
+ });
}
-
}