package net.sourceforge.pain.logic.event.console.command;
import net.sourceforge.pain.*;
import net.sourceforge.pain.logic.fn.*;
import net.sourceforge.pain.plugin.*;
import net.sourceforge.pain.plugin.shutdown.*;
import net.sourceforge.pain.util.*;
public final class Shutdown extends GrantedCommand {
final static String SHUTDOWN_PLUGIN_NAME = "shutdown.ShutdownTimer";
public void processCommand() throws Exception {
if (commandParams == null) {
showUsage();
return;
}
if ("EMERGENCY".equals(commandParams)) { // if there are some problems with plugins or code..
Core.getTime().stopTimer();
} else {
PluginManager plm = Core.getPluginManager();
ShutdownTimer shutTimer = (ShutdownTimer) plm.getPlugin(SHUTDOWN_PLUGIN_NAME);
if (shutTimer == null) {
MessageOutFn.outln(console, "ShutdownTimer plugin is not loaded!");
return;
}
commandParams = commandParams.toUpperCase();
final boolean wasRunned = shutTimer.isShutdownInProcess();
Log.debug("shutdown was runned:" + wasRunned);
if ("STOP".equals(commandParams) || "CANCEL".equals(commandParams)) {
if (wasRunned) {
shutTimer.cancel(true);
} else {
MessageOutFn.outln(console, "Shutdown was not runned!");
return;
}
} else if ("NOW".equals(commandParams)) {
shutTimer.now();
} else if ("TIME".equals(commandParams)) {
if (!wasRunned) {
MessageOutFn.outln(console, "Shutdown was not runned!");
return;
}
MessageOutFn.outln(console, "Shutdown time " + shutTimer.getTimeBeforeShutdown() + " sec.");
} else {
int minutes;
try {
minutes = Integer.parseInt(commandParams);
} catch (Exception e) {
showUsage();
return;
}
minutes = Math.max(minutes, 0);
shutTimer.reset(minutes * 60, !wasRunned);
if (wasRunned) {
MessageOutFn.outln(console, "Shutdown time changed");
}
}
}
}
private void showUsage() {
MessageOutFn.outln(console, command.name + ": specify time in minutes to shutdown, 'NOW' for instant shutdown or 'CANCEL' to stop");
}
}