KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > backup > HistoryManager


1 /*
2  * HistoryManager.java
3  *
4  * Created on December 13, 2000, 8:49 AM
5  */

6
7 package com.sun.enterprise.config.backup;
8
9 import java.io.File JavaDoc;
10 import java.io.FileOutputStream JavaDoc;
11 import java.io.FileInputStream JavaDoc;
12 import java.io.BufferedReader JavaDoc;
13 import java.io.FileReader JavaDoc;
14 import java.util.ArrayList JavaDoc;
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 /**
23  * History Manager manages the history for the backup-restore
24  * functionality. It keeps track of the backupHistory and
25  * saves it in a file.
26  * @author sridatta
27  */

28 public class HistoryManager
29     implements StatusConstants, EnvironmentConstants {
30     
31     private String JavaDoc _backupHistoryFile;
32     
33     /** Creates a new instance of HistoryManager */
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         //open file and add the entry
44
if (historyEntry == null) return;
45         
46         FileOutputStream JavaDoc fos = null;
47         try {
48             File JavaDoc f = new File JavaDoc(_backupHistoryFile);
49             if(!f.exists()) {
50                 f.createNewFile();
51             }
52             fos = new FileOutputStream JavaDoc(f, true);
53             
54             //add a newline between history entries
55
if(f.exists() && f.length() != 0)
56                 fos.write(DefaultConstants.NEWLINE_CHARACTER.getBytes());
57             
58             fos.write(historyEntry.toString().getBytes());
59         } catch(Exception JavaDoc e) {
60             //log and ignore
61
LoggerHelper.warning("append_to_backup_history_failed", e);
62         } finally {
63              try {
64                 fos.close();
65             } catch(Exception JavaDoc ex){}
66         }
67     }
68     
69     public BackupHistory getBackupHistory(int num) {
70         ArrayList JavaDoc arr = new ArrayList JavaDoc();
71         BackupHistory res;
72         
73         BufferedReader JavaDoc in = null;
74         try {
75             File JavaDoc f = new File JavaDoc(_backupHistoryFile);
76            if(!f.exists()) {
77                 return new BackupHistory(_backupHistoryFile);
78             }
79             
80             in = new BufferedReader JavaDoc(new FileReader JavaDoc(f));
81
82             String JavaDoc line;
83           while((line=in.readLine()) != null) {
84               HistoryEntry he = new HistoryEntry(line);
85               arr.add(he);
86           }
87          
88         } catch(Exception JavaDoc e) {
89             //log and ignore
90
LoggerHelper.warning("loading_backup_history_failed", e);
91         } finally {
92             try {
93                 in.close();
94             } catch(Exception JavaDoc ex){}
95         }
96         
97         res = new BackupHistory(ArrayListToHistoryEntryArr(arr, num), _backupHistoryFile);
98         //FIXME Return last n
99
return res;
100     }
101     
102     private HistoryEntry[] ArrayListToHistoryEntryArr(ArrayList JavaDoc 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 JavaDoc initBackupHistoryFile() throws BackupException {
117         BackupEnvironment be = FactoryHelper.getEnv();
118         String JavaDoc name = be.getBackupHistoryFileName();
119         String JavaDoc dir = be.getDirectoryToStoreHistoryFile();
120         
121         return dir + File.separator + name;
122     }
123         
124     public String JavaDoc getBackupHistoryFile() {
125         return _backupHistoryFile;
126     }
127     
128     public void clearHistory() {
129         try {
130             File JavaDoc f = new File JavaDoc(_backupHistoryFile);
131             f.delete();
132             f.createNewFile();
133         } catch(Exception JavaDoc io) {
134             LoggerHelper.warning("error_clearing_history", io);
135         }
136     }
137 }
Popular Tags