1 6 7 package com.sun.enterprise.config.backup; 8 9 import java.io.File ; 10 import java.io.FileOutputStream ; 11 import java.io.FileInputStream ; 12 import java.io.BufferedReader ; 13 import java.io.FileReader ; 14 import java.util.ArrayList ; 15 import com.sun.enterprise.config.backup.status.StatusConstants; 16 import com.sun.enterprise.config.backup.status.Status; 17 import com.sun.enterprise.config.backup.pluggable.EnvironmentFactory; 18 import com.sun.enterprise.config.backup.pluggable.BackupEnvironment; 19 import com.sun.enterprise.config.backup.utils.LoggerHelper; 20 import com.sun.enterprise.config.backup.utils.FactoryHelper; 21 22 28 public class HistoryManager 29 implements StatusConstants, EnvironmentConstants { 30 31 private String _backupHistoryFile; 32 33 34 public HistoryManager() throws BackupException { 35 _backupHistoryFile = initBackupHistoryFile(); 36 } 37 38 public void addHistoryEntry(Status status) { 39 addHistoryEntry(new HistoryEntry(status)); 40 } 41 42 public void addHistoryEntry(HistoryEntry historyEntry) { 43 if (historyEntry == null) return; 45 46 FileOutputStream fos = null; 47 try { 48 File f = new File (_backupHistoryFile); 49 if(!f.exists()) { 50 f.createNewFile(); 51 } 52 fos = new FileOutputStream (f, true); 53 54 if(f.exists() && f.length() != 0) 56 fos.write(DefaultConstants.NEWLINE_CHARACTER.getBytes()); 57 58 fos.write(historyEntry.toString().getBytes()); 59 } catch(Exception e) { 60 LoggerHelper.warning("append_to_backup_history_failed", e); 62 } finally { 63 try { 64 fos.close(); 65 } catch(Exception ex){} 66 } 67 } 68 69 public BackupHistory getBackupHistory(int num) { 70 ArrayList arr = new ArrayList (); 71 BackupHistory res; 72 73 BufferedReader in = null; 74 try { 75 File f = new File (_backupHistoryFile); 76 if(!f.exists()) { 77 return new BackupHistory(_backupHistoryFile); 78 } 79 80 in = new BufferedReader (new FileReader (f)); 81 82 String line; 83 while((line=in.readLine()) != null) { 84 HistoryEntry he = new HistoryEntry(line); 85 arr.add(he); 86 } 87 88 } catch(Exception e) { 89 LoggerHelper.warning("loading_backup_history_failed", e); 91 } finally { 92 try { 93 in.close(); 94 } catch(Exception ex){} 95 } 96 97 res = new BackupHistory(ArrayListToHistoryEntryArr(arr, num), _backupHistoryFile); 98 return res; 100 } 101 102 private HistoryEntry[] ArrayListToHistoryEntryArr(ArrayList arr, int num) { 103 if(arr==null || arr.size() == 0) return null; 104 int size = arr.size(); 105 106 if(size > num) size = num; 107 108 HistoryEntry[] res = new HistoryEntry[size]; 109 int j=0; 110 for(int i=(arr.size()-size);i<arr.size();i++,j++) { 111 res[j] = (HistoryEntry) arr.get(i); 112 } 113 return res; 114 } 115 116 private String initBackupHistoryFile() throws BackupException { 117 BackupEnvironment be = FactoryHelper.getEnv(); 118 String name = be.getBackupHistoryFileName(); 119 String dir = be.getDirectoryToStoreHistoryFile(); 120 121 return dir + File.separator + name; 122 } 123 124 public String getBackupHistoryFile() { 125 return _backupHistoryFile; 126 } 127 128 public void clearHistory() { 129 try { 130 File f = new File (_backupHistoryFile); 131 f.delete(); 132 f.createNewFile(); 133 } catch(Exception io) { 134 LoggerHelper.warning("error_clearing_history", io); 135 } 136 } 137 } | Popular Tags |