KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * BackupRestore.java
3  *
4  * Created on December 13, 2000, 6:08 AM
5  */

6
7 package com.sun.enterprise.config.backup;
8
9 import com.sun.enterprise.config.backup.status.BackupStatus;
10 import com.sun.enterprise.config.backup.status.SnapShotStatus;
11 import com.sun.enterprise.config.backup.status.Status;
12 import com.sun.enterprise.config.backup.status.RemoveStatus;
13 import com.sun.enterprise.config.backup.status.RestoreStatus;
14 import com.sun.enterprise.config.backup.status.UndoRestoreStatus;
15 import com.sun.enterprise.config.backup.status.StatusConstants;
16 import com.sun.enterprise.config.backup.pluggable.EnvironmentFactory;
17 import com.sun.enterprise.config.backup.pluggable.BackupEnvironment;
18 import com.sun.enterprise.config.backup.pluggable.BackupSynchronizer;
19 import com.sun.enterprise.config.backup.phase.BackupPhases;
20 import com.sun.enterprise.config.backup.phase.RestorePhases;
21 import com.sun.enterprise.config.backup.phase.UndoRestorePhases;
22 import com.sun.enterprise.config.backup.phase.RemovePhases;
23
24 import com.sun.enterprise.config.backup.pluggable.BackupStorage;
25 import com.sun.enterprise.config.backup.utils.LoggerHelper;
26 import com.sun.enterprise.config.backup.utils.BackupHelper;
27 import com.sun.enterprise.config.backup.utils.LocalStringsHelper;
28 import com.sun.enterprise.config.backup.utils.FactoryHelper;
29
30 import java.io.File JavaDoc;
31
32 /**
33  * This Interface is exposed by the mbean and also
34  * by the backupRestore utility.
35  * This class has all methods required to implement
36  * backup-restore functionality
37  *
38  * @author sridatta
39  */

