Browse Source

Added multiple Exception Handlers.

- Added Exception Handlers.
- Tested code, added fixes.
- Added README.
Domenic Horner 9 years ago
parent
commit
ab72debd72

+ 21 - 0
README

@@ -0,0 +1,21 @@
+/*
+ *  Backup - CraftBukkit server Backup plugin (continued)
+ *  Copyright (C) 2011 Domenic Horner <https://github.com/gamerx/Backup>
+ *  Copyright (C) 2011 Lycano <https://github.com/gamerx/Backup>
+ *
+ *  Backup - CraftBukkit server Backup plugin (original author)
+ *  Copyright (C) 2011 Kilian Gaertner <https://github.com/Meldanor/Backup>
+ * 
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ * 
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ * 
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */

+ 31 - 27
src/main/java/net/tgxn/bukkit/backup/BackupMain.java

@@ -1,23 +1,10 @@
 /*
- *  Backup - CraftBukkit server Backup plugin (continued)
- *  Copyright (C) 2011 Domenic Horner <https://github.com/gamerx/Backup>
- *  Copyright (C) 2011 Lycano <https://github.com/gamerx/Backup>
- *
- *  Backup - CraftBukkit server Backup plugin (original author)
- *  Copyright (C) 2011 Kilian Gaertner <https://github.com/Meldanor/Backup>
+ *  Backup - CraftBukkit Server Backup Plugin.
+ *   
+ *  Copyright - Domenic Horner, lycano, Kilian Gaertner.
+ *  URL: https://github.com/gamerx/Backup
  * 
- *  This program is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- * 
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- * 
- *  You should have received a copy of the GNU General Public License
- *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *  Please read README and LICENSE for more details.
  */
 
 package net.tgxn.bukkit.backup;
@@ -38,6 +25,7 @@ import org.bukkit.plugin.PluginManager;
 import org.bukkit.plugin.java.JavaPlugin;
 
 import java.io.File;
