Stop the error on shutdown if GM failed to load at startup.

GroupManager will now generate it's own log (in the GM folder) to
keep things tidy, but also to account of those players unable to
find/access their server.log.
Startup errors will now lock out ALL commands other than '/manload'
This commit is contained in:
ElgarL 2012-04-10 20:40:04 +01:00
parent f02691863b
commit cdae0898c7
3 changed files with 174 additions and 44 deletions

View file

@ -170,3 +170,6 @@ v 2.0:
- Fix a concurrent modification error when removing all attachments.
- Better handling of errors in user and group yml's.
- Added missing confirmation message on '/manload'.
- Stop the error on shutdown if GM failed to load at startup.
- GroupManager will now generate it's own log (in the GM folder) to keep things tidy, but also to account of those players unable to find/access their server.log.
- Startup errors will now lock out ALL commands other than '/manload'

View file

@ -14,6 +14,7 @@ import org.anjocaido.groupmanager.data.Group;
import org.anjocaido.groupmanager.dataholder.OverloadedWorldHolder;
import org.anjocaido.groupmanager.dataholder.WorldDataHolder;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
@ -60,6 +61,9 @@ public class GroupManager extends JavaPlugin {
private Map<CommandSender, String> selectedWorlds = new HashMap<CommandSender, String>();
private WorldsHolder worldsHolder;
private boolean validateOnlinePlayer = true;
private String lastError = "";
/**
* @return the validateOnlinePlayer
*/
@ -108,8 +112,10 @@ public class GroupManager extends JavaPlugin {
WorldEvents = null;
// Remove all attachments before clearing
if (BukkitPermissions != null) {
BukkitPermissions.removeAllAttachments();
BukkitPermissions = null;
}
// EXAMPLE: Custom code, here we just output some info so we can check that
// all is well
@ -118,8 +124,12 @@ public class GroupManager extends JavaPlugin {
GroupManager.logger.removeHandler(ch);
}
@Override
//@Override
public void onEnable() {
try {
lastError = "";
GroupManager.logger.setUseParentHandlers(false);
ch = new GMLoggerHandler();
GroupManager.logger.addHandler(ch);
@ -165,6 +175,57 @@ public class GroupManager extends JavaPlugin {
// Register as a service
this.getServer().getServicesManager().register(WorldsHolder.class, this.worldsHolder, this, ServicePriority.Lowest);
} catch (Exception ex) {
/*
* Store the error and write to the log.
*/
saveErrorLog(ex);
/*
* Throw an error so Bukkit knows about it.
*/
throw new IllegalArgumentException(ex.getMessage(),ex);
}
}
/**
* Write an error.log
*
* @param ex
*/
private void saveErrorLog(Exception ex) {
if (!getDataFolder().exists()) {
getDataFolder().mkdirs();
}
lastError = ex.getMessage();
GroupManager.logger.severe("===================================================");
GroupManager.logger.severe("= ERROR REPORT START =");
GroupManager.logger.severe("===================================================");
GroupManager.logger.severe("=== PLEASE COPY AND PASTE THE ERROR.LOG FROM THE ==");
GroupManager.logger.severe("= GROUPMANAGER FOLDER TO AN ESSENTIALS DEVELOPER =");
GroupManager.logger.severe("===================================================");
GroupManager.logger.severe(lastError);
GroupManager.logger.severe("===================================================");
GroupManager.logger.severe("= ERROR REPORT ENDED =");
GroupManager.logger.severe("===================================================");
// Append this error to the error log.
try {
String error = "=============================== GM ERROR LOG ===============================\n\n";
error += Tasks.getStackTraceAsString(ex);
error += "\n============================================================================\n";
Tasks.appendStringToFile(error, (getDataFolder() + System.getProperty("file.separator") + "ERROR.LOG"));
} catch (IOException e) {
// Failed to write file.
e.printStackTrace();
}
}
public static boolean isLoaded() {
@ -301,9 +362,16 @@ public class GroupManager extends JavaPlugin {
User senderUser = null;
boolean isOpOverride = config.isOpOverride();
// DETERMINING PLAYER INFORMATION
if (sender instanceof Player) {
senderPlayer = (Player) sender;
if (!lastError.isEmpty() && !commandLabel.equalsIgnoreCase("manload")) {
sender.sendMessage(ChatColor.RED + "All commands are locked due to an error. Check the log and then try a '/manload'.)");
return true;
}
senderUser = worldsHolder.getWorldData(senderPlayer).getUser(senderPlayer.getName());
senderGroup = senderUser.getGroup();
isOpOverride = (isOpOverride && (senderPlayer.isOp() || worldsHolder.getWorldPermissions(senderPlayer).has(senderPlayer, "groupmanager.op")));
@ -313,6 +381,12 @@ public class GroupManager extends JavaPlugin {
playerCanDo = true;
}
} else if (sender instanceof ConsoleCommandSender) {
if (!lastError.isEmpty() && !commandLabel.equalsIgnoreCase("manload")) {
sender.sendMessage(ChatColor.RED + "All commands are locked due to an error. Check the log and then try a '/manload'.)");
return true;
}
isConsole = true;
}
@ -1560,10 +1634,17 @@ public class GroupManager extends JavaPlugin {
return true;
case manload:
/**
* Attempt to reload a specific world
*/
if (args.length > 0) {
if (!lastError.isEmpty()) {
sender.sendMessage(ChatColor.RED + "All commands are locked due to an error. Check the log and then try a '/manload'.)");
return true;
}
auxString = "";
for (int i = 0; i < args.length; i++) {
auxString += args[i];
@ -1588,6 +1669,11 @@ public class GroupManager extends JavaPlugin {
/**
* Reload all settings and data as no world was specified.
*/
/*
* Reset the last error as we are attempting a fresh load.
*/
lastError = "";
onDisable();
onEnable();

View file

@ -4,12 +4,17 @@
*/
package org.anjocaido.groupmanager.utils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
@ -17,12 +22,26 @@ import java.util.List;
import org.anjocaido.groupmanager.GroupManager;
import org.anjocaido.groupmanager.data.Group;
/**
*
* @author gabrielcouto
*/
public abstract class Tasks {
/**
* Gets the exception stack trace as a string.
*
* @param exception
* @return stack trace as a string
*/
public static String getStackTraceAsString(Exception exception) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
exception.printStackTrace(pw);
return sw.toString();
}
public static void copy(InputStream src, File dst) throws IOException {
InputStream in = src;
OutputStream out = new FileOutputStream(dst);
@ -45,6 +64,28 @@ public abstract class Tasks {
copy(in, dst);
}
/**
* Appends a string to a file
*
* @param data
* @param file
*/
public static void appendStringToFile(String data, String file) throws IOException {
FileWriter outStream = new FileWriter("." + System.getProperty("file.separator") + file, true);
BufferedWriter out = new BufferedWriter(outStream);
data.replaceAll("\n", System.getProperty("line.separator"));
out.append(new SimpleDateFormat("yyyy-MM-dd HH-mm").format(System.currentTimeMillis()));
out.append(System.getProperty("line.separator"));
out.append(data);
out.append(System.getProperty("line.separator"));
out.close();
}
public static void removeOldFiles(GroupManager gm, File folder) {
if (folder.isDirectory()) {
long oldTime = System.currentTimeMillis() - (((long)gm.getGMConfig().getBackupDuration()*60*60)*1000);