1 6 7 package com.sun.enterprise.config.backup; 8 9 import java.io.IOException ; 10 import java.io.File ; 11 import java.util.Arrays ; 12 import java.util.Collections ; 13 import java.util.ArrayList ; 14 import java.util.List ; 15 import java.io.FilenameFilter ; 16 import com.sun.enterprise.config.backup.utils.LoggerHelper; 17 import com.sun.enterprise.config.backup.utils.LocalStringsHelper; 18 import com.sun.enterprise.config.backup.pluggable.EnvironmentFactory; 19 import com.sun.enterprise.config.backup.pluggable.BackupEnvironment; 20 import com.sun.enterprise.config.backup.utils.FactoryHelper; 21 import com.sun.enterprise.config.backup.phase.RemovePhases; 22 23 24 29 public abstract class StorageMonitor { 30 31 32 public StorageMonitor() { 33 } 34 35 43 public void run() { 44 try { 45 File [] f = getFiles(); 46 int num = getNumberOfBackups(f); 47 int max = getMaxBackups(); 48 49 if(num <= max) { 50 LoggerHelper.fine("no_backup_file_to_delete"); 51 return; } 53 54 deleteBackups(f, num-max); 55 } catch(Exception e) { 56 LoggerHelper.info("error_monitoring_max_backups", e); 58 } 59 } 60 61 public File getLatestFile() throws BackupException { 62 File [] f = getFiles(); 63 if(f == null) { 64 LoggerHelper.fine("no_backup_file_found"); 65 return null; 66 } 67 Arrays.sort(f, Collections.reverseOrder()); 68 return f[0]; 69 } 70 71 protected File [] getFiles() throws BackupException { 72 String dir = getMonitoredDirectory(); 73 File f = new File (dir); 74 FilenameFilter filter = getFileNameFilter(); 75 return f.listFiles(filter); 76 } 77 78 private int getNumberOfBackups(File [] f) { 79 if (f == null) return 0; 80 81 return f.length; 82 } 83 84 85 private void deleteBackups(File [] f, int number) 86 throws IOException , BackupException { 87 88 if(f == null || number == 0) { 89 LoggerHelper.fine("no_backup_file_to_delete"); 90 return; 91 } 92 93 99 Arrays.sort(f); 100 deleteFiles(f, number); 101 } 102 103 106 131 private File [] convertToFileArr(ArrayList arr) { 132 if(arr==null || arr.size() == 0) return null; 133 File [] res = new File [arr.size()]; 134 for(int i=0;i<arr.size();i++) { 135 res[i] = (File ) arr.get(i); 136 } 137 return res; 138 } 139 140 private void deleteFiles(File [] delete, int number) { 141 if(delete == null) return; 142 int max; 143 144 if(number > delete.length) 145 max = delete.length; 146 else 147 max = number; 148 149 for(int i=0;i<max;i++) { 150 try { 151 RemovePhases rp = new RemovePhases(); 152 rp.deleteBackup(delete[i], true); 153 } catch(Exception e) { 154 LoggerHelper.info("error_deleting_file" + delete[i].getName(), e); 155 } 156 } 157 } 158 159 protected BackupEnvironment getEnv() { 160 return getFactory().getBackupEnvironment(); 161 } 162 163 protected EnvironmentFactory getFactory() { 164 try { 165 return EnvironmentFactory.getEnvironmentFactory(); 166 } catch(Exception e) { 167 throw new BackupAbortException( 168 "error_getting_environment_factory", 169 LocalStringsHelper.getString("error_getting_environment_factory"), 170 e); 171 } 172 } 173 174 protected abstract FilenameFilter getFileNameFilter(); 175 protected abstract String getMonitoredDirectory(); 176 protected abstract int getMaxBackups(); 177 protected abstract String getFileNamePrefix(); 178 } 179 | Popular Tags |