40 public class BackupRestoreImpl implements BackupRestore, StatusConstants {
41                     
42     public BackupRestoreImpl(String JavaDoc type) throws BackupException {
43         BackupHelper.setExecutionTypeInEnvironment(type);
44     }
45     
46     /**
47      * Backsup the configuration into a default location
48      * DefaultLocation is specified by $DomainRoot/backup
49      * Use the other method backup(String targetFileName) if
50      * you need to specify the source file
51      *
52      * @return BackupRestoreStatus Detailed backup information
53      */

54     public BackupStatus backup(String JavaDoc userInfo) {
55         BackupStatus bs = BackupHelper.createBackupStatus(userInfo, true);
56         String JavaDoc targetFile = BackupHelper.getAbsoluteBackupFileName(bs);
57         return backup(targetFile, bs);
58     }
59     
60     /**
61      * backs up the configuration to the target filename
62      * @param targetFile absolute path including the filename
63      * @return BackupRestoreStatus Detailed backup information
64      */

65       public BackupStatus backup(String JavaDoc absoluteTargetFile, String JavaDoc userInfo) {
66           BackupStatus bs = BackupHelper.createBackupStatus(userInfo, true);
67           return backup(absoluteTargetFile, bs);
68       }
69       
70       /**
71        * backupStatus cannot be null
72        * It SHOULD be initialized
73        */

74       private synchronized BackupStatus backup(String JavaDoc absoluteTargetFile,
75                                         BackupStatus backupStatus) {
76          BackupPhases bp = new BackupPhases();
77          try {
78               bp.assertStatusNotNull(backupStatus);
79               bp.setStatusInRegistry(backupStatus);
80               bp.assertStatusNotFailed();
81               bp.lock();
82               bp.setAbsoluteBackupFileName(absoluteTargetFile);
83               long size = bp.backup();
84               bp.setBackupFileSize(size);
85               bp.setSuccess();
86               bp.addStatusFile();
87           } catch(BackupAbortException bae) {
88               LoggerHelper.error("error_during_backup", bae);
89               bp.setFailure(bae);
90           }catch (Exception JavaDoc e) {
91               LoggerHelper.error("error_during_backup", e);
92               bp.setFailure(e);
93           } finally {
94               bp.monitorBackupDir();
95               bp.addHistory();
96               bp.unlock();
97               bp.addAssistance();
98           }
99           return bp.getStatus();
100       }
101     
102     /**
103      * Restores the configuration from the last backup file
104      *
105      * @return RestoreStatus Detailed Status Information
106      */

107       public synchronized RestoreStatus restore() {
108               File JavaDoc f = BackupHelper.getLastBackupFile();
109               RestoreStatus rs = BackupHelper.createRestoreStatus(f);
110         return restore(f, rs);
111     }
112     
113     /**
114       * restores the configuration from the specified file
115      * Note that this can be a backup from this domain OR
116      * from other domain!! Hence it can be used to replicate
117      * servers.
118      *
119      * @param can be absolute or relative path.
120      * @return RestoreStatus detailed status information
121      */

122       public synchronized RestoreStatus restore(String JavaDoc backupFileName) {
123     
124           
125           String JavaDoc absName = null;
126           RestoreStatus rs = null;
127           //FIXME: NEEDS TO BE CLEANED UP
128
try {
129             absName =
130                BackupHelper.getAbsoluteBackupFileName(backupFileName);
131           } catch(Exception JavaDoc e) {
132                   return RestoreStatus.
133                     createFailedRestoreStatus(
134                         "wrong_backup_file", backupFileName);
135           }
136               
137           File JavaDoc f = new File JavaDoc(absName);
138           rs = BackupHelper.createRestoreStatus(f);
139           return restore(f, rs);
140     }
141       
142     private RestoreStatus restore(File JavaDoc f, RestoreStatus rs) {
143         //lock
144
//delete previous snapshot (yes, it is lost for ever)
145
//perform a best-effort snapshot
146
//restore files
147
//enter history
148
//return restorestatus
149
RestorePhases rp = new RestorePhases();
150
151         try {
152               rp.assertStatusNotNull(rs);
153               rp.setStatusInRegistry(rs);
154               rp.assertStatusNotFailed();
155               rp.lock();
156               rp.takeSnapShot();
157               rp.deleteTarget();
158               rp.performRestore(f);
159               rp.setSuccess();
160           } catch (BackupAbortException bae) {
161               LoggerHelper.error("error_during_restore: " + bae.getCode(), bae);
162               rp.setFailure(bae);
163           } catch (Exception JavaDoc e) {
164               LoggerHelper.error("error_during_restore", e);
165               rp.setFailure(e);
166           } finally {
167               rp.addHistory();
168               rp.unlock();
169               rp.addAssistance();
170           }
171           return rp.getStatus();
172     }
173     
174     /**
175      * This method can be used to undo a just performed restore.
176      * Every restore keeps the information before restoring in
177      * a file. So that restore can be undone.
178      *
179      * @UndoRestoreStatus status of the undo restore operation
180      */

181       public synchronized UndoRestoreStatus undoRestore() {
182           LoggerHelper.info("risky_operation");
183           UndoRestorePhases urp = new UndoRestorePhases();
184           File JavaDoc f = BackupHelper.getLastSnapShotFile();
185           UndoRestoreStatus urs = BackupHelper.createUndoRestoreStatus(f);
186         
187         try {
188               urp.assertStatusNotNull(urs);
189               urp.setStatusInRegistry(urs);
190               urp.assertStatusNotFailed();
191               urp.lock();
192               urp.deleteTarget();
193               urp.performUndoRestore(f, urs);
194           } catch (BackupAbortException bae) {
195               LoggerHelper.error("error_during_undo_restore", bae);
196               urp.setFailure(bae);
197           } catch (Exception JavaDoc e) {
198               LoggerHelper.error("error_during_undo_restore", e);
199               urp.setFailure(e);
200           } finally {
201               urp.addHistory();
202               urp.unlock();
203               urp.addAssistance();
204           }
205           return urp.getStatus();
206     }
207     
208     /**
209      * Get a report of all available backups
210      */

211     public synchronized BackupStatus[] listBackups() {
212         BackupStatus[] bss = null;
213         try {
214             bss = FactoryHelper.getBackupStorageMonitor().listBackupStatus();
215         } catch(Exception JavaDoc e) {
216             LoggerHelper.error("error_listing_backups", e);
217         }
218         return bss;
219     }
220
221     /**
222      * remove by specifying a file name
223      * relative path. (absolute path can be deleted by hand)
224      */

225     public synchronized RemoveStatus removeBackup(String JavaDoc backupFileName) {
226         RemovePhases rp = new RemovePhases();
227         return rp.deleteBackup(backupFileName, false);
228     }
229     
230     /**
231      * Get a report of previous history
232      * -1 for
233      */

234     public synchronized BackupHistory getBackupHistory(int num)
235                                     throws BackupException {
236         try {
237             return FactoryHelper.getHistoryManager().getBackupHistory(num);
238         } catch(Exception JavaDoc e) {
239             LoggerHelper.warning("error_retrieving_backup_history");
240             LoggerHelper.fine("", e);
241             throw new BackupException(
242                 "error_retrieving_backup_history",
243                 LocalStringsHelper.getString("error_retrieving_backup_history"),e);
244         }
245     }
246     
247     /**
248      * cleanup the history
249      */

250     public synchronized BackupHistory clearBackupHistory() {
251         BackupHistory bh = null;
252         try {
253             bh = getBackupHistory(-1);
254         } catch(Exception JavaDoc e) {
255             //ignore
256
}
257         try {
258             FactoryHelper.getHistoryManager().clearHistory();
259         } catch(Exception JavaDoc e) {
260             LoggerHelper.warning("error_clearing_backup_history");
261             LoggerHelper.fine("", e);
262         }
263         return bh;
264     }
265     
266     private BackupEnvironment getEnv() throws BackupException {
267         return FactoryHelper.getEnv();
268     }
269    
270 }
271
Popular Tags