KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

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 /**
31  *
32  * @author Byron Nevins
33  */

34
35 public class RestoreManager extends BackupRestoreManager
36 {
37     public RestoreManager(BackupRequest req) throws BackupException
38     {
39         super(req);
40     }
41     
42     ///////////////////////////////////////////////////////////////////////////////
43

44     public String JavaDoc 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 JavaDoc mesg = readAndDeletePropsFile();
56             return mesg;
57         }
58         catch(BackupException be)
59         {
60             throw be;
61         }
62         catch(Exception JavaDoc e)
63         {
64             throw new BackupException("Restore Error", e);
65         }
66     }
67     
68     ///////////////////////////////////////////////////////////////////////////////
69

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     ///////////////////////////////////////////////////////////////////////////////
84

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         // It's NOT an error to not exist. The domain may not exist currently and, besides,
96
// they are specifying the backup-file from anywhere potentially...
97
if(!FileUtils.safeIsDirectory(backupDir))
98             backupDir = null;
99
100         //throw new BackupException("NOT YET IMPLEMENTED");
101

102     }
103     
104     ///////////////////////////////////////////////////////////////////////////////
105

106     private void initWithNoSpecifiedBackupFile() throws BackupException
107     {
108         // if they did NOT specify a backupFile, then we *must* have a pre-existing
109
// backups directory in a pre-existing domain directory.
110

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         // It's an error to not exist...
117
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         //request.backupFile = getNewestZip(backupDir);
124
}
125     
126     ///////////////////////////////////////////////////////////////////////////////
127

128     /*
129     private File getNewestZip(File dir) throws BackupException
130     {
131         File newestFile = null;
132         long newestTime = 0;
133
134         File[] zips = dir.listFiles(new ZipFilenameFilter());
135         
136         for(int i = 0; i < zips.length; i++)
137         {
138             //long time = zips[i].lastModified();
139             Status status = new Status();
140             long time = status.getInternalTimestamp(zips[i]);
141
142             String msg = "filename: " + zips[i] + ", ts= " + time;
143             if(time > newestTime)
144             {
145                 newestFile = zips[i];
146                 newestTime = time;
147                 msg += " --- newest file so far";
148             }
149             else
150                 msg += " -- NOT newest";
151
152             System.out.println(msg);
153         }
154         
155         if(newestFile == null)
156             throw new BackupException("backup-res.NoBackupFiles", dir);
157         
158         return newestFile;
159     }
160     */

161     ///////////////////////////////////////////////////////////////////////////////
162

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     ///////////////////////////////////////////////////////////////////////////////
173

174     private void atomicSwap() throws BackupException
175     {
176         // 1 -- rename original domain dir
177
// 2 -- rename new restored dir to domain dir
178
// 3 -- delete original domain dir
179

180         // tempRestoreDir
181
File oldDomain = new File(request.domainsDir,
182             request.domainName + OLD_DOMAIN_SUFFIX + System.currentTimeMillis());
183         
184         // On Error -- just fail and delete the new files
185
if(!request.domainDir.renameTo(oldDomain))
186         {
187             FileUtils.whack(tempRestoreDir);
188             throw new BackupException("backup-res.CantRenameOriginalDomain", request.domainDir);
189         }
190         
191         // On Error -- Delete the new files and undo the rename that was done
192
//successfully above
193
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     ///////////////////////////////////////////////////////////////////////////////
204

205     private String JavaDoc readAndDeletePropsFile()
206     {
207         // The "backup.properties" file from the restored zip should be
208
// in the domain dir now.
209
File propsFile = new File(request.domainDir, Constants.PROPS_FILENAME);
210         Status status = new Status();
211
212         String JavaDoc 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     /** zip is platform dependent -- so non-default permissions are gone!
225      */

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         // note that makeExecutable(File f) will make all the files under f
236
// executable if f happens to be a directory.
237
FileUtils.makeExecutable(bin);
238         
239         FileUtils.protect(backups);
240         FileUtils.protect(config);
241         FileUtils.protect(masterPassword);
242         FileUtils.protect(webtmp);
243         
244         // Jan 19, 2005 -- rolled back the fix for 6206176. It has been decided
245
// that this is not a bug but rather a security feature.
246
//FileUtils.whack(webtmp);
247

248         // fix: 6206176 -- instead of setting permissions for the tmp dir,
249
// we just delete it. This will allow, say, user 'A' to do the restore,
250
// and then allow user 'B' (including root) to start the domain without
251
// getting a web-container error.
252
// see: bug 6194504 for the tmp dir details
253
//old:
254
//new:
255
}
256     
257     ///////////////////////////////////////////////////////////////////////////////
258

259     private void sanityCheckExplodedFiles() throws BackupException
260     {
261         // Is the "magic" properties file where it is supposed to be?
262

263         File statusFile = new File(tempRestoreDir, Constants.PROPS_FILENAME);
264         
265         if(!statusFile.exists())
266         {
267             // cleanup -- we are officially failing the restore!
268
FileUtils.whack(tempRestoreDir);
269             throw new BackupException("backup-res.RestoreError.CorruptBackupFile.NoStatusFile", request.domainName);
270         }
271     }
272     
273     ///////////////////////////////////////////////////////////////////////////////
274

275     private void checkDomainName() throws BackupException
276     {
277         Status status = new Status();
278         status.read(request.backupFile);
279         String JavaDoc 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     ///////////////////////////////////////////////////////////////////////////////
286

287     private static final String JavaDoc OLD_DOMAIN_SUFFIX = "_beforeRestore_";
288     private File tempRestoreDir;
289     private File backupDir;
290 }
291
Popular Tags