TF-EssentialsX/Essentials/src/com/earth2me/essentials/Backup.java

169 lines
3.5 KiB
Java
Raw Normal View History

package com.earth2me.essentials;
import static com.earth2me.essentials.I18n.tl;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
2013-10-11 02:44:41 +00:00
import net.ess3.api.IEssentials;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
public class Backup implements Runnable
{
2013-12-07 20:03:05 +00:00
private static final Logger LOGGER = Logger.getLogger("Essentials");
private transient final Server 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 = ess.getServer();
if (!ess.getOnlinePlayers().isEmpty())
{
2013-04-13 22:10:01 +00:00
ess.runTaskAsynchronously(new Runnable()
{
@Override
public void run()
{
startTask();
}
});
}
}
2013-04-13 22:10:01 +00:00
public void onPlayerJoin()
{
startTask();
}
2013-04-14 01:44:16 +00:00
public synchronized void stopTask()
2013-04-13 22:10:01 +00:00
{
running = false;
if (taskId != -1)
{
server.getScheduler().cancelTask(taskId);
}
taskId = -1;
}
private synchronized void startTask()
{
if (!running)
{
final long interval = ess.getSettings().getBackupInterval() * 1200; // minutes -> ticks
if (interval < 1200)
{
return;
}
taskId = ess.scheduleSyncRepeatingTask(this, interval, interval);
running = true;
}
}
2011-11-18 18:07:28 +00:00
@Override
public void run()
{
if (active)
{
return;
}
active = true;
final String command = ess.getSettings().getBackupCommand();
if (command == null || "".equals(command))
{
return;
}
2013-04-13 22:10:01 +00:00
if ("save-all".equalsIgnoreCase(command))
{
final CommandSender cs = server.getConsoleSender();
server.dispatchCommand(cs, "save-all");
active = false;
return;
}
LOGGER.log(Level.INFO, tl("backupStarted"));
final CommandSender cs = server.getConsoleSender();
server.dispatchCommand(cs, "save-all");
server.dispatchCommand(cs, "save-off");
2014-01-25 14:33:33 +00:00
ess.runTaskAsynchronously(new Runnable()
{
@Override
public void run()
{
try
{
2014-01-25 14:33:33 +00:00
final ProcessBuilder childBuilder = new ProcessBuilder(command);
childBuilder.redirectErrorStream(true);
childBuilder.directory(ess.getDataFolder().getParentFile().getParentFile());
final Process child = childBuilder.start();
ess.runTaskAsynchronously(new Runnable()
{
2014-01-25 14:33:33 +00:00
@Override
public void run()
{
try
{
2014-01-25 14:33:33 +00:00
final BufferedReader reader = new BufferedReader(new InputStreamReader(child.getInputStream()));
try
{
2014-01-25 14:33:33 +00:00
String line;
do
{
2014-01-25 14:33:33 +00:00
line = reader.readLine();
if (line != null)
{
LOGGER.log(Level.INFO, line);
}
}
2014-01-25 14:33:33 +00:00
while (line != null);
}
finally
{
reader.close();
}
}
2014-01-25 14:33:33 +00:00
catch (IOException ex)
{
2014-01-25 14:33:33 +00:00
LOGGER.log(Level.SEVERE, null, ex);
}
}
2014-01-25 14:33:33 +00:00
});
child.waitFor();
}
catch (InterruptedException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
}
catch (IOException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
}
finally
{
class BackupEnableSaveTask implements Runnable
2014-01-25 14:33:33 +00:00
{
@Override
public void run()
{
2014-01-25 14:33:33 +00:00
server.dispatchCommand(cs, "save-on");
if (ess.getOnlinePlayers().isEmpty())
2014-01-25 14:33:33 +00:00
{
stopTask();
}
active = false;
LOGGER.log(Level.INFO, tl("backupFinished"));
}
}
ess.scheduleSyncDelayedTask(new BackupEnableSaveTask());
2014-01-25 14:33:33 +00:00
}
}
});
}
}