+import java.util.logging.Level;
 
 public class BackupMain extends JavaPlugin {
      
@@ -46,17 +34,25 @@ public class BackupMain extends JavaPlugin {
     protected static Settings settings;
     private PrepareBackupTask preparedBackupTask;
     public int mainBackupTaskID;
-    
+
     @Override
     public void onLoad() {
          
-        // Init LogUtils.
+        // Init LogUtils, for logging purposes.
         LogUtils.initLogUtils(this);
         
-        // Check plugin Data Folder, create if not exist.
+        // Check the plugin's data folder exists.
         if (!this.getDataFolder().exists()) {
-            // @TODO create try catch exception class on error
-            this.getDataFolder().mkdirs();
+            
+            // Try to create the folder.
+            try {
+                this.getDataFolder().mkdirs();
+            } catch(SecurityException se) {
+                
+                // Advise this failed.
+                LogUtils.sendLog(Level.SEVERE, "Failed to create plugin's data folder: Security Exception." );
+                //se.printStackTrace(System.out);
+            }
         }
         
         // Load Properties, create if needed.
@@ -65,12 +61,20 @@ public class BackupMain extends JavaPlugin {
         // Load Strings, create if needed.
         strings = new Strings(this);
 
-        // Check backup folder, create if needed.
+        // Check the specified backup folder exists.
         File backupsFolder = new File(settings.getStringProperty("backuppath"));
         if (!backupsFolder.exists()) {
-            //@TODO create try catch exception class on error
-            backupsFolder.mkdirs();
-            LogUtils.sendLog(strings.getString("createbudir"));
+            
+            // Try to create the folder.
+            try {
+                 if(backupsFolder.mkdirs())
+                    LogUtils.sendLog(strings.getString("createbudir"));
+            } catch(SecurityException se) {
+                
+                // Advise this failed.
+                LogUtils.sendLog(Level.SEVERE, "Failed to create backup folder: Security Exception." );
+                //se.printStackTrace(System.out);
+            }
         }
     }
     

+ 34 - 33
src/main/java/net/tgxn/bukkit/backup/config/Settings.java

@@ -19,7 +19,6 @@
  *  You should have received a copy of the GNU General Public License
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-
 package net.tgxn.bukkit.backup.config;
 
 import net.tgxn.bukkit.backup.utils.LogUtils;
@@ -30,7 +29,7 @@ import java.io.*;
 import java.util.logging.Level;
 
 public class Settings {
-    
+
     private Configuration config;
     private Strings strings;
     private File configFile;
@@ -42,19 +41,24 @@ public class Settings {
      * @param plugin The plugin this is for.
      */
     public Settings(Plugin plugin) {
-        
+
         // Load strings.
         strings = new Strings(plugin);
-        
+
         // Create the file object used in this class.
         configFile = new File(plugin.getDataFolder(), "config.yml");
-        
+
         // Check for the config file, have it created if needed.
-        if (!configFile.exists()) {
-            LogUtils.sendLog(Level.WARNING, strings.getString("newconfigfile"), true);
-            createDefaultSettings();
+        try {
+            if (!configFile.exists()) {
+                LogUtils.sendLog(Level.WARNING, strings.getString("newconfigfile"));
+                createDefaultSettings();
+            }
+        } catch (SecurityException se) {
+            LogUtils.sendLog(Level.SEVERE, "Failed to check config file: Security Exception.");
+            //se.printStackTrace(System.out);
         }
-        
+
         // Load the properties.
         loadProperties(plugin);
     }
@@ -65,35 +69,36 @@ public class Settings {
     private void createDefaultSettings() {
         BufferedReader bReader = null;
         BufferedWriter bWriter = null;
-        
+        String line;
+
         try {
-            
+
             // Open a stream to the properties file in the jar, because we can only access over the class loader.
             bReader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/settings/config.yml")));
             bWriter = new BufferedWriter(new FileWriter(configFile));
-            
-            String line;
-            
+
             // Copy the content to the configfile location.
             while ((line = bReader.readLine()) != null) {
                 bWriter.write(line);
                 bWriter.newLine();
             }
-        } catch (Exception e) {
-            /** @TODO create exception classes **/
-            e.printStackTrace(System.out);
+        } catch (IOException ioe) {
+            LogUtils.sendLog(Level.SEVERE, "Could not create default config.yml: IO Exception.");
+            //ioe.printStackTrace(System.out);
         }
         
         // Make sure everything is closed.
         finally {
             try {
-                if (bReader != null)
+                if (bReader != null) {
                     bReader.close();
-                if (bWriter != null)
+                }
+                if (bWriter != null) {
                     bWriter.close();
-            } catch (Exception e) {
-                /** @TODO create exception classes **/
-                e.printStackTrace(System.out);
+                }
+            } catch (IOException ioe) {
+                LogUtils.sendLog(Level.SEVERE, "Failed to close bReader or bWriter: IO Exception.");
+                //ioe.printStackTrace(System.out);
             }
         }
     }
@@ -104,22 +109,18 @@ public class Settings {
      * @param plugin The plugin this is for.
      */
     private void loadProperties(Plugin plugin) {
-        
+
         // Create new configuration file.
         config = new Configuration(configFile);
-        
-        // Attempt to load the configfile.
-        try {
-            config.load();
-        } catch (Exception ex) {
-            /** @TODO create exception classes **/
-            ex.printStackTrace(System.out);
-        }
+
+        // Attempt to load configuration.
+        config.load();
 
         // Get version, and log message if out-of-date.
         String version = config.getString("version", plugin.getDescription().getVersion());
-        if (version == null || !version.equals(plugin.getDescription().getVersion()))
-            LogUtils.sendLog(Level.SEVERE, strings.getString("configoutdated"), true);
+        if (version == null || !version.equals(plugin.getDescription().getVersion())) {
+            LogUtils.sendLog(Level.WARNING, strings.getString("configoutdated"), true);
+        }
 
     }
 

+ 3 - 8
src/main/java/net/tgxn/bukkit/backup/config/Strings.java

@@ -43,13 +43,8 @@ public class Strings {
         strings = new Configuration(new File(plugin.getDataFolder(), "strings.yml"));
         
         // Attempt to load the strings.
-        try {
-            strings.load();
-        } catch (Exception ex) {
-            /** @TODO create exception classes **/
-            ex.printStackTrace(System.out);
-        }
-
+        strings.load();
+        
         // In-Game Messages
         /** Backup started **/
         strings.getString("backupstarted", "Started Backup...");
@@ -94,7 +89,7 @@ public class Strings {
         /** Backup failure **/
         strings.getString("backupfailed", "An error occured while backing up. Please report to an admin!");
         
-        strings.getString("newconfigfile",  "Your config file was not found, creating default...");
+        strings.getString("newconfigfile",  "Your config file was not found, Loaded defaults.");
         strings.getString("configoutdated", "Your config file file is outdated, please delete it and a new one will be created!");
         
         

+ 77 - 48
src/main/java/net/tgxn/bukkit/backup/threading/BackupTask.java

@@ -80,23 +80,13 @@ public class BackupTask implements Runnable {
 
     @Override
     public void run() {
-
-        // This will catch any backup errors.
-        try {
-            // Run the backup.
-            backup();
-        } catch (Exception ex) {
-            /** @TODO create exception classes **/
-            ex.printStackTrace(System.out);
-        }
+        backup();
     }
 
     /**
      * Run the backup.
-     * 
-     * @throws Exception 
      */
-    public void backup() throws Exception {
+    public void backup() {
 
         // Settings.
         String backupPath = settings.getStringProperty("backuppath").concat(FILE_SEPARATOR);
@@ -126,7 +116,7 @@ public class BackupTask implements Runnable {
                     if (f.getName().equals(settings.getStringProperty("backuppath"))) {
                         return false;
                     }
-                    
+
                     if (f.getName().equals("server.log")) {
                         return false;
                     }
@@ -141,20 +131,24 @@ public class BackupTask implements Runnable {
 
             // Copy this world into the backup directory, in a folder called the worlds name.
             try {
-                
+
                 // Copy the directory.
                 FileUtils.copyDirectory(srcDIR, destDIR, ff, true);
-                
+
                 // Perform the zipping action. 
                 doZIP(backupDirName);
 
-            } catch (FileNotFoundException ex) {
-                ex.printStackTrace(System.out);
-            } catch (IOException e) {
-                LogUtils.sendLog("Error with full backup");
-                /** @TODO create exception classes **/
-                e.printStackTrace(System.out);
+            } catch (FileNotFoundException fnfe) {
+
+                LogUtils.sendLog("Full backup: Source not found.");
+                //fnfe.printStackTrace(System.out);
+
+            } catch (IOException ioe) {
+
+                LogUtils.sendLog("Error occurred while performing backup: IO Exception.");
                 server.broadcastMessage(strings.getString("backupfailed"));
+
+                //ioe.printStackTrace(System.out);
             }
 
             // If we are just backing up worlds/plugins.
@@ -169,31 +163,45 @@ public class BackupTask implements Runnable {
                     // Remove first world from the array and put it into a var.
                     String worldName = worldsToBackup.removeFirst();
 
-                    
+
                     if (splitbackup) {
                         // Split into world folders.
 
                         // Check this worlds folder exists.
                         File woldBUfolder = new File(backupPath.concat(FILE_SEPARATOR).concat(worldName));
-                        if (!woldBUfolder.exists()) {
-                            //@TODO create try catch exception class on error
-                            woldBUfolder.mkdirs();
+
+                        // Try to create the folder.
+                        try {
+                            if (!woldBUfolder.exists()) {
+                                woldBUfolder.mkdirs();
+                            }
+                        } catch (SecurityException se) {
+
+                            // Advise this failed.
+                            LogUtils.sendLog(Level.SEVERE, "Failed to make the world folder: Security Exception.");
+                            //se.printStackTrace(System.out);
                         }
-                        
+
                         // "backups/world/30092011-142238"
                         String thisbackupfname = backupPath.concat(worldName).concat(FILE_SEPARATOR).concat(getFolderName());
-                        
+
                         // Copy the world into its backup folder.
-                        FileUtils.copyDirectory(worldName, thisbackupfname);
+                        try {
+                            FileUtils.copyDirectory(worldName, thisbackupfname);
+                        } catch (IOException ioe) {
 
+                            // Advise this failed.
+                            LogUtils.sendLog(Level.SEVERE, "Failed to Copy the world's folder: IO Exception.");
+                            //ioe.printStackTrace(System.out);
+                        }
                         // ZIP if required.
                         doZIP(thisbackupfname);
 
                     } else {
                         // Not split backup.
-                        
+
                         try {
-                            
+
                             // Copy this world into the backup directory, in a folder called the worlds name.
                             FileUtils.copyDirectory(worldName, backupDirName.concat(FILE_SEPARATOR).concat(worldName));
 
@@ -241,17 +249,17 @@ public class BackupTask implements Runnable {
 
                 // Setup Source and destination DIR's.
                 File srcDIR = new File("plugins");
-                
+
                 // Touch the folder to update the modified date.
                 srcDIR.setLastModified(System.currentTimeMillis());
                 String destDIR;
-                
+
                 if (splitbackup) {
                     // Splitting backup.
-                    
+
                     // "backups/plugins/30092011-142238"
                     String thisbackupfname = backupPath.concat("plugins").concat(FILE_SEPARATOR).concat(getFolderName());
-                    
+
                     destDIR = thisbackupfname;
                 } else {
                     destDIR = backupDirName.concat(FILE_SEPARATOR).concat("plugins");
@@ -265,7 +273,7 @@ public class BackupTask implements Runnable {
                         LogUtils.sendLog(skippedPlugins.toString());
 
                     }
-                    
+
                     // erform the copy.
                     FileUtils.copyDirectory(srcDIR, new File(destDIR), ffplugins, true);
 
@@ -297,7 +305,8 @@ public class BackupTask implements Runnable {
         }
 
         // Delete old backups.
-        deleteOldBackups();
+        if(!deleteOldBackups())
+            LogUtils.sendLog("Failed to delete old backups.");
 
         // Clean up.
         finish();
@@ -336,7 +345,7 @@ public class BackupTask implements Runnable {
      */
     private void doZIP(String path) {
 
-        // Check we are ZIPing.
+        // Check we are ZIPing the backups.
         if (ShouldZIP) {
             try {
 
@@ -345,8 +354,11 @@ public class BackupTask implements Runnable {
 
                 // Delete the original backup directory.
                 FileUtils.deleteDirectory(new File(path));
-            } catch (Exception e) {
-                //@TODO Exception handler.
+            } catch (IOException ioe) {
+
+                // Advise this failed.
+                LogUtils.sendLog("Failed to ZIP Backup: IO Exception.");
+                //ioe.printStackTrace(System.out);
             }
         }
     }
@@ -355,20 +367,38 @@ public class BackupTask implements Runnable {
      * Check whether there are more backups as allowed to store. 
      * When this case is true, it deletes oldest ones.
      */
-    private void deleteOldBackups() {
-        //@TODO We need to modify this to support split backups.
+    private boolean deleteOldBackups() {
+
+        // Get the backup's directory.
         File backupDir = new File(settings.getStringProperty("backuppath"));
+
+        // Check if split backup or not.
         if (splitbackup) {
+
+            // Loop the folders, and crean for each.
             File[] foldersToClean = backupDir.listFiles();
             for (int l = 0; l < foldersToClean.length; l++) {
-                cleanFolder(foldersToClean[l]);
+                try {
+                    cleanFolder(foldersToClean[l]);
+                } catch (IOException ioe) {
+                    //ioe.printStackTrace(System.out);
+                    return false;
+                }
             }
         } else {
-            cleanFolder(backupDir);
+
+            // Clean entire directory.
+            try {
+                cleanFolder(backupDir);
+            } catch (IOException ioe) {
+                //ioe.printStackTrace(System.out);
+                return false;
+            }
         }
+        return true;
     }
 
-    private void cleanFolder(File backupDir) {
+    private void cleanFolder(File backupDir) throws IOException {
 
         // Get properties.
         try {
@@ -412,12 +442,11 @@ public class BackupTask implements Runnable {
             //@TODO write exception class
             e.printStackTrace(System.out);
         }
-        
+
 
 
     }
-    
-    
+
     public boolean deleteDir(File dir) {
         if (dir.isDirectory()) {
             String[] children = dir.list();
@@ -430,7 +459,7 @@ public class BackupTask implements Runnable {
         }
         return dir.delete();
     }
-    
+
     /**
      * Creates a temporary Runnable that is running on the main thread by the scheduler to prevent thread problems.
      */

+ 10 - 0
src/main/java/net/tgxn/bukkit/backup/utils/LogUtils.java

@@ -81,6 +81,16 @@ public class LogUtils {
     public static void sendLog(String message, boolean tags) {
         sendLog(Level.INFO, message, tags);
     }
+    
+    /**
+     * Sends log message.
+     * 
+     * @param message
+     * @param tags 
+     */
+    public static void sendLog(Level level, String message) {
+        sendLog(level, message);
+    }
 
     /**
      * Sends log message.