1 23 24 package com.sun.enterprise.config.backup; 25 26 import com.sun.enterprise.config.backup.util.*; 27 import java.io.*; 28 import java.util.*; 29 30 34 35 public class RestoreManager extends BackupRestoreManager 36 { 37 public RestoreManager(BackupRequest req) throws BackupException 38 { 39 super(req); 40 } 41 42 44 public String restore() throws BackupException 45 { 46 try 47 { 48 checkDomainName(); 49 ZipFile zf = new ZipFile(request.backupFile, tempRestoreDir); 50 zf.explode(); 51 sanityCheckExplodedFiles(); 52 copyBackups(); 53 atomicSwap(); 54 setPermissions(); 55 String mesg = readAndDeletePropsFile(); 56 return mesg; 57 } 58 catch(BackupException be) 59 { 60 throw be; 61 } 62 catch(Exception e) 63 { 64 throw new BackupException("Restore Error", e); 65 } 66 } 67 68 70 void init() throws BackupException 71 { 72 super.init(); 73 74 if(request.backupFile == null) 75 initWithNoSpecifiedBackupFile(); 76 else 77 initWithSpecifiedBackupFile(); 78 79 tempRestoreDir = new File(request.domainsDir, request.domainName + 80 "_" + System.currentTimeMillis()); 81 } 82 83 85 private void initWithSpecifiedBackupFile() throws BackupException 86 { 87 if(request.backupFile.length() <= 0) 88 throw new BackupException("backup-res.BadBackupFile", request.backupFile); 89 90 if(!FileUtils.safeIsDirectory(request.domainDir)) 91 request.domainDir.mkdirs(); 92 93 backupDir = new File(request.domainDir, Constants.BACKUP_DIR); 94 95 if(!FileUtils.safeIsDirectory(backupDir)) 98 backupDir = null; 99 100 102 } 103 104 106 private void initWithNoSpecifiedBackupFile() throws BackupException 107 { 108 111 if(!FileUtils.safeIsDirectory(request.domainDir)) 112 throw new BackupException("backup-res.NoDomainDir", request.domainDir); 113 114 backupDir = new File(request.domainDir, Constants.BACKUP_DIR); 115 116 if(!FileUtils.safeIsDirectory(backupDir)) 118 throw new BackupException("backup-res.NoBackupDir", backupDir); 119 120 BackupFilenameManager bfmgr = new BackupFilenameManager(backupDir); 121 request.backupFile = bfmgr.latest(); 122 123 } 125 126 128 161 163 private void copyBackups() throws IOException 164 { 165 if(backupDir != null) 166 { 167 File tempRestoreDirBackups = new File(tempRestoreDir, Constants.BACKUP_DIR); 168 FileUtils.copyTree(backupDir, tempRestoreDirBackups); 169 } 170 } 171 172 174 private void atomicSwap() throws BackupException 175 { 176 180 File oldDomain = new File(request.domainsDir, 182 request.domainName + OLD_DOMAIN_SUFFIX + System.currentTimeMillis()); 183 184 if(!request.domainDir.renameTo(oldDomain)) 186 { 187 FileUtils.whack(tempRestoreDir); 188 throw new BackupException("backup-res.CantRenameOriginalDomain", request.domainDir); 189 } 190 191 if(!tempRestoreDir.renameTo(request.domainDir)) 194 { 195 oldDomain.renameTo(request.domainDir); 196 FileUtils.whack(tempRestoreDir); 197 throw new BackupException("backup-res.CantRenameRestoredDomain"); 198 } 199 200 FileUtils.whack(oldDomain); 201 } 202 203 205 private String readAndDeletePropsFile() 206 { 207 File propsFile = new File(request.domainDir, Constants.PROPS_FILENAME); 210 Status status = new Status(); 211 212 String mesg = StringHelper.get("backup-res.SuccessfulRestore", 213 request.domainName, request.backupFile); 214 215 if(request.terse == false) 216 mesg += "\n" + status.read(propsFile, false); 217 218 if(!propsFile.delete()) 219 propsFile.deleteOnExit(); 220 221 return mesg; 222 } 223 224 226 227 private void setPermissions() 228 { 229 File backups = new File(request.domainDir, "backups"); 230 File bin = new File(request.domainDir, "bin"); 231 File config = new File(request.domainDir, "config"); 232 File webtmp = new File(request.domainDir, "generated/tmp"); 233 File masterPassword = new File(request.domainDir, "master-password"); 234 235 FileUtils.makeExecutable(bin); 238 239 FileUtils.protect(backups); 240 FileUtils.protect(config); 241 FileUtils.protect(masterPassword); 242 FileUtils.protect(webtmp); 243 244 248 } 256 257 259 private void sanityCheckExplodedFiles() throws BackupException 260 { 261 263 File statusFile = new File(tempRestoreDir, Constants.PROPS_FILENAME); 264 265 if(!statusFile.exists()) 266 { 267 FileUtils.whack(tempRestoreDir); 269 throw new BackupException("backup-res.RestoreError.CorruptBackupFile.NoStatusFile", request.domainName); 270 } 271 } 272 273 275 private void checkDomainName() throws BackupException 276 { 277 Status status = new Status(); 278 status.read(request.backupFile); 279 String buDomainName = status.getDomainName(); 280 281 if(buDomainName == null || !request.domainName.equals(buDomainName)) 282 throw new BackupException(StringHelper.get("backup-res.DomainNameDifferent", buDomainName, request.domainName)); 283 } 284 285 287 private static final String OLD_DOMAIN_SUFFIX = "_beforeRestore_"; 288 private File tempRestoreDir; 289 private File backupDir; 290 } 291 | Popular Tags |