1 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 ; 31 32 40 public class BackupRestoreImpl implements BackupRestore, StatusConstants { 41 42 public BackupRestoreImpl(String type) throws BackupException { 43 BackupHelper.setExecutionTypeInEnvironment(type); 44 } 45 46 54 public BackupStatus backup(String userInfo) { 55 BackupStatus bs = BackupHelper.createBackupStatus(userInfo, true); 56 String targetFile = BackupHelper.getAbsoluteBackupFileName(bs); 57 return backup(targetFile, bs); 58 } 59 60 65 public BackupStatus backup(String absoluteTargetFile, String userInfo) { 66 BackupStatus bs = BackupHelper.createBackupStatus(userInfo, true); 67 return backup(absoluteTargetFile, bs); 68 } 69 70 74 private synchronized BackupStatus backup(String 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 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 107 public synchronized RestoreStatus restore() { 108 File f = BackupHelper.getLastBackupFile(); 109 RestoreStatus rs = BackupHelper.createRestoreStatus(f); 110 return restore(f, rs); 111 } 112 113 122 public synchronized RestoreStatus restore(String backupFileName) { 123 124 125 String absName = null; 126 RestoreStatus rs = null; 127 try { 129 absName = 130 BackupHelper.getAbsoluteBackupFileName(backupFileName); 131 } catch(Exception e) { 132 return RestoreStatus. 133 createFailedRestoreStatus( 134 "wrong_backup_file", backupFileName); 135 } 136 137 File f = new File (absName); 138 rs = BackupHelper.createRestoreStatus(f); 139 return restore(f, rs); 140 } 141 142 private RestoreStatus restore(File f, RestoreStatus rs) { 143 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 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 181 public synchronized UndoRestoreStatus undoRestore() { 182 LoggerHelper.info("risky_operation"); 183 UndoRestorePhases urp = new UndoRestorePhases(); 184 File 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 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 211 public synchronized BackupStatus[] listBackups() { 212 BackupStatus[] bss = null; 213 try { 214 bss = FactoryHelper.getBackupStorageMonitor().listBackupStatus(); 215 } catch(Exception e) { 216 LoggerHelper.error("error_listing_backups", e); 217 } 218 return bss; 219 } 220 221 225 public synchronized RemoveStatus removeBackup(String backupFileName) { 226 RemovePhases rp = new RemovePhases(); 227 return rp.deleteBackup(backupFileName, false); 228 } 229 230 234 public synchronized BackupHistory getBackupHistory(int num) 235 throws BackupException { 236 try { 237 return FactoryHelper.getHistoryManager().getBackupHistory(num); 238 } catch(Exception 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 250 public synchronized BackupHistory clearBackupHistory() { 251 BackupHistory bh = null; 252 try { 253 bh = getBackupHistory(-1); 254 } catch(Exception e) { 255 } 257 try { 258 FactoryHelper.getHistoryManager().clearHistory(); 259 } catch(Exception 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 |